2014-03-26 22:41:15 +01:00
|
|
|
/* global tinymce, MediaElementPlayer, WPPlaylistView */
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2012-09-26 03:00:08 +02:00
|
|
|
// Ensure the global `wp` object exists.
|
2012-10-06 02:43:36 +02:00
|
|
|
window.wp = window.wp || {};
|
2012-09-24 02:13:18 +02:00
|
|
|
|
|
|
|
(function($){
|
|
|
|
var views = {},
|
2014-03-05 08:01:14 +01:00
|
|
|
instances = {},
|
|
|
|
media = wp.media,
|
|
|
|
viewOptions = ['encodedText'];
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2012-09-26 03:00:08 +02:00
|
|
|
// Create the `wp.mce` object if necessary.
|
|
|
|
wp.mce = wp.mce || {};
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01: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.
|
|
|
|
*/
|
|
|
|
wp.mce.View = function( options ) {
|
|
|
|
options || (options = {});
|
|
|
|
_.extend(this, _.pick(options, viewOptions));
|
|
|
|
this.initialize.apply(this, arguments);
|
|
|
|
};
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
_.extend( wp.mce.View.prototype, {
|
|
|
|
initialize: function() {},
|
2014-04-02 04:18:15 +02:00
|
|
|
getHtml: function() {},
|
2014-03-05 08:01:14 +01:00
|
|
|
render: function() {
|
|
|
|
var html = this.getHtml();
|
|
|
|
// Search all tinymce editor instances and update the placeholders
|
|
|
|
_.each( tinymce.editors, function( editor ) {
|
2014-03-19 08:02:15 +01:00
|
|
|
var doc, self = this;
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( editor.plugins.wpview ) {
|
|
|
|
doc = editor.getDoc();
|
2014-03-19 08:02:15 +01:00
|
|
|
$( doc ).find( '[data-wpview-text="' + this.encodedText + '"]' ).each(function (i, elem) {
|
|
|
|
var node = $( elem );
|
|
|
|
node.html( html );
|
|
|
|
$( self ).trigger( 'ready', elem );
|
|
|
|
});
|
2012-09-26 16:12:54 +02:00
|
|
|
}
|
2014-03-05 08:01:14 +01:00
|
|
|
}, this );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// 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 ) {
|
|
|
|
views[ type ] = constructor;
|
2012-09-24 02:13:18 +02:00
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
/**
|
|
|
|
* wp.mce.views.get( id )
|
|
|
|
*
|
|
|
|
* Returns a TinyMCE view constructor.
|
|
|
|
*/
|
|
|
|
get: function( type ) {
|
|
|
|
return views[ type ];
|
2012-09-24 02:13:18 +02:00
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
/**
|
|
|
|
* wp.mce.views.unregister( type )
|
|
|
|
*
|
|
|
|
* Unregisters a TinyMCE view.
|
|
|
|
*/
|
|
|
|
unregister: function( type ) {
|
|
|
|
delete views[ type ];
|
2012-09-24 02:13:18 +02:00
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01: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.
|
|
|
|
*
|
|
|
|
*/
|
2012-09-24 02:13:18 +02:00
|
|
|
toViews: function( content ) {
|
2012-09-26 03:00:08 +02:00
|
|
|
var pieces = [ { content: content } ],
|
|
|
|
current;
|
|
|
|
|
2012-09-24 02:13:18 +02:00
|
|
|
_.each( views, function( view, viewType ) {
|
2012-09-26 03:00:08 +02: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 08:01:14 +01:00
|
|
|
if ( result.index ) {
|
2012-09-26 03:00:08 +02:00
|
|
|
pieces.push({ content: remaining.substring( 0, result.index ) });
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-09-26 03:00:08 +02:00
|
|
|
|
|
|
|
// Add the processed piece for the match.
|
|
|
|
pieces.push({
|
2014-03-05 08:01:14 +01:00
|
|
|
content: wp.mce.views.toView( viewType, result.content, result.options ),
|
2012-09-26 03:00:08 +02: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 08:01:14 +01:00
|
|
|
if ( remaining ) {
|
2012-09-26 03:00:08 +02:00
|
|
|
pieces.push({ content: remaining });
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-09-26 03:00:08 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.pluck( pieces, 'content' ).join('');
|
|
|
|
},
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01: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-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( ! wp.mce.views.getInstance( encodedText ) ) {
|
|
|
|
viewOptions = options;
|
|
|
|
viewOptions.encodedText = encodedText;
|
|
|
|
instance = new view.View( viewOptions );
|
|
|
|
instances[ encodedText ] = instance;
|
|
|
|
}
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2012-10-12 20:36:21 +02:00
|
|
|
return wp.html.string({
|
2014-03-05 08:01:14 +01:00
|
|
|
tag: 'div',
|
2012-10-12 20:36:21 +02:00
|
|
|
|
|
|
|
attrs: {
|
2014-03-05 08:01:14 +01:00
|
|
|
'class': 'wpview-wrap wpview-type-' + viewType,
|
|
|
|
'data-wpview-text': encodedText,
|
|
|
|
'data-wpview-type': viewType,
|
|
|
|
'contenteditable': 'false'
|
|
|
|
},
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
content: '\u00a0'
|
2012-09-24 02:13:18 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
/**
|
|
|
|
* Refresh views after an update is made
|
2014-03-19 08:02:15 +01:00
|
|
|
*
|
2014-03-05 08:01:14 +01: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;
|
|
|
|
viewOptions.encodedText = encodedText;
|
|
|
|
instance = new view.View( viewOptions );
|
|
|
|
instances[ encodedText ] = instance;
|
|
|
|
}
|
2012-09-24 02:13:18 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
wp.mce.views.render();
|
2012-09-26 16:12:54 +02:00
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
getInstance: function( encodedText ) {
|
|
|
|
return instances[ encodedText ];
|
2012-09-27 00:20:15 +02:00
|
|
|
},
|
|
|
|
|
2014-03-19 08:02:15 +01:00
|
|
|
/**
|
2014-03-05 08:01:14 +01:00
|
|
|
* render( scope )
|
2014-03-19 08:02:15 +01:00
|
|
|
*
|
2014-03-05 08:01:14 +01: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-27 00:20:15 +02:00
|
|
|
},
|
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
edit: function( node ) {
|
|
|
|
var viewType = $( node ).data('wpview-type'),
|
|
|
|
view = wp.mce.views.get( viewType );
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( view ) {
|
|
|
|
view.edit( node );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-10-12 05:28:22 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
wp.mce.gallery = {
|
|
|
|
shortcode: 'gallery',
|
|
|
|
toView: function( content ) {
|
|
|
|
var match = wp.shortcode.next( this.shortcode, content );
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
if ( ! match ) {
|
2012-10-12 01:52:09 +02:00
|
|
|
return;
|
2014-03-05 08:01:14 +01:00
|
|
|
}
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
return {
|
|
|
|
index: match.index,
|
|
|
|
content: match.content,
|
|
|
|
options: {
|
|
|
|
shortcode: match.shortcode
|
|
|
|
}
|
|
|
|
};
|
2012-10-12 01:52:09 +02:00
|
|
|
},
|
2014-03-05 08:01:14 +01:00
|
|
|
View: wp.mce.View.extend({
|
|
|
|
className: 'editor-gallery',
|
|
|
|
template: media.template('editor-gallery'),
|
|
|
|
|
|
|
|
// 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-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
fetch: function() {
|
|
|
|
this.attachments = wp.media.gallery.attachments( this.shortcode, this.postID );
|
2014-04-02 04:18:15 +02:00
|
|
|
this.dfd = this.attachments.more().done( _.bind( this.render, this ) );
|
2014-03-05 08:01:14 +01:00
|
|
|
},
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
getHtml: function() {
|
|
|
|
var attrs = this.shortcode.attrs.named,
|
2014-04-02 04:18:15 +02:00
|
|
|
attachments = false,
|
|
|
|
options;
|
2014-03-05 08:01:14 +01:00
|
|
|
|
2014-04-02 04:18:15 +02:00
|
|
|
// Don't render errors while still fetching attachments
|
|
|
|
if ( this.dfd && 'pending' === this.dfd.state() && ! this.attachments.length ) {
|
2014-03-05 08:01:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-02 04:18:15 +02:00
|
|
|
if ( this.attachments.length ) {
|
|
|
|
attachments = this.attachments.toJSON();
|
2014-03-18 04:25:14 +01:00
|
|
|
|
2014-04-02 04:18:15 +02:00
|
|
|
_.each( attachments, function( attachment ) {
|
|
|
|
if ( attachment.sizes.thumbnail ) {
|
|
|
|
attachment.thumbnail = attachment.sizes.thumbnail;
|
|
|
|
} else {
|
|
|
|
attachment.thumbnail = attachment.sizes.full;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2014-03-18 04:25:14 +01:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
options = {
|
2014-03-18 04:25:14 +01:00
|
|
|
attachments: attachments,
|
2014-03-05 08:01:14 +01:00
|
|
|
columns: attrs.columns ? parseInt( attrs.columns, 10 ) : 3
|
|
|
|
};
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
return this.template( options );
|
|
|
|
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
edit: function( node ) {
|
|
|
|
var gallery = wp.media.gallery,
|
|
|
|
self = this,
|
|
|
|
frame, data;
|
|
|
|
|
2014-03-18 04:48:15 +01:00
|
|
|
data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );
|
2014-03-05 08:01:14 +01: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-09 02:55:44 +02:00
|
|
|
}
|
2012-09-26 16:12:54 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
};
|
|
|
|
wp.mce.views.register( 'gallery', wp.mce.gallery );
|
2014-03-26 13:11:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tiny MCE Views for Audio / Video
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These are base methods that are shared by each shortcode's MCE controller
|
|
|
|
*
|
|
|
|
* @mixin
|
|
|
|
*/
|
|
|
|
wp.mce.media = {
|
|
|
|
/**
|
|
|
|
* @global wp.shortcode
|
|
|
|
*
|
|
|
|
* @param {string} content
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
toView: function( content ) {
|
|
|
|
var match = wp.shortcode.next( this.shortcode, content );
|
|
|
|
|
|
|
|
if ( ! match ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
index: match.index,
|
|
|
|
content: match.content,
|
|
|
|
options: {
|
|
|
|
shortcode: match.shortcode
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
|
|
|
var media = wp.media[ this.shortcode ],
|
|
|
|
self = this,
|
2014-03-27 18:58:15 +01:00
|
|
|
frame, data, callback;
|
2014-03-26 13:11:14 +01:00
|
|
|
|
|
|
|
wp.media.mixin.pauseAllPlayers();
|
|
|
|
|
|
|
|
data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );
|
|
|
|
frame = media.edit( data );
|
|
|
|
frame.on( 'close', function() {
|
|
|
|
frame.detach();
|
|
|
|
} );
|
2014-03-27 18:58:15 +01:00
|
|
|
|
|
|
|
callback = function( selection ) {
|
2014-03-26 13:11:14 +01:00
|
|
|
var shortcode = wp.media[ self.shortcode ].shortcode( selection ).string();
|
|
|
|
$( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );
|
|
|
|
wp.mce.views.refreshView( self, shortcode );
|
|
|
|
frame.detach();
|
2014-03-27 18:58:15 +01: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 13:11:14 +01:00
|
|
|
frame.open();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base View class for audio and video shortcodes
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments wp.mce.View
|
|
|
|
* @mixes wp.media.mixin
|
|
|
|
*/
|
|
|
|
wp.mce.media.View = wp.mce.View.extend({
|
|
|
|
initialize: function( options ) {
|
|
|
|
this.shortcode = options.shortcode;
|
|
|
|
_.bindAll( this, 'setPlayer' );
|
|
|
|
$(this).on( 'ready', this.setPlayer );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the player instance for the current node
|
|
|
|
*
|
|
|
|
* @global MediaElementPlayer
|
|
|
|
* @global _wpmejsSettings
|
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
* @param {HTMLElement} node
|
|
|
|
*/
|
|
|
|
setPlayer: function(e, node) {
|
|
|
|
// if the ready event fires on an empty node
|
|
|
|
if ( ! node ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
media,
|
|
|
|
firefox = this.ua.is( 'ff' ),
|
|
|
|
className = '.wp-' + this.shortcode.tag + '-shortcode';
|
|
|
|
|
|
|
|
if ( this.player ) {
|
|
|
|
this.unsetPlayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
media = $( node ).find( className );
|
|
|
|
|
|
|
|
if ( ! this.isCompatible( media ) ) {
|
|
|
|
media.closest( '.wpview-wrap' ).addClass( 'wont-play' );
|
|
|
|
if ( ! media.parent().hasClass( 'wpview-wrap' ) ) {
|
|
|
|
media.parent().replaceWith( media );
|
|
|
|
}
|
|
|
|
media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' );
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
media.closest( '.wpview-wrap' ).removeClass( 'wont-play' );
|
|
|
|
if ( firefox ) {
|
|
|
|
media.prop( 'preload', 'metadata' );
|
|
|
|
} else {
|
|
|
|
media.prop( 'preload', 'none' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
media = wp.media.view.MediaDetails.prepareSrc( media.get(0) );
|
|
|
|
|
|
|
|
// Thanks, Firefox!
|
|
|
|
if ( firefox ) {
|
|
|
|
setTimeout( function() {
|
|
|
|
self.player = new MediaElementPlayer( media, this.mejsSettings );
|
|
|
|
}, 50 );
|
|
|
|
} else {
|
|
|
|
this.player = new MediaElementPlayer( media, this.mejsSettings );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass data to the View's Underscore template and return the compiled output
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getHtml: function() {
|
|
|
|
var attrs = _.defaults(
|
|
|
|
this.shortcode.attrs.named,
|
|
|
|
wp.media[ this.shortcode.tag ].defaults
|
|
|
|
);
|
|
|
|
return this.template({ model: attrs });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_.extend( wp.mce.media.View.prototype, wp.media.mixin );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TinyMCE handler for the video shortcode
|
|
|
|
*
|
|
|
|
* @mixes wp.mce.media
|
|
|
|
*/
|
|
|
|
wp.mce.video = _.extend( {}, wp.mce.media, {
|
|
|
|
shortcode: 'video',
|
|
|
|
state: 'video-details',
|
|
|
|
View: wp.mce.media.View.extend({
|
|
|
|
className: 'editor-video',
|
|
|
|
template: media.template('editor-video')
|
|
|
|
})
|
|
|
|
} );
|
|
|
|
wp.mce.views.register( 'video', wp.mce.video );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TinyMCE handler for the audio shortcode
|
|
|
|
*
|
|
|
|
* @mixes wp.mce.media
|
|
|
|
*/
|
|
|
|
wp.mce.audio = _.extend( {}, wp.mce.media, {
|
|
|
|
shortcode: 'audio',
|
|
|
|
state: 'audio-details',
|
|
|
|
View: wp.mce.media.View.extend({
|
|
|
|
className: 'editor-audio',
|
|
|
|
template: media.template('editor-audio')
|
|
|
|
})
|
|
|
|
} );
|
|
|
|
wp.mce.views.register( 'audio', wp.mce.audio );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base View class for playlist shortcodes
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @augments wp.mce.View
|
|
|
|
* @mixes wp.media.mixin
|
|
|
|
*/
|
|
|
|
wp.mce.media.PlaylistView = wp.mce.View.extend({
|
|
|
|
className: 'editor-playlist',
|
|
|
|
template: media.template('editor-playlist'),
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
this.data = {};
|
|
|
|
this.attachments = [];
|
|
|
|
this.shortcode = options.shortcode;
|
|
|
|
_.bindAll( this, 'setPlayer' );
|
|
|
|
$(this).on('ready', this.setNode);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the element context for the view, and then fetch the playlist's
|
|
|
|
* associated attachments.
|
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
* @param {HTMLElement} node
|
|
|
|
*/
|
|
|
|
setNode: function(e, node) {
|
|
|
|
this.node = node;
|
|
|
|
this.fetch();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously fetch the shortcode's attachments
|
|
|
|
*/
|
|
|
|
fetch: function() {
|
2014-03-27 18:58:15 +01:00
|
|
|
this.attachments = wp.media.playlist.attachments( this.shortcode );
|
2014-03-26 13:11:14 +01:00
|
|
|
this.attachments.more().done( this.setPlayer );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HTML for the view (which also set's the data), replace the
|
|
|
|
* current HTML, and then invoke the WPPlaylistView instance to render
|
|
|
|
* the playlist in the editor
|
|
|
|
*
|
|
|
|
* @global WPPlaylistView
|
|
|
|
* @global tinymce.editors
|
|
|
|
*/
|
|
|
|
setPlayer: function() {
|
|
|
|
var p,
|
|
|
|
html = this.getHtml(),
|
|
|
|
t = this.encodedText,
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
this.unsetPlayer();
|
|
|
|
|
|
|
|
_.each( tinymce.editors, function( editor ) {
|
|
|
|
var doc;
|
|
|
|
if ( editor.plugins.wpview ) {
|
|
|
|
doc = editor.getDoc();
|
|
|
|
$( doc ).find( '[data-wpview-text="' + t + '"]' ).each(function(i, elem) {
|
|
|
|
var node = $( elem );
|
|
|
|
node.html( html );
|
|
|
|
self.node = elem;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
p = new WPPlaylistView({
|
|
|
|
el: $( self.node ).find( '.wp-playlist' ).get(0),
|
|
|
|
metadata: this.data
|
|
|
|
});
|
|
|
|
|
|
|
|
this.player = p._player;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2014-03-27 18:58:15 +01:00
|
|
|
model = wp.media.playlist,
|
2014-03-26 13:11:14 +01:00
|
|
|
options,
|
|
|
|
attachments,
|
|
|
|
tracks = [];
|
|
|
|
|
|
|
|
if ( ! this.attachments.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each( model.defaults, function( value, key ) {
|
|
|
|
data[ key ] = model.coerce( data, key );
|
|
|
|
});
|
|
|
|
|
|
|
|
attachments = this.attachments.toJSON();
|
|
|
|
|
|
|
|
options = {
|
2014-03-27 18:58:15 +01:00
|
|
|
type: data.type,
|
2014-03-26 13:11:14 +01:00
|
|
|
style: data.style,
|
|
|
|
tracklist: data.tracklist,
|
|
|
|
tracknumbers: data.tracknumbers,
|
|
|
|
images: data.images,
|
|
|
|
artists: data.artists
|
|
|
|
};
|
|
|
|
|
|
|
|
_.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-27 18:58:15 +01:00
|
|
|
if ( 'video' === data.type ) {
|
2014-03-26 13:11:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
track.dimensions = {
|
|
|
|
original : size,
|
|
|
|
resized : _.isEmpty( resize ) ? size : resize
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
options.width = 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
track.image = attachment.image;
|
|
|
|
track.thumb = attachment.thumb;
|
|
|
|
|
|
|
|
tracks.push( track );
|
|
|
|
} );
|
|
|
|
|
|
|
|
options.tracks = tracks;
|
|
|
|
this.data = options;
|
|
|
|
|
|
|
|
return this.template( options );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_.extend( wp.mce.media.PlaylistView.prototype, wp.media.mixin );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TinyMCE handler for the playlist shortcode
|
|
|
|
*
|
|
|
|
* @mixes wp.mce.media
|
|
|
|
*/
|
|
|
|
wp.mce.playlist = _.extend( {}, wp.mce.media, {
|
|
|
|
shortcode: 'playlist',
|
2014-03-27 18:58:15 +01:00
|
|
|
state: ['playlist-edit', 'video-playlist-edit'],
|
2014-03-26 13:11:14 +01:00
|
|
|
View: wp.mce.media.PlaylistView
|
|
|
|
} );
|
|
|
|
wp.mce.views.register( 'playlist', wp.mce.playlist );
|
2014-03-05 08:01:14 +01:00
|
|
|
}(jQuery));
|