mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-27 03:27:46 +01:00
b502e1e0c2
git-svn-id: http://core.svn.wordpress.org/trunk@22334 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
22 KiB
JavaScript
1 line
22 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.StateMachine=function(g){this.states=new Backbone.Collection(g)};f.controller.StateMachine.extend=Backbone.Model.extend;_.extend(f.controller.StateMachine.prototype,{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){if(g=this.state()){g.trigger("deactivate")}this._state=h;return this.state().trigger("activate")}if(this._state){return this.get(this._state)}}});_.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.Library=Backbone.Model.extend({defaults:{id:"library",multiple:false,describe:false,title:b.mediaLibrary},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)}this.on("activate",this.activate,this);this.on("deactivate",this.deactivate,this);this.on("change:details",this.details,this)},activate:function(){this.toolbar();this.sidebar();this.content();if(this.get("multiple")){wp.Uploader.queue.on("add",this.selectUpload,this)}},deactivate:function(){var g=this._postLibraryToolbar;if(g){this.get("selection").off("add remove",g.visibility,g)}wp.Uploader.queue.off("add",this.selectUpload,this)},toolbar:function(){var h=this.frame,g;g=this._postLibraryToolbar=new f.view.Toolbar.PostLibrary({controller:h,state:this});h.toolbar(g);this.get("selection").on("add remove",g.visibility,g)},sidebar:function(){var g=this.frame;g.sidebar(new f.view.Sidebar({controller:g}));this.details({silent:true});g.sidebar().add({search:new f.view.Search({controller:g,model:this.get("library").props,priority:20}),selection:new f.view.SelectionPreview({controller:g,collection:this.get("selection"),priority:40})})},content:function(){var g=this.frame;g.content(new f.view.Attachments({controller:g,collection:this.get("library"),AttachmentView:f.view.Attachment.Library}).render())},selectUpload:function(g){this.get("selection").add(g)},details:function(i){var h=this.get("details"),g;if(h){g=new f.view.Attachment.Details({controller:this.frame,model:h,priority:80})}else{g=new Backbone.View()}if(!i||!i.silent){g.render()}this.frame.sidebar().add("details",g,i)},toggleSelection:function(g){var h=this.get("details"),j=this.get("selection"),i=j.has(g);if(!j){return}if(!i){j.add(g)}if(h!==g){this.set("details",g);return}j.remove(g);if(j.length){this.set("details",j.last())}else{this.unset("details")}}});f.controller.Gallery=f.controller.Library.extend({defaults:{id:"gallery",multiple:false,describe:true,title:b.createGallery,edge:199},toolbar:function(){this.frame.toolbar(new f.view.Toolbar.Gallery({controller:this.frame,state:this}))},sidebar:function(){var g=this.frame;g.sidebar(new f.view.Sidebar({controller:g}));this.details()},content:function(){this.frame.content(new f.view.Attachments({controller:this.frame,collection:this.get("library"),sortable:true,AttachmentView:f.view.Attachment.Gallery}).render())}});f.view.Frame=Backbone.View.extend({tagName:"div",className:"media-frame",template:f.template("media-frame"),initialize:function(){_.defaults(this.options,{state:"library",title:"",selection:[],library:{},modal:true,multiple:false,uploader:true});this.createSelection();this.createSubviews();this.createStates()},render:function(){var g=[this.toolbar().el,this.sidebar().el,this.content().el];if(this.modal){this.modal.render()}this.$el.children().filter(g).detach();this.$el.empty().append(g);if(this.uploader){this.uploader.render().$el.appendTo(this.$el)}return 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=new Backbone.Collection();this.states.on("add",function(h){h.frame=this},this);this.states.add([new f.controller.Library({selection:g.selection,library:f.query(g.library),multiple:this.options.multiple}),new f.controller.Gallery({library:g.selection})]);this.state(g.state)},createSubviews:function(){_.each(["toolbar","sidebar","content"],function(g){this["_"+g]=new Backbone.View({tagName:"div",className:"media-"+g})},this);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}})}}});_.extend(f.view.Frame.prototype,f.controller.StateMachine.prototype);_.each(["toolbar","sidebar","content"],function(g){f.view.Frame.prototype[g]=function(h){var i=this["_"+g];if(!h){return i}h.$el.addClass("media-"+g);if(i.destroy){i.destroy()}i.undelegateEvents();i.$el.replaceWith(h.$el);this["_"+g]=h}});_.each(["open","close","attach","detach"],function(g){f.view.Frame.prototype[g]=function(h){if(this.modal){this.modal[g].apply(this.modal,arguments)}return this}});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.inline=new f.view.UploaderInline({controller:this.controller,uploaderWindow:this}).render();this.inline.$el.appendTo("body");g=this.options.uploader=_.defaults(this.options.uploader||{},{container:this.inline.$el,dropzone:this.$el,browser:this.inline.$(".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;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(){this.renderUploadProgress();this.$el.html(this.template(this.options));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()}},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"));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}});f.view.Toolbar.PostLibrary=f.view.Toolbar.extend({initialize:function(){var i=this.options.state,h=i.get("selection"),g=this.options.controller;this.options.items={"create-new-gallery":{style:"primary",text:b.createNewGallery,priority:40,click:function(){this.controller.state("gallery")}},"insert-into-post":new f.view.ButtonGroup({priority:30,classes:"dropdown-flip-x",buttons:[{text:b.insertIntoPost,click:function(){g.close();i.trigger("insert",h);h.clear()}},{classes:["down-arrow"],dropdown:new f.view.AttachmentDisplaySettings().render().$el,click:function(k){var j=this.$el;if(!e(k.target).closest(".dropdown").length){j.toggleClass("active")}k.stopPropagation();if(j.is(":visible")){e(document.body).one("click",function(){j.removeClass("active")})}}}]}).render(),"add-to-gallery":{text:b.addToGallery,priority:20}};f.view.Toolbar.prototype.initialize.apply(this,arguments);this.visibility()},visibility:function(){var k=this.options.state,h=k.get("selection"),g=this.options.controller,j=h.length,i;i=j>1&&h.all(function(l){return"image"===l.get("type")});this.get("create-new-gallery").$el.toggle(i);insert=this.get("insert-into-post");_.each(insert.buttons,function(l){l.model.set("style",i?"":"primary")});_.first(insert.buttons).model.set("disabled",!j)}});f.view.Toolbar.Gallery=f.view.Toolbar.extend({initialize:function(){var j=this.options.state,h=j.get("editing"),i=j.get("library"),g=this.options.controller;this.options.items={"update-gallery":{style:"primary",text:h?b.updateGallery:b.insertGalleryIntoPost,priority:40,click:function(){g.close();j.trigger("update",i);i.clear();g.state("library")}},"return-to-library":{text:h?b.addImagesFromLibrary:b.returnToLibrary,priority:-40,click:function(){this.controller.state("library")}}};f.view.Toolbar.prototype.initialize.apply(this,arguments)}});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.Sidebar=Backbone.View.extend({tagName:"div",className:"media-sidebar",template:f.template("sidebar"),initialize:function(){this.controller=this.options.controller;this._views={};if(this.options.views){this.add(this.options.views,{silent:true})}if(!this.options.silent){this.render()}},render:function(){var g=_(this._views).chain().sortBy(function(h){return h.options.priority||10}).pluck("el").value();e(g).detach();this.$el.html(this.template({title:this.controller.state().get("title")||"",uploader:this.controller.options.uploader}));this.$(".sidebar-content").html(g);if(this.controller.uploader){this.$el.append(this.controller.uploader.inline.$el);this.controller.uploader.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}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}});f.view.Attachment=Backbone.View.extend({tagName:"li",className:"attachment",template:f.template("attachment"),events:{"click .attachment-preview":"toggleSelection","change .describe":"describe"},buttons:{},initialize:function(){this.controller=this.options.controller;this.model.on("change:sizes change:uploading",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.$el.on("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()}this.controller.state().on("change:details",this.details,this);this.details();return this},destroy:function(){this.controller.state().off("change:details",this.details,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(){var g=this.controller.state().get("details");this.$el.toggleClass("details",g===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(){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();this.controller.state().on("change:edge change:gutter",this.css,this);this.css()},destroy:function(){this.collection.off("add remove reset",null,this);this.controller.state().off("change:edge change:gutter",this.css,this)},css:function(){var g=e("#"+this.el.id+"-css"),h=this.controller.state();if(g.length){g.remove()}f.view.Attachments.$head().append(this.template({id:this.el.id,edge:h.get("edge"),gutter:h.get("gutter")}))},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();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()}}},{$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.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.AttachmentDisplaySettings=Backbone.View.extend({tagName:"div",className:"attachment-display-settings",template:f.template("attachment-display-settings"),events:{"click button":"updateHandler"},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"}},initialize:function(){var g=this.settings;this.model=new Backbone.Model();_.each(g,function(i,h){this.model.set(h,getUserSetting(i.name,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]){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(g){var h=this.$('[data-setting="'+g+'"] button').removeClass("active");h.filter('[value="'+this.model.get(g)+'"]').addClass("active")},updateHandler:function(g){var h=e(g.target).closest(".button-group");g.preventDefault();if(h.length){this.model.set(h.data("setting"),g.target.value)}},updateChanges:function(h,g){if(g.changes){_(g.changes).chain().keys().each(this.update,this)}}});f.view.Attachment.Details=f.view.Attachment.extend({tagName:"div",className:"attachment-details",template:f.template("attachment-details"),events:{"change .describe":"describe"}})}(jQuery)); |