2014-03-26 17:41:15 -04:00
|
|
|
/* global tinymce, MediaElementPlayer, WPPlaylistView */
|
2014-04-07 02:59:16 -04:00
|
|
|
/**
|
|
|
|
* Note: this API is "experimental" meaning that it will probably change
|
|
|
|
* in the next few releases based on feedback from 3.9.0.
|
|
|
|
* If you decide to use it, please follow the development closely.
|
2014-04-07 05:03:14 -04:00
|
|
|
*/
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2012-09-25 21:00:08 -04:00
|
|
|
// Ensure the global `wp` object exists.
|
2012-10-05 20:43:36 -04:00
|
|
|
window.wp = window.wp || {};
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-06-11 22:49:16 -04:00
|
|
|
( function( $ ) {
|
|
|
|
'use strict';
|
|
|
|
|
2012-09-23 20:13:18 -04:00
|
|
|
var views = {},
|
2014-03-05 02:01:14 -05:00
|
|
|
instances = {},
|
|
|
|
media = wp.media,
|
|
|
|
viewOptions = ['encodedText'];
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2012-09-25 21:00:08 -04:00
|
|
|
// Create the `wp.mce` object if necessary.
|
|
|
|
wp.mce = wp.mce || {};
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* wp.mce.View
|
|
|
|
*
|
|
|
|
* A Backbone-like View constructor intended for use when rendering a TinyMCE View. The main difference is
|
|
|
|
* that the TinyMCE View is not tied to a particular DOM node.
|
2014-06-05 13:27:13 -04:00
|
|
|
*
|
|
|
|
* @param {Object} [options={}]
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
|
|
|
wp.mce.View = function( options ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
options = options || {};
|
2014-06-11 22:49:16 -04:00
|
|
|
this.type = options.type;
|
2014-06-05 13:27:13 -04:00
|
|
|
_.extend( this, _.pick( options, viewOptions ) );
|
|
|
|
this.initialize.apply( this, arguments );
|
2014-03-05 02:01:14 -05:00
|
|
|
};
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
_.extend( wp.mce.View.prototype, {
|
|
|
|
initialize: function() {},
|
2014-04-01 22:18:15 -04:00
|
|
|
getHtml: function() {},
|
2014-03-05 02:01:14 -05:00
|
|
|
render: function() {
|
2014-06-11 22:49:16 -04:00
|
|
|
this.setContent(
|
|
|
|
'<div class="toolbar">' +
|
|
|
|
( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +
|
|
|
|
'<div class="dashicons dashicons-no-alt remove"></div>' +
|
|
|
|
'</div>' +
|
|
|
|
'<div class="wpview-content">' +
|
|
|
|
this.getHtml() +
|
|
|
|
'</div>' +
|
|
|
|
( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
|
|
|
|
// The <ins> is used to mark the end of the wrapper div (has to be the last child node).
|
|
|
|
// Needed when comparing the content as string for preventing extra undo levels.
|
|
|
|
'<ins data-wpview-end="1"></ins>',
|
|
|
|
function( self, editor, node ) {
|
|
|
|
$( self ).trigger( 'ready', [ editor, node ] );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
unbind: function() {},
|
|
|
|
setContent: function( html, callback, replace ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
_.each( tinymce.editors, function( editor ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
var self = this;
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( editor.plugins.wpview ) {
|
2014-06-11 22:49:16 -04:00
|
|
|
$( editor.getBody() )
|
|
|
|
.find( '[data-wpview-text="' + this.encodedText + '"]' )
|
|
|
|
.each( function ( i, element ) {
|
|
|
|
var contentWrap = $( element ).children( '.wpview-content' ),
|
|
|
|
wrap = element;
|
|
|
|
|
|
|
|
if ( contentWrap.length ) {
|
|
|
|
element = contentWrap = contentWrap[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _.isString( html ) ) {
|
|
|
|
if ( replace ) {
|
|
|
|
element = editor.dom.replace( editor.dom.createFragment( html ), wrap );
|
|
|
|
} else {
|
|
|
|
editor.dom.setHTML( element, html );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( replace ) {
|
|
|
|
element = editor.dom.replace( html, wrap );
|
|
|
|
} else {
|
|
|
|
element.appendChild( html );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _.isFunction( callback ) ) {
|
|
|
|
callback( self, editor, $( element ).children( '.wpview-content' )[0] );
|
|
|
|
}
|
|
|
|
} );
|
2012-09-26 10:12:54 -04:00
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}, this );
|
2014-04-11 20:46:14 -04:00
|
|
|
},
|
2014-06-11 22:49:16 -04:00
|
|
|
setError: function( message, dashicon ) {
|
|
|
|
this.setContent(
|
|
|
|
'<div class="wpview-error">' +
|
|
|
|
'<div class="dashicons dashicons-' + ( dashicon ? dashicon : 'no' ) + '"></div>' +
|
|
|
|
'<p>' + message + '</p>' +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
// take advantage of the Backbone extend method
|
|
|
|
wp.mce.View.extend = Backbone.View.extend;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wp.mce.views
|
|
|
|
*
|
|
|
|
* A set of utilities that simplifies adding custom UI within a TinyMCE editor.
|
|
|
|
* At its core, it serves as a series of converters, transforming text to a
|
|
|
|
* custom UI, and back again.
|
|
|
|
*/
|
|
|
|
wp.mce.views = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wp.mce.views.register( type, view )
|
|
|
|
*
|
|
|
|
* Registers a new TinyMCE view.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
register: function( type, constructor ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
var defaultConstructor = {
|
2014-06-18 19:03:15 -04:00
|
|
|
type: type,
|
2014-06-05 13:27:13 -04:00
|
|
|
View: {},
|
|
|
|
toView: function( content ) {
|
2014-06-18 19:03:15 -04:00
|
|
|
var match = wp.shortcode.next( this.type, content );
|
2014-06-05 13:27:13 -04:00
|
|
|
|
|
|
|
if ( ! match ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
index: match.index,
|
|
|
|
content: match.content,
|
|
|
|
options: {
|
|
|
|
shortcode: match.shortcode
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor = _.defaults( constructor, defaultConstructor );
|
|
|
|
constructor.View = wp.mce.View.extend( constructor.View );
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
views[ type ] = constructor;
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* wp.mce.views.get( id )
|
|
|
|
*
|
|
|
|
* Returns a TinyMCE view constructor.
|
2014-06-05 13:27:13 -04:00
|
|
|
*
|
|
|
|
* @param type
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
|
|
|
get: function( type ) {
|
|
|
|
return views[ type ];
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* wp.mce.views.unregister( type )
|
|
|
|
*
|
|
|
|
* Unregisters a TinyMCE view.
|
2014-06-05 13:27:13 -04:00
|
|
|
*
|
|
|
|
* @param type
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
|
|
|
unregister: function( type ) {
|
|
|
|
delete views[ type ];
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
/**
|
|
|
|
* wp.mce.views.unbind( editor )
|
|
|
|
*
|
|
|
|
* The editor DOM is being rebuilt, run cleanup.
|
|
|
|
*/
|
|
|
|
unbind: function() {
|
|
|
|
_.each( instances, function( instance ) {
|
|
|
|
instance.unbind();
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* toViews( content )
|
|
|
|
* Scans a `content` string for each view's pattern, replacing any
|
|
|
|
* matches with wrapper elements, and creates a new instance for
|
|
|
|
* every match, which triggers the related data to be fetched.
|
|
|
|
*
|
2014-06-05 13:27:13 -04:00
|
|
|
* @param content
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2012-09-23 20:13:18 -04:00
|
|
|
toViews: function( content ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
var pieces = [ { content: content } ],
|
|
|
|
current;
|
|
|
|
|
2012-09-23 20:13:18 -04:00
|
|
|
_.each( views, function( view, viewType ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
current = pieces.slice();
|
|
|
|
pieces = [];
|
|
|
|
|
|
|
|
_.each( current, function( piece ) {
|
|
|
|
var remaining = piece.content,
|
|
|
|
result;
|
|
|
|
|
|
|
|
// Ignore processed pieces, but retain their location.
|
|
|
|
if ( piece.processed ) {
|
|
|
|
pieces.push( piece );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the string progressively matching views
|
|
|
|
// and slicing the string as we go.
|
|
|
|
while ( remaining && (result = view.toView( remaining )) ) {
|
|
|
|
// Any text before the match becomes an unprocessed piece.
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( result.index ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
pieces.push({ content: remaining.substring( 0, result.index ) });
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-09-25 21:00:08 -04:00
|
|
|
|
|
|
|
// Add the processed piece for the match.
|
|
|
|
pieces.push({
|
2014-03-05 02:01:14 -05:00
|
|
|
content: wp.mce.views.toView( viewType, result.content, result.options ),
|
2012-09-25 21:00:08 -04:00
|
|
|
processed: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update the remaining content.
|
|
|
|
remaining = remaining.slice( result.index + result.content.length );
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no additional matches. If any content remains,
|
|
|
|
// add it as an unprocessed piece.
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( remaining ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
pieces.push({ content: remaining });
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-09-25 21:00:08 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.pluck( pieces, 'content' ).join('');
|
|
|
|
},
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* Create a placeholder for a particular view type
|
|
|
|
*
|
|
|
|
* @param viewType
|
|
|
|
* @param text
|
|
|
|
* @param options
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
toView: function( viewType, text, options ) {
|
|
|
|
var view = wp.mce.views.get( viewType ),
|
|
|
|
encodedText = window.encodeURIComponent( text ),
|
|
|
|
instance, viewOptions;
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! view ) {
|
|
|
|
return text;
|
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( ! wp.mce.views.getInstance( encodedText ) ) {
|
|
|
|
viewOptions = options;
|
2014-06-11 22:49:16 -04:00
|
|
|
viewOptions.type = viewType;
|
2014-03-05 02:01:14 -05:00
|
|
|
viewOptions.encodedText = encodedText;
|
|
|
|
instance = new view.View( viewOptions );
|
|
|
|
instances[ encodedText ] = instance;
|
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2012-10-12 14:36:21 -04:00
|
|
|
return wp.html.string({
|
2014-03-05 02:01:14 -05:00
|
|
|
tag: 'div',
|
2012-10-12 14:36:21 -04:00
|
|
|
|
|
|
|
attrs: {
|
2014-03-05 02:01:14 -05:00
|
|
|
'class': 'wpview-wrap wpview-type-' + viewType,
|
|
|
|
'data-wpview-text': encodedText,
|
|
|
|
'data-wpview-type': viewType,
|
|
|
|
'contenteditable': 'false'
|
|
|
|
},
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
content: '\u00a0'
|
2012-09-23 20:13:18 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
|
|
|
* Refresh views after an update is made
|
2014-03-19 03:02:15 -04:00
|
|
|
*
|
2014-03-05 02:01:14 -05:00
|
|
|
* @param view {object} being refreshed
|
|
|
|
* @param text {string} textual representation of the view
|
|
|
|
*/
|
|
|
|
refreshView: function( view, text ) {
|
|
|
|
var encodedText = window.encodeURIComponent( text ),
|
|
|
|
viewOptions,
|
|
|
|
result, instance;
|
|
|
|
|
|
|
|
instance = wp.mce.views.getInstance( encodedText );
|
|
|
|
|
|
|
|
if ( ! instance ) {
|
|
|
|
result = view.toView( text );
|
|
|
|
viewOptions = result.options;
|
2014-06-11 22:49:16 -04:00
|
|
|
viewOptions.type = view.type;
|
2014-03-05 02:01:14 -05:00
|
|
|
viewOptions.encodedText = encodedText;
|
|
|
|
instance = new view.View( viewOptions );
|
|
|
|
instances[ encodedText ] = instance;
|
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
wp.mce.views.render();
|
2012-09-26 10:12:54 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
getInstance: function( encodedText ) {
|
|
|
|
return instances[ encodedText ];
|
2012-09-26 18:20:15 -04:00
|
|
|
},
|
|
|
|
|
2014-03-19 03:02:15 -04:00
|
|
|
/**
|
2014-03-05 02:01:14 -05:00
|
|
|
* render( scope )
|
2014-03-19 03:02:15 -04:00
|
|
|
*
|
2014-03-05 02:01:14 -05:00
|
|
|
* Renders any view instances inside a DOM node `scope`.
|
|
|
|
*
|
|
|
|
* View instances are detected by the presence of wrapper elements.
|
|
|
|
* To generate wrapper elements, pass your content through
|
|
|
|
* `wp.mce.view.toViews( content )`.
|
|
|
|
*/
|
|
|
|
render: function() {
|
|
|
|
_.each( instances, function( instance ) {
|
|
|
|
instance.render();
|
|
|
|
} );
|
2012-09-26 18:20:15 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
edit: function( node ) {
|
|
|
|
var viewType = $( node ).data('wpview-type'),
|
|
|
|
view = wp.mce.views.get( viewType );
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( view ) {
|
|
|
|
view.edit( node );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
wp.mce.views.register( 'gallery', {
|
|
|
|
View: {
|
|
|
|
template: media.template( 'editor-gallery' ),
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
// The fallback post ID to use as a parent for galleries that don't
|
|
|
|
// specify the `ids` or `include` parameters.
|
|
|
|
//
|
|
|
|
// Uses the hidden input on the edit posts page by default.
|
|
|
|
postID: $('#post_ID').val(),
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
this.shortcode = options.shortcode;
|
|
|
|
this.fetch();
|
|
|
|
},
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
fetch: function() {
|
|
|
|
this.attachments = wp.media.gallery.attachments( this.shortcode, this.postID );
|
2014-04-01 22:18:15 -04:00
|
|
|
this.dfd = this.attachments.more().done( _.bind( this.render, this ) );
|
2014-03-05 02:01:14 -05:00
|
|
|
},
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
getHtml: function() {
|
|
|
|
var attrs = this.shortcode.attrs.named,
|
2014-04-01 22:18:15 -04:00
|
|
|
attachments = false,
|
|
|
|
options;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2014-04-01 22:18:15 -04:00
|
|
|
// Don't render errors while still fetching attachments
|
|
|
|
if ( this.dfd && 'pending' === this.dfd.state() && ! this.attachments.length ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-01 22:18:15 -04:00
|
|
|
if ( this.attachments.length ) {
|
|
|
|
attachments = this.attachments.toJSON();
|
2014-03-17 23:25:14 -04:00
|
|
|
|
2014-04-01 22:18:15 -04:00
|
|
|
_.each( attachments, function( attachment ) {
|
2014-04-07 17:25:16 -04:00
|
|
|
if ( attachment.sizes ) {
|
|
|
|
if ( attachment.sizes.thumbnail ) {
|
|
|
|
attachment.thumbnail = attachment.sizes.thumbnail;
|
|
|
|
} else if ( attachment.sizes.full ) {
|
|
|
|
attachment.thumbnail = attachment.sizes.full;
|
|
|
|
}
|
2014-04-01 22:18:15 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2014-03-17 23:25:14 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
options = {
|
2014-03-17 23:25:14 -04:00
|
|
|
attachments: attachments,
|
2014-03-05 02:01:14 -05:00
|
|
|
columns: attrs.columns ? parseInt( attrs.columns, 10 ) : 3
|
|
|
|
};
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
return this.template( options );
|
|
|
|
}
|
2014-06-05 13:27:13 -04:00
|
|
|
},
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
edit: function( node ) {
|
|
|
|
var gallery = wp.media.gallery,
|
|
|
|
self = this,
|
|
|
|
frame, data;
|
|
|
|
|
2014-03-17 23:48:15 -04:00
|
|
|
data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );
|
2014-03-05 02:01:14 -05:00
|
|
|
frame = gallery.edit( data );
|
|
|
|
|
|
|
|
frame.state('gallery-edit').on( 'update', function( selection ) {
|
|
|
|
var shortcode = gallery.shortcode( selection ).string();
|
|
|
|
$( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );
|
|
|
|
wp.mce.views.refreshView( self, shortcode );
|
|
|
|
frame.detach();
|
|
|
|
});
|
2012-10-08 20:55:44 -04:00
|
|
|
}
|
2014-06-05 13:27:13 -04:00
|
|
|
} );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
/**
|
2014-06-05 13:27:13 -04:00
|
|
|
* These are base methods that are shared by the audio and video shortcode's MCE controller.
|
2014-03-26 08:11:14 -04:00
|
|
|
*
|
|
|
|
* @mixin
|
|
|
|
*/
|
2014-06-05 13:27:13 -04:00
|
|
|
wp.mce.av = {
|
2014-04-11 20:46:14 -04:00
|
|
|
loaded: false,
|
2014-06-05 13:27:13 -04:00
|
|
|
|
|
|
|
View: _.extend( {}, wp.media.mixin, {
|
2014-06-11 22:49:16 -04:00
|
|
|
overlay: true,
|
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
initialize: function( options ) {
|
|
|
|
this.players = [];
|
|
|
|
this.shortcode = options.shortcode;
|
|
|
|
_.bindAll( this, 'setPlayer', 'pausePlayers' );
|
|
|
|
$( this ).on( 'ready', this.setPlayer );
|
2014-06-20 12:30:16 -04:00
|
|
|
$( this ).on( 'ready', function( event, editor ) {
|
|
|
|
editor.on( 'hide', this.pausePlayers );
|
|
|
|
} );
|
2014-06-05 13:27:13 -04:00
|
|
|
$( document ).on( 'media:edit', this.pausePlayers );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the player instance for the current node
|
|
|
|
*
|
|
|
|
* @global MediaElementPlayer
|
|
|
|
*
|
2014-06-11 22:49:16 -04:00
|
|
|
* @param {Event} event
|
|
|
|
* @param {Object} editor
|
2014-06-05 13:27:13 -04:00
|
|
|
* @param {HTMLElement} node
|
|
|
|
*/
|
2014-06-11 22:49:16 -04:00
|
|
|
setPlayer: function( event, editor, node ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
var self = this,
|
2014-06-11 22:49:16 -04:00
|
|
|
media;
|
2014-06-05 13:27:13 -04:00
|
|
|
|
2014-06-11 22:49:16 -04:00
|
|
|
media = $( node ).find( '.wp-' + this.shortcode.tag + '-shortcode' );
|
2014-06-05 13:27:13 -04:00
|
|
|
|
|
|
|
if ( ! this.isCompatible( media ) ) {
|
|
|
|
media.closest( '.wpview-wrap' ).addClass( 'wont-play' );
|
|
|
|
media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' );
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
media.closest( '.wpview-wrap' ).removeClass( 'wont-play' );
|
2014-06-11 22:49:16 -04:00
|
|
|
if ( this.ua.is( 'ff' ) ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
media.prop( 'preload', 'metadata' );
|
|
|
|
} else {
|
|
|
|
media.prop( 'preload', 'none' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
media = wp.media.view.MediaDetails.prepareSrc( media.get(0) );
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
wp.mce.av.loaded = true;
|
|
|
|
self.players.push( new MediaElementPlayer( media, self.mejsSettings ) );
|
|
|
|
}, wp.mce.av.loaded ? 10 : 500 );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass data to the View's Underscore template and return the compiled output
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getHtml: function() {
|
|
|
|
var attrs = this.shortcode.attrs.named;
|
|
|
|
attrs.content = this.shortcode.content;
|
|
|
|
|
|
|
|
return this.template({ model: _.defaults(
|
|
|
|
attrs,
|
|
|
|
wp.media[ this.shortcode.tag ].defaults )
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
unbind: function() {
|
|
|
|
this.unsetPlayers();
|
|
|
|
}
|
|
|
|
} ),
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a TinyMCE view is clicked for editing.
|
|
|
|
* - Parses the shortcode out of the element's data attribute
|
|
|
|
* - Calls the `edit` method on the shortcode model
|
|
|
|
* - Launches the model window
|
|
|
|
* - Bind's an `update` callback which updates the element's data attribute
|
|
|
|
* re-renders the view
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node
|
|
|
|
*/
|
|
|
|
edit: function( node ) {
|
2014-06-18 19:03:15 -04:00
|
|
|
var media = wp.media[ this.type ],
|
2014-03-26 08:11:14 -04:00
|
|
|
self = this,
|
2014-03-27 13:58:15 -04:00
|
|
|
frame, data, callback;
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-05-10 22:07:14 -04:00
|
|
|
$( document ).trigger( 'media:edit' );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );
|
|
|
|
frame = media.edit( data );
|
|
|
|
frame.on( 'close', function() {
|
|
|
|
frame.detach();
|
|
|
|
} );
|
2014-03-27 13:58:15 -04:00
|
|
|
|
|
|
|
callback = function( selection ) {
|
2014-06-18 19:03:15 -04:00
|
|
|
var shortcode = wp.media[ self.type ].shortcode( selection ).string();
|
2014-03-26 08:11:14 -04:00
|
|
|
$( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );
|
|
|
|
wp.mce.views.refreshView( self, shortcode );
|
|
|
|
frame.detach();
|
2014-03-27 13:58:15 -04:00
|
|
|
};
|
|
|
|
if ( _.isArray( self.state ) ) {
|
|
|
|
_.each( self.state, function (state) {
|
|
|
|
frame.state( state ).on( 'update', callback );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
frame.state( self.state ).on( 'update', callback );
|
|
|
|
}
|
2014-03-26 08:11:14 -04:00
|
|
|
frame.open();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TinyMCE handler for the video shortcode
|
|
|
|
*
|
2014-06-05 13:27:13 -04:00
|
|
|
* @mixes wp.mce.av
|
2014-03-26 08:11:14 -04:00
|
|
|
*/
|
2014-06-05 13:27:13 -04:00
|
|
|
wp.mce.views.register( 'video', _.extend( {}, wp.mce.av, {
|
2014-03-26 08:11:14 -04:00
|
|
|
state: 'video-details',
|
2014-06-05 13:27:13 -04:00
|
|
|
View: _.extend( {}, wp.mce.av.View, {
|
|
|
|
template: media.template( 'editor-video' )
|
|
|
|
} )
|
|
|
|
} ) );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TinyMCE handler for the audio shortcode
|
|
|
|
*
|
2014-06-05 13:27:13 -04:00
|
|
|
* @mixes wp.mce.av
|
2014-03-26 08:11:14 -04:00
|
|
|
*/
|
2014-06-05 13:27:13 -04:00
|
|
|
wp.mce.views.register( 'audio', _.extend( {}, wp.mce.av, {
|
2014-03-26 08:11:14 -04:00
|
|
|
state: 'audio-details',
|
2014-06-05 13:27:13 -04:00
|
|
|
View: _.extend( {}, wp.mce.av.View, {
|
|
|
|
template: media.template( 'editor-audio' )
|
|
|
|
} )
|
|
|
|
} ) );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
/**
|
2014-06-05 13:27:13 -04:00
|
|
|
* TinyMCE handler for the playlist shortcode
|
2014-03-26 08:11:14 -04:00
|
|
|
*
|
2014-06-05 13:27:13 -04:00
|
|
|
* @mixes wp.mce.av
|
2014-03-26 08:11:14 -04:00
|
|
|
*/
|
2014-06-05 13:27:13 -04:00
|
|
|
wp.mce.views.register( 'playlist', _.extend( {}, wp.mce.av, {
|
|
|
|
state: ['playlist-edit', 'video-playlist-edit'],
|
|
|
|
View: _.extend( {}, wp.media.mixin, {
|
|
|
|
template: media.template( 'editor-playlist' ),
|
2014-06-11 22:49:16 -04:00
|
|
|
overlay: true,
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
initialize: function( options ) {
|
|
|
|
this.players = [];
|
|
|
|
this.data = {};
|
|
|
|
this.attachments = [];
|
|
|
|
this.shortcode = options.shortcode;
|
2014-05-10 22:07:14 -04:00
|
|
|
|
2014-06-20 12:30:16 -04:00
|
|
|
$( this ).on( 'ready', function( event, editor ) {
|
|
|
|
editor.on( 'hide', this.pausePlayers );
|
|
|
|
} );
|
2014-06-05 13:27:13 -04:00
|
|
|
$( document ).on( 'media:edit', this.pausePlayers );
|
2014-05-10 22:07:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
this.fetch();
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
$( this ).on( 'ready', this.setPlaylist );
|
|
|
|
},
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
/**
|
|
|
|
* Asynchronously fetch the shortcode's attachments
|
|
|
|
*/
|
|
|
|
fetch: function() {
|
|
|
|
this.attachments = wp.media.playlist.attachments( this.shortcode );
|
|
|
|
this.dfd = this.attachments.more().done( _.bind( this.render, this ) );
|
|
|
|
},
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-11 22:49:16 -04:00
|
|
|
setPlaylist: function( event, editor, element ) {
|
2014-06-05 13:27:13 -04:00
|
|
|
if ( ! this.data.tracks ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
this.players.push( new WPPlaylistView( {
|
|
|
|
el: $( element ).find( '.wp-playlist' ).get( 0 ),
|
|
|
|
metadata: this.data
|
|
|
|
} ).player );
|
|
|
|
},
|
2014-04-15 20:23:15 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
/**
|
|
|
|
* Set the data that will be used to compile the Underscore template,
|
|
|
|
* compile the template, and then return it.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getHtml: function() {
|
|
|
|
var data = this.shortcode.attrs.named,
|
|
|
|
model = wp.media.playlist,
|
|
|
|
options,
|
|
|
|
attachments,
|
|
|
|
tracks = [];
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
// Don't render errors while still fetching attachments
|
|
|
|
if ( this.dfd && 'pending' === this.dfd.state() && ! this.attachments.length ) {
|
|
|
|
return;
|
2014-04-22 13:53:16 -04:00
|
|
|
}
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
_.each( model.defaults, function( value, key ) {
|
|
|
|
data[ key ] = model.coerce( data, key );
|
|
|
|
});
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
options = {
|
|
|
|
type: data.type,
|
|
|
|
style: data.style,
|
|
|
|
tracklist: data.tracklist,
|
|
|
|
tracknumbers: data.tracknumbers,
|
|
|
|
images: data.images,
|
|
|
|
artists: data.artists
|
|
|
|
};
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
if ( ! this.attachments.length ) {
|
|
|
|
return this.template( options );
|
|
|
|
}
|
2014-04-15 20:23:15 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
attachments = this.attachments.toJSON();
|
2014-04-15 20:23:15 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
_.each( attachments, function( attachment ) {
|
|
|
|
var size = {}, resize = {}, track = {
|
|
|
|
src : attachment.url,
|
|
|
|
type : attachment.mime,
|
|
|
|
title : attachment.title,
|
|
|
|
caption : attachment.caption,
|
|
|
|
description : attachment.description,
|
|
|
|
meta : attachment.meta
|
|
|
|
};
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
if ( 'video' === data.type ) {
|
|
|
|
size.width = attachment.width;
|
|
|
|
size.height = attachment.height;
|
|
|
|
if ( media.view.settings.contentWidth ) {
|
|
|
|
resize.width = media.view.settings.contentWidth - 22;
|
|
|
|
resize.height = Math.ceil( ( size.height * resize.width ) / size.width );
|
|
|
|
if ( ! options.width ) {
|
|
|
|
options.width = resize.width;
|
|
|
|
options.height = resize.height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! options.width ) {
|
|
|
|
options.width = attachment.width;
|
|
|
|
options.height = attachment.height;
|
|
|
|
}
|
2014-03-26 08:11:14 -04:00
|
|
|
}
|
2014-06-05 13:27:13 -04:00
|
|
|
track.dimensions = {
|
|
|
|
original : size,
|
|
|
|
resized : _.isEmpty( resize ) ? size : resize
|
|
|
|
};
|
2014-03-26 08:11:14 -04:00
|
|
|
} else {
|
2014-06-05 13:27:13 -04:00
|
|
|
options.width = 400;
|
2014-03-26 08:11:14 -04:00
|
|
|
}
|
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
track.image = attachment.image;
|
|
|
|
track.thumb = attachment.thumb;
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
tracks.push( track );
|
|
|
|
} );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
options.tracks = tracks;
|
|
|
|
this.data = options;
|
2014-04-22 13:53:16 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
return this.template( options );
|
|
|
|
},
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2014-06-05 13:27:13 -04:00
|
|
|
unbind: function() {
|
|
|
|
this.unsetPlayers();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
} ) );
|
2014-05-10 19:36:18 -04:00
|
|
|
|
2014-05-27 16:00:15 -04:00
|
|
|
/**
|
|
|
|
* TinyMCE handler for the embed shortcode
|
|
|
|
*/
|
2014-06-11 22:49:16 -04:00
|
|
|
wp.mce.embedView = _.extend( {}, wp.media.mixin, {
|
|
|
|
overlay: true,
|
|
|
|
initialize: function( options ) {
|
|
|
|
this.players = [];
|
|
|
|
this.content = options.content;
|
|
|
|
this.fetching = false;
|
|
|
|
this.parsed = false;
|
|
|
|
this.original = options.url || options.shortcode.string();
|
|
|
|
|
|
|
|
if ( options.url ) {
|
|
|
|
this.shortcode = '[embed]' + options.url + '[/embed]';
|
|
|
|
} else {
|
|
|
|
this.shortcode = options.shortcode.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
_.bindAll( this, 'setHtml', 'setNode', 'fetch' );
|
|
|
|
$( this ).on( 'ready', this.setNode );
|
|
|
|
},
|
|
|
|
unbind: function() {
|
|
|
|
var self = this;
|
|
|
|
_.each( this.players, function ( player ) {
|
|
|
|
player.pause();
|
|
|
|
self.removePlayer( player );
|
|
|
|
} );
|
|
|
|
this.players = [];
|
|
|
|
},
|
|
|
|
setNode: function () {
|
|
|
|
if ( this.parsed ) {
|
|
|
|
this.setHtml( this.parsed );
|
|
|
|
this.parseMediaShortcodes();
|
|
|
|
} else if ( ! this.fetching ) {
|
|
|
|
this.fetch();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fetch: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.fetching = true;
|
|
|
|
|
|
|
|
wp.ajax.send( 'parse-embed', {
|
|
|
|
data: {
|
2014-06-15 18:53:16 -04:00
|
|
|
post_ID: $( '#post_ID' ).val() || 0,
|
|
|
|
shortcode: this.shortcode
|
2014-06-11 22:49:16 -04:00
|
|
|
}
|
|
|
|
} )
|
2014-06-15 18:53:16 -04:00
|
|
|
.always( function() {
|
2014-06-11 22:49:16 -04:00
|
|
|
self.fetching = false;
|
2014-06-15 18:53:16 -04:00
|
|
|
} )
|
|
|
|
.done( function( response ) {
|
|
|
|
if ( response ) {
|
|
|
|
self.parsed = response;
|
|
|
|
self.setHtml( response );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.fail( function( response ) {
|
|
|
|
if ( response && response.message ) {
|
2014-06-30 01:49:16 -04:00
|
|
|
if ( ( response.type === 'not-embeddable' && self.type === 'embed' ) ||
|
|
|
|
response.type === 'not-ssl' ) {
|
|
|
|
|
2014-06-15 18:53:16 -04:00
|
|
|
self.setError( response.message, 'admin-media' );
|
2014-06-11 22:49:16 -04:00
|
|
|
} else {
|
2014-06-15 18:53:16 -04:00
|
|
|
self.setContent( '<p>' + self.original + '</p>', null, true );
|
2014-06-11 22:49:16 -04:00
|
|
|
}
|
2014-06-15 18:53:16 -04:00
|
|
|
} else if ( response && response.statusText ) {
|
|
|
|
self.setError( response.statusText, 'admin-media' );
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2014-06-11 22:49:16 -04:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
/* jshint scripturl: true */
|
|
|
|
setHtml: function ( content ) {
|
|
|
|
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
|
|
|
|
iframe, iframeDoc, i, resize,
|
|
|
|
dom = tinymce.DOM;
|
|
|
|
|
|
|
|
if ( content.indexOf( '<script' ) !== -1 ) {
|
|
|
|
iframe = dom.create( 'iframe', {
|
|
|
|
src: tinymce.Env.ie ? 'javascript:""' : '',
|
|
|
|
frameBorder: '0',
|
|
|
|
allowTransparency: 'true',
|
|
|
|
style: {
|
|
|
|
width: '100%',
|
|
|
|
display: 'block'
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
|
|
|
} );
|
2014-06-11 22:49:16 -04:00
|
|
|
|
|
|
|
this.setContent( iframe );
|
|
|
|
iframeDoc = iframe.contentWindow.document;
|
|
|
|
|
|
|
|
iframeDoc.open();
|
|
|
|
iframeDoc.write(
|
|
|
|
'<!DOCTYPE html>' +
|
|
|
|
'<html>' +
|
|
|
|
'<head>' +
|
|
|
|
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
|
|
|
|
'</head>' +
|
|
|
|
'<body>' +
|
|
|
|
content +
|
|
|
|
'</body>' +
|
|
|
|
'</html>'
|
|
|
|
);
|
|
|
|
iframeDoc.close();
|
|
|
|
|
|
|
|
resize = function() {
|
|
|
|
$( iframe ).height( $( iframeDoc ).height() );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( MutationObserver ) {
|
|
|
|
new MutationObserver( _.debounce( function() {
|
|
|
|
resize();
|
|
|
|
}, 100 ) )
|
|
|
|
.observe( iframeDoc.body, {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
for ( i = 1; i < 6; i++ ) {
|
|
|
|
setTimeout( resize, i * 700 );
|
|
|
|
}
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2014-06-11 22:49:16 -04:00
|
|
|
} else {
|
|
|
|
this.setContent( content );
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2014-06-11 22:49:16 -04:00
|
|
|
|
|
|
|
this.parseMediaShortcodes();
|
|
|
|
},
|
|
|
|
parseMediaShortcodes: function () {
|
|
|
|
var self = this;
|
|
|
|
$( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
|
|
|
|
self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
getHtml: function() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-06-13 17:42:15 -04:00
|
|
|
wp.mce.embedMixin = {
|
|
|
|
View: wp.mce.embedView,
|
|
|
|
edit: function( node ) {
|
|
|
|
var embed = media.embed,
|
|
|
|
self = this,
|
|
|
|
frame,
|
|
|
|
data,
|
2014-06-18 19:03:15 -04:00
|
|
|
isURL = 'embedURL' === this.type;
|
2014-06-13 17:42:15 -04:00
|
|
|
|
|
|
|
$( document ).trigger( 'media:edit' );
|
|
|
|
|
|
|
|
data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );
|
|
|
|
frame = embed.edit( data, isURL );
|
|
|
|
frame.on( 'close', function() {
|
|
|
|
frame.detach();
|
|
|
|
} );
|
|
|
|
frame.state( 'embed' ).props.on( 'change:url', function (model, url) {
|
|
|
|
if ( ! url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
frame.state( 'embed' ).metadata = model.toJSON();
|
|
|
|
} );
|
|
|
|
frame.state( 'embed' ).on( 'select', function() {
|
|
|
|
var shortcode;
|
|
|
|
|
|
|
|
if ( isURL ) {
|
|
|
|
shortcode = frame.state( 'embed' ).metadata.url;
|
|
|
|
} else {
|
|
|
|
shortcode = embed.shortcode( frame.state( 'embed' ).metadata ).string();
|
|
|
|
}
|
|
|
|
$( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );
|
|
|
|
wp.mce.views.refreshView( self, shortcode );
|
|
|
|
frame.detach();
|
|
|
|
} );
|
|
|
|
frame.open();
|
|
|
|
}
|
|
|
|
};
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2014-06-13 17:42:15 -04:00
|
|
|
wp.mce.views.register( 'embed', _.extend( {}, wp.mce.embedMixin ) );
|
|
|
|
|
|
|
|
wp.mce.views.register( 'embedURL', _.extend( {}, wp.mce.embedMixin, {
|
2014-06-11 22:49:16 -04:00
|
|
|
toView: function( content ) {
|
|
|
|
var re = /(?:^|<p>)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi,
|
|
|
|
match = re.exec( tinymce.trim( content ) );
|
|
|
|
|
|
|
|
if ( ! match ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
index: match.index,
|
|
|
|
content: match[0],
|
|
|
|
options: {
|
|
|
|
url: match[1]
|
|
|
|
}
|
|
|
|
};
|
2014-06-13 17:42:15 -04:00
|
|
|
}
|
|
|
|
} ) );
|
2014-05-10 19:36:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
}(jQuery));
|