2014-03-05 08:01:14 +01:00
|
|
|
/* global tinymce */
|
|
|
|
|
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() {},
|
|
|
|
html: function() {},
|
|
|
|
render: function() {
|
|
|
|
var html = this.getHtml();
|
|
|
|
// Search all tinymce editor instances and update the placeholders
|
|
|
|
_.each( tinymce.editors, function( editor ) {
|
|
|
|
var doc;
|
|
|
|
if ( editor.plugins.wpview ) {
|
|
|
|
doc = editor.getDoc();
|
|
|
|
$( doc ).find( '[data-wpview-text="' + this.encodedText + '"]' ).html( html );
|
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
|
|
|
|
*
|
|
|
|
* @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-05 08:01:14 +01:00
|
|
|
/**
|
|
|
|
* render( scope )
|
|
|
|
*
|
|
|
|
* 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 );
|
|
|
|
this.attachments.more().done( _.bind( this.render, this ) );
|
|
|
|
},
|
2012-10-12 01:52:09 +02:00
|
|
|
|
2014-03-05 08:01:14 +01:00
|
|
|
getHtml: function() {
|
|
|
|
var attrs = this.shortcode.attrs.named,
|
|
|
|
options;
|
|
|
|
|
|
|
|
if ( ! this.attachments.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
attachments: this.attachments.toJSON(),
|
|
|
|
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;
|
|
|
|
|
|
|
|
data = window.decodeURIComponent( $( node ).data('wpview-text') );
|
|
|
|
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 );
|
|
|
|
}(jQuery));
|