2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
tinymce.create('tinymce.plugins.wpGallery', {
|
|
|
|
|
|
|
|
init : function(ed, url) {
|
|
|
|
var t = this;
|
|
|
|
|
|
|
|
t.url = url;
|
2012-11-16 00:49:19 +01:00
|
|
|
t.editor = ed;
|
2009-01-09 13:02:59 +01:00
|
|
|
t._createButtons();
|
|
|
|
|
|
|
|
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
|
|
|
|
ed.addCommand('WP_Gallery', function() {
|
2012-11-27 02:31:51 +01:00
|
|
|
if ( tinymce.isIE )
|
|
|
|
ed.selection.moveToBookmark( ed.wpGalleryBookmark );
|
|
|
|
|
2012-11-14 10:06:10 +01:00
|
|
|
var el = ed.selection.getNode(),
|
|
|
|
gallery = wp.media.gallery,
|
|
|
|
frame;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-11-14 10:06:10 +01:00
|
|
|
// Check if the `wp.media.gallery` API exists.
|
|
|
|
if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery )
|
|
|
|
return;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-11-14 10:06:10 +01:00
|
|
|
// Make sure we've selected a gallery node.
|
|
|
|
if ( el.nodeName != 'IMG' || ed.dom.getAttrib(el, 'class').indexOf('wpGallery') == -1 )
|
|
|
|
return;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-11-14 10:06:10 +01:00
|
|
|
frame = gallery.edit( '[' + ed.dom.getAttrib( el, 'title' ) + ']' );
|
|
|
|
|
|
|
|
frame.get('gallery-edit').on( 'update', function( selection ) {
|
|
|
|
var shortcode = gallery.shortcode( selection ).string().slice( 1, -1 );
|
|
|
|
ed.dom.setAttrib( el, 'title', shortcode );
|
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
|
2012-11-16 00:49:19 +01:00
|
|
|
ed.onInit.add(function(ed) {
|
|
|
|
// iOS6 doesn't show the buttons properly on click, show them on 'touchstart'
|
|
|
|
if ( 'ontouchstart' in window ) {
|
|
|
|
ed.dom.events.add(ed.getBody(), 'touchstart', function(e){
|
|
|
|
var target = e.target;
|
|
|
|
|
|
|
|
if ( target.nodeName == 'IMG' && ed.dom.hasClass(target, 'wpGallery') ) {
|
|
|
|
ed.selection.select(target);
|
|
|
|
ed.dom.events.cancel(e);
|
|
|
|
ed.plugins.wordpress._hideButtons();
|
|
|
|
ed.plugins.wordpress._showButtons(target, 'wp_gallerybtns');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.onMouseDown.add(function(ed, e) {
|
2012-11-16 00:49:19 +01:00
|
|
|
if ( e.target.nodeName == 'IMG' && ed.dom.hasClass(e.target, 'wpGallery') ) {
|
2012-11-27 02:31:51 +01:00
|
|
|
ed.plugins.wordpress._hideButtons();
|
2009-10-24 13:43:42 +02:00
|
|
|
ed.plugins.wordpress._showButtons(e.target, 'wp_gallerybtns');
|
2012-11-16 00:49:19 +01:00
|
|
|
}
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
ed.onBeforeSetContent.add(function(ed, o) {
|
|
|
|
o.content = t._do_gallery(o.content);
|
|
|
|
});
|
|
|
|
|
|
|
|
ed.onPostProcess.add(function(ed, o) {
|
|
|
|
if (o.get)
|
|
|
|
o.content = t._get_gallery(o.content);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_do_gallery : function(co) {
|
|
|
|
return co.replace(/\[gallery([^\]]*)\]/g, function(a,b){
|
|
|
|
return '<img src="'+tinymce.baseURL+'/plugins/wpgallery/img/t.gif" class="wpGallery mceItem" title="gallery'+tinymce.DOM.encode(b)+'" />';
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_get_gallery : function(co) {
|
|
|
|
|
|
|
|
function getAttr(s, n) {
|
|
|
|
n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);
|
|
|
|
return n ? tinymce.DOM.decode(n[1]) : '';
|
|
|
|
};
|
|
|
|
|
|
|
|
return co.replace(/(?:<p[^>]*>)*(<img[^>]+>)(?:<\/p>)*/g, function(a,im) {
|
|
|
|
var cls = getAttr(im, 'class');
|
|
|
|
|
|
|
|
if ( cls.indexOf('wpGallery') != -1 )
|
|
|
|
return '<p>['+tinymce.trim(getAttr(im, 'title'))+']</p>';
|
|
|
|
|
|
|
|
return a;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_createButtons : function() {
|
2012-11-27 02:31:51 +01:00
|
|
|
var t = this, ed = tinymce.activeEditor, DOM = tinymce.DOM, editButton, dellButton, isRetina;
|
|
|
|
|
|
|
|
if ( DOM.get('wp_gallerybtns') )
|
|
|
|
return;
|
2012-11-15 00:21:59 +01:00
|
|
|
|
|
|
|
isRetina = ( window.devicePixelRatio && window.devicePixelRatio > 1 ) || // WebKit, Opera
|
|
|
|
( window.matchMedia && window.matchMedia('(min-resolution:130dpi)').matches ); // Firefox, IE10, Opera
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
DOM.add(document.body, 'div', {
|
2009-01-09 13:02:59 +01:00
|
|
|
id : 'wp_gallerybtns',
|
|
|
|
style : 'display:none;'
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
editButton = DOM.add('wp_gallerybtns', 'img', {
|
2012-11-15 00:21:59 +01:00
|
|
|
src : isRetina ? t.url+'/img/edit-2x.png' : t.url+'/img/edit.png',
|
2009-01-09 13:02:59 +01:00
|
|
|
id : 'wp_editgallery',
|
|
|
|
width : '24',
|
|
|
|
height : '24',
|
|
|
|
title : ed.getLang('wordpress.editgallery')
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
|
2012-11-27 02:31:51 +01:00
|
|
|
var ed = tinymce.activeEditor;
|
|
|
|
ed.wpGalleryBookmark = ed.selection.getBookmark('simple');
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.execCommand("WP_Gallery");
|
2012-11-16 00:49:19 +01:00
|
|
|
ed.plugins.wordpress._hideButtons();
|
2009-10-24 13:43:42 +02:00
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
dellButton = DOM.add('wp_gallerybtns', 'img', {
|
2012-11-15 00:21:59 +01:00
|
|
|
src : isRetina ? t.url+'/img/delete-2x.png' : t.url+'/img/delete.png',
|
2009-01-09 13:02:59 +01:00
|
|
|
id : 'wp_delgallery',
|
|
|
|
width : '24',
|
|
|
|
height : '24',
|
|
|
|
title : ed.getLang('wordpress.delgallery')
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
|
2012-11-27 02:31:51 +01:00
|
|
|
var ed = tinymce.activeEditor, el = ed.selection.getNode();
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
if ( el.nodeName == 'IMG' && ed.dom.hasClass(el, 'wpGallery') ) {
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.dom.remove(el);
|
|
|
|
|
|
|
|
ed.execCommand('mceRepaint');
|
2012-11-16 00:49:19 +01:00
|
|
|
ed.dom.events.cancel(e);
|
2009-01-09 13:02:59 +01:00
|
|
|
}
|
2012-11-16 00:49:19 +01:00
|
|
|
|
|
|
|
ed.plugins.wordpress._hideButtons();
|
2009-10-24 13:43:42 +02:00
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getInfo : function() {
|
|
|
|
return {
|
|
|
|
longname : 'Gallery Settings',
|
|
|
|
author : 'WordPress',
|
|
|
|
authorurl : 'http://wordpress.org',
|
|
|
|
infourl : '',
|
|
|
|
version : "1.0"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tinymce.PluginManager.add('wpgallery', tinymce.plugins.wpGallery);
|
|
|
|
})();
|