Wordpress/wp-includes/js/mce-view.min.js
2012-10-12 22:58:48 +00:00

1 line
7.1 KiB
JavaScript

window.wp=window.wp||{};(function(){wp.html=_.extend(wp.html||{},{attrs:function(c){var a,b;if("/"===c[c.length-1]){c=c.slice(0,-1)}a=wp.shortcode.attrs(c);b=a.named;_.each(a.numeric,function(d){if(/\s/.test(d)){return}b[d]=""});return b},string:function(a){var c="<"+a.tag,b=a.content||"";_.each(a.attrs,function(e,d){c+=" "+d;if(""===e){return}if(_.isBoolean(e)){e=e?"true":"false"}c+='="'+e+'"'});if(a.single){return c+" />"}c+=">";c+=_.isObject(b)?wp.html.string(b):b;return c+"</"+a.tag+">"}})}());(function(b){var a={},c={};wp.mce=wp.mce||{};wp.mce.view={defaults:{pattern:{view:Backbone.View,text:function(d){return d.options.original},toView:function(e){if(!this.pattern){return}this.pattern.lastIndex=0;var d=this.pattern.exec(e);if(!d){return}return{index:d.index,content:d[0],options:{original:d[0],results:d}}}},shortcode:{view:Backbone.View,text:function(d){return d.options.shortcode.string()},toView:function(e){var d=wp.shortcode.next(this.shortcode,e);if(!d){return}return{index:d.index,content:d.content,options:{shortcode:d.shortcode}}}}},add:function(i,e){var g,d,h,f;if(e.extend){g=wp.mce.view.get(e.extend)}else{if(e.shortcode){g=wp.mce.view.defaults.shortcode}else{g=wp.mce.view.defaults.pattern}}_.defaults(e,g);e.id=i;f={remove:function(){delete c[this.el.id];this.$el.parent().remove();if(d){d.apply(this,arguments)}return this}};if(_.isFunction(e.view)){h=e.view}else{h=g.view;d=e.view.remove;_.defaults(f,e.view)}if(!d&&!h._mceview){d=h.prototype.remove}e.view=h.extend(f,{_mceview:true});a[i]=e},get:function(d){return a[d]},remove:function(d){delete a[d]},toViews:function(e){var d=[{content:e}],f;_.each(a,function(h,g){f=d.slice();d=[];_.each(f,function(k){var j=k.content,i;if(k.processed){d.push(k);return}while(j&&(i=h.toView(j))){if(i.index){d.push({content:j.substring(0,i.index)})}d.push({content:wp.mce.view.toView(g,i.options),processed:true});j=j.slice(i.index+i.content.length)}if(j){d.push({content:j})}})});return _.pluck(d,"content").join("")},toView:function(e,g){var f=wp.mce.view.get(e),d,h;if(!f){return""}d=new f.view(_.extend(g||{},{viewType:e}));h=d.el.id=d.el.id||_.uniqueId("__wpmce-");c[h]=d;d.$wrapper=b();return wp.html.string({tag:"span"===d.tagName?"span":"div",attrs:{"class":"wp-view-wrap wp-view-type-"+e,"data-wp-view":h,contenteditable:false}})},render:function(d){b(".wp-view-wrap",d).each(function(){var f=b(this),e=wp.mce.view.instance(this);if(!e){return}e.$wrapper=f;e.render();e.$el.detach();f.empty().append(e.el).append('<span data-wp-view-end class="wp-view-end"></span>')})},toText:function(d){return d.replace(/<(?:div|span)[^>]+data-wp-view="([^"]+)"[^>]*>.*?<span[^>]+data-wp-view-end[^>]*><\/span><\/(?:div|span)>/g,function(g,h){var e=c[h],f;if(e){f=wp.mce.view.get(e.options.viewType)}return e&&f?f.text(e):""})},removeInternalAttrs:function(e){var d={};_.each(e,function(g,f){if(-1===f.indexOf("data-mce")){d[f]=g}});return d},attrs:function(d){return wp.mce.view.removeInternalAttrs(wp.html.attrs(d))},instance:function(d){var e=b(d).data("wp-view");if(e){return c[e]}},select:function(e){var d=b(e);if(d.hasClass("selected")){return}d.addClass("selected");b(e.firstChild).trigger("select")},deselect:function(e){var d=b(e);if(!d.hasClass("selected")){return}d.removeClass("selected");b(e.firstChild).trigger("deselect")},l10n:_.isUndefined(_wpMceViewL10n)?{}:_wpMceViewL10n}}(jQuery));(function(b){var a=wp.mce.view;wp.media.string={};wp.media.string.link=function(e){var d=getUserSetting("urlbutton","post"),c={tag:"a",content:e.get("title")||e.get("filename"),attrs:{rel:"attachment wp-att-"+e.id}};c.attrs.href=(d==="file")?e.get("url"):e.get("link");return wp.html.string(c)};wp.media.string.image=function(h,g){var f,c,d,e;h=h.toJSON();g=_.defaults(g||{},{img:{},align:getUserSetting("align","none"),size:getUserSetting("imgsize","medium"),link:getUserSetting("urlbutton","post")});c=_.clone(g.img);f=c["class"]?c["class"].split(/\s+/):[];e=h.sizes?h.sizes[g.size]:{};if(!e){delete g.size;e=h}c.width=e.width;c.height=e.height;c.src=e.url;if(g.align){f.push("align"+g.align)}if(g.size){f.push("size-"+g.size)}f.push("wp-image-"+h.id);c["class"]=_.compact(f).join(" ");d={tag:"img",attrs:c,single:true};if(g.anchor){d={tag:"a",attrs:g.anchor,content:d}}return wp.html.string(d)};a.add("attachment",{pattern:new RegExp("(?:<a([^>]*)>)?<img([^>]*class=(?:\"[^\"]*|'[^']*)\\bwp-image-(\\d+)[^>]*)>(?:</a>)?"),text:function(c){var d=_.pick(c,"align","size","link","img","anchor");return wp.media.string.image(c.model,d)},view:{className:"editor-attachment",template:media.template("editor-attachment"),events:{"click .close":"remove"},initialize:function(){var c=this,d=this.options.results,f=d[3],e;this.model=wp.media.model.Attachment.get(f);if(d[1]){this.anchor=a.attrs(d[1])}this.img=a.attrs(d[2]);e=this.img["class"];e=e.replace(/(?:^|\s)wp-image-\d+/,"");e=e.replace(/(?:^|\s)size-(\S+)/,function(g,h){c.size=h;return""});e=e.replace(/(?:^|\s)align(left|center|right|none)(?:\s|$)/,function(g,h){c.align=h;return""});this.img["class"]=e;this.$el.addClass("spinner");this.model.fetch().done(_.bind(this.render,this))},render:function(){var d=this.model.toJSON(),c;if(!d.url){return}if(this.align){this.$wrapper.addClass("align"+this.align)}c={url:"image"===d.type?d.url:d.icon,uploading:d.uploading};_.extend(c,wp.media.fit({width:d.width,height:d.height,maxWidth:a.l10n.contentWidth}));if(this.size&&d.sizes&&d.sizes[this.size]){_.extend(c,_.pick(d.sizes[this.size],"url","width","height"))}this.$el.html(this.template(c))}}});a.add("gallery",{shortcode:"gallery",gallery:(function(){var c={};return{attachments:function(h,g){var i=h.string(),d=c[i],f,e;delete c[i];if(d){return d}f=h.attrs.named;e=_.pick(f,"orderby","order");e.type="image";e.perPage=-1;if(f.ids){e.post__in=f.ids.split(",");e.orderby="post__in"}else{if(f.include){e.post__in=f.include.split(",")}}if(f.exclude){e.post__not_in=f.exclude.split(",")}if(!e.post__in){e.parent=f.id||g}return media.query(e)},shortcode:function(d){var f=d.props.toJSON(),e=_.pick(f,"include","exclude","orderby","order"),g;e.ids=d.pluck("id");g=new wp.shortcode({tag:"gallery",attrs:e,type:"single"});c[g.string()]=new wp.media.model.Attachments(d.models,{props:f});return g}}}()),view:{className:"editor-gallery",template:media.template("editor-gallery"),parent:b("#post_ID").val(),events:{"click .close":"remove","click .edit":"edit"},initialize:function(){this.update()},update:function(){var c=a.get("gallery");this.attachments=c.gallery.attachments(this.options.shortcode,this.parent);this.attachments.more().done(_.bind(this.render,this))},render:function(){var c,e,d;if(!this.attachments.length){return}e=this.attachments.first().toJSON();d=e.sizes&&e.sizes.thumbnail?e.sizes.thumbnail:e;c={url:d.url,orientation:d.orientation,count:this.attachments.length};this.$el.html(this.template(c))},edit:function(){if(!wp.media.view||this.workflow){return}this.workflow=wp.media({view:"gallery",selection:this.attachments.models,title:a.l10n.editGallery,editing:true,multiple:true,describe:true});this.workflow.on("close",function(){this.workflow.detach();delete this.workflow},this);this.workflow.on("update:gallery",function(d){var c=a.get("gallery");this.options.shortcode=c.gallery.shortcode(d);this.update()},this)}}})}(jQuery));