2014-07-15 22:19:15 -04:00
|
|
|
/* global tinymce */
|
2015-02-25 18:13:25 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The TinyMCE view API.
|
|
|
|
*
|
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.
|
2015-02-25 18:13:25 -05:00
|
|
|
*
|
|
|
|
* Diagram
|
|
|
|
*
|
|
|
|
* |- registered view constructor (type)
|
|
|
|
* | |- view instance (unique text)
|
|
|
|
* | | |- editor 1
|
|
|
|
* | | | |- view node
|
|
|
|
* | | | |- view node
|
|
|
|
* | | | |- ...
|
|
|
|
* | | |- editor 2
|
|
|
|
* | | | |- ...
|
|
|
|
* | |- view instance
|
|
|
|
* | | |- ...
|
|
|
|
* |- registered view
|
|
|
|
* | |- ...
|
2014-04-07 05:03:14 -04:00
|
|
|
*/
|
2015-07-26 06:11:24 -04:00
|
|
|
( function( window, wp, shortcode, $ ) {
|
2014-06-11 22:49:16 -04:00
|
|
|
'use strict';
|
|
|
|
|
2012-09-23 20:13:18 -04:00
|
|
|
var views = {},
|
2015-03-05 07:36:28 -05:00
|
|
|
instances = {};
|
2014-07-15 18:18:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
wp.mce = wp.mce || {};
|
2014-03-05 02:01:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = {
|
|
|
|
|
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Registers a new view type.
|
2014-03-05 02:01:14 -05:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {String} type The view type.
|
|
|
|
* @param {Object} extend An object to extend wp.mce.View.prototype with.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-02-25 18:13:25 -05:00
|
|
|
register: function( type, extend ) {
|
|
|
|
views[ type ] = wp.mce.View.extend( _.extend( extend, { type: type } ) );
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Unregisters a view type.
|
2014-06-05 13:27:13 -04:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {String} type The view type.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-02-25 18:13:25 -05:00
|
|
|
unregister: function( type ) {
|
|
|
|
delete views[ type ];
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Returns the settings of a view type.
|
2014-06-05 13:27:13 -04:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {String} type The view type.
|
2015-02-26 14:32:24 -05:00
|
|
|
*
|
|
|
|
* @return {Function} The view constructor.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-02-25 18:13:25 -05:00
|
|
|
get: function( type ) {
|
|
|
|
return views[ type ];
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-04-11 20:46:14 -04:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Unbinds all view nodes.
|
|
|
|
* Runs before removing all view nodes from the DOM.
|
2014-04-11 20:46:14 -04:00
|
|
|
*/
|
|
|
|
unbind: function() {
|
|
|
|
_.each( instances, function( instance ) {
|
|
|
|
instance.unbind();
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Scans a given string for each view's pattern,
|
|
|
|
* replacing any matches with markers,
|
|
|
|
* and creates a new instance for every match.
|
2014-03-05 02:01:14 -05:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {String} content The string to scan.
|
2015-04-04 19:00:26 -04:00
|
|
|
*
|
|
|
|
* @return {String} The string with markers.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-02-25 18:13:25 -05:00
|
|
|
setMarkers: function( content ) {
|
2015-03-05 07:36:28 -05:00
|
|
|
var pieces = [ { content: content } ],
|
2015-02-25 18:13:25 -05:00
|
|
|
self = this,
|
2015-06-01 14:09:26 -04:00
|
|
|
instance, current;
|
2012-09-25 21:00:08 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
_.each( views, function( view, type ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
current = pieces.slice();
|
|
|
|
pieces = [];
|
|
|
|
|
|
|
|
_.each( current, function( piece ) {
|
|
|
|
var remaining = piece.content,
|
2015-06-01 14:09:26 -04:00
|
|
|
result, text;
|
2012-09-25 21:00:08 -04:00
|
|
|
|
|
|
|
// 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.
|
2015-02-25 18:13:25 -05:00
|
|
|
while ( remaining && ( result = view.prototype.match( remaining ) ) ) {
|
2012-09-25 21:00:08 -04:00
|
|
|
// Any text before the match becomes an unprocessed piece.
|
2014-03-05 02:01:14 -05:00
|
|
|
if ( result.index ) {
|
2015-02-25 18:13:25 -05: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
|
|
|
|
2015-03-09 15:09:27 -04:00
|
|
|
instance = self.createInstance( type, result.content, result.options );
|
2015-06-01 14:09:26 -04:00
|
|
|
text = instance.loader ? '.' : instance.text;
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2012-09-25 21:00:08 -04:00
|
|
|
// Add the processed piece for the match.
|
2015-02-25 18:13:25 -05:00
|
|
|
pieces.push( {
|
2015-10-16 01:43:26 -04:00
|
|
|
content: instance.ignore ? text : '<p data-wpview-marker="' + instance.encodedText + '">' + text + '</p>',
|
2012-09-25 21:00:08 -04:00
|
|
|
processed: true
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
2012-09-25 21:00:08 -04:00
|
|
|
|
|
|
|
// Update the remaining content.
|
|
|
|
remaining = remaining.slice( result.index + result.content.length );
|
|
|
|
}
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
// 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 ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
pieces.push( { content: remaining } );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
} );
|
2012-09-25 21:00:08 -04:00
|
|
|
|
2015-06-01 14:09:26 -04:00
|
|
|
content = _.pluck( pieces, 'content' ).join( '' );
|
|
|
|
return content.replace( /<p>\s*<p data-wpview-marker=/g, '<p data-wpview-marker=' ).replace( /<\/p>\s*<\/p>/g, '</p>' );
|
2012-09-25 21:00:08 -04:00
|
|
|
},
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Create a view instance.
|
2014-03-05 02:01:14 -05:00
|
|
|
*
|
2015-07-20 19:56:25 -04:00
|
|
|
* @param {String} type The view type.
|
|
|
|
* @param {String} text The textual representation of the view.
|
|
|
|
* @param {Object} options Options.
|
|
|
|
* @param {Boolean} force Recreate the instance. Optional.
|
2015-02-26 14:32:24 -05:00
|
|
|
*
|
|
|
|
* @return {wp.mce.View} The view instance.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-07-20 19:56:25 -04:00
|
|
|
createInstance: function( type, text, options, force ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
var View = this.get( type ),
|
2015-03-09 15:09:27 -04:00
|
|
|
encodedText,
|
|
|
|
instance;
|
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
if ( text.indexOf( '[' ) !== -1 && text.indexOf( ']' ) !== -1 ) {
|
|
|
|
// Looks like a shortcode? Remove any line breaks from inside of shortcodes
|
|
|
|
// or autop will replace them with <p> and <br> later and the string won't match.
|
|
|
|
text = text.replace( /\[[^\]]+\]/g, function( match ) {
|
|
|
|
return match.replace( /[\r\n]/g, '' );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:56:25 -04:00
|
|
|
if ( ! force ) {
|
|
|
|
instance = this.getInstance( text );
|
|
|
|
|
|
|
|
if ( instance ) {
|
|
|
|
return instance;
|
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-06-08 15:54:25 -04:00
|
|
|
encodedText = encodeURIComponent( text );
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
options = _.extend( options || {}, {
|
|
|
|
text: text,
|
|
|
|
encodedText: encodedText
|
|
|
|
} );
|
2012-09-23 20:13:18 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
return instances[ encodedText ] = new View( options );
|
2012-09-23 20:13:18 -04:00
|
|
|
},
|
|
|
|
|
2014-03-05 02:01:14 -05:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Get a view instance.
|
2014-03-19 03:02:15 -04:00
|
|
|
*
|
2015-02-26 14:32:24 -05:00
|
|
|
* @param {(String|HTMLElement)} object The textual representation of the view or the view node.
|
|
|
|
*
|
|
|
|
* @return {wp.mce.View} The view instance or undefined.
|
|
|
|
*/
|
|
|
|
getInstance: function( object ) {
|
|
|
|
if ( typeof object === 'string' ) {
|
|
|
|
return instances[ encodeURIComponent( object ) ];
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:40:28 -04:00
|
|
|
return instances[ $( object ).attr( 'data-wpview-text' ) ];
|
2015-02-26 14:32:24 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a view node, get the view's text.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node The view node.
|
|
|
|
*
|
|
|
|
* @return {String} The textual representation of the view.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2015-02-26 14:32:24 -05:00
|
|
|
getText: function( node ) {
|
2015-03-11 18:40:28 -04:00
|
|
|
return decodeURIComponent( $( node ).attr( 'data-wpview-text' ) || '' );
|
2012-09-26 18:20:15 -04:00
|
|
|
},
|
|
|
|
|
2014-03-19 03:02:15 -04:00
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Renders all view nodes that are not yet rendered.
|
2014-03-05 02:01:14 -05:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {Boolean} force Rerender all view nodes.
|
2014-03-05 02:01:14 -05:00
|
|
|
*/
|
2014-08-17 23:59:17 -04:00
|
|
|
render: function( force ) {
|
2014-03-05 02:01:14 -05:00
|
|
|
_.each( instances, function( instance ) {
|
2016-11-17 15:32:32 -05:00
|
|
|
instance.render( null, force );
|
2014-03-05 02:01:14 -05:00
|
|
|
} );
|
2012-09-26 18:20:15 -04:00
|
|
|
},
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Update the text of a given view node.
|
|
|
|
*
|
|
|
|
* @param {String} text The new text.
|
|
|
|
* @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
|
|
|
|
* @param {HTMLElement} node The view node to update.
|
2015-07-20 19:56:25 -04:00
|
|
|
* @param {Boolean} force Recreate the instance. Optional.
|
2015-02-25 18:13:25 -05:00
|
|
|
*/
|
2015-07-20 19:56:25 -04:00
|
|
|
update: function( text, editor, node, force ) {
|
2015-02-26 14:32:24 -05:00
|
|
|
var instance = this.getInstance( node );
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( instance ) {
|
2015-07-20 19:56:25 -04:00
|
|
|
instance.update( text, editor, node, force );
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders any editing interface based on the view type.
|
|
|
|
*
|
|
|
|
* @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
|
|
|
|
* @param {HTMLElement} node The view node to edit.
|
|
|
|
*/
|
|
|
|
edit: function( editor, node ) {
|
2015-02-26 14:32:24 -05:00
|
|
|
var instance = this.getInstance( node );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
|
|
|
if ( instance && instance.edit ) {
|
2015-07-20 19:56:25 -04:00
|
|
|
instance.edit( instance.text, function( text, force ) {
|
|
|
|
instance.update( text, editor, node, force );
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2015-02-26 14:32:24 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a given view node from the DOM.
|
|
|
|
*
|
|
|
|
* @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
|
|
|
|
* @param {HTMLElement} node The view node to remove.
|
|
|
|
*/
|
|
|
|
remove: function( editor, node ) {
|
|
|
|
var instance = this.getInstance( node );
|
|
|
|
|
|
|
|
if ( instance ) {
|
|
|
|
instance.remove( editor, node );
|
|
|
|
}
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
|
|
|
};
|
2012-10-11 23:28:22 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2015-02-26 14:32:24 -05:00
|
|
|
* @param {Object} options Options.
|
2015-02-25 18:13:25 -05:00
|
|
|
*/
|
|
|
|
wp.mce.View = function( options ) {
|
|
|
|
_.extend( this, options );
|
|
|
|
this.initialize();
|
|
|
|
};
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
wp.mce.View.extend = Backbone.View.extend;
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
_.extend( wp.mce.View.prototype, {
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* The content.
|
|
|
|
*
|
|
|
|
* @type {*}
|
|
|
|
*/
|
|
|
|
content: null,
|
2014-08-17 23:59:17 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Whether or not to display a loader.
|
|
|
|
*
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
loader: true,
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Runs after the view instance is created.
|
|
|
|
*/
|
|
|
|
initialize: function() {},
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
2016-10-31 02:28:32 -04:00
|
|
|
* Returns the content to render in the view node.
|
2015-02-25 18:13:25 -05:00
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
getContent: function() {
|
|
|
|
return this.content;
|
|
|
|
},
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Renders all view nodes tied to this view instance that are not yet rendered.
|
|
|
|
*
|
2015-07-20 19:56:25 -04:00
|
|
|
* @param {String} content The content to render. Optional.
|
|
|
|
* @param {Boolean} force Rerender all view nodes tied to this view instance. Optional.
|
2015-02-25 18:13:25 -05:00
|
|
|
*/
|
2015-04-04 19:00:26 -04:00
|
|
|
render: function( content, force ) {
|
|
|
|
if ( content != null ) {
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
content = this.getContent();
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
// If there's nothing to render an no loader needs to be shown, stop.
|
2015-04-04 19:00:26 -04:00
|
|
|
if ( ! this.loader && ! content ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-03-17 23:25:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
// We're about to rerender all views of this instance, so unbind rendered views.
|
|
|
|
force && this.unbind();
|
|
|
|
|
|
|
|
// Replace any left over markers.
|
|
|
|
this.replaceMarkers();
|
2012-10-11 19:52:09 -04:00
|
|
|
|
2015-04-04 19:00:26 -04:00
|
|
|
if ( content ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
this.setContent( content, function( editor, node ) {
|
2015-04-04 19:00:26 -04:00
|
|
|
$( node ).data( 'rendered', true );
|
2016-05-17 14:49:30 -04:00
|
|
|
this.bindNode.call( this, editor, node );
|
2015-02-25 18:13:25 -05:00
|
|
|
}, force ? null : false );
|
|
|
|
} else {
|
|
|
|
this.setLoader();
|
2014-03-05 02:01:14 -05:00
|
|
|
}
|
2014-06-05 13:27:13 -04:00
|
|
|
},
|
2014-03-05 02:01:14 -05:00
|
|
|
|
2015-04-04 19:00:26 -04:00
|
|
|
/**
|
|
|
|
* Binds a given node after its content is added to the DOM.
|
|
|
|
*/
|
|
|
|
bindNode: function() {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbinds a given node before its content is removed from the DOM.
|
|
|
|
*/
|
|
|
|
unbindNode: function() {},
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Unbinds all view nodes tied to this view instance.
|
|
|
|
* Runs before their content is removed from the DOM.
|
|
|
|
*/
|
|
|
|
unbind: function() {
|
2016-05-17 14:49:30 -04:00
|
|
|
this.getNodes( function( editor, node ) {
|
|
|
|
this.unbindNode.call( this, editor, node );
|
2015-02-25 18:13:25 -05:00
|
|
|
}, true );
|
|
|
|
},
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Gets all the TinyMCE editor instances that support views.
|
|
|
|
*
|
|
|
|
* @param {Function} callback A callback.
|
|
|
|
*/
|
|
|
|
getEditors: function( callback ) {
|
|
|
|
_.each( tinymce.editors, function( editor ) {
|
|
|
|
if ( editor.plugins.wpview ) {
|
|
|
|
callback.call( this, editor );
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
},
|
2014-07-15 18:18:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Gets all view nodes tied to this view instance.
|
|
|
|
*
|
|
|
|
* @param {Function} callback A callback.
|
|
|
|
* @param {Boolean} rendered Get (un)rendered view nodes. Optional.
|
|
|
|
*/
|
|
|
|
getNodes: function( callback, rendered ) {
|
|
|
|
this.getEditors( function( editor ) {
|
2014-07-16 18:10:16 -04:00
|
|
|
var self = this;
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
$( editor.getBody() )
|
|
|
|
.find( '[data-wpview-text="' + self.encodedText + '"]' )
|
|
|
|
.filter( function() {
|
|
|
|
var data;
|
2014-07-15 18:18:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( rendered == null ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = $( this ).data( 'rendered' ) === true;
|
2014-07-16 14:19:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
return rendered ? data : ! data;
|
|
|
|
} )
|
|
|
|
.each( function() {
|
2016-07-26 19:13:28 -04:00
|
|
|
callback.call( self, editor, this, this /* back compat */ );
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
},
|
2014-06-05 13:27:13 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Gets all marker nodes tied to this view instance.
|
|
|
|
*
|
|
|
|
* @param {Function} callback A callback.
|
|
|
|
*/
|
|
|
|
getMarkers: function( callback ) {
|
|
|
|
this.getEditors( function( editor ) {
|
|
|
|
var self = this;
|
2014-07-16 18:10:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
$( editor.getBody() )
|
|
|
|
.find( '[data-wpview-marker="' + this.encodedText + '"]' )
|
|
|
|
.each( function() {
|
|
|
|
callback.call( self, editor, this );
|
2014-11-30 03:45:22 -05:00
|
|
|
} );
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
},
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Replaces all marker nodes tied to this view instance.
|
|
|
|
*/
|
|
|
|
replaceMarkers: function() {
|
|
|
|
this.getMarkers( function( editor, node ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
var $viewNode;
|
2015-07-29 09:49:24 -04:00
|
|
|
|
2017-09-19 08:42:05 -04:00
|
|
|
if ( ! this.loader && $( node ).text() !== tinymce.DOM.decode( this.text ) ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
editor.dom.setAttrib( node, 'data-wpview-marker', null );
|
|
|
|
return;
|
|
|
|
}
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2015-07-29 09:49:24 -04:00
|
|
|
$viewNode = editor.$(
|
2016-07-26 19:13:28 -04:00
|
|
|
'<div class="wpview wpview-wrap" data-wpview-text="' + this.encodedText + '" data-wpview-type="' + this.type + '" contenteditable="false"></div>'
|
2015-02-25 18:13:25 -05:00
|
|
|
);
|
2015-07-29 09:49:24 -04:00
|
|
|
|
|
|
|
editor.$( node ).replaceWith( $viewNode );
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
},
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Removes all marker nodes tied to this view instance.
|
|
|
|
*/
|
|
|
|
removeMarkers: function() {
|
|
|
|
this.getMarkers( function( editor, node ) {
|
|
|
|
editor.dom.setAttrib( node, 'data-wpview-marker', null );
|
|
|
|
} );
|
|
|
|
},
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Sets the content for all view nodes tied to this view instance.
|
|
|
|
*
|
|
|
|
* @param {*} content The content to set.
|
|
|
|
* @param {Function} callback A callback. Optional.
|
|
|
|
* @param {Boolean} rendered Only set for (un)rendered nodes. Optional.
|
|
|
|
*/
|
|
|
|
setContent: function( content, callback, rendered ) {
|
|
|
|
if ( _.isObject( content ) && content.body.indexOf( '<script' ) !== -1 ) {
|
2015-03-11 19:53:26 -04:00
|
|
|
this.setIframes( content.head || '', content.body, callback, rendered );
|
2015-02-25 18:13:25 -05:00
|
|
|
} else if ( _.isString( content ) && content.indexOf( '<script' ) !== -1 ) {
|
2015-03-11 19:53:26 -04:00
|
|
|
this.setIframes( '', content, callback, rendered );
|
2015-02-25 18:13:25 -05:00
|
|
|
} else {
|
2016-05-17 14:49:30 -04:00
|
|
|
this.getNodes( function( editor, node ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
content = content.body || content;
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( content.indexOf( '<iframe' ) !== -1 ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
content += '<span class="mce-shim"></span>';
|
2014-11-30 03:45:22 -05:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
node.innerHTML = '';
|
|
|
|
node.appendChild( _.isString( content ) ? editor.dom.createFragment( content ) : content );
|
|
|
|
editor.dom.add( node, 'span', { 'class': 'wpview-end' } );
|
|
|
|
} );
|
2014-11-30 03:45:22 -05:00
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
callback && callback.call( this, editor, node );
|
2015-02-25 18:13:25 -05:00
|
|
|
}, rendered );
|
|
|
|
}
|
|
|
|
},
|
2014-06-05 13:27:13 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Sets the content in an iframe for all view nodes tied to this view instance.
|
|
|
|
*
|
|
|
|
* @param {String} head HTML string to be added to the head of the document.
|
|
|
|
* @param {String} body HTML string to be added to the body of the document.
|
|
|
|
* @param {Function} callback A callback. Optional.
|
|
|
|
* @param {Boolean} rendered Only set for (un)rendered nodes. Optional.
|
|
|
|
*/
|
|
|
|
setIframes: function( head, body, callback, rendered ) {
|
2016-07-26 19:13:28 -04:00
|
|
|
var self = this;
|
2014-06-05 13:27:13 -04:00
|
|
|
|
2017-09-19 08:42:05 -04:00
|
|
|
if ( body.indexOf( '[' ) !== -1 && body.indexOf( ']' ) !== -1 ) {
|
|
|
|
var shortcodesRegExp = new RegExp( '\\[\\/?(?:' + window.mceViewL10n.shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
|
|
|
|
// Escape tags inside shortcode previews.
|
|
|
|
body = body.replace( shortcodesRegExp, function( match ) {
|
|
|
|
return match.replace( /</g, '<' ).replace( />/g, '>' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2016-05-17 14:49:30 -04:00
|
|
|
this.getNodes( function( editor, node ) {
|
2015-04-04 19:00:26 -04:00
|
|
|
var dom = editor.dom,
|
|
|
|
styles = '',
|
|
|
|
bodyClasses = editor.getBody().className || '',
|
2016-11-17 15:32:32 -05:00
|
|
|
editorHead = editor.getDoc().getElementsByTagName( 'head' )[0],
|
|
|
|
iframe, iframeWin, iframeDoc, MutationObserver, observer, i, block;
|
2015-04-04 19:00:26 -04:00
|
|
|
|
|
|
|
tinymce.each( dom.$( 'link[rel="stylesheet"]', editorHead ), function( link ) {
|
|
|
|
if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 &&
|
|
|
|
link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) {
|
|
|
|
|
|
|
|
styles += dom.getOuterHTML( link );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-06-08 17:32:27 -04:00
|
|
|
if ( self.iframeHeight ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
dom.add( node, 'span', {
|
|
|
|
'data-mce-bogus': 1,
|
|
|
|
style: {
|
|
|
|
display: 'block',
|
|
|
|
width: '100%',
|
|
|
|
height: self.iframeHeight
|
|
|
|
}
|
|
|
|
}, '\u200B' );
|
2015-06-08 17:32:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
node.innerHTML = '';
|
2015-04-06 14:55:28 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
iframe = dom.add( node, 'iframe', {
|
|
|
|
/* jshint scripturl: true */
|
|
|
|
src: tinymce.Env.ie ? 'javascript:""' : '',
|
|
|
|
frameBorder: '0',
|
|
|
|
allowTransparency: 'true',
|
|
|
|
scrolling: 'no',
|
|
|
|
'class': 'wpview-sandbox',
|
|
|
|
style: {
|
|
|
|
width: '100%',
|
|
|
|
display: 'block'
|
|
|
|
},
|
|
|
|
height: self.iframeHeight
|
|
|
|
} );
|
2016-05-17 14:49:30 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
dom.add( node, 'span', { 'class': 'mce-shim' } );
|
|
|
|
dom.add( node, 'span', { 'class': 'wpview-end' } );
|
|
|
|
} );
|
2015-03-11 19:53:26 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
// Bail if the iframe node is not attached to the DOM.
|
|
|
|
// Happens when the view is dragged in the editor.
|
|
|
|
// There is a browser restriction when iframes are moved in the DOM. They get emptied.
|
|
|
|
// The iframe will be rerendered after dropping the view node at the new location.
|
|
|
|
if ( ! iframe.contentWindow ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iframeWin = iframe.contentWindow;
|
|
|
|
iframeDoc = iframeWin.document;
|
|
|
|
iframeDoc.open();
|
|
|
|
|
|
|
|
iframeDoc.write(
|
|
|
|
'<!DOCTYPE html>' +
|
|
|
|
'<html>' +
|
|
|
|
'<head>' +
|
|
|
|
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
|
|
|
|
head +
|
|
|
|
styles +
|
|
|
|
'<style>' +
|
|
|
|
'html {' +
|
|
|
|
'background: transparent;' +
|
|
|
|
'padding: 0;' +
|
|
|
|
'margin: 0;' +
|
|
|
|
'}' +
|
|
|
|
'body#wpview-iframe-sandbox {' +
|
|
|
|
'background: transparent;' +
|
|
|
|
'padding: 1px 0 !important;' +
|
|
|
|
'margin: -1px 0 0 !important;' +
|
|
|
|
'}' +
|
|
|
|
'body#wpview-iframe-sandbox:before,' +
|
|
|
|
'body#wpview-iframe-sandbox:after {' +
|
|
|
|
'display: none;' +
|
|
|
|
'content: "";' +
|
|
|
|
'}' +
|
|
|
|
'</style>' +
|
|
|
|
'</head>' +
|
|
|
|
'<body id="wpview-iframe-sandbox" class="' + bodyClasses + '">' +
|
|
|
|
body +
|
|
|
|
'</body>' +
|
|
|
|
'</html>'
|
|
|
|
);
|
|
|
|
|
|
|
|
iframeDoc.close();
|
2014-08-22 14:55:15 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
function resize() {
|
|
|
|
var $iframe;
|
|
|
|
|
|
|
|
if ( block ) {
|
2016-07-26 18:06:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
// Make sure the iframe still exists.
|
|
|
|
if ( iframe.contentWindow ) {
|
|
|
|
$iframe = $( iframe );
|
|
|
|
self.iframeHeight = $( iframeDoc.body ).height();
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
if ( $iframe.height() !== self.iframeHeight ) {
|
|
|
|
$iframe.height( self.iframeHeight );
|
|
|
|
editor.nodeChanged();
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
2015-03-07 11:53:27 -05:00
|
|
|
}
|
2016-11-17 15:32:32 -05:00
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
if ( self.iframeHeight ) {
|
|
|
|
block = true;
|
2015-06-08 17:32:27 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
setTimeout( function() {
|
|
|
|
block = false;
|
|
|
|
resize();
|
|
|
|
}, 3000 );
|
|
|
|
}
|
2015-06-08 17:32:27 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
function reload() {
|
|
|
|
$( node ).data( 'rendered', null );
|
2016-07-26 19:13:28 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
setTimeout( function() {
|
|
|
|
wp.mce.views.render();
|
|
|
|
} );
|
|
|
|
}
|
2015-04-06 14:55:28 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
$( iframeWin ).on( 'load', resize ).on( 'unload', reload );
|
2015-02-26 14:32:24 -05:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver;
|
|
|
|
|
|
|
|
if ( MutationObserver ) {
|
|
|
|
observer = new MutationObserver( _.debounce( resize, 100 ) );
|
|
|
|
|
|
|
|
observer.observe( iframeDoc.body, {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
for ( i = 1; i < 6; i++ ) {
|
|
|
|
setTimeout( resize, i * 700 );
|
2014-08-22 14:55:15 -04:00
|
|
|
}
|
2016-11-17 15:32:32 -05:00
|
|
|
}
|
2014-06-05 13:27:13 -04:00
|
|
|
|
2016-11-17 15:32:32 -05:00
|
|
|
callback && callback.call( self, editor, node );
|
2015-02-25 18:13:25 -05:00
|
|
|
}, rendered );
|
|
|
|
},
|
2014-07-16 15:32:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Sets a loader for all view nodes tied to this view instance.
|
|
|
|
*/
|
2016-10-10 20:56:29 -04:00
|
|
|
setLoader: function( dashicon ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
this.setContent(
|
|
|
|
'<div class="loading-placeholder">' +
|
2016-10-10 20:56:29 -04:00
|
|
|
'<div class="dashicons dashicons-' + ( dashicon || 'admin-media' ) + '"></div>' +
|
2015-02-25 18:13:25 -05:00
|
|
|
'<div class="wpview-loading"><ins></ins></div>' +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
},
|
2014-07-22 17:49:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Sets an error for all view nodes tied to this view instance.
|
|
|
|
*
|
|
|
|
* @param {String} message The error message to set.
|
2015-07-20 19:56:25 -04:00
|
|
|
* @param {String} dashicon A dashicon ID. Optional. {@link https://developer.wordpress.org/resource/dashicons/}
|
2015-02-25 18:13:25 -05:00
|
|
|
*/
|
|
|
|
setError: function( message, dashicon ) {
|
|
|
|
this.setContent(
|
|
|
|
'<div class="wpview-error">' +
|
|
|
|
'<div class="dashicons dashicons-' + ( dashicon || 'no' ) + '"></div>' +
|
|
|
|
'<p>' + message + '</p>' +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
},
|
2014-07-16 14:19:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
/**
|
|
|
|
* Tries to find a text match in a given string.
|
|
|
|
*
|
|
|
|
* @param {String} content The string to scan.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
match: function( content ) {
|
2015-07-26 06:11:24 -04:00
|
|
|
var match = shortcode.next( this.type, content );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
|
|
|
if ( match ) {
|
|
|
|
return {
|
|
|
|
index: match.index,
|
|
|
|
content: match.content,
|
|
|
|
options: {
|
|
|
|
shortcode: match.shortcode
|
|
|
|
}
|
|
|
|
};
|
2014-06-05 13:27:13 -04:00
|
|
|
}
|
2014-07-15 18:18:14 -04:00
|
|
|
},
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
/**
|
2015-02-25 18:13:25 -05:00
|
|
|
* Update the text of a given view node.
|
2014-03-26 08:11:14 -04:00
|
|
|
*
|
2015-02-25 18:13:25 -05:00
|
|
|
* @param {String} text The new text.
|
|
|
|
* @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
|
|
|
|
* @param {HTMLElement} node The view node to update.
|
2015-07-20 19:56:25 -04:00
|
|
|
* @param {Boolean} force Recreate the instance. Optional.
|
2014-03-26 08:11:14 -04:00
|
|
|
*/
|
2015-07-20 19:56:25 -04:00
|
|
|
update: function( text, editor, node, force ) {
|
2015-02-27 17:29:26 -05:00
|
|
|
_.find( views, function( view, type ) {
|
|
|
|
var match = view.prototype.match( text );
|
|
|
|
|
|
|
|
if ( match ) {
|
|
|
|
$( node ).data( 'rendered', false );
|
|
|
|
editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) );
|
2015-07-20 19:56:25 -04:00
|
|
|
wp.mce.views.createInstance( type, text, match.options, force ).render();
|
2017-04-19 18:18:36 -04:00
|
|
|
|
|
|
|
editor.selection.select( node );
|
|
|
|
editor.nodeChanged();
|
2015-04-01 17:50:27 -04:00
|
|
|
editor.focus();
|
2015-02-27 17:29:26 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} );
|
2015-02-26 14:32:24 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a given view node from the DOM.
|
|
|
|
*
|
|
|
|
* @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
|
|
|
|
* @param {HTMLElement} node The view node to remove.
|
|
|
|
*/
|
|
|
|
remove: function( editor, node ) {
|
2016-05-17 14:49:30 -04:00
|
|
|
this.unbindNode.call( this, editor, node );
|
2015-02-26 14:32:24 -05:00
|
|
|
editor.dom.remove( node );
|
2015-04-01 17:50:27 -04:00
|
|
|
editor.focus();
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
|
|
|
} );
|
2015-07-26 06:11:24 -04:00
|
|
|
} )( window, window.wp, window.wp.shortcode, window.jQuery );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The WordPress core TinyMCE views.
|
|
|
|
* Views for the gallery, audio, video, playlist and embed shortcodes,
|
|
|
|
* and a view for embeddable URLs.
|
|
|
|
*/
|
2015-07-26 14:18:24 -04:00
|
|
|
( function( window, views, media, $ ) {
|
2015-10-21 16:38:26 -04:00
|
|
|
var base, gallery, av, embed,
|
|
|
|
schema, parser, serializer;
|
|
|
|
|
|
|
|
function verifyHTML( string ) {
|
|
|
|
var settings = {};
|
|
|
|
|
|
|
|
if ( ! window.tinymce ) {
|
|
|
|
return string.replace( /<[^>]+>/g, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! string || ( string.indexOf( '<' ) === -1 && string.indexOf( '>' ) === -1 ) ) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
schema = schema || new window.tinymce.html.Schema( settings );
|
|
|
|
parser = parser || new window.tinymce.html.DomParser( settings, schema );
|
|
|
|
serializer = serializer || new window.tinymce.html.Serializer( settings, schema );
|
|
|
|
|
|
|
|
return serializer.serialize( parser.parse( string, { forced_root_block: false } ) );
|
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2015-07-26 06:11:24 -04:00
|
|
|
base = {
|
2015-02-25 18:13:25 -05:00
|
|
|
state: [],
|
|
|
|
|
|
|
|
edit: function( text, update ) {
|
2015-07-20 19:56:25 -04:00
|
|
|
var type = this.type,
|
2015-07-26 06:11:24 -04:00
|
|
|
frame = media[ type ].edit( text );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2015-02-25 19:31:31 -05:00
|
|
|
this.pausePlayers && this.pausePlayers();
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
_.each( this.state, function( state ) {
|
|
|
|
frame.state( state ).on( 'update', function( selection ) {
|
2015-07-26 06:11:24 -04:00
|
|
|
update( media[ type ].shortcode( selection ).string(), type === 'gallery' );
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
} );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
|
|
|
frame.on( 'close', function() {
|
|
|
|
frame.detach();
|
|
|
|
} );
|
2015-02-26 14:32:24 -05:00
|
|
|
|
|
|
|
frame.open();
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
|
|
|
};
|
2014-03-27 13:58:15 -04:00
|
|
|
|
2015-07-26 06:11:24 -04:00
|
|
|
gallery = _.extend( {}, base, {
|
2015-02-25 18:13:25 -05:00
|
|
|
state: [ 'gallery-edit' ],
|
2015-07-26 06:11:24 -04:00
|
|
|
template: media.template( 'editor-gallery' ),
|
2015-02-25 18:13:25 -05:00
|
|
|
|
|
|
|
initialize: function() {
|
2015-07-26 06:11:24 -04:00
|
|
|
var attachments = media.gallery.attachments( this.shortcode, media.view.settings.post.id ),
|
2015-02-25 18:13:25 -05:00
|
|
|
attrs = this.shortcode.attrs.named,
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
attachments.more()
|
|
|
|
.done( function() {
|
|
|
|
attachments = attachments.toJSON();
|
|
|
|
|
|
|
|
_.each( attachments, function( attachment ) {
|
|
|
|
if ( attachment.sizes ) {
|
|
|
|
if ( attrs.size && attachment.sizes[ attrs.size ] ) {
|
|
|
|
attachment.thumbnail = attachment.sizes[ attrs.size ];
|
|
|
|
} else if ( attachment.sizes.thumbnail ) {
|
|
|
|
attachment.thumbnail = attachment.sizes.thumbnail;
|
|
|
|
} else if ( attachment.sizes.full ) {
|
|
|
|
attachment.thumbnail = attachment.sizes.full;
|
|
|
|
}
|
|
|
|
}
|
2014-03-27 13:58:15 -04:00
|
|
|
} );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2015-04-04 19:00:26 -04:00
|
|
|
self.render( self.template( {
|
2015-10-21 16:38:26 -04:00
|
|
|
verifyHTML: verifyHTML,
|
2015-02-25 18:13:25 -05:00
|
|
|
attachments: attachments,
|
2015-07-26 06:11:24 -04:00
|
|
|
columns: attrs.columns ? parseInt( attrs.columns, 10 ) : media.galleryDefaults.columns
|
2015-04-04 19:00:26 -04:00
|
|
|
} ) );
|
2015-02-25 18:13:25 -05:00
|
|
|
} )
|
|
|
|
.fail( function( jqXHR, textStatus ) {
|
|
|
|
self.setError( textStatus );
|
|
|
|
} );
|
2014-03-26 08:11:14 -04:00
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2015-07-26 06:11:24 -04:00
|
|
|
av = _.extend( {}, base, {
|
2015-02-25 18:13:25 -05:00
|
|
|
action: 'parse-media-shortcode',
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
initialize: function() {
|
|
|
|
var self = this;
|
2014-03-26 08:11:14 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( this.url ) {
|
|
|
|
this.loader = false;
|
2015-07-26 06:11:24 -04:00
|
|
|
this.shortcode = media.embed.shortcode( {
|
2015-03-09 15:09:27 -04:00
|
|
|
url: this.text
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
}
|
2014-05-10 19:36:18 -04:00
|
|
|
|
2015-04-04 19:00:26 -04:00
|
|
|
wp.ajax.post( this.action, {
|
2015-07-26 06:11:24 -04:00
|
|
|
post_ID: media.view.settings.post.id,
|
2015-04-04 19:00:26 -04:00
|
|
|
type: this.shortcode.tag,
|
|
|
|
shortcode: this.shortcode.string()
|
2015-02-25 18:13:25 -05:00
|
|
|
} )
|
|
|
|
.done( function( response ) {
|
2015-04-04 19:00:26 -04:00
|
|
|
self.render( response );
|
2015-02-25 18:13:25 -05:00
|
|
|
} )
|
|
|
|
.fail( function( response ) {
|
2015-02-25 19:31:31 -05:00
|
|
|
if ( self.url ) {
|
2015-10-16 01:43:26 -04:00
|
|
|
self.ignore = true;
|
2015-02-25 18:13:25 -05:00
|
|
|
self.removeMarkers();
|
2014-06-11 22:49:16 -04:00
|
|
|
} else {
|
2015-02-25 18:13:25 -05:00
|
|
|
self.setError( response.message || response.statusText, 'admin-media' );
|
2014-05-10 19:36:18 -04:00
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
this.getEditors( function( editor ) {
|
|
|
|
editor.on( 'wpview-selected', function() {
|
2015-02-25 19:31:31 -05:00
|
|
|
self.pausePlayers();
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
},
|
2014-06-13 17:42:15 -04:00
|
|
|
|
2015-02-25 19:31:31 -05:00
|
|
|
pausePlayers: function() {
|
2015-02-25 18:13:25 -05:00
|
|
|
this.getNodes( function( editor, node, content ) {
|
|
|
|
var win = $( 'iframe.wpview-sandbox', content ).get( 0 );
|
2014-06-13 17:42:15 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( win && ( win = win.contentWindow ) && win.mejs ) {
|
|
|
|
_.each( win.mejs.players, function( player ) {
|
|
|
|
try {
|
2015-02-25 19:31:31 -05:00
|
|
|
player.pause();
|
2015-02-25 18:13:25 -05:00
|
|
|
} catch ( e ) {}
|
|
|
|
} );
|
|
|
|
}
|
2014-06-13 17:42:15 -04:00
|
|
|
} );
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
embed = _.extend( {}, av, {
|
|
|
|
action: 'parse-embed',
|
|
|
|
|
|
|
|
edit: function( text, update ) {
|
2015-07-26 06:11:24 -04:00
|
|
|
var frame = media.embed.edit( text, this.url ),
|
2015-04-21 18:41:26 -04:00
|
|
|
self = this;
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2015-02-25 19:31:31 -05:00
|
|
|
this.pausePlayers();
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2015-04-21 18:41:26 -04:00
|
|
|
frame.state( 'embed' ).props.on( 'change:url', function( model, url ) {
|
2015-03-05 01:00:26 -05:00
|
|
|
if ( url && model.get( 'url' ) ) {
|
2015-02-25 18:13:25 -05:00
|
|
|
frame.state( 'embed' ).metadata = model.toJSON();
|
2014-06-13 17:42:15 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
frame.state( 'embed' ).on( 'select', function() {
|
2015-03-05 01:00:26 -05:00
|
|
|
var data = frame.state( 'embed' ).metadata;
|
|
|
|
|
2015-04-21 18:41:26 -04:00
|
|
|
if ( self.url ) {
|
2015-03-05 01:00:26 -05:00
|
|
|
update( data.url );
|
2014-06-13 17:42:15 -04:00
|
|
|
} else {
|
2015-07-26 06:11:24 -04:00
|
|
|
update( media.embed.shortcode( data ).string() );
|
2014-06-13 17:42:15 -04:00
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
frame.on( 'close', function() {
|
2014-06-13 17:42:15 -04:00
|
|
|
frame.detach();
|
|
|
|
} );
|
2015-02-25 18:13:25 -05:00
|
|
|
|
2014-06-13 17:42:15 -04:00
|
|
|
frame.open();
|
|
|
|
}
|
2015-02-25 18:13:25 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
views.register( 'gallery', _.extend( {}, gallery ) );
|
|
|
|
|
|
|
|
views.register( 'audio', _.extend( {}, av, {
|
|
|
|
state: [ 'audio-details' ]
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
views.register( 'video', _.extend( {}, av, {
|
|
|
|
state: [ 'video-details' ]
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
views.register( 'playlist', _.extend( {}, av, {
|
|
|
|
state: [ 'playlist-edit', 'video-playlist-edit' ]
|
|
|
|
} ) );
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
views.register( 'embed', _.extend( {}, embed ) );
|
2014-06-13 17:42:15 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
views.register( 'embedURL', _.extend( {}, embed, {
|
|
|
|
match: function( content ) {
|
2015-02-20 18:17:27 -05:00
|
|
|
var re = /(^|<p>)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi,
|
2015-02-25 19:31:31 -05:00
|
|
|
match = re.exec( content );
|
2014-06-11 22:49:16 -04:00
|
|
|
|
2015-02-25 18:13:25 -05:00
|
|
|
if ( match ) {
|
|
|
|
return {
|
|
|
|
index: match.index + match[1].length,
|
|
|
|
content: match[2],
|
|
|
|
options: {
|
2015-03-09 15:09:27 -04:00
|
|
|
url: true
|
2015-02-25 18:13:25 -05:00
|
|
|
}
|
|
|
|
};
|
2014-06-11 22:49:16 -04:00
|
|
|
}
|
2014-06-13 17:42:15 -04:00
|
|
|
}
|
|
|
|
} ) );
|
2015-07-26 14:18:24 -04:00
|
|
|
} )( window, window.wp.mce.views, window.wp.media, window.jQuery );
|