1 line
54 KiB
JavaScript
1 line
54 KiB
JavaScript
!function(r){var e,t,i,s,o=wp.media,n=o.model.Attachment,a=o.model.Attachments,l=(o.model.Query,o.view.l10n="undefined"==typeof _wpMediaViewsL10n?{}:_wpMediaViewsL10n);o.view.settings=l.settings||{},delete l.settings,o.model.settings.post=o.view.settings.post,r.support.transition=(e=document.documentElement.style,t={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"},(i=_.find(_.keys(t),function(t){return!_.isUndefined(e[t])}))&&{end:t[i]}),o.transition=function(t,e){var i=r.Deferred();return e=e||2e3,r.support.transition?((t=!(t instanceof r)?r(t):t).first().one(r.support.transition.end,i.resolve),_.delay(i.resolve,e)):i.resolve(),i.promise()},o.controller.Region=function(t){_.extend(this,_.pick(t||{},"id","view","selector"))},o.controller.Region.extend=Backbone.Model.extend,_.extend(o.controller.Region.prototype,{mode:function(t){return t?(t===this._mode||(this.trigger("deactivate"),this._mode=t,this.render(t),this.trigger("activate")),this):this._mode},render:function(t){if(t&&t!==this._mode)return this.mode(t);t={view:null};return this.trigger("create",t),t=t.view,this.trigger("render",t),t&&this.set(t),this},get:function(){return this.view.views.first(this.selector)},set:function(t,e){return e&&(e.add=!1),this.view.views.set(this.selector,t,e)},trigger:function(t){if(this._mode){var e=_.toArray(arguments),i=this.id+":"+t;return e[0]=i+":"+this._mode,this.view.trigger.apply(this.view,e),e[0]=i,this.view.trigger.apply(this.view,e),this}}}),o.controller.StateMachine=function(t){this.states=new Backbone.Collection(t)},o.controller.StateMachine.extend=Backbone.Model.extend,_.extend(o.controller.StateMachine.prototype,Backbone.Events,{state:function(t){return this.states=this.states||new Backbone.Collection,(t=t||this._state)&&!this.states.get(t)&&this.states.add({id:t}),this.states.get(t)},setState:function(t){var e=this.state();return e&&t===e.id||!this.states||!this.states.get(t)||(e&&(e.trigger("deactivate"),this._lastState=e.id),this._state=t,this.state().trigger("activate")),this},lastState:function(){if(this._lastState)return this.state(this._lastState)}}),_.each(["on","off","trigger"],function(t){o.controller.StateMachine.prototype[t]=function(){return this.states=this.states||new Backbone.Collection,this.states[t].apply(this.states,arguments),this}}),o.controller.State=Backbone.Model.extend({constructor:function(){this.on("activate",this._preActivate,this),this.on("activate",this.activate,this),this.on("activate",this._postActivate,this),this.on("deactivate",this._deactivate,this),this.on("deactivate",this.deactivate,this),this.on("reset",this.reset,this),this.on("ready",this._ready,this),this.on("ready",this.ready,this),Backbone.Model.apply(this,arguments),this.on("change:menu",this._updateMenu,this)},ready:function(){},activate:function(){},deactivate:function(){},reset:function(){},_ready:function(){this._updateMenu()},_preActivate:function(){this.active=!0},_postActivate:function(){this.on("change:menu",this._menu,this),this.on("change:titleMode",this._title,this),this.on("change:content",this._content,this),this.on("change:toolbar",this._toolbar,this),this.frame.on("title:render:default",this._renderTitle,this),this._title(),this._menu(),this._toolbar(),this._content(),this._router()},_deactivate:function(){this.active=!1,this.frame.off("title:render:default",this._renderTitle,this),this.off("change:menu",this._menu,this),this.off("change:titleMode",this._title,this),this.off("change:content",this._content,this),this.off("change:toolbar",this._toolbar,this)},_title:function(){this.frame.title.render(this.get("titleMode")||"default")},_renderTitle:function(t){t.$el.text(this.get("title")||"")},_router:function(){var t=this.frame.router,e=this.get("router");this.frame.$el.toggleClass("hide-router",!e),e&&(this.frame.router.render(e),(t=t.get())&&t.select&&t.select(this.frame.content.mode()))},_menu:function(){var t=this.frame.menu,e=this.get("menu");e&&(t.mode(e),(t=t.get())&&t.select&&t.select(this.id))},_updateMenu:function(){var t=this.previous("menu"),e=this.get("menu");t&&this.frame.off("menu:render:"+t,this._renderMenu,this),e&&this.frame.on("menu:render:"+e,this._renderMenu,this)},_renderMenu:function(t){var e=this.get("menuItem"),i=this.get("title"),s=this.get("priority");!e&&i&&(e={text:i},s&&(e.priority=s)),e&&t.set(this.id,e)}}),_.each(["toolbar","content"],function(e){o.controller.State.prototype["_"+e]=function(){var t=this.get(e);t&&this.frame[e].render(t)}}),o.controller.Library=o.controller.State.extend({defaults:{id:"library",multiple:!1,describe:!1,toolbar:"select",sidebar:"settings",content:"upload",router:"browse",menu:"default",searchable:!0,filterable:!1,sortable:!0,title:l.mediaLibraryTitle,contentUserSetting:!0,syncSelection:!0},initialize:function(){var t=this.get("selection");this.get("library")||this.set("library",o.query()),t instanceof o.model.Selection||((t=t)||(t=this.get("library").props.toJSON(),t=_.omit(t,"orderby","query")),this.set("selection",new o.model.Selection(null,{multiple:this.get("multiple"),props:t}))),this.get("edge")||this.set("edge",120),this.get("gutter")||this.set("gutter",8),this.resetDisplays()},activate:function(){this.syncSelection(),wp.Uploader.queue.on("add",this.uploading,this),this.get("selection").on("add remove reset",this.refreshContent,this),this.get("contentUserSetting")&&(this.frame.on("content:activate",this.saveContentMode,this),this.set("content",getUserSetting("libraryContent",this.get("content"))))},deactivate:function(){this.recordSelection(),this.frame.off("content:activate",this.saveContentMode,this),this.get("selection").off(null,null,this),wp.Uploader.queue.off(null,null,this)},reset:function(){this.get("selection").reset(),this.resetDisplays(),this.refreshContent()},resetDisplays:function(){var t=o.view.settings.defaultProps;this._displays=[],this._defaultDisplaySettings={align:t.align||getUserSetting("align","none"),size:t.size||getUserSetting("imgsize","medium"),link:t.link||getUserSetting("urlbutton","file")}},display:function(t){var e=this._displays;return e[t.cid]||(e[t.cid]=new Backbone.Model(this.defaultDisplaySettings(t))),e[t.cid]},defaultDisplaySettings:function(t){return settings=this._defaultDisplaySettings,(settings.canEmbed=this.canEmbed(t))&&(settings.link="embed"),settings},canEmbed:function(t){if(!t.get("uploading")){var e=t.get("type");if("audio"!==e&&"video"!==e)return!1}return _.contains(o.view.settings.embedExts,t.get("filename").split(".").pop())},syncSelection:function(){var t=this.get("selection"),e=this.frame._selection;this.get("syncSelection")&&e&&t&&(t.multiple&&(t.reset([],{silent:!0}),t.validateAll(e.attachments),e.difference=_.difference(e.attachments.models,t.models)),t.single(e.single))},recordSelection:function(){var t=this.get("selection"),e=this.frame._selection;this.get("syncSelection")&&e&&t&&(t.multiple?(e.attachments.reset(t.toArray().concat(e.difference)),e.difference=[]):e.attachments.add(t.toArray()),e.single=t._single)},refreshContent:function(){var t=this.get("selection"),e=this.frame,i=e.router.get(),e=e.content.mode();this.active&&!t.length&&i&&!i.get(e)&&this.frame.content.render(this.get("content"))},uploading:function(t){"upload"===this.frame.content.mode()&&this.frame.content.mode("browse"),this.get("selection").add(t)},saveContentMode:function(){var t,e;"browse"===this.get("router")&&(t=this.frame.content.mode(),(e=this.frame.router.get())&&e.get(t)&&setUserSetting("libraryContent",t))}}),o.controller.GalleryEdit=o.controller.Library.extend({defaults:{id:"gallery-edit",multiple:!1,describe:!0,edge:199,editing:!1,sortable:!0,searchable:!1,toolbar:"gallery-edit",content:"browse",title:l.editGalleryTitle,priority:60,dragInfo:!0,syncSelection:!1},initialize:function(){this.get("library")||this.set("library",new o.model.Selection),this.get("AttachmentView")||this.set("AttachmentView",o.view.Attachment.EditLibrary),o.controller.Library.prototype.initialize.apply(this,arguments)},activate:function(){this.get("library").props.set("type","image"),this.get("library").observe(wp.Uploader.queue),this.frame.on("content:render:browse",this.gallerySettings,this),o.controller.Library.prototype.activate.apply(this,arguments)},deactivate:function(){this.get("library").unobserve(wp.Uploader.queue),this.frame.off("content:render:browse",this.gallerySettings,this),o.controller.Library.prototype.deactivate.apply(this,arguments)},gallerySettings:function(t){var e=this.get("library");e&&t&&(e.gallery=e.gallery||new Backbone.Model,t.sidebar.set({gallery:new o.view.Settings.Gallery({controller:this,model:e.gallery,priority:40})}),t.toolbar.set("reverse",{text:l.reverseOrder,priority:80,click:function(){e.reset(e.toArray().reverse())}}))}}),o.controller.GalleryAdd=o.controller.Library.extend({defaults:_.defaults({id:"gallery-library",filterable:"uploaded",multiple:"add",menu:"gallery",toolbar:"gallery-add",title:l.addToGalleryTitle,priority:100,syncSelection:!1},o.controller.Library.prototype.defaults),initialize:function(){this.get("library")||this.set("library",o.query({type:"image"})),o.controller.Library.prototype.initialize.apply(this,arguments)},activate:function(){var t=this.get("library"),e=this.frame.state("gallery-edit").get("library");this.editLibrary&&this.editLibrary!==e&&t.unobserve(this.editLibrary),t.validator=function(t){return!!this.mirroring.get(t.cid)&&!e.get(t.cid)&&o.model.Selection.prototype.validator.apply(this,arguments)},t.reset(t.mirroring.models,{silent:!0}),t.observe(e),this.editLibrary=e,o.controller.Library.prototype.activate.apply(this,arguments)}}),o.controller.FeaturedImage=o.controller.Library.extend({defaults:_.defaults({id:"featured-image",filterable:"uploaded",multiple:!1,toolbar:"featured-image",title:l.setFeaturedImageTitle,priority:60,syncSelection:!1},o.controller.Library.prototype.defaults),initialize:function(){var t,n;this.get("library")||this.set("library",o.query({type:"image"})),o.controller.Library.prototype.initialize.apply(this,arguments),t=this.get("library"),n=t.comparator,t.comparator=function(t,e){var i=!!this.mirroring.get(t.cid),s=!!this.mirroring.get(e.cid);return!i&&s?-1:i&&!s?1:n.apply(this,arguments)},t.observe(this.get("selection"))},activate:function(){this.updateSelection(),this.frame.on("open",this.updateSelection,this),o.controller.Library.prototype.activate.apply(this,arguments)},deactivate:function(){this.frame.off("open",this.updateSelection,this),o.controller.Library.prototype.deactivate.apply(this,arguments)},updateSelection:function(){var t,e=this.get("selection"),i=o.view.settings.post.featuredImageId;""!==i&&-1!==i&&(t=n.get(i)).fetch(),e.reset(t?[t]:[])}}),o.controller.Embed=o.controller.State.extend({defaults:{id:"embed",url:"",menu:"default",content:"embed",toolbar:"main-embed",type:"link",title:l.insertFromUrlTitle,priority:120},sensitivity:200,initialize:function(){this.debouncedScan=_.debounce(_.bind(this.scan,this),this.sensitivity),this.props=new Backbone.Model({url:""}),this.props.on("change:url",this.debouncedScan,this),this.props.on("change:url",this.refresh,this),this.on("scan",this.scanImage,this)},scan:function(){var t,e=this,i={type:"link",scanners:[]};this.props.get("url")&&this.trigger("scan",i),i.scanners.length?(t=i.scanners=r.when.apply(r,i.scanners)).always(function(){e.get("scanners")===t&&e.set("loading",!1)}):i.scanners=null,i.loading=!!i.scanners,this.set(i)},scanImage:function(t){var e=this.frame,i=this,s=this.props.get("url"),n=new Image,o=r.Deferred();t.scanners.push(o.promise()),n.onload=function(){o.resolve(),i===e.state()&&s===i.props.get("url")&&(i.set({type:"image"}),i.props.set({width:n.width,height:n.height}))},n.onerror=o.reject,n.src=s},refresh:function(){this.frame.toolbar.get().refresh()},reset:function(){this.props.clear().set({url:""}),this.active&&this.refresh()}}),o.View=wp.Backbone.View.extend({constructor:function(t){t&&t.controller&&(this.controller=t.controller),wp.Backbone.View.apply(this,arguments)},dispose:function(){return this.undelegateEvents(),this.model&&this.model.off&&this.model.off(null,null,this),this.collection&&this.collection.off&&this.collection.off(null,null,this),this.controller&&this.controller.off&&this.controller.off(null,null,this),this},remove:function(){return this.dispose(),wp.Backbone.View.prototype.remove.apply(this,arguments)}}),o.view.Frame=o.View.extend({initialize:function(){this._createRegions(),this._createStates()},_createRegions:function(){this.regions=this.regions?this.regions.slice():[],_.each(this.regions,function(t){this[t]=new o.controller.Region({view:this,id:t,selector:".media-frame-"+t})},this)},_createStates:function(){this.states=new Backbone.Collection(null,{model:o.controller.State}),this.states.on("add",function(t){t.frame=this,t.trigger("ready")},this),this.options.states&&this.states.add(this.options.states)},reset:function(){return this.states.invoke("trigger","reset"),this}}),_.extend(o.view.Frame.prototype,o.controller.StateMachine.prototype),o.view.MediaFrame=o.view.Frame.extend({className:"media-frame",template:o.template("media-frame"),regions:["menu","title","content","toolbar","router"],initialize:function(){o.view.Frame.prototype.initialize.apply(this,arguments),_.defaults(this.options,{title:"",modal:!0,uploader:!0}),this.$el.addClass("wp-core-ui"),this.options.modal&&(this.modal=new o.view.Modal({controller:this,title:this.options.title}),this.modal.content(this)),!wp.Uploader.limitExceeded&&wp.Uploader.browser.supported||(this.options.uploader=!1),this.options.uploader&&(this.uploader=new o.view.UploaderWindow({controller:this,uploader:{dropzone:(this.modal||this).$el,container:this.$el}}),this.views.set(".media-frame-uploader",this.uploader)),this.on("attach",_.bind(this.views.ready,this.views),this),this.on("title:create:default",this.createTitle,this),this.title.mode("default"),this.on("menu:create:default",this.createMenu,this)},render:function(){return!this.state()&&this.options.state&&this.setState(this.options.state),o.view.Frame.prototype.render.apply(this,arguments)},createTitle:function(t){t.view=new o.View({controller:this,tagName:"h1"})},createMenu:function(t){t.view=new o.view.Menu({controller:this})},createToolbar:function(t){t.view=new o.view.Toolbar({controller:this})},createRouter:function(t){t.view=new o.view.Router({controller:this})},createIframeStates:function(i){var t=o.view.settings,e=t.tabs,s=t.tabUrl;e&&s&&((t=r("#post_ID")).length&&(s+="&post_id="+t.val()),_.each(e,function(t,e){this.state("iframe:"+e).set(_.defaults({tab:e,src:s+"&tab="+e,title:t,content:"iframe",menu:"default"},i))},this),this.on("content:create:iframe",this.iframeContent,this),this.on("menu:render:default",this.iframeMenu,this),this.on("open",this.hijackThickbox,this),this.on("close",this.restoreThickbox,this))},iframeContent:function(t){this.$el.addClass("hide-toolbar"),t.view=new o.view.Iframe({controller:this})},iframeMenu:function(t){var i={};t&&(_.each(o.view.settings.tabs,function(t,e){i["iframe:"+e]={text:this.state("iframe:"+e).get("title"),priority:200}},this),t.set(i))},hijackThickbox:function(){var t=this;window.tb_remove&&!this._tb_remove&&(this._tb_remove=window.tb_remove,window.tb_remove=function(){t.close(),t.reset(),t.setState(t.options.state),t._tb_remove.call(window)})},restoreThickbox:function(){this._tb_remove&&(window.tb_remove=this._tb_remove,delete this._tb_remove)}}),_.each(["open","close","attach","detach","escape"],function(e){o.view.MediaFrame.prototype[e]=function(t){return this.modal&&this.modal[e].apply(this.modal,arguments),this}}),o.view.MediaFrame.Select=o.view.MediaFrame.extend({initialize:function(){o.view.MediaFrame.prototype.initialize.apply(this,arguments),_.defaults(this.options,{selection:[],library:{},multiple:!1,state:"library"}),this.createSelection(),this.createStates(),this.bindHandlers()},createSelection:function(){var t=this.options.selection;t instanceof o.model.Selection||(this.options.selection=new o.model.Selection(t,{multiple:this.options.multiple})),this._selection={attachments:new a,difference:[]}},createStates:function(){var t=this.options;this.options.states||this.states.add([new o.controller.Library({library:o.query(t.library),multiple:t.multiple,title:t.title,priority:20})])},bindHandlers:function(){this.on("router:create:browse",this.createRouter,this),this.on("router:render:browse",this.browseRouter,this),this.on("content:create:browse",this.browseContent,this),this.on("content:render:upload",this.uploadContent,this),this.on("toolbar:create:select",this.createSelectToolbar,this)},browseRouter:function(t){t.set({upload:{text:l.uploadFilesTitle,priority:20},browse:{text:l.mediaLibraryTitle,priority:40}})},browseContent:function(t){var e=this.state();this.$el.removeClass("hide-toolbar"),t.view=new o.view.AttachmentsBrowser({controller:this,collection:e.get("library"),selection:e.get("selection"),model:e,sortable:e.get("sortable"),search:e.get("searchable"),filters:e.get("filterable"),display:e.get("displaySettings"),dragInfo:e.get("dragInfo"),AttachmentView:e.get("AttachmentView")})},uploadContent:function(){this.$el.removeClass("hide-toolbar"),this.content.set(new o.view.UploaderInline({controller:this}))},createSelectToolbar:function(t,e){(e=e||this.options.button||{}).controller=this,t.view=new o.view.Toolbar.Select(e)}}),o.view.MediaFrame.Post=o.view.MediaFrame.Select.extend({initialize:function(){_.defaults(this.options,{multiple:!0,editing:!1,state:"insert"}),o.view.MediaFrame.Select.prototype.initialize.apply(this,arguments),this.createIframeStates()},createStates:function(){var t=this.options;this.states.add([new o.controller.Library({id:"insert",title:l.insertMediaTitle,priority:20,toolbar:"main-insert",filterable:"all",library:o.query(t.library),multiple:!!t.multiple&&"reset",editable:!0,allowLocalEdits:!0,displaySettings:!0,displayUserSettings:!0}),new o.controller.Library({id:"gallery",title:l.createGalleryTitle,priority:40,toolbar:"main-gallery",filterable:"uploaded",multiple:"add",editable:!1,library:o.query(_.defaults({type:"image"},t.library))}),new o.controller.Embed,new o.controller.GalleryEdit({library:t.selection,editing:t.editing,menu:"gallery"}),new o.controller.GalleryAdd]),o.view.settings.post.featuredImageId&&this.states.add(new o.controller.FeaturedImage)},bindHandlers:function(){o.view.MediaFrame.Select.prototype.bindHandlers.apply(this,arguments),this.on("menu:create:gallery",this.createMenu,this),this.on("toolbar:create:main-insert",this.createToolbar,this),this.on("toolbar:create:main-gallery",this.createToolbar,this),this.on("toolbar:create:featured-image",this.featuredImageToolbar,this),this.on("toolbar:create:main-embed",this.mainEmbedToolbar,this);_.each({menu:{default:"mainMenu",gallery:"galleryMenu"},content:{embed:"embedContent","edit-selection":"editSelectionContent"},toolbar:{"main-insert":"mainInsertToolbar","main-gallery":"mainGalleryToolbar","gallery-edit":"galleryEditToolbar","gallery-add":"galleryAddToolbar"}},function(t,i){_.each(t,function(t,e){this.on(i+":render:"+e,this[t],this)},this)},this)},mainMenu:function(t){t.set({"library-separator":new o.View({className:"separator",priority:100})})},galleryMenu:function(t){var e=this.lastState(),i=e&&e.id,s=this;t.set({cancel:{text:l.cancelGalleryTitle,priority:20,click:function(){i?s.setState(i):s.close()}},separateCancel:new o.View({className:"separator",priority:40})})},embedContent:function(){var t=new o.view.Embed({controller:this,model:this.state()}).render();this.content.set(t),t.url.focus()},editSelectionContent:function(){var t=this.state(),e=t.get("selection"),t=new o.view.AttachmentsBrowser({controller:this,collection:e,selection:e,model:t,sortable:!0,search:!1,dragInfo:!0,AttachmentView:o.view.Attachment.EditSelection}).render();t.toolbar.set("backToLibrary",{text:l.returnToLibrary,priority:-100,click:function(){this.controller.content.mode("browse")}}),this.content.set(t)},selectionStatusToolbar:function(t){var e=this.state().get("editable");t.set("selection",new o.view.Selection({controller:this,collection:this.state().get("selection"),priority:-40,editable:e&&function(){this.controller.content.mode("edit-selection")}}).render())},mainInsertToolbar:function(t){var i=this;this.selectionStatusToolbar(t),t.set("insert",{style:"primary",priority:80,text:l.insertIntoPost,requires:{selection:!0},click:function(){var t=i.state(),e=t.get("selection");i.close(),t.trigger("insert",e).reset()}})},mainGalleryToolbar:function(t){var s=this;this.selectionStatusToolbar(t),t.set("gallery",{style:"primary",text:l.createNewGallery,priority:60,requires:{selection:!0},click:function(){var t=s.state().get("selection"),e=s.state("gallery-edit"),i=t.where({type:"image"});e.set("library",new o.model.Selection(i,{props:t.props.toJSON(),multiple:!0})),this.controller.setState("gallery-edit")}})},featuredImageToolbar:function(t){this.createSelectToolbar(t,{text:l.setFeaturedImage,state:this.options.state})},mainEmbedToolbar:function(t){t.view=new o.view.Toolbar.Embed({controller:this})},galleryEditToolbar:function(){var t=this.state().get("editing");this.toolbar.set(new o.view.Toolbar({controller:this,items:{insert:{style:"primary",text:t?l.updateGallery:l.insertGallery,priority:80,requires:{library:!0},click:function(){var t=this.controller,e=t.state();t.close(),e.trigger("update",e.get("library")),t.setState(t.options.state),t.reset()}}}}))},galleryAddToolbar:function(){this.toolbar.set(new o.view.Toolbar({controller:this,items:{insert:{style:"primary",text:l.addToGallery,priority:80,requires:{selection:!0},click:function(){var t=this.controller,e=t.state();t.state("gallery-edit").get("library").add(e.get("selection").models),e.trigger("reset"),t.setState("gallery-edit")}}}}))}}),o.view.Modal=o.View.extend({tagName:"div",template:o.template("media-modal"),attributes:{tabindex:0},events:{"click .media-modal-backdrop, .media-modal-close":"escapeHandler",keydown:"keydown"},initialize:function(){_.defaults(this.options,{container:document.body,title:"",propagate:!0,freeze:!0})},prepare:function(){return{title:this.options.title}},attach:function(){return this.views.attached?this:(this.views.rendered||this.render(),this.$el.appendTo(this.options.container),this.views.attached=!0,this.views.ready(),this.propagate("attach"))},detach:function(){return this.$el.is(":visible")&&this.close(),this.$el.detach(),this.views.attached=!1,this.propagate("detach")},open:function(){var t=this.$el,e=this.options;return t.is(":visible")?this:(this.views.attached||this.attach(),e.freeze&&(this._freeze={scrollTop:r(window).scrollTop()}),t.show().focus(),this.propagate("open"))},close:function(t){var e=this._freeze;return this.views.attached&&this.$el.is(":visible")&&(this.$el.hide(),this.propagate("close"),e&&r(window).scrollTop(e.scrollTop),t&&t.escape&&this.propagate("escape")),this},escape:function(){return this.close({escape:!0})},escapeHandler:function(t){t.preventDefault(),this.escape()},content:function(t){return this.views.set(".media-modal-content",t),this},propagate:function(t){return this.trigger(t),this.options.propagate&&this.controller.trigger(t),this},keydown:function(t){27===t.which&&(t.preventDefault(),this.escape())}}),o.view.FocusManager=o.View.extend({events:{keydown:"recordTab",focusin:"updateIndex"},focus:function(){_.isUndefined(this.index)||(this.$tabbables=this.$(":tabbable"),this.$tabbables.eq(this.index).focus())},recordTab:function(t){9===t.keyCode&&(_.isUndefined(this.index)&&this.updateIndex(t),_.isUndefined(this.index)||(0<=(t=this.index+(t.shiftKey?-1:1))&&t<this.$tabbables.length?this.index=t:delete this.index))},updateIndex:function(t){this.$tabbables=this.$(":tabbable");t=this.$tabbables.index(t.target);-1===t?delete this.index:this.index=t}}),o.view.UploaderWindow=o.View.extend({tagName:"div",className:"uploader-window",template:o.template("uploader-window"),initialize:function(){var t;this.$browser=r('<a href="#" class="browser" />').hide().appendTo("body"),!(t=this.options.uploader=_.defaults(this.options.uploader||{},{dropzone:this.$el,browser:this.$browser,params:{}})).dropzone||t.dropzone instanceof r||(t.dropzone=r(t.dropzone)),this.controller.on("activate",this.refresh,this)},refresh:function(){this.uploader&&this.uploader.refresh()},ready:function(){var t=o.view.settings.post.id;this.uploader||(t&&(this.options.uploader.params.post_id=t),this.uploader=new wp.Uploader(this.options.uploader),(t=this.uploader.dropzone).on("dropzone:enter",_.bind(this.show,this)),t.on("dropzone:leave",_.bind(this.hide,this)))},show:function(){var t=this.$el.show();_.defer(function(){t.css({opacity:1})})},hide:function(){var t=this.$el.css({opacity:0});o.transition(t).done(function(){"0"===t.css("opacity")&&t.hide()})}}),o.view.UploaderInline=o.View.extend({tagName:"div",className:"uploader-inline",template:o.template("uploader-inline"),initialize:function(){_.defaults(this.options,{message:"",status:!0}),!this.options.$browser&&this.controller.uploader&&(this.options.$browser=this.controller.uploader.$browser),_.isUndefined(this.options.postId)&&(this.options.postId=o.view.settings.post.id),this.options.status&&this.views.set(".upload-inline-status",new o.view.UploaderStatus({controller:this.controller}))},dispose:function(){return this.disposing?o.View.prototype.dispose.apply(this,arguments):(this.disposing=!0,this.remove())},remove:function(){var t=o.View.prototype.remove.apply(this,arguments);return _.defer(_.bind(this.refresh,this)),t},refresh:function(){var t=this.controller.uploader;t&&t.refresh()},ready:function(){var t,e=this.options.$browser;if(this.controller.uploader){if((t=this.$(".browser"))[0]===e[0])return;e.detach().text(t.text()),e[0].className=t[0].className,t.replaceWith(e.show())}return this.refresh(),this}}),o.view.UploaderStatus=o.View.extend({className:"media-uploader-status",template:o.template("uploader-status"),events:{"click .upload-dismiss-errors":"dismiss"},initialize:function(){this.queue=wp.Uploader.queue,this.queue.on("add remove reset",this.visibility,this),this.queue.on("add remove reset change:percent",this.progress,this),this.queue.on("add remove reset change:uploading",this.info,this),this.errors=wp.Uploader.errors,this.errors.reset(),this.errors.on("add remove reset",this.visibility,this),this.errors.on("add",this.error,this)},dispose:function(){return wp.Uploader.queue.off(null,null,this),o.View.prototype.dispose.apply(this,arguments),this},visibility:function(){this.$el.toggleClass("uploading",!!this.queue.length),this.$el.toggleClass("errors",!!this.errors.length),this.$el.toggle(!!this.queue.length||!!this.errors.length)},ready:function(){_.each({$bar:".media-progress-bar div",$index:".upload-index",$total:".upload-total",$filename:".upload-filename"},function(t,e){this[e]=this.$(t)},this),this.visibility(),this.progress(),this.info()},progress:function(){var t=this.queue,e=this.$bar;e&&t.length&&e.width(t.reduce(function(t,e){if(!e.get("uploading"))return t+100;e=e.get("percent");return t+(_.isNumber(e)?e:100)},0)/t.length+"%")},info:function(){var t,e=this.queue,i=0;e.length&&(t=this.queue.find(function(t,e){return i=e,t.get("uploading")}),this.$index.text(i+1),this.$total.text(e.length),this.$filename.html(t?this.filename(t.get("filename")):""))},filename:function(t){return o.truncate(_.escape(t),24)},error:function(t){this.views.add(".upload-errors",new o.view.UploaderStatusError({filename:this.filename(t.get("file").name),message:t.get("message")}),{at:0})},dismiss:function(t){var e=this.views.get(".upload-errors");t.preventDefault(),e&&_.invoke(e,"remove"),wp.Uploader.errors.reset()}}),o.view.UploaderStatusError=o.View.extend({className:"upload-error",template:o.template("uploader-status-error")}),o.view.Toolbar=o.View.extend({tagName:"div",className:"media-toolbar",initialize:function(){var t=this.controller.state(),e=this.selection=t.get("selection"),t=this.library=t.get("library");this._views={},this.primary=new o.view.PriorityList,this.secondary=new o.view.PriorityList,this.primary.$el.addClass("media-toolbar-primary"),this.secondary.$el.addClass("media-toolbar-secondary"),this.views.set([this.secondary,this.primary]),this.options.items&&this.set(this.options.items,{silent:!0}),this.options.silent||this.render(),e&&e.on("add remove reset",this.refresh,this),t&&t.on("add remove reset",this.refresh,this)},dispose:function(){return this.selection&&this.selection.off(null,null,this),this.library&&this.library.off(null,null,this),o.View.prototype.dispose.apply(this,arguments)},ready:function(){this.refresh()},set:function(t,e,i){return i=i||{},_.isObject(t)?_.each(t,function(t,e){this.set(e,t,{silent:!0})},this):(e instanceof Backbone.View||(e.classes=["media-button-"+t].concat(e.classes||[]),e=new o.view.Button(e).render()),e.controller=e.controller||this.controller,this._views[t]=e,this[e.options.priority<0?"secondary":"primary"].set(t,e,i)),i.silent||this.refresh(),this},get:function(t){return this._views[t]},unset:function(t,e){return delete this._views[t],this.primary.unset(t,e),this.secondary.unset(t,e),e&&e.silent||this.refresh(),this},refresh:function(){var t=this.controller.state(),s=t.get("library"),n=t.get("selection");_.each(this._views,function(t){var e,i;t.model&&t.options&&t.options.requires&&(e=t.options.requires,i=!1,i=_.some(n.models,function(t){return!0===t.get("uploading")}),(e.selection&&n&&!n.length||e.library&&s&&!s.length)&&(i=!0),t.model.set("disabled",i))})}}),o.view.Toolbar.Select=o.view.Toolbar.extend({initialize:function(){var t=this.options;t.controller.state().get("selection");_.bindAll(this,"clickSelect"),_.defaults(t,{event:"select",state:!1,reset:!0,close:!0,text:l.select,requires:{selection:!0}}),t.items=_.defaults(t.items||{},{select:{style:"primary",text:t.text,priority:80,click:this.clickSelect,requires:t.requires}}),o.view.Toolbar.prototype.initialize.apply(this,arguments)},clickSelect:function(){var t=this.options,e=this.controller;t.close&&e.close(),t.event&&e.state().trigger(t.event),t.state&&e.setState(t.state),t.reset&&e.reset()}}),o.view.Toolbar.Embed=o.view.Toolbar.Select.extend({initialize:function(){_.defaults(this.options,{text:l.insertIntoPost,requires:!1}),o.view.Toolbar.Select.prototype.initialize.apply(this,arguments)},refresh:function(){var t=this.controller.state().props.get("url");this.get("select").model.set("disabled",!t||"http://"===t),o.view.Toolbar.Select.prototype.refresh.apply(this,arguments)}}),o.view.Button=o.View.extend({tagName:"a",className:"media-button",attributes:{href:"#"},events:{click:"click"},defaults:{text:"",style:"",size:"large",disabled:!1},initialize:function(){this.model=new Backbone.Model(this.defaults),_.each(this.defaults,function(t,e){var i=this.options[e];_.isUndefined(i)||(this.model.set(e,i),delete this.options[e])},this),this.model.on("change",this.render,this)},render:function(){var t=["button",this.className],e=this.model.toJSON();return e.style&&t.push("button-"+e.style),e.size&&t.push("button-"+e.size),t=_.uniq(t.concat(this.options.classes)),this.el.className=t.join(" "),this.$el.attr("disabled",e.disabled),this.$el.text(this.model.get("text")),this},click:function(t){"#"===this.attributes.href&&t.preventDefault(),this.options.click&&!this.model.get("disabled")&&this.options.click.apply(this,arguments)}}),o.view.ButtonGroup=o.View.extend({tagName:"div",className:"button-group button-large media-button-group",initialize:function(){this.buttons=_.map(this.options.buttons||[],function(t){return t instanceof Backbone.View?t:new o.view.Button(t).render()}),delete this.options.buttons,this.options.classes&&this.$el.addClass(this.options.classes)},render:function(){return this.$el.html(r(_.pluck(this.buttons,"el")).detach()),this}}),o.view.PriorityList=o.View.extend({tagName:"div",initialize:function(){this._views={},this.set(_.extend({},this._views,this.options.views),{silent:!0}),delete this.options.views,this.options.silent||this.render()},set:function(t,e,i){var s,n;return i=i||{},_.isObject(t)?_.each(t,function(t,e){this.set(e,t)},this):((e=!(e instanceof Backbone.View)?this.toView(e,t,i):e).controller=e.controller||this.controller,this.unset(t),s=e.options.priority||10,i=this.views.get()||[],_.find(i,function(t,e){if(t.options.priority>s)return n=e,!0}),this._views[t]=e,this.views.add(e,{at:_.isNumber(n)?n:i.length||0})),this},get:function(t){return this._views[t]},unset:function(t){var e=this.get(t);return e&&e.remove(),delete this._views[t],this},toView:function(t){return new o.View(t)}}),o.view.MenuItem=o.View.extend({tagName:"a",className:"media-menu-item",attributes:{href:"#"},events:{click:"_click"},_click:function(t){var e=this.options.click;t&&t.preventDefault(),e?e.call(this):this.click()},click:function(){var t=this.options.state;t&&this.controller.setState(t)},render:function(){var t=this.options;return t.text?this.$el.text(t.text):t.html&&this.$el.html(t.html),this}}),o.view.Menu=o.view.PriorityList.extend({tagName:"div",className:"media-menu",property:"state",ItemView:o.view.MenuItem,region:"menu",toView:function(t,e){return(t=t||{})[this.property]=t[this.property]||e,new this.ItemView(t).render()},ready:function(){o.view.PriorityList.prototype.ready.apply(this,arguments),this.visibility()},set:function(){o.view.PriorityList.prototype.set.apply(this,arguments),this.visibility()},unset:function(){o.view.PriorityList.prototype.unset.apply(this,arguments),this.visibility()},visibility:function(){var t=this.region,e=this.controller[t].get(),i=this.views.get(),i=!i||i.length<2;this===e&&this.controller.$el.toggleClass("hide-"+t,i)},select:function(t){t=this.get(t);t&&(this.deselect(),t.$el.addClass("active"))},deselect:function(){this.$el.children().removeClass("active")}}),o.view.RouterItem=o.view.MenuItem.extend({click:function(){var t=this.options.contentMode;t&&this.controller.content.mode(t)}}),o.view.Router=o.view.Menu.extend({tagName:"div",className:"media-router",property:"contentMode",ItemView:o.view.RouterItem,region:"router",initialize:function(){this.controller.on("content:render",this.update,this),o.view.Menu.prototype.initialize.apply(this,arguments)},update:function(){var t=this.controller.content.mode();t&&this.select(t)}}),o.view.Sidebar=o.view.PriorityList.extend({className:"media-sidebar"}),o.view.Attachment=o.View.extend({tagName:"li",className:"attachment",template:o.template("attachment"),events:{"click .attachment-preview":"toggleSelectionHandler","change [data-setting]":"updateSetting","change [data-setting] input":"updateSetting","change [data-setting] select":"updateSetting","change [data-setting] textarea":"updateSetting","click .close":"removeFromLibrary","click .check":"removeFromSelection","click a":"preventDefault"},buttons:{},initialize:function(){var t=this.options.selection;this.model.on("change:sizes change:uploading",this.render,this),this.model.on("change:title",this._syncTitle,this),this.model.on("change:caption",this._syncCaption,this),this.model.on("change:percent",this.progress,this),this.model.on("add",this.select,this),this.model.on("remove",this.deselect,this),t&&t.on("reset",this.updateSelect,this),this.model.on("selection:single selection:unsingle",this.details,this),this.details(this.model,this.controller.state().get("selection"))},dispose:function(){var t=this.options.selection;return this.updateAll(),t&&t.off(null,null,this),o.View.prototype.dispose.apply(this,arguments),this},render:function(){var t=_.defaults(this.model.toJSON(),{orientation:"landscape",uploading:!1,type:"",subtype:"",icon:"",filename:"",caption:"",title:"",dateFormatted:"",width:"",height:"",compat:!1,alt:"",description:""});return t.buttons=this.buttons,t.describe=this.controller.state().get("describe"),"image"===t.type&&(t.size=this.imageSize()),t.can={},t.nonces&&(t.can.remove=!!t.nonces.delete,t.can.save=!!t.nonces.update),this.controller.state().get("allowLocalEdits")&&(t.allowLocalEdits=!0),this.views.detach(),this.$el.html(this.template(t)),this.$el.toggleClass("uploading",t.uploading),t.uploading?this.$bar=this.$(".media-progress-bar div"):delete this.$bar,this.updateSelect(),this.updateSave(),this.views.render(),this},progress:function(){this.$bar&&this.$bar.length&&this.$bar.width(this.model.get("percent")+"%")},toggleSelectionHandler:function(t){var e;t.shiftKey?e="between":(t.ctrlKey||t.metaKey)&&(e="toggle"),this.toggleSelection({method:e})},toggleSelection:function(t){var e,i,s=this.collection,n=this.options.selection,o=this.model,r=t&&t.method;if(n){if(e=n.single(),"between"===(r=_.isUndefined(r)?n.multiple:r)&&e&&n.multiple)return e===o?void 0:(i=(i=s.indexOf(e))<(t=s.indexOf(this.model))?s.models.slice(i,t+1):s.models.slice(t,i+1),void n.add(i).single(o));"toggle"!==r?("add"!==r&&(r="reset"),this.selected()?n[e===o?"remove":"single"](o):n[r](o).single(o)):n[this.selected()?"remove":"add"](o).single(o)}},updateSelect:function(){this[this.selected()?"select":"deselect"]()},selected:function(){var t=this.options.selection;if(t)return!!t.get(this.model.cid)},select:function(t,e){var i=this.options.selection;!i||e&&e!==i||this.$el.addClass("selected")},deselect:function(t,e){var i=this.options.selection;!i||e&&e!==i||this.$el.removeClass("selected")},details:function(t,e){var i=this.options.selection;i===e&&(i=i.single(),this.$el.toggleClass("details",i===this.model))},preventDefault:function(t){t.preventDefault()},imageSize:function(t){var e=this.model.get("sizes");return t=t||"medium",e&&e[t]?_.clone(e[t]):{url:this.model.get("url"),width:this.model.get("width"),height:this.model.get("height"),orientation:this.model.get("orientation")}},updateSetting:function(t){var e=r(t.target).closest("[data-setting]");e.length&&(e=e.data("setting"),t=t.target.value,this.model.get(e)!==t&&this.save(e,t))},save:function(){var t=this,e=this._save=this._save||{status:"ready"},i=this.model.save.apply(this.model,arguments),s=e.requests?r.when(i,e.requests):i;e.savedTimer&&clearTimeout(e.savedTimer),this.updateSave("waiting"),(e.requests=s).always(function(){e.requests===s&&(t.updateSave("resolved"===s.state()?"complete":"error"),e.savedTimer=setTimeout(function(){t.updateSave("ready"),delete e.savedTimer},2e3))})},updateSave:function(t){var e=this._save=this._save||{status:"ready"};return t&&t!==e.status&&(this.$el.removeClass("save-"+e.status),e.status=t),this.$el.addClass("save-"+e.status),this},updateAll:function(){var t=this.$("[data-setting]"),i=this.model,t=_.chain(t).map(function(t){var e=r("input, textarea, select, [value]",t);if(e.length)return t=r(t).data("setting"),e=e.val(),i.get(t)!==e?[t,e]:void 0}).compact().object().value();_.isEmpty(t)||i.save(t)},removeFromLibrary:function(t){t.stopPropagation(),this.collection.remove(this.model)},removeFromSelection:function(t){var e=this.options.selection;e&&(t.stopPropagation(),e.remove(this.model))}}),_.each({caption:"_syncCaption",title:"_syncTitle"},function(t,s){o.view.Attachment.prototype[t]=function(t,e){var i=this.$('[data-setting="'+s+'"]');return!i.length||e===i.find("input, textarea, select, [value]").val()?this:this.render()}}),o.view.Attachment.Library=o.view.Attachment.extend({buttons:{check:!0}}),o.view.Attachment.EditLibrary=o.view.Attachment.extend({buttons:{close:!0}}),o.view.Attachments=o.View.extend({tagName:"ul",className:"attachments",cssTemplate:o.template("attachments-css"),events:{scroll:"scroll"},initialize:function(){this.el.id=_.uniqueId("__attachments-view-"),_.defaults(this.options,{refreshSensitivity:200,refreshThreshold:3,AttachmentView:o.view.Attachment,sortable:!1,resize:!0}),this._viewsByCid={},this.collection.on("add",function(t,e,i){this.views.add(this.createAttachmentView(t),{at:this.collection.indexOf(t)})},this),this.collection.on("remove",function(t,e,i){var s=this._viewsByCid[t.cid];delete this._viewsByCid[t.cid],s&&s.remove()},this),this.collection.on("reset",this.render,this),this.scroll=_.chain(this.scroll).bind(this).throttle(this.options.refreshSensitivity).value(),this.initSortable(),_.bindAll(this,"css"),this.model.on("change:edge change:gutter",this.css,this),this._resizeCss=_.debounce(_.bind(this.css,this),this.refreshSensitivity),this.options.resize&&r(window).on("resize.attachments",this._resizeCss),this.css()},dispose:function(){this.collection.props.off(null,null,this),r(window).off("resize.attachments",this._resizeCss),o.View.prototype.dispose.apply(this,arguments)},css:function(){var t=r("#"+this.el.id+"-css");t.length&&t.remove(),o.view.Attachments.$head().append(this.cssTemplate({id:this.el.id,edge:this.edge(),gutter:this.model.get("gutter")}))},edge:function(){var t,e,i,s=this.model.get("edge");return this.$el.is(":visible")?(t=2*this.model.get("gutter"),e=this.$el.width()-t,i=Math.ceil(e/(s+t)),s=Math.floor((e-i*t)/i)):s},initSortable:function(){var n=this.collection;this.options.sortable&&r.fn.sortable&&(this.$el.sortable(_.extend({disabled:!!n.comparator,containment:this.$el,tolerance:"pointer",start:function(t,e){e.item.data("sortableIndexStart",e.item.index())},update:function(t,e){var i=n.at(e.item.data("sortableIndexStart")),s=n.comparator;delete n.comparator,n.remove(i,{silent:!0}).add(i,{silent:!0,at:e.item.index()}),n.comparator=s,n.trigger("reset",n),n.saveMenuOrder()}},this.options.sortable)),n.props.on("change:orderby",function(){this.$el.sortable("option","disabled",!!n.comparator)},this),this.collection.props.on("change:orderby",this.refreshSortable,this),this.refreshSortable())},refreshSortable:function(){var t;this.options.sortable&&r.fn.sortable&&(t="menuOrder"===(t=this.collection).props.get("orderby")||!t.comparator,this.$el.sortable("option","disabled",!t))},createAttachmentView:function(t){var e=new this.options.AttachmentView({controller:this.controller,model:t,collection:this.collection,selection:this.options.selection});return this._viewsByCid[t.cid]=e},prepare:function(){this.collection.length?this.views.set(this.collection.map(this.createAttachmentView,this)):(this.views.unset(),this.collection.more().done(this.scroll))},ready:function(){this.scroll()},scroll:function(t){this.$el.is(":visible")&&this.collection.hasMore()&&this.el.scrollHeight<this.el.scrollTop+this.el.clientHeight*this.options.refreshThreshold&&this.collection.more().done(this.scroll)}},{$head:function(){return s=s||r("head")}}),o.view.Search=o.View.extend({tagName:"input",className:"search",attributes:{type:"search",placeholder:l.search},events:{input:"search",keyup:"search",change:"search",search:"search"},render:function(){return this.el.value=this.model.escape("search"),this},search:function(t){t.target.value?this.model.set("search",t.target.value):this.model.unset("search")}}),o.view.AttachmentFilters=o.View.extend({tagName:"select",className:"attachment-filters",events:{change:"change"},keys:[],initialize:function(){this.createFilters(),_.extend(this.filters,this.options.filters),this.$el.html(_.chain(this.filters).map(function(t,e){return{el:r("<option></option>").val(e).text(t.text)[0],priority:t.priority||50}},this).sortBy("priority").pluck("el").value()),this.model.on("change",this.select,this),this.select()},createFilters:function(){this.filters={}},change:function(t){var e=this.filters[this.el.value];e&&this.model.set(e.props)},select:function(){var t=this.model,i="all",s=t.toJSON();_.find(this.filters,function(t,e){if(_.all(t.props,function(t,e){return t===(_.isUndefined(s[e])?null:s[e])}))return i=e}),this.$el.val(i)}}),o.view.AttachmentFilters.Uploaded=o.view.AttachmentFilters.extend({createFilters:function(){var t,e=this.model.get("type"),i=o.view.settings.mimeTypes;i&&e&&(t=i[e]),this.filters={all:{text:t||l.allMediaItems,props:{uploadedTo:null,orderby:"date",order:"DESC"},priority:10},uploaded:{text:l.uploadedToThisPost,props:{uploadedTo:o.view.settings.post.id,orderby:"menuOrder",order:"ASC"},priority:20}}}}),o.view.AttachmentFilters.All=o.view.AttachmentFilters.extend({createFilters:function(){var i={};_.each(o.view.settings.mimeTypes||{},function(t,e){i[e]={text:t,props:{type:e,uploadedTo:null,orderby:"date",order:"DESC"}}}),i.all={text:l.allMediaItems,props:{type:null,uploadedTo:null,orderby:"date",order:"DESC"},priority:10},i.uploaded={text:l.uploadedToThisPost,props:{type:null,uploadedTo:o.view.settings.post.id,orderby:"menuOrder",order:"ASC"},priority:20},this.filters=i}}),o.view.AttachmentsBrowser=o.View.extend({tagName:"div",className:"attachments-browser",initialize:function(){_.defaults(this.options,{filters:!1,search:!0,display:!1,AttachmentView:o.view.Attachment.Library}),this.createToolbar(),this.updateContent(),this.createSidebar(),this.collection.on("add remove reset",this.updateContent,this)},dispose:function(){return this.options.selection.off(null,null,this),o.View.prototype.dispose.apply(this,arguments),this},createToolbar:function(){var t,e;this.toolbar=new o.view.Toolbar({controller:this.controller}),this.views.add(this.toolbar),"uploaded"===(t=this.options.filters)?e=o.view.AttachmentFilters.Uploaded:"all"===t&&(e=o.view.AttachmentFilters.All),e&&this.toolbar.set("filters",new e({controller:this.controller,model:this.collection.props,priority:-80}).render()),this.options.search&&this.toolbar.set("search",new o.view.Search({controller:this.controller,model:this.collection.props,priority:60}).render()),this.options.dragInfo&&this.toolbar.set("dragInfo",new o.View({el:r('<div class="instructions">'+l.dragInfo+"</div>")[0],priority:-40}))},updateContent:function(){var t=this;this.attachments||this.createAttachments(),this.collection.length||this.collection.more().done(function(){t.collection.length||t.createUploader()})},removeContent:function(){_.each(["attachments","uploader"],function(t){this[t]&&(this[t].remove(),delete this[t])},this)},createUploader:function(){this.removeContent(),this.uploader=new o.view.UploaderInline({controller:this.controller,status:!1,message:l.noItemsFound}),this.views.add(this.uploader)},createAttachments:function(){this.removeContent(),this.attachments=new o.view.Attachments({controller:this.controller,collection:this.collection,selection:this.options.selection,model:this.model,sortable:this.options.sortable,AttachmentView:this.options.AttachmentView}),this.views.add(this.attachments)},createSidebar:function(){var t=this.options.selection,e=this.sidebar=new o.view.Sidebar({controller:this.controller});this.views.add(e),this.controller.uploader&&e.set("uploads",new o.view.UploaderStatus({controller:this.controller,priority:40})),t.on("selection:single",this.createSingle,this),t.on("selection:unsingle",this.disposeSingle,this),t.single()&&this.createSingle()},createSingle:function(){var t=this.sidebar,e=this.options.selection.single();t.set("details",new o.view.Attachment.Details({controller:this.controller,model:e,priority:80})),t.set("compat",new o.view.AttachmentCompat({controller:this.controller,model:e,priority:120})),this.options.display&&t.set("display",new o.view.Settings.AttachmentDisplay({controller:this.controller,model:this.model.display(e),attachment:e,priority:160,userSettings:this.model.get("displayUserSettings")}))},disposeSingle:function(){var t=this.sidebar;t.unset("details"),t.unset("compat"),t.unset("display")}}),o.view.Selection=o.View.extend({tagName:"div",className:"media-selection",template:o.template("media-selection"),events:{"click .edit-selection":"edit","click .clear-selection":"clear"},initialize:function(){_.defaults(this.options,{editable:!1,clearable:!0}),this.attachments=new o.view.Attachments.Selection({controller:this.controller,collection:this.collection,selection:this.collection,model:new Backbone.Model({edge:40,gutter:5})}),this.views.set(".selection-view",this.attachments),this.collection.on("add remove reset",this.refresh,this),this.controller.on("content:activate",this.refresh,this)},ready:function(){this.refresh()},refresh:function(){var t,e;this.$el.children().length&&(t=this.collection,e="edit-selection"===this.controller.content.mode(),this.$el.toggleClass("empty",!t.length),this.$el.toggleClass("one",1===t.length),this.$el.toggleClass("editing",e),this.$(".count").text(l.selected.replace("%d",t.length)))},edit:function(t){t.preventDefault(),this.options.editable&&this.options.editable.call(this,this.collection)},clear:function(t){t.preventDefault(),this.collection.reset()}}),o.view.Attachment.Selection=o.view.Attachment.extend({className:"attachment selection",toggleSelection:function(){this.options.selection.single(this.model)}}),o.view.Attachments.Selection=o.view.Attachments.extend({events:{},initialize:function(){return _.defaults(this.options,{sortable:!0,resize:!1,AttachmentView:o.view.Attachment.Selection}),o.view.Attachments.prototype.initialize.apply(this,arguments)}}),o.view.Attachment.EditSelection=o.view.Attachment.Selection.extend({buttons:{close:!0}}),o.view.Settings=o.View.extend({events:{"click button":"updateHandler","change input":"updateHandler","change select":"updateHandler","change textarea":"updateHandler"},initialize:function(){this.model=this.model||new Backbone.Model,this.model.on("change",this.updateChanges,this)},prepare:function(){return _.defaults({model:this.model.toJSON()},this.options)},render:function(){return o.View.prototype.render.apply(this,arguments),_(this.model.attributes).chain().keys().each(this.update,this),this},update:function(t){var e,i=this.model.get(t),s=this.$('[data-setting="'+t+'"]');s.length&&(s.is("select")?(e=s.find('[value="'+i+'"]')).length?(s.find("option").prop("selected",!1),e.prop("selected",!0)):this.model.set(t,s.find(":selected").val()):s.hasClass("button-group")?s.find("button").removeClass("active").filter('[value="'+i+'"]').addClass("active"):s.is('input[type="text"], textarea')?s.is(":focus")||s.val(i):s.is('input[type="checkbox"]')&&s.attr("checked",!!i))},updateHandler:function(t){var e=r(t.target).closest("[data-setting]"),i=t.target.value;t.preventDefault(),e.length&&(e.is('input[type="checkbox"]')&&(i=e[0].checked),this.model.set(e.data("setting"),i),(e=e.data("userSetting"))&&setUserSetting(e,i))},updateChanges:function(t,e){t.hasChanged()&&_(t.changed).chain().keys().each(this.update,this)}}),o.view.Settings.AttachmentDisplay=o.view.Settings.extend({className:"attachment-display-settings",template:o.template("attachment-display-settings"),initialize:function(){var t=this.options.attachment;_.defaults(this.options,{userSettings:!1}),o.view.Settings.prototype.initialize.apply(this,arguments),this.model.on("change:link",this.updateLinkTo,this),t&&t.on("change:uploading",this.render,this)},dispose:function(){var t=this.options.attachment;t&&t.off(null,null,this),o.view.Settings.prototype.dispose.apply(this,arguments)},render:function(){var t=this.options.attachment;return t&&_.extend(this.options,{sizes:t.get("sizes"),type:t.get("type")}),o.view.Settings.prototype.render.call(this),this.updateLinkTo(),this},updateLinkTo:function(){var t=this.model.get("link"),e=this.$(".link-to-custom"),i=this.options.attachment;"none"===t||"embed"===t||!i&&"custom"!==t?e.hide():(i&&("post"===t?e.val(i.get("link")):"file"===t?e.val(i.get("url")):this.model.get("linkUrl")||e.val("http://"),e.prop("readonly","custom"!==t)),e.show(),e.is(":visible")&&e.focus()[0].select())}}),o.view.Settings.Gallery=o.view.Settings.extend({className:"gallery-settings",template:o.template("gallery-settings")}),o.view.Attachment.Details=o.view.Attachment.extend({tagName:"div",className:"attachment-details",template:o.template("attachment-details"),events:{"change [data-setting]":"updateSetting","change [data-setting] input":"updateSetting","change [data-setting] select":"updateSetting","change [data-setting] textarea":"updateSetting","click .delete-attachment":"deleteAttachment","click .edit-attachment":"editAttachment","click .refresh-attachment":"refreshAttachment"},initialize:function(){this.focusManager=new o.view.FocusManager({el:this.el}),o.view.Attachment.prototype.initialize.apply(this,arguments)},render:function(){return o.view.Attachment.prototype.render.apply(this,arguments),this.focusManager.focus(),this},deleteAttachment:function(t){t.preventDefault(),confirm(l.warnDelete)&&this.model.destroy()},editAttachment:function(t){this.$el.addClass("needs-refresh")},refreshAttachment:function(t){this.$el.removeClass("needs-refresh"),t.preventDefault(),this.model.fetch()}}),o.view.AttachmentCompat=o.View.extend({tagName:"form",className:"compat-item",events:{submit:"preventDefault","change input":"save","change select":"save","change textarea":"save"},initialize:function(){this.focusManager=new o.view.FocusManager({el:this.el}),this.model.on("change:compat",this.render,this)},dispose:function(){return this.$(":focus").length&&this.save(),o.View.prototype.dispose.apply(this,arguments)},render:function(){var t=this.model.get("compat");if(t&&t.item)return this.views.detach(),this.$el.html(t.item),this.views.render(),this.focusManager.focus(),this},preventDefault:function(t){t.preventDefault()},save:function(t){var e={};t&&t.preventDefault(),_.each(this.$el.serializeArray(),function(t){e[t.name]=t.value}),this.model.saveCompat(e)}}),o.view.Iframe=o.View.extend({className:"media-iframe",render:function(){return this.views.detach(),this.$el.html('<iframe src="'+this.controller.state().get("src")+'" />'),this.views.render(),this}}),o.view.Embed=o.View.extend({className:"media-embed",initialize:function(){this.url=new o.view.EmbedUrl({controller:this.controller,model:this.model.props}).render(),this.views.set([this.url]),this.refresh(),this.model.on("change:type",this.refresh,this),this.model.on("change:loading",this.loading,this)},settings:function(t){this._settings&&this._settings.remove(),this._settings=t,this.views.add(t)},refresh:function(){var t,e=this.model.get("type");if("image"===e)t=o.view.EmbedImage;else{if("link"!==e)return;t=o.view.EmbedLink}this.settings(new t({controller:this.controller,model:this.model.props,priority:40}))},loading:function(){this.$el.toggleClass("embed-loading",this.model.get("loading"))}}),o.view.EmbedUrl=o.View.extend({tagName:"label",className:"embed-url",events:{input:"url",keyup:"url",change:"url"},initialize:function(){this.$input=r("<input/>").attr("type","text").val(this.model.get("url")),this.input=this.$input[0],this.spinner=r('<span class="spinner" />')[0],this.$el.append([this.input,this.spinner]),this.model.on("change:url",this.render,this)},render:function(){if(!this.$input.is(":focus"))return this.input.value=this.model.get("url")||"http://",o.View.prototype.render.apply(this,arguments),this},ready:function(){this.focus()},url:function(t){this.model.set("url",t.target.value)},focus:function(){var t=this.$input;t.is(":visible")&&t.focus()[0].select()}}),o.view.EmbedLink=o.view.Settings.extend({className:"embed-link-settings",template:o.template("embed-link-settings")}),o.view.EmbedImage=o.view.Settings.AttachmentDisplay.extend({className:"embed-image-settings",template:o.template("embed-image-settings"),initialize:function(){o.view.Settings.AttachmentDisplay.prototype.initialize.apply(this,arguments),this.model.on("change:url",this.updateImage,this)},updateImage:function(){this.$("img").attr("src",this.model.get("url"))}})}(jQuery); |