1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
!function(i,n,e){var s=wp.media,t={},a="undefined"==typeof _wpMediaViewsL10n?{}:_wpMediaViewsL10n;n.isUndefined(window._wpmejsSettings)||(t.pluginPath=_wpmejsSettings.pluginPath),wp.media.mixin={mejsSettings:t,pauseAllPlayers:function(){if(window.mejs&&window.mejs.players)for(var e in window.mejs.players)window.mejs.players[e].pause()},ua:{is:function(e){var t=!1,i=window.navigator.userAgent;switch(e){case"oldie":t=null!==i.match(/MSIE [6-8]/gi);break;case"ie":t=null!==i.match(/MSIE/gi);break;case"ff":t=null!==i.match(/firefox/gi);break;case"opera":t=null!==i.match(/OPR/);break;case"safari":t=null!==i.match(/safari/gi)&&null===i.match(/chrome/gi);break;case"chrome":t=null!==i.match(/safari/gi)&&null!==i.match(/chrome/gi)}return t}},compat:{opera:{audio:["ogg","wav"],video:["ogg","webm"]},chrome:{audio:["ogg","mpeg"],video:["ogg","webm","mp4","m4v","mpeg"]},ff:{audio:["ogg","mpeg"],video:["ogg","webm"]},safari:{audio:["mpeg","wav"],video:["mp4","m4v","mpeg","x-ms-wmv","quicktime"]},ie:{audio:["mpeg"],video:["mp4","m4v","mpeg"]}},isCompatible:function(e){if(!e.find("source").length)return!1;var t,i=this.ua,s=!1,o=!1;return!i.is("oldIE")&&(t=e.find("source"),n.find(this.compat,function(a,e){return i.is(e)&&(o=!0,n.each(t,function(e){var t=new RegExp("audio/("+a.audio.join("|")+")","gi"),i=new RegExp("video/("+a.video.join("|")+")","gi");null===e.type.match(i)&&null===e.type.match(t)||(s=!0)})),s||o}),s)},removePlayer:function(e){var t,i;for(t in e.options.features)if(e["clean"+(i=e.options.features[t])])try{e["clean"+i](e)}catch(e){}e.isDynamic||e.$node.remove(),"native"!==e.media.pluginType&&e.media.remove(),delete window.mejs.players[e.id],e.container.remove(),e.globalUnbind(),delete e.node.player},unsetPlayers:function(){this.players&&this.players.length&&(wp.media.mixin.pauseAllPlayers(),n.each(this.players,function(e){wp.media.mixin.removePlayer(e)}),this.players=[])}},wp.media.playlist=new wp.media.collection({tag:"playlist",editTitle:a.editPlaylistTitle,defaults:{id:wp.media.view.settings.post.id,style:"light",tracklist:!0,tracknumbers:!0,images:!0,artists:!0,type:"audio"}}),wp.media.audio={coerce:wp.media.coerce,defaults:{id:wp.media.view.settings.post.id,src:"",loop:!1,autoplay:!1,preload:"none",width:400},edit:function(e){e=wp.shortcode.next("audio",e).shortcode;return wp.media({frame:"audio",state:"audio-details",metadata:n.defaults(e.attrs.named,this.defaults)})},shortcode:function(i){var e,a=this;return n.each(this.defaults,function(e,t){i[t]=a.coerce(i,t),e===i[t]&&delete i[t]}),e=i.content,delete i.content,new wp.shortcode({tag:"audio",attrs:i,content:e})}},wp.media.video={coerce:wp.media.coerce,defaults:{id:wp.media.view.settings.post.id,src:"",poster:"",loop:!1,autoplay:!1,preload:"metadata",content:"",width:640,height:360},edit:function(e){var t=wp.shortcode.next("video",e).shortcode,e=t.attrs.named;return e.content=t.content,wp.media({frame:"video",state:"video-details",metadata:n.defaults(e,this.defaults)})},shortcode:function(i){var e,a=this;return n.each(this.defaults,function(e,t){i[t]=a.coerce(i,t),e===i[t]&&delete i[t]}),e=i.content,delete i.content,new wp.shortcode({tag:"video",attrs:i,content:e})}},s.model.PostMedia=e.Model.extend({initialize:function(){this.attachment=!1},setSource:function(e){this.attachment=e,this.extension=e.get("filename").split(".").pop(),this.get("src")&&this.extension===this.get("src").split(".").pop()&&this.unset("src"),n.contains(wp.media.view.settings.embedExts,this.extension)?this.set(this.extension,this.attachment.get("url")):this.unset(this.extension)},changeAttachment:function(e){var t=this;this.setSource(e),this.unset("src"),n.each(n.without(wp.media.view.settings.embedExts,this.extension),function(e){t.unset(e)})}}),s.controller.AudioDetails=s.controller.State.extend({defaults:{id:"audio-details",toolbar:"audio-details",title:a.audioDetailsTitle,content:"audio-details",menu:"audio-details",router:!1,priority:60},initialize:function(e){this.media=e.media,s.controller.State.prototype.initialize.apply(this,arguments)}}),s.controller.VideoDetails=s.controller.State.extend({defaults:{id:"video-details",toolbar:"video-details",title:a.videoDetailsTitle,content:"video-details",menu:"video-details",router:!1,priority:60},initialize:function(e){this.media=e.media,s.controller.State.prototype.initialize.apply(this,arguments)}}),s.view.MediaFrame.MediaDetails=s.view.MediaFrame.Select.extend({defaults:{id:"media",url:"",menu:"media-details",content:"media-details",toolbar:"media-details",type:"link",priority:120},initialize:function(e){this.DetailsView=e.DetailsView,this.cancelText=e.cancelText,this.addText=e.addText,this.media=new s.model.PostMedia(e.metadata),this.options.selection=new s.model.Selection(this.media.attachment,{multiple:!1}),s.view.MediaFrame.Select.prototype.initialize.apply(this,arguments)},bindHandlers:function(){var e=this.defaults.menu;s.view.MediaFrame.Select.prototype.bindHandlers.apply(this,arguments),this.on("menu:create:"+e,this.createMenu,this),this.on("content:render:"+e,this.renderDetailsContent,this),this.on("menu:render:"+e,this.renderMenu,this),this.on("toolbar:render:"+e,this.renderDetailsToolbar,this)},renderDetailsContent:function(){var e=new this.DetailsView({controller:this,model:this.state().media,attachment:this.state().media.attachment}).render();this.content.set(e)},renderMenu:function(e){var t=this.lastState(),i=t&&t.id,a=this;e.set({cancel:{text:this.cancelText,priority:20,click:function(){i?a.setState(i):a.close()}},separateCancel:new s.View({className:"separator",priority:40})})},setPrimaryButton:function(e,t){this.toolbar.set(new s.view.Toolbar({controller:this,items:{button:{style:"primary",text:e,priority:80,click:function(){var e=this.controller;t.call(this,e,e.state()),e.setState(e.options.state),e.reset()}}}}))},renderDetailsToolbar:function(){this.setPrimaryButton(a.update,function(e,t){e.close(),t.trigger("update",e.media.toJSON())})},renderReplaceToolbar:function(){this.setPrimaryButton(a.replace,function(e,t){var i=t.get("selection").single();e.media.changeAttachment(i),t.trigger("replace",e.media.toJSON())})},renderAddSourceToolbar:function(){this.setPrimaryButton(this.addText,function(e,t){var i=t.get("selection").single();e.media.setSource(i),t.trigger("add-source",e.media.toJSON())})}}),s.view.MediaFrame.AudioDetails=s.view.MediaFrame.MediaDetails.extend({defaults:{id:"audio",url:"",menu:"audio-details",content:"audio-details",toolbar:"audio-details",type:"link",title:a.audioDetailsTitle,priority:120},initialize:function(e){e.DetailsView=s.view.AudioDetails,e.cancelText=a.audioDetailsCancel,e.addText=a.audioAddSourceTitle,s.view.MediaFrame.MediaDetails.prototype.initialize.call(this,e)},bindHandlers:function(){s.view.MediaFrame.MediaDetails.prototype.bindHandlers.apply(this,arguments),this.on("toolbar:render:replace-audio",this.renderReplaceToolbar,this),this.on("toolbar:render:add-audio-source",this.renderAddSourceToolbar,this)},createStates:function(){this.states.add([new s.controller.AudioDetails({media:this.media}),new s.controller.MediaLibrary({type:"audio",id:"replace-audio",title:a.audioReplaceTitle,toolbar:"replace-audio",media:this.media,menu:"audio-details"}),new s.controller.MediaLibrary({type:"audio",id:"add-audio-source",title:a.audioAddSourceTitle,toolbar:"add-audio-source",media:this.media,menu:!1})])}}),s.view.MediaFrame.VideoDetails=s.view.MediaFrame.MediaDetails.extend({defaults:{id:"video",url:"",menu:"video-details",content:"video-details",toolbar:"video-details",type:"link",title:a.videoDetailsTitle,priority:120},initialize:function(e){e.DetailsView=s.view.VideoDetails,e.cancelText=a.videoDetailsCancel,e.addText=a.videoAddSourceTitle,s.view.MediaFrame.MediaDetails.prototype.initialize.call(this,e)},bindHandlers:function(){s.view.MediaFrame.MediaDetails.prototype.bindHandlers.apply(this,arguments),this.on("toolbar:render:replace-video",this.renderReplaceToolbar,this),this.on("toolbar:render:add-video-source",this.renderAddSourceToolbar,this),this.on("toolbar:render:select-poster-image",this.renderSelectPosterImageToolbar,this),this.on("toolbar:render:add-track",this.renderAddTrackToolbar,this)},createStates:function(){this.states.add([new s.controller.VideoDetails({media:this.media}),new s.controller.MediaLibrary({type:"video",id:"replace-video",title:a.videoReplaceTitle,toolbar:"replace-video",media:this.media,menu:"video-details"}),new s.controller.MediaLibrary({type:"video",id:"add-video-source",title:a.videoAddSourceTitle,toolbar:"add-video-source",media:this.media,menu:!1}),new s.controller.MediaLibrary({type:"image",id:"select-poster-image",title:a.videoSelectPosterImageTitle,toolbar:"select-poster-image",media:this.media,menu:"video-details"}),new s.controller.MediaLibrary({type:"text",id:"add-track",title:a.videoAddTrackTitle,toolbar:"add-track",media:this.media,menu:"video-details"})])},renderSelectPosterImageToolbar:function(){this.setPrimaryButton(a.videoSelectPosterImageTitle,function(e,t){var i=t.get("selection").single();e.media.set("poster",i.get("url")),t.trigger("set-poster-image",e.media.toJSON())})},renderAddTrackToolbar:function(){this.setPrimaryButton(a.videoAddTrackTitle,function(e,t){var i=t.get("selection").single(),a=e.media.get("content");-1===a.indexOf(i.get("url"))&&(a+=['<track srclang="en" label="English"kind="subtitles" src="',i.get("url"),'" />'].join(""),e.media.set("content",a)),t.trigger("add-track",e.media.toJSON())})}}),s.view.MediaDetails=s.view.Settings.AttachmentDisplay.extend({initialize:function(){n.bindAll(this,"success"),this.players=[],this.listenTo(this.controller,"close",s.mixin.unsetPlayers),this.on("ready",this.setPlayer),this.on("media:setting:remove",s.mixin.unsetPlayers,this),this.on("media:setting:remove",this.render),this.on("media:setting:remove",this.setPlayer),this.events=n.extend(this.events,{"click .remove-setting":"removeSetting","change .content-track":"setTracks","click .remove-track":"setTracks"}),s.view.Settings.AttachmentDisplay.prototype.initialize.apply(this,arguments)},prepare:function(){return n.defaults({model:this.model.toJSON()},this.options)},removeSetting:function(e){var t=i(e.currentTarget).parent(),e=t.find("input").data("setting");e&&(this.model.unset(e),this.trigger("media:setting:remove",this)),t.remove()},setTracks:function(){var t="";n.each(this.$(".content-track"),function(e){t+=i(e).val()}),this.model.set("content",t),this.trigger("media:setting:remove",this)},setPlayer:function(){!this.players.length&&this.media&&this.players.push(new MediaElementPlayer(this.media,this.settings))},setMedia:function(){return this},success:function(e){var t=e.attributes.autoplay&&"false"!==e.attributes.autoplay;"flash"===e.pluginType&&t&&e.addEventListener("canplay",function(){e.play()},!1),this.mejs=e},render:function(){var e=this;return s.view.Settings.AttachmentDisplay.prototype.render.apply(this,arguments),setTimeout(function(){e.resetFocus()},10),this.settings=n.defaults({success:this.success},t),this.setMedia()},resetFocus:function(){this.$(".embed-media-settings").scrollTop(0)}},{instances:0,prepareSrc:function(e){var t=s.view.MediaDetails.instances++;return n.each(i(e).find("source"),function(e){e.src=[e.src,-1<e.src.indexOf("?")?"&":"?","_=",t].join("")}),e}}),s.view.AudioDetails=s.view.MediaDetails.extend({className:"audio-details",template:s.template("audio-details"),setMedia:function(){var e=this.$(".wp-audio-shortcode");return e.find("source").length?(e.is(":hidden")&&e.show(),this.media=s.view.MediaDetails.prepareSrc(e.get(0))):(e.hide(),this.media=!1),this}}),s.view.VideoDetails=s.view.MediaDetails.extend({className:"video-details",template:s.template("video-details"),setMedia:function(){var e=this.$(".wp-video-shortcode");return e.find("source").length?(e.is(":hidden")&&e.show(),e.hasClass("youtube-video")?this.media=e.get(0):this.media=s.view.MediaDetails.prepareSrc(e.get(0))):(e.hide(),this.media=!1),this}}),i(function(){i(document.body).on("click",".wp-switch-editor",wp.media.mixin.pauseAllPlayers).on("click",".add-media-source",function(e){s.frame.lastMime=i(e.currentTarget).data("mime"),s.frame.setState("add-"+s.frame.defaults.id+"-source")})})}(jQuery,_,Backbone); |