|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wp.mediaWidgets = ( function( $ ) { |
|
'use strict'; |
|
|
|
var component = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.controlConstructors = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.modelConstructors = {}; |
|
|
|
component.PersistentDisplaySettingsLibrary = wp.media.controller.Library.extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize: function initialize( options ) { |
|
_.bindAll( this, 'handleDisplaySettingChange' ); |
|
wp.media.controller.Library.prototype.initialize.call( this, options ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleDisplaySettingChange: function handleDisplaySettingChange( displaySettings ) { |
|
this.get( 'selectedDisplaySettings' ).set( displaySettings.attributes ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display: function getDisplaySettingsModel( model ) { |
|
var display, selectedDisplaySettings = this.get( 'selectedDisplaySettings' ); |
|
display = wp.media.controller.Library.prototype.display.call( this, model ); |
|
|
|
display.off( 'change', this.handleDisplaySettingChange ); |
|
display.set( selectedDisplaySettings.attributes ); |
|
if ( 'custom' === selectedDisplaySettings.get( 'link_type' ) ) { |
|
display.linkUrl = selectedDisplaySettings.get( 'link_url' ); |
|
} |
|
display.on( 'change', this.handleDisplaySettingChange ); |
|
return display; |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.MediaEmbedView = wp.media.view.Embed.extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize: function( options ) { |
|
var view = this, embedController; |
|
wp.media.view.Embed.prototype.initialize.call( view, options ); |
|
if ( 'image' !== view.controller.options.mimeType ) { |
|
embedController = view.controller.states.get( 'embed' ); |
|
embedController.off( 'scan', embedController.scanImage, embedController ); |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
refresh: function refresh() { |
|
|
|
|
|
|
|
var Constructor; |
|
|
|
if ( 'image' === this.controller.options.mimeType ) { |
|
Constructor = wp.media.view.EmbedImage; |
|
} else { |
|
|
|
|
|
Constructor = wp.media.view.EmbedLink.extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setAddToWidgetButtonDisabled: function setAddToWidgetButtonDisabled( disabled ) { |
|
this.views.parent.views.parent.views.get( '.media-frame-toolbar' )[0].$el.find( '.media-button-select' ).prop( 'disabled', disabled ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setErrorNotice: function setErrorNotice( notice ) { |
|
var embedLinkView = this, noticeContainer; |
|
|
|
noticeContainer = embedLinkView.views.parent.$el.find( '> .notice:first-child' ); |
|
if ( ! notice ) { |
|
if ( noticeContainer.length ) { |
|
noticeContainer.slideUp( 'fast' ); |
|
} |
|
} else { |
|
if ( ! noticeContainer.length ) { |
|
noticeContainer = $( '<div class="media-widget-embed-notice notice notice-error notice-alt" role="alert"></div>' ); |
|
noticeContainer.hide(); |
|
embedLinkView.views.parent.$el.prepend( noticeContainer ); |
|
} |
|
noticeContainer.empty(); |
|
noticeContainer.append( $( '<p>', { |
|
html: notice |
|
})); |
|
noticeContainer.slideDown( 'fast' ); |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateoEmbed: function() { |
|
var embedLinkView = this, url; |
|
|
|
url = embedLinkView.model.get( 'url' ); |
|
|
|
|
|
if ( ! url ) { |
|
embedLinkView.setErrorNotice( '' ); |
|
embedLinkView.setAddToWidgetButtonDisabled( true ); |
|
return; |
|
} |
|
|
|
if ( ! url.match( /^(http|https):\/\/.+\// ) ) { |
|
embedLinkView.controller.$el.find( '#embed-url-field' ).addClass( 'invalid' ); |
|
embedLinkView.setAddToWidgetButtonDisabled( true ); |
|
} |
|
|
|
wp.media.view.EmbedLink.prototype.updateoEmbed.call( embedLinkView ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch: function() { |
|
var embedLinkView = this, fetchSuccess, matches, fileExt, urlParser, url, re, youTubeEmbedMatch; |
|
url = embedLinkView.model.get( 'url' ); |
|
|
|
if ( embedLinkView.dfd && 'pending' === embedLinkView.dfd.state() ) { |
|
embedLinkView.dfd.abort(); |
|
} |
|
|
|
fetchSuccess = function( response ) { |
|
embedLinkView.renderoEmbed({ |
|
data: { |
|
body: response |
|
} |
|
}); |
|
|
|
embedLinkView.controller.$el.find( '#embed-url-field' ).removeClass( 'invalid' ); |
|
embedLinkView.setErrorNotice( '' ); |
|
embedLinkView.setAddToWidgetButtonDisabled( false ); |
|
}; |
|
|
|
urlParser = document.createElement( 'a' ); |
|
urlParser.href = url; |
|
matches = urlParser.pathname.toLowerCase().match( /\.(\w+)$/ ); |
|
if ( matches ) { |
|
fileExt = matches[1]; |
|
if ( ! wp.media.view.settings.embedMimes[ fileExt ] ) { |
|
embedLinkView.renderFail(); |
|
} else if ( 0 !== wp.media.view.settings.embedMimes[ fileExt ].indexOf( embedLinkView.controller.options.mimeType ) ) { |
|
embedLinkView.renderFail(); |
|
} else { |
|
fetchSuccess( '<!--success-->' ); |
|
} |
|
return; |
|
} |
|
|
|
|
|
re = /https?:\/\/www\.youtube\.com\/embed\/([^/]+)/; |
|
youTubeEmbedMatch = re.exec( url ); |
|
if ( youTubeEmbedMatch ) { |
|
url = 'https://www.youtube.com/watch?v=' + youTubeEmbedMatch[ 1 ]; |
|
|
|
embedLinkView.model.attributes.url = url; |
|
} |
|
|
|
embedLinkView.dfd = wp.apiRequest({ |
|
url: wp.media.view.settings.oEmbedProxyUrl, |
|
data: { |
|
url: url, |
|
maxwidth: embedLinkView.model.get( 'width' ), |
|
maxheight: embedLinkView.model.get( 'height' ), |
|
discover: false |
|
}, |
|
type: 'GET', |
|
dataType: 'json', |
|
context: embedLinkView |
|
}); |
|
|
|
embedLinkView.dfd.done( function( response ) { |
|
if ( embedLinkView.controller.options.mimeType !== response.type ) { |
|
embedLinkView.renderFail(); |
|
return; |
|
} |
|
fetchSuccess( response.html ); |
|
}); |
|
embedLinkView.dfd.fail( _.bind( embedLinkView.renderFail, embedLinkView ) ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderFail: function renderFail() { |
|
var embedLinkView = this; |
|
embedLinkView.controller.$el.find( '#embed-url-field' ).addClass( 'invalid' ); |
|
embedLinkView.setErrorNotice( embedLinkView.controller.options.invalidEmbedTypeError || 'ERROR' ); |
|
embedLinkView.setAddToWidgetButtonDisabled( true ); |
|
} |
|
}); |
|
} |
|
|
|
this.settings( new Constructor({ |
|
controller: this.controller, |
|
model: this.model.props, |
|
priority: 40 |
|
})); |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.MediaFrameSelect = wp.media.view.MediaFrame.Post.extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
createStates: function createStates() { |
|
var mime = this.options.mimeType, specificMimes = []; |
|
_.each( wp.media.view.settings.embedMimes, function( embedMime ) { |
|
if ( 0 === embedMime.indexOf( mime ) ) { |
|
specificMimes.push( embedMime ); |
|
} |
|
}); |
|
if ( specificMimes.length > 0 ) { |
|
mime = specificMimes; |
|
} |
|
|
|
this.states.add([ |
|
|
|
|
|
new component.PersistentDisplaySettingsLibrary({ |
|
id: 'insert', |
|
title: this.options.title, |
|
selection: this.options.selection, |
|
priority: 20, |
|
toolbar: 'main-insert', |
|
filterable: 'dates', |
|
library: wp.media.query({ |
|
type: mime |
|
}), |
|
multiple: false, |
|
editable: true, |
|
|
|
selectedDisplaySettings: this.options.selectedDisplaySettings, |
|
displaySettings: _.isUndefined( this.options.showDisplaySettings ) ? true : this.options.showDisplaySettings, |
|
displayUserSettings: false |
|
}), |
|
|
|
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 mainInsertToolbar( view ) { |
|
var controller = this; |
|
view.set( 'insert', { |
|
style: 'primary', |
|
priority: 80, |
|
text: controller.options.text, |
|
requires: { selection: true }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
click: function onClick() { |
|
var state = controller.state(), |
|
selection = state.get( 'selection' ); |
|
|
|
controller.close(); |
|
state.trigger( 'insert', selection ).reset(); |
|
} |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainEmbedToolbar: function mainEmbedToolbar( toolbar ) { |
|
toolbar.view = new wp.media.view.Toolbar.Embed({ |
|
controller: this, |
|
text: this.options.text, |
|
event: 'insert' |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
embedContent: function embedContent() { |
|
var view = new component.MediaEmbedView({ |
|
controller: this, |
|
model: this.state() |
|
}).render(); |
|
|
|
this.content.set( view ); |
|
} |
|
}); |
|
|
|
component.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: true, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize: function initialize( options ) { |
|
var control = this; |
|
|
|
Backbone.View.prototype.initialize.call( control, options ); |
|
|
|
if ( ! ( control.model instanceof component.MediaWidgetModel ) ) { |
|
throw new Error( 'Missing options.model' ); |
|
} |
|
if ( ! options.el ) { |
|
throw new Error( 'Missing options.el' ); |
|
} |
|
if ( ! options.syncContainer ) { |
|
throw new Error( 'Missing options.syncContainer' ); |
|
} |
|
|
|
control.syncContainer = options.syncContainer; |
|
|
|
control.$el.addClass( 'media-widget-control' ); |
|
|
|
|
|
_.bindAll( control, 'syncModelToInputs', 'render', 'updateSelectedAttachment', 'renderPreview' ); |
|
|
|
if ( ! control.id_base ) { |
|
_.find( component.controlConstructors, function( Constructor, idBase ) { |
|
if ( control instanceof Constructor ) { |
|
control.id_base = idBase; |
|
return true; |
|
} |
|
return false; |
|
}); |
|
if ( ! control.id_base ) { |
|
throw new Error( 'Missing id_base.' ); |
|
} |
|
} |
|
|
|
|
|
control.previewTemplateProps = new Backbone.Model( control.mapModelToPreviewTemplateProps() ); |
|
|
|
|
|
control.selectedAttachment = new wp.media.model.Attachment(); |
|
control.renderPreview = _.debounce( control.renderPreview ); |
|
control.listenTo( control.previewTemplateProps, 'change', control.renderPreview ); |
|
|
|
|
|
control.model.on( 'change:attachment_id', control.updateSelectedAttachment ); |
|
control.model.on( 'change:url', control.updateSelectedAttachment ); |
|
control.updateSelectedAttachment(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
control.listenTo( control.model, 'change', control.syncModelToInputs ); |
|
control.listenTo( control.model, 'change', control.syncModelToPreviewProps ); |
|
control.listenTo( control.model, 'change', control.render ); |
|
|
|
|
|
control.$el.on( 'input change', '.title', function updateTitle() { |
|
control.model.set({ |
|
title: $( this ).val().trim() |
|
}); |
|
}); |
|
|
|
|
|
control.$el.on( 'input change', '.link', function updateLinkUrl() { |
|
var linkUrl = $( this ).val().trim(), linkType = 'custom'; |
|
if ( control.selectedAttachment.get( 'linkUrl' ) === linkUrl || control.selectedAttachment.get( 'link' ) === linkUrl ) { |
|
linkType = 'post'; |
|
} else if ( control.selectedAttachment.get( 'url' ) === linkUrl ) { |
|
linkType = 'file'; |
|
} |
|
control.model.set( { |
|
link_url: linkUrl, |
|
link_type: linkType |
|
}); |
|
|
|
|
|
control.displaySettings.set( { |
|
link: linkType, |
|
linkUrl: linkUrl |
|
}); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
control.displaySettings = new Backbone.Model( _.pick( |
|
control.mapModelToMediaFrameProps( |
|
_.extend( control.model.defaults(), control.model.toJSON() ) |
|
), |
|
_.keys( wp.media.view.settings.defaultProps ) |
|
) ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
updateSelectedAttachment: function updateSelectedAttachment() { |
|
var control = this, attachment; |
|
|
|
if ( 0 === control.model.get( 'attachment_id' ) ) { |
|
control.selectedAttachment.clear(); |
|
control.model.set( 'error', false ); |
|
} else if ( control.model.get( 'attachment_id' ) !== control.selectedAttachment.get( 'id' ) ) { |
|
attachment = new wp.media.model.Attachment({ |
|
id: control.model.get( 'attachment_id' ) |
|
}); |
|
attachment.fetch() |
|
.done( function done() { |
|
control.model.set( 'error', false ); |
|
control.selectedAttachment.set( attachment.toJSON() ); |
|
}) |
|
.fail( function fail() { |
|
control.model.set( 'error', 'missing_attachment' ); |
|
}); |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
syncModelToPreviewProps: function syncModelToPreviewProps() { |
|
var control = this; |
|
control.previewTemplateProps.set( control.mapModelToPreviewTemplateProps() ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
syncModelToInputs: function syncModelToInputs() { |
|
var control = this; |
|
control.syncContainer.find( '.media-widget-instance-property' ).each( function() { |
|
var input = $( this ), value, propertyName; |
|
propertyName = input.data( 'property' ); |
|
value = control.model.get( propertyName ); |
|
if ( _.isUndefined( value ) ) { |
|
return; |
|
} |
|
|
|
if ( 'array' === control.model.schema[ propertyName ].type && _.isArray( value ) ) { |
|
value = value.join( ',' ); |
|
} else if ( 'boolean' === control.model.schema[ propertyName ].type ) { |
|
value = value ? '1' : ''; |
|
} else { |
|
value = String( value ); |
|
} |
|
|
|
if ( input.val() !== value ) { |
|
input.val( value ); |
|
input.trigger( 'change' ); |
|
} |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
template: function template() { |
|
var control = this; |
|
if ( ! $( '#tmpl-widget-media-' + control.id_base + '-control' ).length ) { |
|
throw new Error( 'Missing widget control template for ' + control.id_base ); |
|
} |
|
return wp.template( 'widget-media-' + control.id_base + '-control' ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
render: function render() { |
|
var control = this, titleInput; |
|
|
|
if ( ! control.templateRendered ) { |
|
control.$el.html( control.template()( control.model.toJSON() ) ); |
|
control.renderPreview(); |
|
control.templateRendered = true; |
|
} |
|
|
|
titleInput = control.$el.find( '.title' ); |
|
if ( ! titleInput.is( document.activeElement ) ) { |
|
titleInput.val( control.model.get( 'title' ) ); |
|
} |
|
|
|
control.$el.toggleClass( 'selected', control.isSelected() ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderPreview: function renderPreview() { |
|
throw new Error( 'renderPreview must be implemented' ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
isSelected: function isSelected() { |
|
var control = this; |
|
|
|
if ( control.model.get( 'error' ) ) { |
|
return false; |
|
} |
|
|
|
return Boolean( control.model.get( 'attachment_id' ) || control.model.get( 'url' ) ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleMediaLibraryLinkClick: function handleMediaLibraryLinkClick( event ) { |
|
var control = this; |
|
event.preventDefault(); |
|
control.selectMedia(); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
selectMedia: function selectMedia() { |
|
var control = this, selection, mediaFrame, defaultSync, mediaFrameProps, selectionModels = []; |
|
|
|
if ( control.isSelected() && 0 !== control.model.get( 'attachment_id' ) ) { |
|
selectionModels.push( control.selectedAttachment ); |
|
} |
|
|
|
selection = new wp.media.model.Selection( selectionModels, { multiple: false } ); |
|
|
|
mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() ); |
|
if ( mediaFrameProps.size ) { |
|
control.displaySettings.set( 'size', mediaFrameProps.size ); |
|
} |
|
|
|
mediaFrame = new component.MediaFrameSelect({ |
|
title: control.l10n.add_media, |
|
frame: 'post', |
|
text: control.l10n.add_to_widget, |
|
selection: selection, |
|
mimeType: control.mime_type, |
|
selectedDisplaySettings: control.displaySettings, |
|
showDisplaySettings: control.showDisplaySettings, |
|
metadata: mediaFrameProps, |
|
state: control.isSelected() && 0 === control.model.get( 'attachment_id' ) ? 'embed' : 'insert', |
|
invalidEmbedTypeError: control.l10n.unsupported_file_type |
|
}); |
|
wp.media.frame = mediaFrame; |
|
|
|
|
|
mediaFrame.on( 'insert', function onInsert() { |
|
var attachment = {}, state = mediaFrame.state(); |
|
|
|
|
|
if ( 'embed' === state.get( 'id' ) ) { |
|
_.extend( attachment, { id: 0 }, state.props.toJSON() ); |
|
} else { |
|
_.extend( attachment, state.get( 'selection' ).first().toJSON() ); |
|
} |
|
|
|
control.selectedAttachment.set( attachment ); |
|
control.model.set( 'error', false ); |
|
|
|
|
|
control.model.set( control.getModelPropsFromMediaFrame( mediaFrame ) ); |
|
}); |
|
|
|
|
|
defaultSync = wp.media.model.Attachment.prototype.sync; |
|
wp.media.model.Attachment.prototype.sync = function( method ) { |
|
if ( 'delete' === method ) { |
|
return defaultSync.apply( this, arguments ); |
|
} else { |
|
return $.Deferred().rejectWith( this ).promise(); |
|
} |
|
}; |
|
mediaFrame.on( 'close', function onClose() { |
|
wp.media.model.Attachment.prototype.sync = defaultSync; |
|
}); |
|
|
|
mediaFrame.$el.addClass( 'media-widget' ); |
|
mediaFrame.open(); |
|
|
|
|
|
if ( selection ) { |
|
selection.on( 'destroy', function onDestroy( attachment ) { |
|
if ( control.model.get( 'attachment_id' ) === attachment.get( 'id' ) ) { |
|
control.model.set({ |
|
attachment_id: 0, |
|
url: '' |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
mediaFrame.$el.find( '.media-frame-menu .media-menu-item.active' ).focus(); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getModelPropsFromMediaFrame: function getModelPropsFromMediaFrame( mediaFrame ) { |
|
var control = this, state, mediaFrameProps, modelProps; |
|
|
|
state = mediaFrame.state(); |
|
if ( 'insert' === state.get( 'id' ) ) { |
|
mediaFrameProps = state.get( 'selection' ).first().toJSON(); |
|
mediaFrameProps.postUrl = mediaFrameProps.link; |
|
|
|
if ( control.showDisplaySettings ) { |
|
_.extend( |
|
mediaFrameProps, |
|
mediaFrame.content.get( '.attachments-browser' ).sidebar.get( 'display' ).model.toJSON() |
|
); |
|
} |
|
if ( mediaFrameProps.sizes && mediaFrameProps.size && mediaFrameProps.sizes[ mediaFrameProps.size ] ) { |
|
mediaFrameProps.url = mediaFrameProps.sizes[ mediaFrameProps.size ].url; |
|
} |
|
} else if ( 'embed' === state.get( 'id' ) ) { |
|
mediaFrameProps = _.extend( |
|
state.props.toJSON(), |
|
{ attachment_id: 0 }, |
|
control.model.getEmbedResetProps() |
|
); |
|
} else { |
|
throw new Error( 'Unexpected state: ' + state.get( 'id' ) ); |
|
} |
|
|
|
if ( mediaFrameProps.id ) { |
|
mediaFrameProps.attachment_id = mediaFrameProps.id; |
|
} |
|
|
|
modelProps = control.mapMediaToModelProps( mediaFrameProps ); |
|
|
|
|
|
_.each( wp.media.view.settings.embedExts, function( ext ) { |
|
if ( ext in control.model.schema && modelProps.url !== modelProps[ ext ] ) { |
|
modelProps[ ext ] = ''; |
|
} |
|
}); |
|
|
|
return modelProps; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapMediaToModelProps: function mapMediaToModelProps( mediaFrameProps ) { |
|
var control = this, mediaFramePropToModelPropMap = {}, modelProps = {}, extension; |
|
_.each( control.model.schema, function( fieldSchema, modelProp ) { |
|
|
|
|
|
if ( 'title' === modelProp ) { |
|
return; |
|
} |
|
mediaFramePropToModelPropMap[ fieldSchema.media_prop || modelProp ] = modelProp; |
|
}); |
|
|
|
_.each( mediaFrameProps, function( value, mediaProp ) { |
|
var propName = mediaFramePropToModelPropMap[ mediaProp ] || mediaProp; |
|
if ( control.model.schema[ propName ] ) { |
|
modelProps[ propName ] = value; |
|
} |
|
}); |
|
|
|
if ( 'custom' === mediaFrameProps.size ) { |
|
modelProps.width = mediaFrameProps.customWidth; |
|
modelProps.height = mediaFrameProps.customHeight; |
|
} |
|
|
|
if ( 'post' === mediaFrameProps.link ) { |
|
modelProps.link_url = mediaFrameProps.postUrl || mediaFrameProps.linkUrl; |
|
} else if ( 'file' === mediaFrameProps.link ) { |
|
modelProps.link_url = mediaFrameProps.url; |
|
} |
|
|
|
|
|
if ( ! mediaFrameProps.attachment_id && mediaFrameProps.id ) { |
|
modelProps.attachment_id = mediaFrameProps.id; |
|
} |
|
|
|
if ( mediaFrameProps.url ) { |
|
extension = mediaFrameProps.url.replace( /#.*$/, '' ).replace( /\?.*$/, '' ).split( '.' ).pop().toLowerCase(); |
|
if ( extension in control.model.schema ) { |
|
modelProps[ extension ] = mediaFrameProps.url; |
|
} |
|
} |
|
|
|
|
|
return _.omit( modelProps, 'title' ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapModelToMediaFrameProps: function mapModelToMediaFrameProps( modelProps ) { |
|
var control = this, mediaFrameProps = {}; |
|
|
|
_.each( modelProps, function( value, modelProp ) { |
|
var fieldSchema = control.model.schema[ modelProp ] || {}; |
|
mediaFrameProps[ fieldSchema.media_prop || modelProp ] = value; |
|
}); |
|
|
|
|
|
mediaFrameProps.attachment_id = mediaFrameProps.id; |
|
|
|
if ( 'custom' === mediaFrameProps.size ) { |
|
mediaFrameProps.customWidth = control.model.get( 'width' ); |
|
mediaFrameProps.customHeight = control.model.get( 'height' ); |
|
} |
|
|
|
return mediaFrameProps; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
mapModelToPreviewTemplateProps: function mapModelToPreviewTemplateProps() { |
|
var control = this, previewTemplateProps = {}; |
|
_.each( control.model.schema, function( value, prop ) { |
|
if ( ! value.hasOwnProperty( 'should_preview_update' ) || value.should_preview_update ) { |
|
previewTemplateProps[ prop ] = control.model.get( prop ); |
|
} |
|
}); |
|
|
|
|
|
previewTemplateProps.error = control.model.get( 'error' ); |
|
return previewTemplateProps; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
editMedia: function editMedia() { |
|
throw new Error( 'editMedia not implemented' ); |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.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 defaults = {}; |
|
_.each( this.schema, function( fieldSchema, field ) { |
|
defaults[ field ] = fieldSchema['default']; |
|
}); |
|
return defaults; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set: function set( key, val, options ) { |
|
var model = this, attrs, opts, castedAttrs; |
|
if ( null === key ) { |
|
return model; |
|
} |
|
if ( 'object' === typeof key ) { |
|
attrs = key; |
|
opts = val; |
|
} else { |
|
attrs = {}; |
|
attrs[ key ] = val; |
|
opts = options; |
|
} |
|
|
|
castedAttrs = {}; |
|
_.each( attrs, function( value, name ) { |
|
var type; |
|
if ( ! model.schema[ name ] ) { |
|
castedAttrs[ name ] = value; |
|
return; |
|
} |
|
type = model.schema[ name ].type; |
|
if ( 'array' === type ) { |
|
castedAttrs[ name ] = value; |
|
if ( ! _.isArray( castedAttrs[ name ] ) ) { |
|
castedAttrs[ name ] = castedAttrs[ name ].split( /,/ ); |
|
} |
|
if ( model.schema[ name ].items && 'integer' === model.schema[ name ].items.type ) { |
|
castedAttrs[ name ] = _.filter( |
|
_.map( castedAttrs[ name ], function( id ) { |
|
return parseInt( id, 10 ); |
|
}, |
|
function( id ) { |
|
return 'number' === typeof id; |
|
} |
|
) ); |
|
} |
|
} else if ( 'integer' === type ) { |
|
castedAttrs[ name ] = parseInt( value, 10 ); |
|
} else if ( 'boolean' === type ) { |
|
castedAttrs[ name ] = ! ( ! value || '0' === value || 'false' === value ); |
|
} else { |
|
castedAttrs[ name ] = value; |
|
} |
|
}); |
|
|
|
return Backbone.Model.prototype.set.call( this, castedAttrs, opts ); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
getEmbedResetProps: function getEmbedResetProps() { |
|
return { |
|
id: 0 |
|
}; |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.modelCollection = new ( Backbone.Collection.extend( { |
|
model: component.MediaWidgetModel |
|
}) )(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.widgetControls = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.handleWidgetAdded = function handleWidgetAdded( event, widgetContainer ) { |
|
var fieldContainer, syncContainer, widgetForm, idBase, ControlConstructor, ModelConstructor, modelAttributes, widgetControl, widgetModel, widgetId, animatedCheckDelay = 50, renderWhenAnimationDone; |
|
widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' ); |
|
idBase = widgetForm.find( '> .id_base' ).val(); |
|
widgetId = widgetForm.find( '> .widget-id' ).val(); |
|
|
|
|
|
if ( component.widgetControls[ widgetId ] ) { |
|
return; |
|
} |
|
|
|
ControlConstructor = component.controlConstructors[ idBase ]; |
|
if ( ! ControlConstructor ) { |
|
return; |
|
} |
|
|
|
ModelConstructor = component.modelConstructors[ idBase ] || component.MediaWidgetModel; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fieldContainer = $( '<div></div>' ); |
|
syncContainer = widgetContainer.find( '.widget-content:first' ); |
|
syncContainer.before( fieldContainer ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
modelAttributes = {}; |
|
syncContainer.find( '.media-widget-instance-property' ).each( function() { |
|
var input = $( this ); |
|
modelAttributes[ input.data( 'property' ) ] = input.val(); |
|
}); |
|
modelAttributes.widget_id = widgetId; |
|
|
|
widgetModel = new ModelConstructor( modelAttributes ); |
|
|
|
widgetControl = new ControlConstructor({ |
|
el: fieldContainer, |
|
syncContainer: syncContainer, |
|
model: widgetModel |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderWhenAnimationDone = function() { |
|
if ( ! widgetContainer.hasClass( 'open' ) ) { |
|
setTimeout( renderWhenAnimationDone, animatedCheckDelay ); |
|
} else { |
|
widgetControl.render(); |
|
} |
|
}; |
|
renderWhenAnimationDone(); |
|
|
|
|
|
|
|
|
|
|
|
component.modelCollection.add( [ widgetModel ] ); |
|
component.widgetControls[ widgetModel.get( 'widget_id' ) ] = widgetControl; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.setupAccessibleMode = function setupAccessibleMode() { |
|
var widgetForm, widgetId, idBase, widgetControl, ControlConstructor, ModelConstructor, modelAttributes, fieldContainer, syncContainer; |
|
widgetForm = $( '.editwidget > form' ); |
|
if ( 0 === widgetForm.length ) { |
|
return; |
|
} |
|
|
|
idBase = widgetForm.find( '.id_base' ).val(); |
|
|
|
ControlConstructor = component.controlConstructors[ idBase ]; |
|
if ( ! ControlConstructor ) { |
|
return; |
|
} |
|
|
|
widgetId = widgetForm.find( '> .widget-control-actions > .widget-id' ).val(); |
|
|
|
ModelConstructor = component.modelConstructors[ idBase ] || component.MediaWidgetModel; |
|
fieldContainer = $( '<div></div>' ); |
|
syncContainer = widgetForm.find( '> .widget-inside' ); |
|
syncContainer.before( fieldContainer ); |
|
|
|
modelAttributes = {}; |
|
syncContainer.find( '.media-widget-instance-property' ).each( function() { |
|
var input = $( this ); |
|
modelAttributes[ input.data( 'property' ) ] = input.val(); |
|
}); |
|
modelAttributes.widget_id = widgetId; |
|
|
|
widgetControl = new ControlConstructor({ |
|
el: fieldContainer, |
|
syncContainer: syncContainer, |
|
model: new ModelConstructor( modelAttributes ) |
|
}); |
|
|
|
component.modelCollection.add( [ widgetControl.model ] ); |
|
component.widgetControls[ widgetControl.model.get( 'widget_id' ) ] = widgetControl; |
|
|
|
widgetControl.render(); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.handleWidgetUpdated = function handleWidgetUpdated( event, widgetContainer ) { |
|
var widgetForm, widgetContent, widgetId, widgetControl, attributes = {}; |
|
widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' ); |
|
widgetId = widgetForm.find( '> .widget-id' ).val(); |
|
|
|
widgetControl = component.widgetControls[ widgetId ]; |
|
if ( ! widgetControl ) { |
|
return; |
|
} |
|
|
|
|
|
widgetContent = widgetForm.find( '> .widget-content' ); |
|
widgetContent.find( '.media-widget-instance-property' ).each( function() { |
|
var property = $( this ).data( 'property' ); |
|
attributes[ property ] = $( this ).val(); |
|
}); |
|
|
|
|
|
widgetControl.stopListening( widgetControl.model, 'change', widgetControl.syncModelToInputs ); |
|
widgetControl.model.set( attributes ); |
|
widgetControl.listenTo( widgetControl.model, 'change', widgetControl.syncModelToInputs ); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.init = function init() { |
|
var $document = $( document ); |
|
$document.on( 'widget-added', component.handleWidgetAdded ); |
|
$document.on( 'widget-synced widget-updated', component.handleWidgetUpdated ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$( function initializeExistingWidgetContainers() { |
|
var widgetContainers; |
|
if ( 'widgets' !== window.pagenow ) { |
|
return; |
|
} |
|
widgetContainers = $( '.widgets-holder-wrap:not(#available-widgets)' ).find( 'div.widget' ); |
|
widgetContainers.one( 'click.toggle-widget-expanded', function toggleWidgetExpanded() { |
|
var widgetContainer = $( this ); |
|
component.handleWidgetAdded( new jQuery.Event( 'widget-added' ), widgetContainer ); |
|
}); |
|
|
|
|
|
if ( document.readyState === 'complete' ) { |
|
|
|
component.setupAccessibleMode(); |
|
} else { |
|
|
|
$( window ).on( 'load', function() { |
|
component.setupAccessibleMode(); |
|
}); |
|
} |
|
}); |
|
}; |
|
|
|
return component; |
|
})( jQuery ); |
|
|