WordPress/wp-includes/js/mce-view.min.js
Andrew Ozz b799bd8e33 TinyMCE: wpView improvements:
- Better structure, simpler "view" registration, better extensibility.
- Better inline documentation.
- Don't show a placeholder for pasted link until we know the link is "embeddable'.
And many more improvements. Props iseulde. See #31412.
Built from https://develop.svn.wordpress.org/trunk@31546


git-svn-id: http://core.svn.wordpress.org/trunk@31527 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-02-25 23:13:25 +00:00

1 line
8.3 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,b,c){"use strict";var d={},e={};b.mce=b.mce||{},b.mce.views={register:function(a,c){d[a]=b.mce.View.extend(_.extend(c,{type:a}))},unregister:function(a){delete d[a]},get:function(a){return d[a]},unbind:function(){_.each(e,function(a){a.unbind()})},setMarkers:function(a){var b,c=[{content:a}],e=this;return _.each(d,function(a,d){b=c.slice(),c=[],_.each(b,function(b){var f,g=b.content;if(b.processed)return void c.push(b);for(;g&&(f=a.prototype.match(g));)f.index&&c.push({content:g.substring(0,f.index)}),e.createInstance(d,f.content,f.options),c.push({content:'<p data-wpview-marker="'+encodeURIComponent(f.content)+'">'+f.content+"</p>",processed:!0}),g=g.slice(f.index+f.content.length);g&&c.push({content:g})})}),_.pluck(c,"content").join("")},createInstance:function(a,b,c){var d=this.get(a),f=encodeURIComponent(b),g=this.getInstance(f);return g?g:(c=_.extend(c||{},{text:b,encodedText:f}),e[f]=new d(c))},getInstance:function(a){return e[encodeURIComponent(a)]},render:function(a){_.each(e,function(b){b.render(a)})},update:function(a,b,d){var e=decodeURIComponent(c(d).data("wpview-text")),f=this.getInstance(e);f&&f.update(a,b,d)},edit:function(a,b){var d=decodeURIComponent(c(b).data("wpview-text")),e=this.getInstance(d);e&&e.edit&&e.edit(d,function(c){e.update(c,a,b)})}},b.mce.View=function(a){_.extend(this,a),this.initialize()},b.mce.View.extend=Backbone.View.extend,_.extend(b.mce.View.prototype,{content:null,loader:!0,initialize:function(){},getContent:function(){return this.content},render:function(a){(this.loader||this.getContent())&&(a&&this.unbind(),this.replaceMarkers(),this.getContent()?this.setContent(this.getContent(),function(a,b){c(b).data("rendered",!0),this.bindNodes.apply(this,arguments)},a?null:!1):this.setLoader())},bindNodes:function(){},unbind:function(){this.getNodes(function(){this.unbindNodes.apply(this,arguments)},!0)},unbindNodes:function(){},getEditors:function(a){_.each(tinymce.editors,function(b){b.plugins.wpview&&a.call(this,b)},this)},getNodes:function(a,b){this.getEditors(function(d){var e=this;c(d.getBody()).find('[data-wpview-text="'+e.encodedText+'"]').filter(function(){var a;return null==b?!0:(a=c(this).data("rendered")===!0,b?a:!a)}).each(function(){a.call(e,d,this,c(this).find(".wpview-content").get(0))})})},getMarkers:function(a){this.getEditors(function(b){var d=this;c(b.getBody()).find('[data-wpview-marker="'+this.encodedText+'"]').each(function(){a.call(d,b,this)})})},replaceMarkers:function(){this.getMarkers(function(a,b){return c(b).text()!==this.text?void a.dom.setAttrib(b,"data-wpview-marker",null):void a.dom.replace(a.dom.createFragment('<div class="wpview-wrap" data-wpview-text="'+this.encodedText+'" data-wpview-type="'+this.type+'"><p class="wpview-selection-before"> </p><div class="wpview-body" contenteditable="false"><div class="toolbar mce-arrow-down">'+(this.edit?'<div class="dashicons dashicons-edit edit"></div>':"")+'<div class="dashicons dashicons-no remove"></div></div><div class="wpview-content wpview-type-'+this.type+'"></div></div><p class="wpview-selection-after"> </p></div>'),b)})},removeMarkers:function(){this.getMarkers(function(a,b){a.dom.setAttrib(b,"data-wpview-marker",null)})},setContent:function(a,b,c){_.isObject(a)&&-1!==a.body.indexOf("<script")?this.setIframes(a.head,a.body,b,c):_.isString(a)&&-1!==a.indexOf("<script")?this.setIframes(null,a,b,c):this.getNodes(function(c,d,e){a=a.body||a,-1!==a.indexOf("<iframe")&&(a+='<div class="wpview-overlay"></div>'),e.innerHTML="",e.appendChild(_.isString(a)?c.dom.createFragment(a):a),b&&b.apply(this,arguments)},c)},setIframes:function(d,e,f,g){var h=a.MutationObserver||a.WebKitMutationObserver||a.MozMutationObserver,i="video"===this.type||"audio"===this.type||"playlist"===this.type;this.getNodes(function(a,g,j){var k,l,m,n,o=a.dom,p="",q=a.getBody().className||"";j.innerHTML="",d=d||"",i&&(b.mce.views.sandboxStyles?p=b.mce.views.sandboxStyles:(tinymce.each(o.$('link[rel="stylesheet"]',a.getDoc().head),function(a){a.href&&-1===a.href.indexOf("skins/lightgray/content.min.css")&&-1===a.href.indexOf("skins/wordpress/wp-content.css")&&(p+=o.getOuterHTML(a)+"\n")}),b.mce.views.sandboxStyles=p)),setTimeout(function(){if(k=o.add(j,"iframe",{src:tinymce.Env.ie?'javascript:""':"",frameBorder:"0",allowTransparency:"true",scrolling:"no","class":"wpview-sandbox",style:{width:"100%",display:"block"}}),o.add(j,"div",{"class":"wpview-overlay"}),l=k.contentWindow.document,l.open(),l.write('<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+d+p+'<style>html {background: transparent;padding: 0;margin: 0;}body#wpview-iframe-sandbox {background: transparent;padding: 1px 0 !important;margin: -1px 0 0 !important;}body#wpview-iframe-sandbox:before,body#wpview-iframe-sandbox:after {display: none;content: "";}</style></head><body id="wpview-iframe-sandbox" class="'+q+'">'+e+"</body></html>"),l.close(),n=function(){var b,d;k.contentWindow&&(b=c(k),d=c(l.body).height(),b.height()!==d&&(b.height(d),a.nodeChanged()))},h)new h(_.debounce(function(){n()},100)).observe(l.body,{attributes:!0,childList:!0,subtree:!0});else for(m=1;6>m;m++)setTimeout(n,700*m);i&&a.on("wp-body-class-change",function(){l.body.className=a.getBody().className})},50),f&&f.apply(this,arguments)},g)},setLoader:function(){this.setContent('<div class="loading-placeholder"><div class="dashicons dashicons-admin-media"></div><div class="wpview-loading"><ins></ins></div></div>')},setError:function(a,b){this.setContent('<div class="wpview-error"><div class="dashicons dashicons-'+(b||"no")+'"></div><p>'+a+"</p></div>")},match:function(a){var c=b.shortcode.next(this.type,a);return c?{index:c.index,content:c.content,options:{shortcode:c.shortcode}}:void 0},update:function(a,d,e){c(e).data("rendered",!1),d.dom.setAttrib(e,"data-wpview-text",encodeURIComponent(a)),b.mce.views.createInstance(this.type,a,this.match(a).options).render()}})}(window,window.wp,window.jQuery),function(a,b,c){var d,e,f,g,h=c("#post_ID").val()||0;d={state:[],edit:function(a,b){var c=wp.media[this.type],d=c.edit(a);this.stopPlayers&&this.stopPlayers(),_.each(this.state,function(a){d.state(a).on("update",function(a){b(c.shortcode(a).string())})}),d.on("close",function(){d.detach()})}},e=_.extend({},d,{state:["gallery-edit"],template:wp.media.template("editor-gallery"),initialize:function(){var a=wp.media.gallery.attachments(this.shortcode,h),b=this.shortcode.attrs.named,c=this;a.more().done(function(){a=a.toJSON(),_.each(a,function(a){a.sizes&&(b.size&&a.sizes[b.size]?a.thumbnail=a.sizes[b.size]:a.sizes.thumbnail?a.thumbnail=a.sizes.thumbnail:a.sizes.full&&(a.thumbnail=a.sizes.full))}),c.content=c.template({attachments:a,columns:b.columns?parseInt(b.columns,10):wp.media.galleryDefaults.columns}),c.render()}).fail(function(a,b){c.setError(b)})}}),f=_.extend({},d,{action:"parse-media-shortcode",initialize:function(){var a=this;this.url&&(this.loader=!1,this.shortcode=wp.media.embed.shortcode({url:this.url})),wp.ajax.send(this.action,{data:{post_ID:h,type:this.shortcode.tag,shortcode:this.shortcode.string()}}).done(function(b){a.content=b,a.render()}).fail(function(b){"embedURL"===a.type?a.removeMarkers():a.setError(b.message||b.statusText,"admin-media")}),this.getEditors(function(b){b.on("wpview-selected",function(){a.stopPlayers()})})},stopPlayers:function(a){this.getNodes(function(b,d,e){var f=c("iframe.wpview-sandbox",e).get(0);f&&(f=f.contentWindow)&&f.mejs&&_.each(f.mejs.players,function(b){try{b[a?"remove":"pause"]()}catch(c){}})})}}),g=_.extend({},f,{action:"parse-embed",edit:function(a,b){var c=wp.media.embed,d="embedURL"===this.type,e=c.edit(a,d);this.stopPlayers(),e.state("embed").props.on("change:url",function(a,b){b&&(e.state("embed").metadata=a.toJSON())}),e.state("embed").on("select",function(){b(d?e.state("embed").metadata.url:c.shortcode(e.state("embed").metadata).string())}),e.on("close",function(){e.detach()}),e.open()}}),b.register("gallery",_.extend({},e)),b.register("audio",_.extend({},f,{state:["audio-details"]})),b.register("video",_.extend({},f,{state:["video-details"]})),b.register("playlist",_.extend({},f,{state:["playlist-edit","video-playlist-edit"]})),b.register("embed",_.extend({},g)),b.register("embedURL",_.extend({},g,{match:function(a){var b=/(^|<p>)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi,c=b.exec(tinymce.trim(a));return c?{index:c.index+c[1].length,content:c[2],options:{url:c[2]}}:void 0}}))}(window,window.wp.mce.views,window.jQuery);