7ec25963e0
git-svn-id: https://develop.svn.wordpress.org/trunk@22438 602fd350-edb4-49c9-b593-d223f7449a82
1 line
32 KiB
JavaScript
1 line
32 KiB
JavaScript
(function(e){var f=wp.media,d=f.model.Attachment,c=f.model.Attachments,a=f.model.Query,b;b=f.view.l10n=_.isUndefined(_wpMediaViewsL10n)?{}:_wpMediaViewsL10n;e.support.transition=(function(){var g=document.documentElement.style,h={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"},i;i=_.find(_.keys(h),function(j){return !_.isUndefined(g[j])});return i&&{end:h[i]}}());f.transition=function(g){var h=e.Deferred();if(e.support.transition){if(!(g instanceof e)){g=e(g)}g.first().one(e.support.transition.end,h.resolve)}else{h.resolve()}return h.promise()};f.controller.Region=function(g){_.extend(this,_.pick(g||{},"id","controller"));this.on("empty",this.empty,this);this.mode("empty")};f.controller.Region.extend=Backbone.Model.extend;_.extend(f.controller.Region.prototype,Backbone.Events,{trigger:function(g){this._mode=g;return Backbone.Events.trigger.apply(this,arguments)},mode:function(g){if(g){return this.trigger(g)}return this._mode},view:function(g){var h=this._view,i=this._mode,j=this.id;if(!g){return h}if(g===h){return}if(j){g.$el.addClass("region-"+j)}if(i){g.$el.addClass("mode-"+i)}if(h){if(h.destroy){h.destroy()}h.undelegateEvents();h.$el.replaceWith(g.$el)}this._view=g},empty:function(){this.view(new Backbone.View())}});f.controller.StateMachine=function(g){this.states=new Backbone.Collection(g)};f.controller.StateMachine.extend=Backbone.Model.extend;_.extend(f.controller.StateMachine.prototype,Backbone.Events,{get:function(g){this.states=this.states||new Backbone.Collection();if(!this.states.get(g)){this.states.add({id:g})}return this.states.get(g)},state:function(h){var g;if(!h){return this._state?this.get(this._state):null}g=this.state();if((g&&h===g.id)||!this.states||!this.states.get(h)){return}if(g){g.trigger("deactivate");this._previous=g.id}this._state=h;this.state().trigger("activate")},previous:function(){return this._previous}});_.each(["on","off","trigger"],function(g){f.controller.StateMachine.prototype[g]=function(){this.states=this.states||new Backbone.Collection();this.states[g].apply(this.states,arguments);return this}});f.controller.State=Backbone.Model.extend({initialize:function(){this.on("activate",this._activate,this);this.on("activate",this.activate,this);this.on("deactivate",this._deactivate,this);this.on("deactivate",this.deactivate,this);this.on("reset",this.reset,this)},activate:function(){},_activate:function(){this.active=true;this.menu();this.toolbar();this.sidebar();this.content()},deactivate:function(){},_deactivate:function(){this.active=false},reset:function(){},menu:function(){var i=this.frame.menu,h=this.get("menu"),g;if(!h){return}if(i.mode()!==h){i.mode(h)}g=i.view();if(g.select){g.select(this.id)}}});_.each(["toolbar","sidebar","content"],function(g){f.controller.State.prototype[g]=function(){var h=this.get(g);if(h){this.frame[g].mode(h)}}});f.controller.Library=f.controller.State.extend({defaults:{id:"library",multiple:false,describe:false,toolbar:"main-attachments",sidebar:"settings"},initialize:function(){if(!this.get("selection")){this.set("selection",new f.model.Selection(null,{multiple:this.get("multiple")}))}if(!this.get("library")){this.set("library",f.query())}if(!this.get("edge")){this.set("edge",120)}if(!this.get("gutter")){this.set("gutter",8)}f.controller.State.prototype.initialize.apply(this,arguments)},activate:function(){var g=this.get("selection");if(this.get("multiple")){wp.Uploader.queue.on("add",this.selectUpload,this)}g.on("selection:single",this.buildDetails,this);g.on("selection:unsingle",this.clearDetails,this);g.on("add remove reset",this.refreshToolbar,this);this._updateEmpty();this.get("library").on("add remove reset",this._updateEmpty,this);this.on("change:empty",this.refresh,this);this.refresh()},deactivate:function(){wp.Uploader.queue.off("add",this.selectUpload,this);this.get("selection").off(null,null,this);this.get("library").off("add remove reset",this._updateEmpty,this);this.off("change:empty",this.refresh,this)},reset:function(){this.get("selection").clear()},sidebar:function(){f.controller.State.prototype.sidebar.apply(this,arguments);this.details()},content:function(){var g=this.frame;if(this.get("empty")){this.get("library").more();g.content.mode("upload")}else{g.content.mode("browse")}},refresh:function(){this.frame.$el.toggleClass("hide-sidebar hide-toolbar",this.get("empty"));this.content()},_updateEmpty:function(){var g=this.get("library");this.set("empty",!g.length&&!g.props.get("search"))},refreshToolbar:function(){this.frame.toolbar.view().refresh()},selectUpload:function(g){this.get("selection").add(g)},details:function(){var g=this.get("selection").single();this[g?"buildDetails":"clearDetails"](g)},buildDetails:function(g){var h=this.frame;h.sidebar.view().add("details",new f.view.Attachment.Details({controller:h,model:g,priority:80}).render());return this},clearDetails:function(g){if(this.get("selection").single()){return this}this.frame.sidebar.view().add("details",new Backbone.View({priority:80}).render());return this},toggleSelection:function(g){var h=this.get("selection");if(h.has(g)){h[h.single()===g?"remove":"single"](g)}else{h.add(g).single()}return this}});f.controller.Upload=f.controller.Library.extend({defaults:_.defaults({id:"upload"},f.controller.Library.prototype.defaults),initialize:function(){var g=this.get("library");if(!g){g=new c();g.props.set({orderby:"date",order:"ASC"});g.observe(wp.Uploader.queue);this.set("library",g)}f.controller.Library.prototype.initialize.apply(this,arguments)}});f.controller.Gallery=f.controller.Library.extend({defaults:{id:"gallery-edit",multiple:false,describe:true,edge:199,editing:false,sortable:true,toolbar:"gallery-edit",sidebar:"settings"},initialize:function(){if(!this.get("AttachmentView")){this.set("AttachmentView",f.view.Attachment.Gallery)}f.controller.Library.prototype.initialize.apply(this,arguments)},sidebar:function(){var g=this.frame;f.controller.State.prototype.sidebar.apply(this,arguments);this.details();g.sidebar.view().add({settings:new f.view.Settings.Gallery({controller:g,model:this.get("library").props,priority:40}).render()})}});f.view.Frame=Backbone.View.extend({initialize:function(){this._createRegions();this._createStates()},_createRegions:function(){this.regions=this.regions?this.regions.slice():[];_.each(this.regions,function(g){this[g]=new f.controller.Region({controller:this,id:g})},this)},_createStates:function(){this.states=new Backbone.Collection();this.states.on("add",function(g){g.frame=this},this)},render:function(){var g=_.map(this.regions,function(h){return this[h].view().el},this);e(g).detach();this.$el.html(g);return this},reset:function(){this.states.invoke("trigger","reset")}});_.extend(f.view.Frame.prototype,f.controller.StateMachine.prototype);f.view.MediaFrame=f.view.Frame.extend({className:"media-frame",regions:["menu","content","sidebar","toolbar"],initialize:function(){f.view.Frame.prototype.initialize.apply(this,arguments);_.defaults(this.options,{title:"",modal:true,uploader:true});if(this.options.modal){this.modal=new f.view.Modal({controller:this,$content:this.$el,title:this.options.title})}if(this.options.uploader){this.uploader=new f.view.UploaderWindow({uploader:{dropzone:this.modal?this.modal.$el:this.$el}})}},render:function(){if(this.modal){this.modal.render()}f.view.Frame.prototype.render.apply(this,arguments);if(this.uploader){this.uploader.render().$el.appendTo(this.$el)}return this}});_.each(["open","close","attach","detach"],function(g){f.view.MediaFrame.prototype[g]=function(h){if(this.modal){this.modal[g].apply(this.modal,arguments)}return this}});f.view.MediaFrame.Post=f.view.MediaFrame.extend({initialize:function(){f.view.MediaFrame.prototype.initialize.apply(this,arguments);_.defaults(this.options,{state:"upload",selection:[],library:{},multiple:false,editing:false});this.bindHandlers();this.createSelection();this.createStates()},bindHandlers:function(){this.menu.on("main",this.mainMenu,this);this.menu.on("batch",this.batchMenu,this);this.menu.on("gallery",this.galleryMenu,this);this.content.on("browse",this.browseContent,this);this.content.on("upload",this.uploadContent,this);this.content.on("embed",this.embedContent,this);this.sidebar.on("settings",this.settingsSidebar,this);this.sidebar.on("attachment-settings",this.attachmentSettingsSidebar,this);this.toolbar.on("main-attachments",this.mainAttachmentsToolbar,this);this.toolbar.on("main-embed",this.mainEmbedToolbar,this);this.toolbar.on("batch-edit",this.batchEditToolbar,this);this.toolbar.on("batch-add",this.batchAddToolbar,this);this.toolbar.on("gallery-edit",this.galleryEditToolbar,this);this.toolbar.on("gallery-add",this.galleryAddToolbar,this)},createSelection:function(){var g=this,h=this.options.selection;if(!(h instanceof f.model.Selection)){h=this.options.selection=new f.model.Selection(h,{multiple:this.options.multiple})}},createStates:function(){var g=this.options;this.states.add([new f.controller.Library({selection:g.selection,library:f.query(g.library),multiple:this.options.multiple,menu:"main",sidebar:"attachment-settings"}),new f.controller.Upload({multiple:this.options.multiple,menu:"main",sidebar:"attachment-settings"}),new f.controller.Gallery({editing:g.editing,menu:"gallery"}),new f.controller.Library({id:"gallery-library",library:f.query({type:"image"}),multiple:true,menu:"gallery",toolbar:"gallery-add"}),new f.controller.Upload({id:"gallery-upload",multiple:true,menu:"gallery",toolbar:"gallery-add"})]);this.get("gallery-edit").on("change:library",this.updateGalleryLibraries,this).set({library:g.selection});this.state(g.state)},updateGalleryLibraries:function(){var g=this.get("gallery-edit").get("library");_.each(["gallery-library","gallery-upload"],function(k){var i=this.get(k),h=i.get("_library"),j;if(!h){i.set("_library",h=i.get("library"))}j=new c(null,{props:_.pick(h.props.toJSON(),"order","orderby")});j.filters.difference=function(l){return !h.getByCid(l.cid)||!!g.getByCid(l.cid)};j.evaluate=function(n){var l=!this.validator(n),m=!!this.getByCid(n.cid);if(!l&&m){this.remove(n)}else{if(l&&!m){this.add(n).sort()}}return this};j.evaluateAll=function(l){_.each(l.models,this.evaluate,this);return this};j.on("add remove",j.evaluate,j);j.on("reset",j.evaluateAll,j);g.on("add remove",j.evaluate,j);g.on("reset",j.evaluateAll,j);j.mirror(h);j.evaluateAll(g);i.set("library",j)},this)},mainMenu:function(){this.menu.view(new f.view.Menu({controller:this,views:{upload:{text:b.uploadFilesTitle,priority:20},library:{text:b.mediaLibraryTitle,priority:40},separateLibrary:new Backbone.View({className:"separator",priority:60}),embed:{text:b.embedFromUrlTitle,priority:80}}}))},batchMenu:function(){},galleryMenu:function(){var g=this.previous(),h=this;this.menu.view(new f.view.Menu({controller:this,views:{cancel:{text:b.cancelGalleryTitle,priority:20,click:function(){if(g){h.state(g)}else{h.close()}}},separateCancel:new Backbone.View({className:"separator",priority:40}),"gallery-edit":{text:b.editGalleryTitle,priority:60},"gallery-upload":{text:b.uploadImagesTitle,priority:80},"gallery-library":{text:b.mediaLibraryTitle,priority:100}}}))},browseContent:function(){var g=this.state();this.content.view(new f.view.AttachmentsBrowser({controller:this,collection:g.get("library"),model:g,sortable:g.get("sortable"),AttachmentView:g.get("AttachmentView")}).render())},uploadContent:function(){this.content.view(new f.view.UploaderInline({controller:this}).render())},embedContent:function(){},settingsSidebar:function(){this.sidebar.view(new f.view.Sidebar({controller:this}))},attachmentSettingsSidebar:function(){this.sidebar.view(new f.view.Sidebar({controller:this,views:{settings:new f.view.Settings.AttachmentDisplay({controller:this,priority:20}).render()}}))},mainAttachmentsToolbar:function(){this.toolbar.view(new f.view.Toolbar.Insert.Post({controller:this}))},mainEmbedToolbar:function(){},batchEditToolbar:function(){},batchAddToolbar:function(){},galleryEditToolbar:function(){var g=this.state().get("editing");this.toolbar.view(new f.view.Toolbar({controller:this,items:{insert:{style:"primary",text:g?b.updateGallery:b.insertGallery,priority:80,click:function(){var h=this.controller,i=h.state();h.close();i.trigger("update",i.get("library"));h.reset();h.state("upload")}}}}))},galleryAddToolbar:function(){this.toolbar.view(new f.view.Toolbar({controller:this,items:{insert:{style:"primary",text:b.addToGallery,priority:80,click:function(){var g=this.controller,i=g.state(),h=g.get("gallery-edit");h.get("library").add(i.get("selection").models);i.trigger("reset");g.state("gallery-edit")}}}}))}});f.view.Modal=Backbone.View.extend({tagName:"div",template:f.template("media-modal"),events:{"click .media-modal-backdrop, .media-modal-close":"closeHandler"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{container:document.body,title:""})},render:function(){this.options.$content=this.options.$content||e("<div />");this.options.$content.detach();this.$el.html(this.template({title:this.options.title}));this.options.$content.addClass("media-modal-content");this.$(".media-modal").append(this.options.$content);return this},attach:function(){this.$el.appendTo(this.options.container);this.controller.trigger("attach",this.controller);return this},detach:function(){this.$el.detach();this.controller.trigger("detach",this.controller);return this},open:function(){this.$el.show();this.controller.trigger("open",this.controller);return this},close:function(){this.$el.hide();this.controller.trigger("close",this.controller);return this},closeHandler:function(g){g.preventDefault();this.close()},content:function(g){if(this.options.$content){this.options.$content.detach()}this.options.$content=(g instanceof Backbone.View)?g.$el:g;return this.render()}});f.view.UploaderWindow=Backbone.View.extend({tagName:"div",className:"uploader-window",template:f.template("uploader-window"),initialize:function(){var g;this.controller=this.options.controller;this.$browser=e('<a href="#" class="browser" />').hide().appendTo("body");g=this.options.uploader=_.defaults(this.options.uploader||{},{dropzone:this.$el,browser:this.$browser,params:{}});if(g.dropzone){if(!(g.dropzone instanceof e)){g.dropzone=e(g.dropzone)}g.dropzone.one("mouseenter dragenter",_.bind(this.maybeInitUploader,this))}},render:function(){this.maybeInitUploader();this.$el.html(this.template(this.options));return this},refresh:function(){if(this.uploader){this.uploader.refresh()}},maybeInitUploader:function(){var h,g;if(this.uploader||!this.$el.closest("body").length){return}h=e("#post_ID");if(h.length){this.options.uploader.params.post_id=h.val()}this.uploader=new wp.Uploader(this.options.uploader);g=this.uploader.dropzone;g.on("dropzone:enter",_.bind(this.show,this));g.on("dropzone:leave",_.bind(this.hide,this))},show:function(){var g=this.$el.show();_.defer(function(){g.css({opacity:1})})},hide:function(){var g=this.$el.css({opacity:0});f.transition(g).done(function(){if("0"===g.css("opacity")){g.hide()}})}});f.view.UploaderInline=Backbone.View.extend({tagName:"div",className:"uploader-inline",template:f.template("uploader-inline"),initialize:function(){this.controller=this.options.controller;if(!this.options.$browser){this.options.$browser=this.controller.uploader.$browser}wp.Uploader.queue.on("add remove reset change:percent",this.renderUploadProgress,this)},destroy:function(){wp.Uploader.queue.off("add remove reset change:percent",this.renderUploadProgress,this)},render:function(){var g=this.options.$browser,h;this.renderUploadProgress();this.$el.html(this.template(this.options));h=this.$(".browser");g.text(h.text());g[0].className=h[0].className;h.replaceWith(g.show());this.$bar=this.$(".media-progress-bar div");return this},renderUploadProgress:function(){var g=wp.Uploader.queue;this.$el.toggleClass("uploading",!!g.length);if(!this.$bar||!g.length){return}this.$bar.width((g.reduce(function(h,i){if(i.get("uploading")){return h+(i.get("percent")||0)}else{return h+100}},0)/g.length)+"%")}});f.view.Toolbar=Backbone.View.extend({tagName:"div",className:"media-toolbar",initialize:function(){this.controller=this.options.controller;this._views={};this.$primary=e('<div class="media-toolbar-primary" />').prependTo(this.$el);this.$secondary=e('<div class="media-toolbar-secondary" />').prependTo(this.$el);if(this.options.items){this.add(this.options.items,{silent:true})}if(!this.options.silent){this.render()}},destroy:function(){_.each(this._views,function(g){if(g.destroy){g.destroy()}})},render:function(){var g=_.chain(this._views).sortBy(function(h){return h.options.priority||10}).groupBy(function(h){return(h.options.priority||10)>0?"primary":"secondary"}).value();e(_.pluck(this._views,"el")).detach();this.$primary.html(_.pluck(g.primary||[],"el"));this.$secondary.html(_.pluck(g.secondary||[],"el"));this.refresh();return this},add:function(i,g,h){h=h||{};if(_.isObject(i)){_.each(i,function(j,k){this.add(k,j,{silent:true})},this);if(!h.silent){this.render()}return this}if(!(g instanceof Backbone.View)){g.classes=[i].concat(g.classes||[]);g=new f.view.Button(g).render()}g.controller=g.controller||this.controller;this._views[i]=g;if(!h.silent){this.render()}return this},get:function(g){return this._views[g]},remove:function(h,g){delete this._views[h];if(!g||!g.silent){this.render()}return this},refresh:function(){}});f.view.Toolbar.Insert=f.view.Toolbar.extend({initialize:function(){var g=this.options.controller,h=g.state().get("selection");this.options.items=_.defaults(this.options.items||{},{selection:new f.view.Selection({controller:g,collection:h,priority:-40}).render(),insert:{style:"primary",priority:80,text:b.insertIntoPost,click:function(){g.close();g.state().trigger("insert",h);h.clear()}}});f.view.Toolbar.prototype.initialize.apply(this,arguments)},refresh:function(){var g=this.controller.state().get("selection");this.get("insert").model.set("disabled",!g.length)}});f.view.Toolbar.Insert.Post=f.view.Toolbar.Insert.extend({initialize:function(){this.options.items=_.defaults(this.options.items||{},{gallery:{text:b.createNewGallery,priority:40,click:function(){var g=this.controller,h=g.state().get("selection"),i=g.get("gallery-edit");i.set("library",new f.model.Selection(h.models,{props:h.props.toJSON(),multiple:true}));this.controller.state("gallery-edit")}},batch:{text:b.batchInsert,priority:60,click:function(){this.controller.state("batch-edit")}}});f.view.Toolbar.Insert.prototype.initialize.apply(this,arguments)},refresh:function(){var g=this.controller.state().get("selection"),h=g.length;f.view.Toolbar.Insert.prototype.refresh.apply(this,arguments);this.get("gallery").$el.toggle(h>1&&g.all(function(i){return"image"===i.get("type")}));this.get("batch").$el.toggle(false&&h>1)}});f.view.Button=Backbone.View.extend({tagName:"a",className:"media-button",attributes:{href:"#"},events:{click:"click"},defaults:{text:"",style:"",size:"large",disabled:false},initialize:function(){this.model=new Backbone.Model(this.defaults);_.each(this.defaults,function(i,g){var h=this.options[g];if(_.isUndefined(h)){return}this.model.set(g,h);delete this.options[g]},this);if(this.options.dropdown){this.options.dropdown.addClass("dropdown")}this.model.on("change",this.render,this)},render:function(){var h=["button",this.className],g=this.model.toJSON();if(g.style){h.push("button-"+g.style)}if(g.size){h.push("button-"+g.size)}h=_.uniq(h.concat(this.options.classes));this.el.className=h.join(" ");this.$el.attr("disabled",g.disabled);if(this.options.dropdown){this.options.dropdown.detach()}this.$el.text(this.model.get("text"));if(this.options.dropdown){this.$el.append(this.options.dropdown)}return this},click:function(g){g.preventDefault();if(this.options.click&&!this.model.get("disabled")){this.options.click.apply(this,arguments)}}});f.view.ButtonGroup=Backbone.View.extend({tagName:"div",className:"button-group button-large media-button-group",initialize:function(){this.buttons=_.map(this.options.buttons||[],function(g){if(g instanceof Backbone.View){return g}else{return new f.view.Button(g).render()}});delete this.options.buttons;if(this.options.classes){this.$el.addClass(this.options.classes)}},render:function(){this.$el.html(e(_.pluck(this.buttons,"el")).detach());return this}});f.view.PriorityList=Backbone.View.extend({tagName:"div",initialize:function(){this.controller=this.options.controller;this._views={};this.add(_.extend({},this.views,this.options.views),{silent:true});delete this.views;delete this.options.views;if(!this.options.silent){this.render()}},destroy:function(){_.each(this._views,function(g){if(g.destroy){g.destroy()}})},render:function(){var g=_(this._views).chain().sortBy(function(h){return h.options.priority||10}).pluck("el").value();e(g).detach();this.$el.html(g);return this},add:function(i,g,h){h=h||{};if(_.isObject(i)){_.each(i,function(j,k){this.add(k,j,{silent:true})},this);if(!h.silent){this.render()}return this}if(!(g instanceof Backbone.View)){g=this.toView(g,i,h)}g.controller=g.controller||this.controller;this._views[i]=g;if(!h.silent){this.render()}return this},get:function(g){return this._views[g]},remove:function(h,g){delete this._views[h];if(!g||!g.silent){this.render()}return this},toView:function(g){return new Backbone.View(g)}});f.view.Menu=f.view.PriorityList.extend({tagName:"ul",className:"media-menu",toView:function(g,h){g=g||{};g.id=g.id||h;return new f.view.MenuItem(g).render()},select:function(h){var g=this.get(h);if(!g){return}this.deselect();g.$el.addClass("active")},deselect:function(){this.$el.children().removeClass("active")}});f.view.MenuItem=Backbone.View.extend({tagName:"li",className:"media-menu-item",events:{click:"click"},click:function(){var g=this.options;if(g.click){g.click.call(this)}else{if(g.id){this.controller.state(g.id)}}},render:function(){var g=this.options;if(g.text){this.$el.text(g.text)}else{if(g.html){this.$el.html(g.html)}}return this}});f.view.Sidebar=f.view.PriorityList.extend({className:"media-sidebar"});f.view.Attachment=Backbone.View.extend({tagName:"li",className:"attachment",template:f.template("attachment"),events:{"mousedown .attachment-preview":"toggleSelection","change .describe":"describe"},buttons:{},initialize:function(){this.controller=this.options.controller;this.model.on("change:sizes change:uploading change:caption change:title",this.render,this);this.model.on("change:percent",this.progress,this);this.model.on("add",this.select,this);this.model.on("remove",this.deselect,this);this.model.on("selection:single selection:unsingle",this.details,this);this.details(this.model,this.controller.state().get("selection"));this.$el.on("click","a",this.preventDefault)},destroy:function(){this.model.off(null,null,this);this.$el.off("click","a",this.preventDefault)},render:function(){var h=this.model.toJSON(),g=_.defaults(this.model.toJSON(),{orientation:"landscape",uploading:false,type:"",subtype:"",icon:"",filename:"",caption:"",title:""});g.buttons=this.buttons;g.describe=this.controller.state().get("describe");if("image"===g.type){_.extend(g,this.imageSize())}this.$el.html(this.template(g));if(g.uploading){this.$bar=this.$(".media-progress-bar div")}else{delete this.$bar}if(this.selected()){this.select()}return this},progress:function(){if(this.$bar&&this.$bar.length){this.$bar.width(this.model.get("percent")+"%")}},toggleSelection:function(g){this.controller.state().toggleSelection(this.model)},selected:function(){var g=this.controller.state().get("selection");if(g){return g.has(this.model)}},select:function(g,i){var h=this.controller.state().get("selection");if(!h||(i&&i!==h)){return}this.$el.addClass("selected")},deselect:function(g,i){var h=this.controller.state().get("selection");if(!h||(i&&i!==h)){return}this.$el.removeClass("selected")},details:function(g,j){var i=this.controller.state().get("selection"),h;if(i!==j){return}h=i.single();this.$el.toggleClass("details",h===this.model)},preventDefault:function(g){g.preventDefault()},imageSize:function(g){var h=this.model.get("sizes");g=g||"medium";if(h&&h[g]){return _.clone(h[g])}else{return{url:this.model.get("url"),width:this.model.get("width"),height:this.model.get("height"),orientation:this.model.get("orientation")}}},describe:function(g){if("image"===this.model.get("type")){this.model.save("caption",g.target.value)}else{this.model.save("title",g.target.value)}}});f.view.Attachment.Library=f.view.Attachment.extend({className:"attachment library"});f.view.Attachment.Gallery=f.view.Attachment.extend({buttons:{close:true},events:(function(){var g=_.clone(f.view.Attachment.prototype.events);g["click .close"]="removeFromGallery";return g}()),removeFromGallery:function(g){g.stopPropagation();this.controller.state().get("library").remove(this.model)}});f.view.Attachments=Backbone.View.extend({tagName:"ul",className:"attachments",template:f.template("attachments-css"),events:{scroll:"scroll"},initialize:function(){this.controller=this.options.controller;this.el.id=_.uniqueId("__attachments-view-");_.defaults(this.options,{refreshSensitivity:200,refreshThreshold:3,AttachmentView:f.view.Attachment,sortable:false});_.each(["add","remove"],function(g){this.collection.on(g,function(j,h,i){this[g](j,i.index)},this)},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);e(window).on("resize.attachments",this._resizeCss);this.css()},destroy:function(){this.collection.off("add remove reset",null,this);this.model.off("change:edge change:gutter",this.css,this);e(window).off("resize.attachments",this._resizeCss)},css:function(){var g=e("#"+this.el.id+"-css");if(g.length){g.remove()}f.view.Attachments.$head().append(this.template({id:this.el.id,edge:this.edge(),gutter:this.model.get("gutter")}))},edge:function(){var i=this.model.get("edge"),j,h,g;if(!this.$el.is(":visible")){return i}j=this.model.get("gutter")*2;h=this.$el.width()-j;g=Math.ceil(h/(i+j));i=Math.floor((h-(g*j))/g);return i},initSortable:function(){var g=this.collection,h;if(!this.options.sortable||!e.fn.sortable){return}this.$el.sortable({disabled:!!g.comparator,containment:this.$el,tolerance:"pointer",start:function(i,j){h=j.item.index()},update:function(j,k){var i=g.at(h);g.remove(i,{silent:true}).add(i,{at:k.item.index(),silent:true})}});g.props.on("change:orderby",function(){this.$el.sortable("option","disabled",!!g.comparator)},this)},render:function(){if(!this.collection.length){this.collection.more().done(this.scroll);this.$el.empty();return this}this.$el.html(this.collection.map(function(g){return new this.options.AttachmentView({controller:this.controller,model:g}).render().$el},this));this.scroll();return this},add:function(j,h){var g,i;g=new this.options.AttachmentView({controller:this.controller,model:j}).render();i=this.$el.children();if(i.length>h){i.eq(h).before(g.$el)}else{this.$el.append(g.$el)}},remove:function(i,g){var h=this.$el.children();if(h.length){h.eq(g).detach()}},scroll:function(g){if(!this.$el.is(":visible")){return}if(this.el.scrollHeight<this.el.scrollTop+(this.el.clientHeight*this.options.refreshThreshold)){this.collection.more().done(this.scroll)}}},{$head:(function(){var g;return function(){return g=g||e("head")}}())});f.view.Search=Backbone.View.extend({tagName:"input",className:"search",attributes:{type:"text",placeholder:b.search},events:{keyup:"search"},render:function(){this.el.value=this.model.escape("search");return this},search:function(g){if(g.target.value){this.model.set("search",g.target.value)}else{this.model.unset("search")}}});f.view.AttachmentsBrowser=Backbone.View.extend({tagName:"div",className:"attachments-browser",initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{search:true,upload:false,total:true,AttachmentView:f.view.Attachment.Library});this.toolbar=new f.view.Toolbar({controller:this.controller});if(this.options.search){this.toolbar.add("search",new f.view.Search({controller:this.controller,model:this.collection.props,priority:-40}))}this.attachments=new f.view.Attachments({controller:this.controller,collection:this.collection,model:this.model,sortable:this.options.sortable,AttachmentView:this.options.AttachmentView})},render:function(){this.toolbar.$el.detach();this.attachments.$el.detach();this.$el.html([this.toolbar.render().el,this.attachments.render().el]);return this}});f.view.SelectionPreview=Backbone.View.extend({tagName:"div",className:"selection-preview",template:f.template("media-selection-preview"),events:{"click .clear-selection":"clear"},initialize:function(){_.defaults(this.options,{clearable:true});this.controller=this.options.controller;this.collection.on("add change:url remove",this.render,this);this.render()},render:function(){var g=_.clone(this.options),j,i,h;if(!this.collection.length){this.$el.empty();return this}g.count=this.collection.length;j=this.collection.last();i=j.get("sizes");if("image"===j.get("type")){g.thumbnail=(i&&i.thumbnail)?i.thumbnail.url:j.get("url")}else{g.thumbnail=j.get("icon")}this.$el.html(this.template(g));return this},clear:function(g){g.preventDefault();this.collection.clear()}});f.view.Selection=Backbone.View.extend({tagName:"div",className:"media-selection",template:f.template("media-selection"),events:{"click .clear-selection":"clear"},initialize:function(){_.defaults(this.options,{clearable:true});this.controller=this.options.controller;this.attachments=new f.view.Attachments({controller:this.controller,collection:this.collection,sortable:true,model:new Backbone.Model({edge:40,gutter:5}),AttachmentView:f.view.Attachment.Selection});this.collection.on("add remove reset",this.refresh,this)},destroy:function(){this.collection.off("add remove reset",this.refresh,this)},render:function(){this.attachments.$el.detach();this.attachments.render();this.$el.html(this.template(this.options));this.$(".selection-view").replaceWith(this.attachments.$el);this.refresh();return this},refresh:function(){if(!this.$el.children().length){return}this.$el.toggleClass("empty",!this.collection.length);this.$(".count").text(this.collection.length+" "+b.selected)},clear:function(g){g.preventDefault();this.collection.clear()}});f.view.Attachment.Selection=f.view.Attachment.extend({toggleSelection:function(){this.controller.state().get("selection").single(this.model)}});f.view.Settings=Backbone.View.extend({events:{"click button":"updateHandler","change input":"updateHandler","change select":"updateHandler","change textarea":"updateHandler"},settings:{},initialize:function(){var g=this.settings;this.model=this.model||new Backbone.Model();_.each(g,function(i,h){if(i.name){this.model.set(h,getUserSetting(i.name,i.fallback))}else{this.model.set(h,this.model.get(h)||i.fallback)}},this);this.model.validate=function(h){return _.any(h,function(j,i){return g[i]&&!_.contains(g[i].accepts,j)})};this.model.on("change",function(i,h){if(!h.changes){return}_.each(_.keys(h.changes),function(j){if(g[j]&&g[j].name){setUserSetting(g[j].name,i.get(j))}})},this);this.model.on("change",this.updateChanges,this)},render:function(){this.$el.html(this.template(this.model.toJSON()));_(this.model.attributes).chain().keys().each(this.update,this);return this},update:function(i){var j=this.settings[i],h=this.$('[data-setting="'+i+'"]'),g;if(!j){return}if("select"===j.type){h.find('[value="'+this.model.get(i)+'"]').attr("selected",true)}else{g=h.find("button").removeClass("active");g.filter('[value="'+this.model.get(i)+'"]').addClass("active")}},updateHandler:function(h){var g=e(h.target).closest("[data-setting]");h.preventDefault();if(g.length){this.model.set(g.data("setting"),h.target.value)}},updateChanges:function(h,g){if(g.changes){_(g.changes).chain().keys().each(this.update,this)}}});f.view.Settings.AttachmentDisplay=f.view.Settings.extend({className:"attachment-display-settings",template:f.template("attachment-display-settings"),settings:{align:{accepts:["left","center","right","none"],name:"align",fallback:"none"},link:{accepts:["post","file","none"],name:"urlbutton",fallback:"post"},size:{accepts:["thumbnail","medium","large","full"],name:"imgsize",fallback:"medium"}}});f.view.Settings.Gallery=f.view.Settings.extend({className:"gallery-settings",template:f.template("gallery-settings"),settings:{columns:{accepts:_.invoke(_.range(1,10),"toString"),fallback:"3",type:"select"},link:{accepts:["post","file"],fallback:"post"}}});f.view.Attachment.Details=f.view.Attachment.extend({tagName:"div",className:"attachment-details",template:f.template("attachment-details"),events:{"change .describe":"describe"}})}(jQuery)); |