| 1 |
var photonicClickedNode; |
| 2 |
(function($) { |
| 3 |
tinymce.PluginManager.add('photonic', function(editor, url) { |
| 4 |
function html(cls, data, type) { |
| 5 |
data = window.encodeURIComponent(data); |
| 6 |
return '<img src="' + tinymce.Env.transparentSrc + '" class="wp-media mceItem ' + cls + '" ' + 'data-wp-media="' + data + '" data-mce-resize="false" data-mce-placeholder="1" alt="" title="Photonic ' + type + ' gallery" />'; |
| 7 |
} |
| 8 |
|
| 9 |
function restoreMediaShortcodes(content) { |
| 10 |
function getAttr(str, name) { |
| 11 |
name = new RegExp(name + '=\"([^\"]+)\"').exec(str); |
| 12 |
return name ? window.decodeURIComponent(name[1]) : ''; |
| 13 |
} |
| 14 |
|
| 15 |
var newContent; |
| 16 |
newContent = content.replace(/(?:<p(?: [^>]+)?>)*(<img [^>]+>)(?:<\/p>)*/g, function(match, image) { |
| 17 |
var data = getAttr(image, 'data-wp-media'); |
| 18 |
if (data) { |
| 19 |
return '<p>' + data + '</p>'; |
| 20 |
} |
| 21 |
|
| 22 |
return match; |
| 23 |
}); |
| 24 |
return newContent; |
| 25 |
} |
| 26 |
|
| 27 |
editor.on('mouseup', function(event) { |
| 28 |
var dom = editor.dom, |
| 29 |
node = event.target; |
| 30 |
|
| 31 |
function unselect() { |
| 32 |
dom.removeClass(dom.select('img.wp-media-selected'), 'wp-media-selected'); |
| 33 |
} |
| 34 |
|
| 35 |
if (node.nodeName === 'IMG' && dom.getAttrib(node, 'data-wp-media')) { |
| 36 |
// Don't trigger on right-click |
| 37 |
unselect(); |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
// Display gallery, audio or video instead of img in the element path |
| 42 |
editor.on('GetContent', function(event) { |
| 43 |
if (event.get) { |
| 44 |
event.content = restoreMediaShortcodes(event.content); |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...'); |
| 49 |
editor.addCommand('Photonic_Gallery', function(ui, v) { |
| 50 |
var node = editor.selection.getNode(); |
| 51 |
var type = v.type; |
| 52 |
|
| 53 |
var shortcode = wp.mce.views.getText(node); |
| 54 |
var shortcodeObj = wp.shortcode.next(Photonic_Admin_JS.shortcode, shortcode); |
| 55 |
var shortcodeAttr = shortcodeObj.shortcode.attrs.named; |
| 56 |
|
| 57 |
var template = $('#tmpl-photonic-editor-' + type).html(); |
| 58 |
template = $(template); |
| 59 |
|
| 60 |
// First, set all the inputs and selects in the template to the shortcode values |
| 61 |
var inputs = template.find('input'); |
| 62 |
$(inputs).each(function(idx, input) { |
| 63 |
if (shortcodeAttr[input.name] !== undefined) { |
| 64 |
template.find('input[name="' + input.name + '"]').attr('value', shortcodeAttr[input.name]); |
| 65 |
} |
| 66 |
else if (shortcodeAttr[$(input).attr('alt_id')] !== undefined) { |
| 67 |
template.find('input[alt_id="' + $(input).attr('alt_id') + '"]').attr('value', shortcodeAttr[$(input).attr('alt_id')]); |
| 68 |
} |
| 69 |
}); |
| 70 |
|
| 71 |
var selects = template.find('select'); |
| 72 |
$(selects).each(function(idx, select) { |
| 73 |
if (shortcodeAttr[select.name] !== undefined) { |
| 74 |
template.find('select[name="' + select.name + '"] option[value="' + shortcodeAttr[select.name] + '"]').prop('selected', 'selected'); |
| 75 |
} |
| 76 |
else if (shortcodeAttr[$(select).attr('alt_id')] !== undefined) { |
| 77 |
template.find('select[alt_id="' + $(select).attr('alt_id') + '"] option[value="' + shortcodeAttr[$(select).attr('alt_id')] + '"]').prop('selected', 'selected'); |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
// Passing the template to the WindowManager has issues retrieving values, so we now dynamically get the fields |
| 82 |
// The previous step is necessary, otherwise the shortcode values are not passed. |
| 83 |
var rows = template.find('label'); |
| 84 |
var fields = []; |
| 85 |
$(rows).each(function(idx, row) { |
| 86 |
var label = $(row).find('span.label').text(); |
| 87 |
var field = $(row).find('input,select'); |
| 88 |
var tooltip = $(row).find('span.hint').text(); |
| 89 |
if (field.length > 0) { |
| 90 |
var fieldObj = { |
| 91 |
type: field[0].nodeName === 'INPUT' ? 'textbox' : (field[0].nodeName === 'SELECT' ? 'listbox' : ''), |
| 92 |
name: field[0].name, |
| 93 |
label: label, |
| 94 |
value: field[0].value, |
| 95 |
tooltip: tooltip |
| 96 |
}; |
| 97 |
if (field[0].nodeName === 'SELECT') { |
| 98 |
fieldObj.values = []; |
| 99 |
$(field[0]).children().each(function() { |
| 100 |
fieldObj.values[fieldObj.values.length] = { |
| 101 |
text: $(this).text(), |
| 102 |
value: this.value |
| 103 |
} |
| 104 |
}); |
| 105 |
} |
| 106 |
fields[fields.length] = fieldObj; |
| 107 |
} |
| 108 |
}); |
| 109 |
|
| 110 |
editor.windowManager.open({ |
| 111 |
title: 'Photonic Shortcode Editor - ' + (type === 'wp' ? 'WP' : type.substr(0,1).toUpperCase() + type.substr(1)), |
| 112 |
id: 'photonic-gallery-editor', |
| 113 |
width: 800, |
| 114 |
height: 400, |
| 115 |
body: fields, |
| 116 |
onsubmit: function(e) { |
| 117 |
var insertCode = '[' + Photonic_Admin_JS.shortcode + ' type="' + (type === 'wp' ? 'default' : type) + '"'; |
| 118 |
var newCode = e.data; |
| 119 |
$.each(newCode, function(idx, obj) { |
| 120 |
if (obj !== '') { |
| 121 |
insertCode += ' ' + idx + "='" + $('<div/>').text(decodeURIComponent(obj)).html() + "'"; |
| 122 |
} |
| 123 |
}); |
| 124 |
insertCode += ']'; |
| 125 |
$(node).attr('data-wpview-text', encodeURIComponent(insertCode)); |
| 126 |
} |
| 127 |
}); |
| 128 |
}); |
| 129 |
|
| 130 |
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...'); |
| 131 |
editor.addCommand('Photonic_Gallery_Flow', function(ui, v) { |
| 132 |
var node = editor.selection.getNode(); |
| 133 |
photonicClickedNode = node; |
| 134 |
var type = v.type; |
| 135 |
|
| 136 |
var shortcode = wp.mce.views.getText(node); |
| 137 |
var shortcodeObj = wp.shortcode.next(Photonic_Admin_JS.shortcode, shortcode); |
| 138 |
var scParameter = window.btoa(JSON.stringify(shortcodeObj)); |
| 139 |
scParameter = scParameter.replace(/^=+|=+$/g, ''); |
| 140 |
// No s**t, but the TB code ignores everything after the TB_iframe=true parameter, hence appending TB_iframe=true to the end |
| 141 |
tb_show('Edit Gallery', (Photonic_Admin_JS.flow_url + '&shortcode=' + scParameter).replace('&TB_iframe=true', '') + '&TB_iframe=true'); |
| 142 |
}); |
| 143 |
|
| 144 |
function verifyHTML(string) { |
| 145 |
var settings = {}; |
| 146 |
|
| 147 |
if (! window.tinymce) { |
| 148 |
return string.replace(/<[^>]+>/g, ''); |
| 149 |
} |
| 150 |
|
| 151 |
if (! string || (string.indexOf('<') === -1 && string.indexOf('>') === -1)) { |
| 152 |
return string; |
| 153 |
} |
| 154 |
|
| 155 |
schema = schema || new window.tinymce.html.Schema(settings); |
| 156 |
parser = parser || new window.tinymce.html.DomParser(settings, schema); |
| 157 |
serializer = serializer || new window.tinymce.html.Serializer(settings, schema); |
| 158 |
|
| 159 |
return serializer.serialize(parser.parse(string, { forced_root_block: false })); |
| 160 |
} |
| 161 |
|
| 162 |
function getPhotonicType(img) { |
| 163 |
img = $(img); |
| 164 |
var type = 'default'; |
| 165 |
if (img.hasClass('photonic-gallery-flickr')) { |
| 166 |
type = 'flickr'; |
| 167 |
} |
| 168 |
else if (img.hasClass('photonic-gallery-picasa')) { |
| 169 |
type = 'picasa'; |
| 170 |
} |
| 171 |
else if (img.hasClass('photonic-gallery-google')) { |
| 172 |
type = 'google'; |
| 173 |
} |
| 174 |
else if (img.hasClass('photonic-gallery-smugmug')) { |
| 175 |
type = 'smugmug'; |
| 176 |
} |
| 177 |
else if (img.hasClass('photonic-gallery-zenfolio')) { |
| 178 |
type = 'zenfolio'; |
| 179 |
} |
| 180 |
else if (img.hasClass('photonic-gallery-instagram')) { |
| 181 |
type = 'instagram'; |
| 182 |
} |
| 183 |
return type; |
| 184 |
} |
| 185 |
|
| 186 |
wp.mce.photonic_view_renderer = _.extend({}, wp.media.gallery, { |
| 187 |
shortcode_string: Photonic_Admin_JS.shortcode, |
| 188 |
state: [ 'gallery-edit' ], |
| 189 |
template: wp.media.template('editor-gallery'), |
| 190 |
|
| 191 |
// Lifted verbatim from mce-view.js, "base" code |
| 192 |
edit: function(text, update) { |
| 193 |
var media = wp.media; |
| 194 |
var type = this.type; |
| 195 |
if (type === Photonic_Admin_JS.shortcode && type !== 'gallery') { |
| 196 |
if (Photonic_Admin_JS.disable_flow) { |
| 197 |
editor.execCommand('Photonic_Gallery', '', {type: 'default'}); |
| 198 |
} |
| 199 |
else { |
| 200 |
editor.execCommand('Photonic_Gallery_Flow', '', {type: 'default'}); |
| 201 |
} |
| 202 |
// editor.execCommand('Photonic_Gallery', '', {type: 'default'}); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
var frame = media[ type ].edit(text); |
| 207 |
|
| 208 |
this.pausePlayers && this.pausePlayers(); |
| 209 |
|
| 210 |
_.each(this.state, function(state) { |
| 211 |
frame.state(state).on('update', function(selection) { |
| 212 |
update(media[ type ].shortcode(selection).string(), type === 'gallery'); |
| 213 |
}); |
| 214 |
}); |
| 215 |
|
| 216 |
frame.on('close', function() { |
| 217 |
frame.detach(); |
| 218 |
}); |
| 219 |
|
| 220 |
frame.open(); |
| 221 |
}, |
| 222 |
|
| 223 |
initialize: function() { |
| 224 |
var shortcodeAttr = this.shortcode.attrs.named; |
| 225 |
var type; |
| 226 |
if (shortcodeAttr['type'] === undefined) { |
| 227 |
type = Photonic_Admin_JS.default_gallery_type; |
| 228 |
} |
| 229 |
else { |
| 230 |
type = shortcodeAttr['type']; |
| 231 |
} |
| 232 |
if (type === 'default') { |
| 233 |
// Lifted, almost verbatim, from wp-includes/js/mce-view.js. This is the default gallery processing code. |
| 234 |
// If Photonic_Admin_JS.shortcode != 'gallery', the code from WP will be called anyway. Otherwise this code will be triggered |
| 235 |
// if type == 'default'. |
| 236 |
var media = wp.media; |
| 237 |
var attachments = media.gallery.attachments(this.shortcode, media.view.settings.post.id), |
| 238 |
attrs = this.shortcode.attrs.named, |
| 239 |
self = this; |
| 240 |
|
| 241 |
attachments.more() |
| 242 |
.done(function () { |
| 243 |
attachments = attachments.toJSON(); |
| 244 |
|
| 245 |
_.each(attachments, function (attachment) { |
| 246 |
if (attachment.sizes) { |
| 247 |
if (attrs.size && attachment.sizes[attrs.size]) { |
| 248 |
attachment.thumbnail = attachment.sizes[attrs.size]; |
| 249 |
} |
| 250 |
else if (attachment.sizes.thumbnail) { |
| 251 |
attachment.thumbnail = attachment.sizes.thumbnail; |
| 252 |
} |
| 253 |
else if (attachment.sizes.full) { |
| 254 |
attachment.thumbnail = attachment.sizes.full; |
| 255 |
} |
| 256 |
} |
| 257 |
}); |
| 258 |
|
| 259 |
self.render(self.template({ |
| 260 |
verifyHTML: verifyHTML, |
| 261 |
attachments: attachments, |
| 262 |
columns: attrs.columns ? parseInt(attrs.columns, 10) : media.galleryDefaults.columns |
| 263 |
}), attrs); |
| 264 |
}) |
| 265 |
.fail(function (jqXHR, textStatus) { |
| 266 |
self.setError(textStatus); |
| 267 |
} |
| 268 |
); |
| 269 |
} |
| 270 |
else { |
| 271 |
this.content = html('wp-gallery photonic-gallery photonic-gallery-' + type, this.shortcode.string(), type === 'wp' ? 'WP' : type.substr(0, 1).toUpperCase() + type.substr(1)); |
| 272 |
} |
| 273 |
} |
| 274 |
}); |
| 275 |
|
| 276 |
editor.addButton('wp_view_edit', { |
| 277 |
tooltip: 'Edit ', // trailing space is needed, used for context |
| 278 |
icon: 'dashicon dashicons-edit', |
| 279 |
onclick: function() { |
| 280 |
var node = editor.selection.getNode(); |
| 281 |
if (editor.dom.hasClass(node, 'wpview')) { |
| 282 |
var img = $(node).find('img.photonic-gallery'); // Placeholder |
| 283 |
var div = $(node).find('div.gallery'); |
| 284 |
|
| 285 |
if (!img.hasClass('photonic-gallery') && editor.dom.hasClass( node, 'wpview' )) { // Open Native Gallery editor if that is what is in "wpview". |
| 286 |
wp.mce.views.edit(editor, node); |
| 287 |
} |
| 288 |
else if ((div.length > 0 && Photonic_Admin_JS.shortcode !== 'gallery') || img.hasClass('photonic-gallery')) { |
| 289 |
var type = getPhotonicType(img); |
| 290 |
if (Photonic_Admin_JS.disable_flow) { |
| 291 |
editor.execCommand('Photonic_Gallery', '', {type: type}); |
| 292 |
} |
| 293 |
else { |
| 294 |
editor.execCommand('Photonic_Gallery_Flow', '', {type: type}); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
}); |
| 300 |
|
| 301 |
editor.on('click keyup', function(e) { |
| 302 |
if (e.target.nodeName === 'IMG' && e.target.className.indexOf('photonic-gallery') > -1) { |
| 303 |
e.preventDefault(); |
| 304 |
|
| 305 |
var type = getPhotonicType(e.target); |
| 306 |
if (Photonic_Admin_JS.disable_flow) { |
| 307 |
editor.execCommand('Photonic_Gallery', '', {type: type}); |
| 308 |
} |
| 309 |
else { |
| 310 |
editor.execCommand('Photonic_Gallery_Flow', '', {type: type}); |
| 311 |
} |
| 312 |
return false; |
| 313 |
} |
| 314 |
else { |
| 315 |
return false; |
| 316 |
} |
| 317 |
}); |
| 318 |
|
| 319 |
wp.mce.views.register(Photonic_Admin_JS.shortcode, wp.mce.photonic_view_renderer); |
| 320 |
}); |
| 321 |
})(jQuery); |
| 322 |
|