| 1 |
var maxMedia; |
| 2 |
|
| 3 |
jQuery(document).ready(function($) { |
| 4 |
|
| 5 |
/* Add button for the post editor screen + integrations */ |
| 6 |
var maxMedia = function() { |
| 7 |
|
| 8 |
this.callback = null; // callback function when clicking 'insert button' |
| 9 |
this.parent = '#poststuff'; // option parent flag as location to write button window. |
| 10 |
this.window_loaded = null; |
| 11 |
this.maxm = null; |
| 12 |
this.closeOnCallback = true; |
| 13 |
} |
| 14 |
|
| 15 |
/* Default events and callback */ |
| 16 |
maxMedia.prototype.init = function() |
| 17 |
{ |
| 18 |
|
| 19 |
this.maxm = new maxModal(); |
| 20 |
this.maxm.init(); |
| 21 |
|
| 22 |
$(document).on('click','.maxbutton_media_button',$.proxy(this.clickAddButton, this)); |
| 23 |
this.callback = 'this.buttonToEditor'; // default |
| 24 |
} |
| 25 |
|
| 26 |
/* Set the callback after selecting a button in the window */ |
| 27 |
maxMedia.prototype.setCallback = function (callback) |
| 28 |
{ |
| 29 |
|
| 30 |
if (typeof callback !== 'function') |
| 31 |
{ |
| 32 |
if (typeof window[callback] === 'function') |
| 33 |
callback = window[callback]; |
| 34 |
else if (typeof eval(callback) === 'function') |
| 35 |
{ |
| 36 |
callback = eval(callback); |
| 37 |
} |
| 38 |
else |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
this.callback = callback; |
| 43 |
} |
| 44 |
|
| 45 |
maxMedia.prototype.showShortcodeOptions = function(button_id, target) |
| 46 |
{ |
| 47 |
this.closeOnCallback = false; |
| 48 |
|
| 49 |
$currentModal = this.maxm.currentModal; |
| 50 |
|
| 51 |
var button = $('[data-button="' + button_id + '"]').find('.shortcode-container'); |
| 52 |
|
| 53 |
options = $('<div class="shortcode_options">'); |
| 54 |
|
| 55 |
|
| 56 |
$('<input>', |
| 57 |
{ |
| 58 |
'type' : 'hidden', |
| 59 |
'id' : 'mb_shortcode_id', |
| 60 |
'name' : 'button_id', |
| 61 |
}).val(button_id).appendTo(options); |
| 62 |
|
| 63 |
$('<h3>').text('Shortcode Options').appendTo(options); |
| 64 |
|
| 65 |
$('<div class="button_example">').append(button).appendTo(options); |
| 66 |
|
| 67 |
$('<label>', { |
| 68 |
'for' : 'mb_shortcode_url', |
| 69 |
}).text(mbtrans.short_url_label).appendTo(options); |
| 70 |
|
| 71 |
|
| 72 |
$('<input>', { |
| 73 |
'type' : 'text', |
| 74 |
'id' : 'mb_shortcode_url', |
| 75 |
'name' : 'shortcode_url', |
| 76 |
'placeholder' : 'http://', |
| 77 |
}).on('change, keyup', |
| 78 |
function (e) { |
| 79 |
var url = $(e.target).val(); |
| 80 |
$('.button_example').find('.maxbutton').prop('href', url); |
| 81 |
}).appendTo(options); |
| 82 |
|
| 83 |
|
| 84 |
$('<label>', { |
| 85 |
'for' : 'mb_shortcode_text', |
| 86 |
}).text(mbtrans.short_text_label).appendTo(options); |
| 87 |
|
| 88 |
|
| 89 |
$('<input>', { |
| 90 |
'type' : 'text', |
| 91 |
'name' : 'shortcode_text', |
| 92 |
'id' : 'mb_shortcode_text', |
| 93 |
}).on('change, keyup', |
| 94 |
function (e) { |
| 95 |
var text = $(e.target).val(); |
| 96 |
$('.button_example').find('.mb-text').text(text); |
| 97 |
}).appendTo(options); |
| 98 |
|
| 99 |
$('<p>').text(mbtrans.short_options_explain).appendTo(options); |
| 100 |
|
| 101 |
$('<input>', { |
| 102 |
'type' : 'button', |
| 103 |
'name' : 'add_shortcode', |
| 104 |
'class' : 'button-primary', |
| 105 |
'value' : mbtrans.short_add_button, |
| 106 |
|
| 107 |
}).on('click', $.proxy(this.addShortcodeOptions, this)).appendTo(options); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
this.maxm.setContent( options ); |
| 112 |
this.maxm.checkResize(); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
maxMedia.prototype.addShortcodeOptions = function(e) |
| 117 |
{ |
| 118 |
e.preventDefault(); |
| 119 |
|
| 120 |
var url = $('#mb_shortcode_url').val(); |
| 121 |
var text = $('#mb_shortcode_text').val(); |
| 122 |
var button_id = $('#mb_shortcode_id').val(); |
| 123 |
|
| 124 |
this.buttonToEditor(button_id, url, text); |
| 125 |
|
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
maxMedia.prototype.clickAddButton = function (e) |
| 130 |
{ |
| 131 |
e.preventDefault(); |
| 132 |
e.stopPropagation(); |
| 133 |
$(document).off('click','.pagination span'); // prevent multiple events |
| 134 |
var self = this; |
| 135 |
|
| 136 |
if (typeof $(e.target).data('callback') !== 'undefined') |
| 137 |
{ |
| 138 |
this.setCallback($(e.target).data('callback')); |
| 139 |
} |
| 140 |
|
| 141 |
if (typeof $(e.target).data('parent') !== 'undefined') |
| 142 |
{ |
| 143 |
this.parent = $(e.target).data('parent'); |
| 144 |
} |
| 145 |
|
| 146 |
$(document).on('click', '.button-row', $.proxy(function (e) |
| 147 |
{ |
| 148 |
var target = $(e.target); |
| 149 |
|
| 150 |
if ( typeof $(target).data('button') === 'undefined') |
| 151 |
{ |
| 152 |
target = $(target).parents('.button-row'); |
| 153 |
} |
| 154 |
|
| 155 |
var button = $(target).data('button'); |
| 156 |
$('.button-row').removeClass('selected'); |
| 157 |
$(target).addClass('selected'); |
| 158 |
$('.controls .insert').data('button', button); |
| 159 |
this.maxm.currentModal.find('.controls .insert').removeClass('disabled'); |
| 160 |
},this)); |
| 161 |
|
| 162 |
$(document).on('click','.pagination span, .pagination-links a', function (e) // eventception |
| 163 |
{ |
| 164 |
e.preventDefault(); |
| 165 |
if ( $(e.target).hasClass('disabled')) |
| 166 |
return false; |
| 167 |
|
| 168 |
var page = $(e.target).data('page'); |
| 169 |
if (page <= 1) page = 1; |
| 170 |
|
| 171 |
self.loadPostEditScreen(page); |
| 172 |
}); |
| 173 |
$(document).on('change', '.input-paging', function (e) |
| 174 |
{ |
| 175 |
e.preventDefault(); |
| 176 |
var page = parseInt($(e.target).val()); |
| 177 |
self.loadPostEditScreen(page); |
| 178 |
}); |
| 179 |
|
| 180 |
this.loadPostEditScreen(0); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
// Callback is the add function on button select |
| 185 |
maxMedia.prototype.loadPostEditScreen = function(page) |
| 186 |
{ |
| 187 |
if (typeof page == 'undefined') |
| 188 |
page = 0; |
| 189 |
|
| 190 |
var data = { action: 'getAjaxButtons', |
| 191 |
paged : page, |
| 192 |
//callback: callback, |
| 193 |
}; |
| 194 |
var url = mbtrans.ajax_url; |
| 195 |
var self = this; |
| 196 |
|
| 197 |
// show load spinner if any |
| 198 |
$('.media-buttons .loading').css('visibility', 'visible'); |
| 199 |
|
| 200 |
$.ajax({ |
| 201 |
url: url, |
| 202 |
data: data, |
| 203 |
success: function (res) |
| 204 |
{ |
| 205 |
self.putResults(res) |
| 206 |
}, |
| 207 |
|
| 208 |
}); |
| 209 |
|
| 210 |
return false; |
| 211 |
} |
| 212 |
maxMedia.prototype.showPostEditScreen = function () |
| 213 |
{ |
| 214 |
|
| 215 |
this.maxm.parent = this.parent; |
| 216 |
this.maxm.newModal('media-buttons'); |
| 217 |
|
| 218 |
this.maxm.setTitle(mbtrans.windowtitle); |
| 219 |
|
| 220 |
$(document).trigger('mb_media_buttons_open', this.maxm); |
| 221 |
|
| 222 |
|
| 223 |
this.maxm.show(); |
| 224 |
this.window_loaded = true; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
maxMedia.prototype.putResults = function(res) |
| 229 |
{ |
| 230 |
|
| 231 |
this.showPostEditScreen(); |
| 232 |
$('.media-buttons .loading').css('visibility', 'hidden'); |
| 233 |
|
| 234 |
this.maxm.addControl('insert', '', $.proxy(this.insertAction, this) ); |
| 235 |
this.maxm.setContent(res); |
| 236 |
this.maxm.setControls(); |
| 237 |
this.maxm.checkResize(); |
| 238 |
|
| 239 |
this.resize(); |
| 240 |
|
| 241 |
// this feature resizes |
| 242 |
$(window).on('resize', $.proxy(this.resize, this)); |
| 243 |
|
| 244 |
// events |
| 245 |
$(document).on('click', ".maxbutton-preview", function(e) { e.preventDefault(); }); // prevent button clicks |
| 246 |
|
| 247 |
$(document).trigger('mb_media_put_results', [res, this.maxm] ); |
| 248 |
} |
| 249 |
|
| 250 |
/** Contains the -inner- window to force scrollbar if inner part is bigger. **/ |
| 251 |
maxMedia.prototype.resize = function(e) |
| 252 |
{ |
| 253 |
//contentHeight = this.maxm.currentModal.find('.modal_content').height(); |
| 254 |
|
| 255 |
if (this.maxm.currentModal === null) |
| 256 |
return; // nothing to resize |
| 257 |
|
| 258 |
topHeight = this.maxm.currentModal.find('.modal_header').height() + 17; // padding |
| 259 |
controlsHeight = this.maxm.currentModal.find('.controls').height() + 21; |
| 260 |
modalHeight = this.maxm.currentModal.height(); |
| 261 |
|
| 262 |
this.maxm.currentModal.find('.modal_content').css('height', modalHeight - topHeight - controlsHeight) |
| 263 |
this.maxm.currentModal.find('.controls .insert').addClass('disabled'); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
maxMedia.prototype.insertAction = function(e) |
| 268 |
{ |
| 269 |
e.preventDefault(); |
| 270 |
var button_id = $(e.target).data('button'); |
| 271 |
if (typeof button_id === 'undefined' || parseInt(button_id) <= 0) |
| 272 |
return; // no button yet. |
| 273 |
|
| 274 |
if (typeof this.callback == 'function') |
| 275 |
this.callback(button_id, $(e.target) ); |
| 276 |
|
| 277 |
if (this.closeOnCallback) |
| 278 |
{ |
| 279 |
this.maxm.close(); |
| 280 |
$(document).trigger('mb_media_buttons_close'); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
maxMedia.prototype.buttonToEditor = function(button_id, url, text) |
| 285 |
{ |
| 286 |
var shortcode = '[maxbutton id="' + button_id + '"'; |
| 287 |
|
| 288 |
if (typeof url !== 'undefined' && url.length > 1) |
| 289 |
shortcode += ' url="' + url + '"'; |
| 290 |
|
| 291 |
if (typeof text !== 'undefined' && text.length > 1) |
| 292 |
shortcode += ' text="' + text + '"'; |
| 293 |
|
| 294 |
shortcode += ' ] '; |
| 295 |
window.send_to_editor(shortcode); |
| 296 |
this.maxm.close(); |
| 297 |
} |
| 298 |
|
| 299 |
maxMedia.prototype.getEditor = function () |
| 300 |
{ |
| 301 |
|
| 302 |
var h2style = 'line-height: 32px; padding-left: 40px; background: url("' + mbtrans.icon + '") no-repeat'; |
| 303 |
var cancelstyle = 'margin-left: 10px; margin-top: 10px;'; |
| 304 |
var editor = $('<div>', { id: 'maxbutton-add-button', class: 'content' }); |
| 305 |
|
| 306 |
//.append( $('<a>', { 'class' : 'button-secondary', 'style' : cancelstyle }).text(mbtrans.cancel) |
| 307 |
editor.append( $('<h2>', { 'style' : h2style } ).text(mbtrans.insert) ) |
| 308 |
.append( $('<p>').text(mbtrans.select) ) |
| 309 |
.append( $('<div>', { id: 'mb_media_buttons' }).append( '<div class="loading"></div>' ) |
| 310 |
|
| 311 |
); |
| 312 |
|
| 313 |
return editor; |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
maxMedia = new maxMedia(); |
| 318 |
maxMedia.init(); |
| 319 |
window.maxMedia = maxMedia; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
}); // jquery |
| 324 |
|