WordPress/wp-includes/js/mce-view.min.js
Scott Taylor ce508e95c2 Simplify the creation of MCE views somewhat:
* Rename the `wp.mce.media` mixin (which was named too ambiguously) to `wp.mce.av`.
* Use the same technique for extending mixins for MCE base classes for views and their base.View property class
* `wp.mce.views.register()` should have default constructor logic if one is not passed.

Props avryl.
See #28458.


Built from https://develop.svn.wordpress.org/trunk@28680


git-svn-id: http://core.svn.wordpress.org/trunk@28498 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2014-06-05 17:27:13 +00:00

1 line
7.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

window.wp=window.wp||{},function(a){var b={},c={},d=wp.media,e=["encodedText"];wp.mce=wp.mce||{},wp.mce.View=function(a){a=a||{},_.extend(this,_.pick(a,e)),this.initialize.apply(this,arguments)},_.extend(wp.mce.View.prototype,{initialize:function(){},getHtml:function(){},render:function(){var b=this.getHtml();_.each(tinymce.editors,function(c){var d=this;c.plugins.wpview&&a(c.getDoc()).find('[data-wpview-text="'+this.encodedText+'"]').each(function(c,e){var f=a(e);f.html(b).append('<ins data-wpview-end="1"></ins>'),a(d).trigger("ready",e)})},this)},unbind:function(){}}),wp.mce.View.extend=Backbone.View.extend,wp.mce.views={register:function(a,c){var d={type:a,View:{},toView:function(a){var b=wp.shortcode.next(this.type,a);if(b)return{index:b.index,content:b.content,options:{shortcode:b.shortcode}}}};c=_.defaults(c,d),c.View=wp.mce.View.extend(c.View),b[a]=c},get:function(a){return b[a]},unregister:function(a){delete b[a]},unbind:function(){_.each(c,function(a){a.unbind()})},toViews:function(a){var c,d=[{content:a}];return _.each(b,function(a,b){c=d.slice(),d=[],_.each(c,function(c){var e,f=c.content;if(c.processed)return void d.push(c);for(;f&&(e=a.toView(f));)e.index&&d.push({content:f.substring(0,e.index)}),d.push({content:wp.mce.views.toView(b,e.content,e.options),processed:!0}),f=f.slice(e.index+e.content.length);f&&d.push({content:f})})}),_.pluck(d,"content").join("")},toView:function(a,b,d){var e,f,g=wp.mce.views.get(a),h=window.encodeURIComponent(b);return g?(wp.mce.views.getInstance(h)||(f=d,f.encodedText=h,e=new g.View(f),c[h]=e),wp.html.string({tag:"div",attrs:{"class":"wpview-wrap wpview-type-"+a,"data-wpview-text":h,"data-wpview-type":a,contenteditable:"false"},content:" "})):b},refreshView:function(a,b){var d,e,f,g=window.encodeURIComponent(b);f=wp.mce.views.getInstance(g),f||(e=a.toView(b),d=e.options,d.encodedText=g,f=new a.View(d),c[g]=f),wp.mce.views.render()},getInstance:function(a){return c[a]},render:function(){_.each(c,function(a){a.render()})},edit:function(b){var c=a(b).data("wpview-type"),d=wp.mce.views.get(c);d&&d.edit(b)}},wp.mce.views.register("gallery",{View:{template:d.template("editor-gallery"),postID:a("#post_ID").val(),initialize:function(a){this.shortcode=a.shortcode,this.fetch()},fetch:function(){this.attachments=wp.media.gallery.attachments(this.shortcode,this.postID),this.dfd=this.attachments.more().done(_.bind(this.render,this))},getHtml:function(){var a,b=this.shortcode.attrs.named,c=!1;if(!this.dfd||"pending"!==this.dfd.state()||this.attachments.length)return this.attachments.length&&(c=this.attachments.toJSON(),_.each(c,function(a){a.sizes&&(a.sizes.thumbnail?a.thumbnail=a.sizes.thumbnail:a.sizes.full&&(a.thumbnail=a.sizes.full))})),a={attachments:c,columns:b.columns?parseInt(b.columns,10):3},this.template(a)}},edit:function(b){var c,d,e=wp.media.gallery,f=this;d=window.decodeURIComponent(a(b).attr("data-wpview-text")),c=e.edit(d),c.state("gallery-edit").on("update",function(d){var g=e.shortcode(d).string();a(b).attr("data-wpview-text",window.encodeURIComponent(g)),wp.mce.views.refreshView(f,g),c.detach()})}}),wp.mce.av={loaded:!1,View:_.extend({},wp.media.mixin,{initialize:function(b){this.players=[],this.shortcode=b.shortcode,_.bindAll(this,"setPlayer","pausePlayers"),a(this).on("ready",this.setPlayer),a("body").on("click",".wp-switch-editor",this.pausePlayers),a(document).on("media:edit",this.pausePlayers)},setPlayer:function(b,c){if(c){var d,e=this,f=this.ua.is("ff"),g=".wp-"+this.shortcode.tag+"-shortcode";if(d=a(c).find(g),!this.isCompatible(d))return d.closest(".wpview-wrap").addClass("wont-play"),d.parent().hasClass("wpview-wrap")||d.parent().replaceWith(d),void d.replaceWith("<p>"+d.find("source").eq(0).prop("src")+"</p>");d.closest(".wpview-wrap").removeClass("wont-play"),f?d.prop("preload","metadata"):d.prop("preload","none"),d=wp.media.view.MediaDetails.prepareSrc(d.get(0)),setTimeout(function(){wp.mce.av.loaded=!0,e.players.push(new MediaElementPlayer(d,e.mejsSettings))},wp.mce.av.loaded?10:500)}},getHtml:function(){var a=this.shortcode.attrs.named;return a.content=this.shortcode.content,this.template({model:_.defaults(a,wp.media[this.shortcode.tag].defaults)})},unbind:function(){this.unsetPlayers()}}),edit:function(b){var c,d,e,f=wp.media[this.shortcode],g=this;a(document).trigger("media:edit"),d=window.decodeURIComponent(a(b).attr("data-wpview-text")),c=f.edit(d),c.on("close",function(){c.detach()}),e=function(d){var e=wp.media[g.shortcode].shortcode(d).string();a(b).attr("data-wpview-text",window.encodeURIComponent(e)),wp.mce.views.refreshView(g,e),c.detach()},_.isArray(g.state)?_.each(g.state,function(a){c.state(a).on("update",e)}):c.state(g.state).on("update",e),c.open()}},wp.mce.views.register("video",_.extend({},wp.mce.av,{shortcode:"video",state:"video-details",View:_.extend({},wp.mce.av.View,{template:d.template("editor-video")})})),wp.mce.views.register("audio",_.extend({},wp.mce.av,{shortcode:"audio",state:"audio-details",View:_.extend({},wp.mce.av.View,{template:d.template("editor-audio")})})),wp.mce.views.register("playlist",_.extend({},wp.mce.av,{shortcode:"playlist",state:["playlist-edit","video-playlist-edit"],View:_.extend({},wp.media.mixin,{template:d.template("editor-playlist"),initialize:function(b){this.players=[],this.data={},this.attachments=[],this.shortcode=b.shortcode,a("body").on("click",".wp-switch-editor",this.pausePlayers),a(document).on("media:edit",this.pausePlayers),this.fetch(),a(this).on("ready",this.setPlaylist)},fetch:function(){this.attachments=wp.media.playlist.attachments(this.shortcode),this.dfd=this.attachments.more().done(_.bind(this.render,this))},setPlaylist:function(b,c){this.data.tracks&&this.players.push(new WPPlaylistView({el:a(c).find(".wp-playlist").get(0),metadata:this.data}).player)},getHtml:function(){var a,b,c=this.shortcode.attrs.named,e=wp.media.playlist,f=[];if(!this.dfd||"pending"!==this.dfd.state()||this.attachments.length)return _.each(e.defaults,function(a,b){c[b]=e.coerce(c,b)}),a={type:c.type,style:c.style,tracklist:c.tracklist,tracknumbers:c.tracknumbers,images:c.images,artists:c.artists},this.attachments.length?(b=this.attachments.toJSON(),_.each(b,function(b){var e={},g={},h={src:b.url,type:b.mime,title:b.title,caption:b.caption,description:b.description,meta:b.meta};"video"===c.type?(e.width=b.width,e.height=b.height,d.view.settings.contentWidth?(g.width=d.view.settings.contentWidth-22,g.height=Math.ceil(e.height*g.width/e.width),a.width||(a.width=g.width,a.height=g.height)):a.width||(a.width=b.width,a.height=b.height),h.dimensions={original:e,resized:_.isEmpty(g)?e:g}):a.width=400,h.image=b.image,h.thumb=b.thumb,f.push(h)}),a.tracks=f,this.data=a,this.template(a)):this.template(a)},unbind:function(){this.unsetPlayers()}})})),wp.mce.views.register("embed",{shortcode:"embed",View:_.extend({},wp.media.mixin,{template:d.template("editor-embed"),initialize:function(b){this.players=[],this.content=b.content,this.parsed=!1,this.shortcode=b.shortcode,_.bindAll(this,"setHtml","setNode","fetch"),a(this).on("ready",this.setNode)},unbind:function(){var a=this;_.each(this.players,function(b){b.pause(),a.removePlayer(b)}),this.players=[]},setNode:function(a,b){this.node=b,this.parsed?this.parseMediaShortcodes():this.fetch()},fetch:function(){wp.ajax.send("parse-embed",{data:{post_ID:a("#post_ID").val(),content:this.shortcode.string()}}).done(this.setHtml)},setHtml:function(b){this.parsed=b,a(this.node).html(this.getHtml()),this.parseMediaShortcodes()},parseMediaShortcodes:function(){var b=this;a(".wp-audio-shortcode, .wp-video-shortcode",this.node).each(function(a,c){b.players.push(new MediaElementPlayer(c,b.mejsSettings))})},getHtml:function(){return this.parsed?this.template({content:this.parsed}):""}}),edit:function(){}})}(jQuery);