WordPress/wp-admin/js/widgets/media-widgets.min.js

2 lines
14 KiB
JavaScript

/*! This file is auto-generated */
wp.mediaWidgets=function(h){"use strict";var g={controlConstructors:{},modelConstructors:{}};return g.PersistentDisplaySettingsLibrary=wp.media.controller.Library.extend({initialize:function(e){_.bindAll(this,"handleDisplaySettingChange"),wp.media.controller.Library.prototype.initialize.call(this,e)},handleDisplaySettingChange:function(e){this.get("selectedDisplaySettings").set(e.attributes)},display:function(e){var t,i=this.get("selectedDisplaySettings");return(t=wp.media.controller.Library.prototype.display.call(this,e)).off("change",this.handleDisplaySettingChange),t.set(i.attributes),"custom"===i.get("link_type")&&(t.linkUrl=i.get("link_url")),t.on("change",this.handleDisplaySettingChange),t}}),g.MediaEmbedView=wp.media.view.Embed.extend({initialize:function(e){var t,i=this;wp.media.view.Embed.prototype.initialize.call(i,e),"image"!==i.controller.options.mimeType&&(t=i.controller.states.get("embed")).off("scan",t.scanImage,t)},refresh:function(){var e;e="image"===this.controller.options.mimeType?wp.media.view.EmbedImage:wp.media.view.EmbedLink.extend({setAddToWidgetButtonDisabled:function(e){this.views.parent.views.parent.views.get(".media-frame-toolbar")[0].$el.find(".media-button-select").prop("disabled",e)},setErrorNotice:function(e){var t;t=this.views.parent.$el.find("> .notice:first-child"),e?(t.length||((t=h('<div class="media-widget-embed-notice notice notice-error notice-alt"></div>')).hide(),this.views.parent.$el.prepend(t)),t.empty(),t.append(h("<p>",{html:e})),t.slideDown("fast")):t.length&&t.slideUp("fast")},updateoEmbed:function(){var e,t=this;if(!(e=t.model.get("url")))return t.setErrorNotice(""),void t.setAddToWidgetButtonDisabled(!0);e.match(/^(http|https):\/\/.+\//)||(t.controller.$el.find("#embed-url-field").addClass("invalid"),t.setAddToWidgetButtonDisabled(!0)),wp.media.view.EmbedLink.prototype.updateoEmbed.call(t)},fetch:function(){var t,e,i,n,d,o,a=this;if(d=a.model.get("url"),a.dfd&&"pending"===a.dfd.state()&&a.dfd.abort(),t=function(e){a.renderoEmbed({data:{body:e}}),a.controller.$el.find("#embed-url-field").removeClass("invalid"),a.setErrorNotice(""),a.setAddToWidgetButtonDisabled(!1)},(n=document.createElement("a")).href=d,e=n.pathname.toLowerCase().match(/\.(\w+)$/))return i=e[1],void(wp.media.view.settings.embedMimes[i]?0!==wp.media.view.settings.embedMimes[i].indexOf(a.controller.options.mimeType)?a.renderFail():t("\x3c!--success--\x3e"):a.renderFail());(o=/https?:\/\/www\.youtube\.com\/embed\/([^/]+)/.exec(d))&&(d="https://www.youtube.com/watch?v="+o[1],a.model.attributes.url=d),a.dfd=wp.apiRequest({url:wp.media.view.settings.oEmbedProxyUrl,data:{url:d,maxwidth:a.model.get("width"),maxheight:a.model.get("height"),discover:!1},type:"GET",dataType:"json",context:a}),a.dfd.done(function(e){a.controller.options.mimeType===e.type?t(e.html):a.renderFail()}),a.dfd.fail(_.bind(a.renderFail,a))},renderFail:function(){var e=this;e.controller.$el.find("#embed-url-field").addClass("invalid"),e.setErrorNotice(e.controller.options.invalidEmbedTypeError||"ERROR"),e.setAddToWidgetButtonDisabled(!0)}}),this.settings(new e({controller:this.controller,model:this.model.props,priority:40}))}}),g.MediaFrameSelect=wp.media.view.MediaFrame.Post.extend({createStates:function(){var t=this.options.mimeType,i=[];_.each(wp.media.view.settings.embedMimes,function(e){0===e.indexOf(t)&&i.push(e)}),0<i.length&&(t=i),this.states.add([new g.PersistentDisplaySettingsLibrary({id:"insert",title:this.options.title,selection:this.options.selection,priority:20,toolbar:"main-insert",filterable:"dates",library:wp.media.query({type:t}),multiple:!1,editable:!0,selectedDisplaySettings:this.options.selectedDisplaySettings,displaySettings:!!_.isUndefined(this.options.showDisplaySettings)||this.options.showDisplaySettings,displayUserSettings:!1}),new wp.media.controller.EditImage({model:this.options.editImage}),new wp.media.controller.Embed({metadata:this.options.metadata,type:"image"===this.options.mimeType?"image":"link",invalidEmbedTypeError:this.options.invalidEmbedTypeError})])},mainInsertToolbar:function(e){var i=this;e.set("insert",{style:"primary",priority:80,text:i.options.text,requires:{selection:!0},click:function(){var e=i.state(),t=e.get("selection");i.close(),e.trigger("insert",t).reset()}})},mainEmbedToolbar:function(e){e.view=new wp.media.view.Toolbar.Embed({controller:this,text:this.options.text,event:"insert"})},embedContent:function(){var e=new g.MediaEmbedView({controller:this,model:this.state()}).render();this.content.set(e)}}),g.MediaWidgetControl=Backbone.View.extend({l10n:{add_to_widget:"{{add_to_widget}}",add_media:"{{add_media}}"},id_base:"",mime_type:"",events:{"click .notice-missing-attachment a":"handleMediaLibraryLinkClick","click .select-media":"selectMedia","click .placeholder":"selectMedia","click .edit-media":"editMedia"},showDisplaySettings:!0,initialize:function(e){var i=this;if(Backbone.View.prototype.initialize.call(i,e),!(i.model instanceof g.MediaWidgetModel))throw new Error("Missing options.model");if(!e.el)throw new Error("Missing options.el");if(!e.syncContainer)throw new Error("Missing options.syncContainer");if(i.syncContainer=e.syncContainer,i.$el.addClass("media-widget-control"),_.bindAll(i,"syncModelToInputs","render","updateSelectedAttachment","renderPreview"),!i.id_base&&(_.find(g.controlConstructors,function(e,t){return i instanceof e&&(i.id_base=t,!0)}),!i.id_base))throw new Error("Missing id_base.");i.previewTemplateProps=new Backbone.Model(i.mapModelToPreviewTemplateProps()),i.selectedAttachment=new wp.media.model.Attachment,i.renderPreview=_.debounce(i.renderPreview),i.listenTo(i.previewTemplateProps,"change",i.renderPreview),i.model.on("change:attachment_id",i.updateSelectedAttachment),i.model.on("change:url",i.updateSelectedAttachment),i.updateSelectedAttachment(),i.listenTo(i.model,"change",i.syncModelToInputs),i.listenTo(i.model,"change",i.syncModelToPreviewProps),i.listenTo(i.model,"change",i.render),i.$el.on("input change",".title",function(){i.model.set({title:h.trim(h(this).val())})}),i.$el.on("input change",".link",function(){var e=h.trim(h(this).val()),t="custom";i.selectedAttachment.get("linkUrl")===e||i.selectedAttachment.get("link")===e?t="post":i.selectedAttachment.get("url")===e&&(t="file"),i.model.set({link_url:e,link_type:t}),i.displaySettings.set({link:t,linkUrl:e})}),i.displaySettings=new Backbone.Model(_.pick(i.mapModelToMediaFrameProps(_.extend(i.model.defaults(),i.model.toJSON())),_.keys(wp.media.view.settings.defaultProps)))},updateSelectedAttachment:function(){var e,t=this;0===t.model.get("attachment_id")?(t.selectedAttachment.clear(),t.model.set("error",!1)):t.model.get("attachment_id")!==t.selectedAttachment.get("id")&&(e=new wp.media.model.Attachment({id:t.model.get("attachment_id")})).fetch().done(function(){t.model.set("error",!1),t.selectedAttachment.set(e.toJSON())}).fail(function(){t.model.set("error","missing_attachment")})},syncModelToPreviewProps:function(){this.previewTemplateProps.set(this.mapModelToPreviewTemplateProps())},syncModelToInputs:function(){var n=this;n.syncContainer.find(".media-widget-instance-property").each(function(){var e,t,i=h(this);t=i.data("property"),e=n.model.get(t),_.isUndefined(e)||(e="array"===n.model.schema[t].type&&_.isArray(e)?e.join(","):"boolean"===n.model.schema[t].type?e?"1":"":String(e),i.val()!==e&&(i.val(e),i.trigger("change")))})},template:function(){if(!h("#tmpl-widget-media-"+this.id_base+"-control").length)throw new Error("Missing widget control template for "+this.id_base);return wp.template("widget-media-"+this.id_base+"-control")},render:function(){var e,t=this;t.templateRendered||(t.$el.html(t.template()(t.model.toJSON())),t.renderPreview(),t.templateRendered=!0),(e=t.$el.find(".title")).is(document.activeElement)||e.val(t.model.get("title")),t.$el.toggleClass("selected",t.isSelected())},renderPreview:function(){throw new Error("renderPreview must be implemented")},isSelected:function(){return!this.model.get("error")&&Boolean(this.model.get("attachment_id")||this.model.get("url"))},handleMediaLibraryLinkClick:function(e){e.preventDefault(),this.selectMedia()},selectMedia:function(){var e,i,t,n,d=this,o=[];d.isSelected()&&0!==d.model.get("attachment_id")&&o.push(d.selectedAttachment),e=new wp.media.model.Selection(o,{multiple:!1}),(n=d.mapModelToMediaFrameProps(d.model.toJSON())).size&&d.displaySettings.set("size",n.size),i=new g.MediaFrameSelect({title:d.l10n.add_media,frame:"post",text:d.l10n.add_to_widget,selection:e,mimeType:d.mime_type,selectedDisplaySettings:d.displaySettings,showDisplaySettings:d.showDisplaySettings,metadata:n,state:d.isSelected()&&0===d.model.get("attachment_id")?"embed":"insert",invalidEmbedTypeError:d.l10n.unsupported_file_type}),(wp.media.frame=i).on("insert",function(){var e={},t=i.state();"embed"===t.get("id")?_.extend(e,{id:0},t.props.toJSON()):_.extend(e,t.get("selection").first().toJSON()),d.selectedAttachment.set(e),d.model.set("error",!1),d.model.set(d.getModelPropsFromMediaFrame(i))}),t=wp.media.model.Attachment.prototype.sync,wp.media.model.Attachment.prototype.sync=function(e){return"delete"===e?t.apply(this,arguments):h.Deferred().rejectWith(this).promise()},i.on("close",function(){wp.media.model.Attachment.prototype.sync=t}),i.$el.addClass("media-widget"),i.open(),e&&e.on("destroy",function(e){d.model.get("attachment_id")===e.get("id")&&d.model.set({attachment_id:0,url:""})}),i.$el.find(".media-frame-menu .media-menu-item.active").focus()},getModelPropsFromMediaFrame:function(e){var t,i,n,d=this;if("insert"===(t=e.state()).get("id"))(i=t.get("selection").first().toJSON()).postUrl=i.link,d.showDisplaySettings&&_.extend(i,e.content.get(".attachments-browser").sidebar.get("display").model.toJSON()),i.sizes&&i.size&&i.sizes[i.size]&&(i.url=i.sizes[i.size].url);else{if("embed"!==t.get("id"))throw new Error("Unexpected state: "+t.get("id"));i=_.extend(t.props.toJSON(),{attachment_id:0},d.model.getEmbedResetProps())}return i.id&&(i.attachment_id=i.id),n=d.mapMediaToModelProps(i),_.each(wp.media.view.settings.embedExts,function(e){e in d.model.schema&&n.url!==n[e]&&(n[e]="")}),n},mapMediaToModelProps:function(e){var t,n=this,d={},o={};return _.each(n.model.schema,function(e,t){"title"!==t&&(d[e.media_prop||t]=t)}),_.each(e,function(e,t){var i=d[t]||t;n.model.schema[i]&&(o[i]=e)}),"custom"===e.size&&(o.width=e.customWidth,o.height=e.customHeight),"post"===e.link?o.link_url=e.postUrl||e.linkUrl:"file"===e.link&&(o.link_url=e.url),!e.attachment_id&&e.id&&(o.attachment_id=e.id),e.url&&(t=e.url.replace(/#.*$/,"").replace(/\?.*$/,"").split(".").pop().toLowerCase())in n.model.schema&&(o[t]=e.url),_.omit(o,"title")},mapModelToMediaFrameProps:function(e){var n=this,d={};return _.each(e,function(e,t){var i=n.model.schema[t]||{};d[i.media_prop||t]=e}),d.attachment_id=d.id,"custom"===d.size&&(d.customWidth=n.model.get("width"),d.customHeight=n.model.get("height")),d},mapModelToPreviewTemplateProps:function(){var i=this,n={};return _.each(i.model.schema,function(e,t){e.hasOwnProperty("should_preview_update")&&!e.should_preview_update||(n[t]=i.model.get(t))}),n.error=i.model.get("error"),n},editMedia:function(){throw new Error("editMedia not implemented")}}),g.MediaWidgetModel=Backbone.Model.extend({idAttribute:"widget_id",schema:{title:{type:"string",default:""},attachment_id:{type:"integer",default:0},url:{type:"string",default:""}},defaults:function(){var i={};return _.each(this.schema,function(e,t){i[t]=e.default}),i},set:function(e,t,i){var n,d,o,a=this;return null===e?a:(d="object"==typeof e?(n=e,t):((n={})[e]=t,i),o={},_.each(n,function(e,t){var i;a.schema[t]?"array"===(i=a.schema[t].type)?(o[t]=e,_.isArray(o[t])||(o[t]=o[t].split(/,/)),a.schema[t].items&&"integer"===a.schema[t].items.type&&(o[t]=_.filter(_.map(o[t],function(e){return parseInt(e,10)},function(e){return"number"==typeof e})))):o[t]="integer"===i?parseInt(e,10):"boolean"===i?!(!e||"0"===e||"false"===e):e:o[t]=e}),Backbone.Model.prototype.set.call(this,o,d))},getEmbedResetProps:function(){return{id:0}}}),g.modelCollection=new(Backbone.Collection.extend({model:g.MediaWidgetModel})),g.widgetControls={},g.handleWidgetAdded=function(e,t){var i,n,d,o,a,r,s,l,c,m,p;o=(d=t.find("> .widget-inside > .form, > .widget-inside > form")).find("> .id_base").val(),m=d.find("> .widget-id").val(),g.widgetControls[m]||(a=g.controlConstructors[o])&&(r=g.modelConstructors[o]||g.MediaWidgetModel,i=h("<div></div>"),(n=t.find(".widget-content:first")).before(i),s={},n.find(".media-widget-instance-property").each(function(){var e=h(this);s[e.data("property")]=e.val()}),s.widget_id=m,c=new r(s),l=new a({el:i,syncContainer:n,model:c}),(p=function(){t.hasClass("open")?l.render():setTimeout(p,50)})(),g.modelCollection.add([c]),g.widgetControls[c.get("widget_id")]=l)},g.setupAccessibleMode=function(){var e,t,i,n,d,o,a,r,s;0!==(e=h(".editwidget > form")).length&&(i=e.find("> .widget-control-actions > .id_base").val(),(d=g.controlConstructors[i])&&(t=e.find("> .widget-control-actions > .widget-id").val(),o=g.modelConstructors[i]||g.MediaWidgetModel,r=h("<div></div>"),(s=e.find("> .widget-inside")).before(r),a={},s.find(".media-widget-instance-property").each(function(){var e=h(this);a[e.data("property")]=e.val()}),a.widget_id=t,n=new d({el:r,syncContainer:s,model:new o(a)}),g.modelCollection.add([n.model]),(g.widgetControls[n.model.get("widget_id")]=n).render()))},g.handleWidgetUpdated=function(e,t){var i,n,d,o={};n=(i=t.find("> .widget-inside > .form, > .widget-inside > form")).find("> .widget-id").val(),(d=g.widgetControls[n])&&(i.find("> .widget-content").find(".media-widget-instance-property").each(function(){var e=h(this).data("property");o[e]=h(this).val()}),d.stopListening(d.model,"change",d.syncModelToInputs),d.model.set(o),d.listenTo(d.model,"change",d.syncModelToInputs))},g.init=function(){var e=h(document);e.on("widget-added",g.handleWidgetAdded),e.on("widget-synced widget-updated",g.handleWidgetUpdated),h(function(){"widgets"===window.pagenow&&(h(".widgets-holder-wrap:not(#available-widgets)").find("div.widget").one("click.toggle-widget-expanded",function(){var e=h(this);g.handleWidgetAdded(new jQuery.Event("widget-added"),e)}),h(window).on("load",function(){g.setupAccessibleMode()}))})},g}(jQuery);