| 1 |
var maxModal; |
| 2 |
|
| 3 |
|
| 4 |
jQuery(document).ready(function($) { |
| 5 |
maxModal = function () { |
| 6 |
|
| 7 |
} |
| 8 |
|
| 9 |
maxModal.prototype = { |
| 10 |
currentModal: null, |
| 11 |
modals: [], |
| 12 |
controls: [], |
| 13 |
parent: '#maxbuttons', // modal will be written to this element. |
| 14 |
multiple: false, |
| 15 |
windowHeight: false, |
| 16 |
windowWidth: false, |
| 17 |
setWidth: false, |
| 18 |
setHeight: false, |
| 19 |
target: false, |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
maxModal.prototype.init = function() |
| 24 |
{ |
| 25 |
this.windowHeight = $(window).height(); |
| 26 |
this.windowWidth = $(window).width(); |
| 27 |
|
| 28 |
$(document).off('click', '.maxmodal'); // should be on next update |
| 29 |
$(document).on('click', '.maxmodal', $.proxy(this.buildModal, this)); |
| 30 |
$(window).on('resize', $.proxy(this.checkResize, this)); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
maxModal.prototype.focus = function() |
| 35 |
{ |
| 36 |
this.currentModal.show(); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
maxModal.prototype.get = function() |
| 41 |
{ |
| 42 |
return this.currentModal; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
maxModal.prototype.show = function() |
| 47 |
{ |
| 48 |
$('.maxmodal_overlay').remove(); |
| 49 |
$('body').removeClass('max-modal-active'); |
| 50 |
this.writeOverlay(); |
| 51 |
|
| 52 |
if (this.setWidth) |
| 53 |
{ |
| 54 |
this.currentModal.width(this.setWidth); |
| 55 |
} |
| 56 |
if (this.setHeight) |
| 57 |
{ |
| 58 |
this.currentModal.height(this.setHeight); |
| 59 |
} |
| 60 |
|
| 61 |
var modalHeight = this.currentModal.height(); |
| 62 |
var modalWidth = this.currentModal.width(); |
| 63 |
|
| 64 |
var top = (this.windowHeight - modalHeight) / 2; |
| 65 |
var left = (this.windowWidth - modalWidth) / 2; |
| 66 |
|
| 67 |
if (top < 30) |
| 68 |
{ |
| 69 |
top = 30; // top + admin bar |
| 70 |
} |
| 71 |
if (left < 0) |
| 72 |
{ |
| 73 |
left: 0; |
| 74 |
} |
| 75 |
|
| 76 |
if (modalHeight > this.windowHeight) |
| 77 |
this.currentModal.height(this.windowHeight - top - 5 + 'px'); |
| 78 |
|
| 79 |
this.currentModal.css('left', left + 'px'); |
| 80 |
this.currentModal.css('top', top + 'px'); |
| 81 |
this.currentModal.css('height', modalHeight); |
| 82 |
|
| 83 |
this.currentModal.show(); |
| 84 |
|
| 85 |
$('.maxmodal_overlay').show(); |
| 86 |
$('body').addClass('max-modal-active'); |
| 87 |
|
| 88 |
$(document).off('keydown', $.proxy(this.keyPressHandler, this)); |
| 89 |
$(document).on('keydown', $.proxy(this.keyPressHandler, this)); |
| 90 |
|
| 91 |
this.currentModal.focus(); |
| 92 |
} |
| 93 |
|
| 94 |
maxModal.prototype.keyPressHandler = function (e) |
| 95 |
{ |
| 96 |
if (e.keyCode === 27) |
| 97 |
this.close(); |
| 98 |
} |
| 99 |
|
| 100 |
maxModal.prototype.checkResize = function () |
| 101 |
{ |
| 102 |
this.windowHeight = $(window).height(); |
| 103 |
this.windowWidth = $(window).width(); |
| 104 |
|
| 105 |
if (this.currentModal === null) |
| 106 |
return; |
| 107 |
|
| 108 |
this.currentModal.removeAttr('style'); |
| 109 |
this.currentModal.find('.modal_content').removeAttr('style'); |
| 110 |
// redo sizes, repaint. |
| 111 |
|
| 112 |
this.show(); |
| 113 |
} |
| 114 |
|
| 115 |
maxModal.prototype.close = function() |
| 116 |
{ |
| 117 |
this.currentModal.trigger('modal_close', [this]); |
| 118 |
this.currentModal.remove(); |
| 119 |
this.currentModal = null; |
| 120 |
$('.maxmodal_overlay').remove(); |
| 121 |
$('body').removeClass('max-modal-active'); |
| 122 |
$(document).off('keydown', $.proxy(this.keyPressHandler, this)); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
maxModal.prototype.fadeOut = function (timeOut) |
| 127 |
{ |
| 128 |
if (typeof timeOut == undefined) |
| 129 |
timeOut = 600; |
| 130 |
|
| 131 |
var self = this; |
| 132 |
this.currentModal.fadeOut(timeOut, function() { self.close(); } ); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
maxModal.prototype.setTitle = function(title) |
| 137 |
{ |
| 138 |
this.currentModal.find('.modal_title').text(title); |
| 139 |
} |
| 140 |
|
| 141 |
maxModal.prototype.setControls = function(controls) |
| 142 |
{ |
| 143 |
var content = this.currentModal.find('.modal_content'); |
| 144 |
var controldiv = $('<div class="controls">'); |
| 145 |
|
| 146 |
for(i =0; i < this.controls.length; i++) |
| 147 |
controldiv.append(this.controls[i]); |
| 148 |
|
| 149 |
if (typeof controls !== 'undefined') |
| 150 |
controldiv.append(controls); |
| 151 |
|
| 152 |
content.append(controldiv); |
| 153 |
|
| 154 |
// general close button |
| 155 |
$(this.currentModal).find('.modal_close').off('click'); |
| 156 |
$(this.currentModal).find('.modal_close').on('click', $.proxy(this.close, this)); |
| 157 |
} |
| 158 |
|
| 159 |
maxModal.prototype.addControl = function (type, data, handler) |
| 160 |
{ |
| 161 |
var text = ''; |
| 162 |
|
| 163 |
switch(type) |
| 164 |
{ |
| 165 |
case 'yes': |
| 166 |
text = modaltext.yes; |
| 167 |
break; |
| 168 |
case 'ok': |
| 169 |
text = modaltext.ok; |
| 170 |
break; |
| 171 |
case 'no': |
| 172 |
text = modaltext.no; |
| 173 |
break; |
| 174 |
case 'cancel': |
| 175 |
text = modaltext.cancel; |
| 176 |
break; |
| 177 |
case 'insert': |
| 178 |
text = mbtrans.insert; // used for mediabutton |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
var control = $('<a class="button-primary ' + type + '">' + text + '</a>'); |
| 183 |
control.on('click', data, handler ); |
| 184 |
this.controls.push(control); |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/* Set the modal content |
| 190 |
|
| 191 |
Sets the content of the modal. Do not run this function after adding controls. |
| 192 |
@param string HTML,text content of the modal |
| 193 |
*/ |
| 194 |
maxModal.prototype.setContent = function(content) |
| 195 |
{ |
| 196 |
this.currentModal.find('.modal_content').html(content); |
| 197 |
} |
| 198 |
|
| 199 |
/* Builds modal from hidden data |
| 200 |
|
| 201 |
Builds modal from an formatted data object in DOM. Triggered on Click |
| 202 |
|
| 203 |
*/ |
| 204 |
maxModal.prototype.buildModal = function(e) |
| 205 |
{ |
| 206 |
e.preventDefault(); |
| 207 |
|
| 208 |
var target = $(e.target); |
| 209 |
if (typeof target.data('modal') == 'undefined') |
| 210 |
target = target.parents('.maxmodal'); |
| 211 |
|
| 212 |
this.target = target; |
| 213 |
var id = target.data('modal'); |
| 214 |
var data = $('#' + id); |
| 215 |
|
| 216 |
// options |
| 217 |
if (typeof data.data('width') !== 'undefined') |
| 218 |
this.setWidth = data.data('width'); |
| 219 |
else |
| 220 |
this.setWidth = false; |
| 221 |
|
| 222 |
if (typeof data.data('height') !== 'undefined') |
| 223 |
this.setHeight = data.data('height'); |
| 224 |
else |
| 225 |
this.setHeight = false; |
| 226 |
|
| 227 |
var title = $(data).find('.title').text(); |
| 228 |
var controls = $(data).find('.controls').html(); |
| 229 |
var content = $(data).find('.content').html(); |
| 230 |
|
| 231 |
this.newModal(id); |
| 232 |
this.setTitle(title); |
| 233 |
this.setContent(content); |
| 234 |
this.setControls(controls); |
| 235 |
|
| 236 |
// callback on init |
| 237 |
if (typeof $(data).data('load') !== 'undefined') |
| 238 |
{ |
| 239 |
|
| 240 |
// default call |
| 241 |
var funcName = data.data('load') + '(modal)'; |
| 242 |
var callFunc = new Function ('modal', funcName); |
| 243 |
|
| 244 |
|
| 245 |
/* Args coming! |
| 246 |
if (typeof(data.data('load-args') !== 'undefined') |
| 247 |
{ |
| 248 |
var args = data.data('load-args').split(','); |
| 249 |
|
| 250 |
for(i=0; i< args.length; i++) |
| 251 |
{ |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
*/ |
| 256 |
|
| 257 |
try |
| 258 |
{ |
| 259 |
callFunc(this); |
| 260 |
} |
| 261 |
catch(err) |
| 262 |
{ |
| 263 |
|
| 264 |
console.log('MB Modal Callback Error: ' + err.message); |
| 265 |
console.log('MB Mobdal tried calling: ' + funcName); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
this.show(); |
| 270 |
} |
| 271 |
|
| 272 |
maxModal.prototype.newModal = function(id) |
| 273 |
{ |
| 274 |
|
| 275 |
if (this.currentModal !== null) |
| 276 |
this.close(); |
| 277 |
|
| 278 |
var modal = $('<div class="max-modal ' + id + '" > \ |
| 279 |
<div class="modal_header"> \ |
| 280 |
<div class="modal_close dashicons dashicons-no"></div><h3 class="modal_title"></h3> \ |
| 281 |
</div> \ |
| 282 |
<div class="inner modal_content"></div>\ |
| 283 |
</div>'); |
| 284 |
if ($(this.parent).length > 0) |
| 285 |
$(this.parent).append(modal); |
| 286 |
else |
| 287 |
$('body').append(modal); // fallback in case of interrupting page builders |
| 288 |
|
| 289 |
$(modal).draggable({ |
| 290 |
handle: '.modal_header' |
| 291 |
}); |
| 292 |
|
| 293 |
this.modals.push(modal); |
| 294 |
this.currentModal = modal; |
| 295 |
this.controls = []; |
| 296 |
return this; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
maxModal.prototype.writeOverlay = function() |
| 301 |
{ |
| 302 |
|
| 303 |
$(this.parent).append('<div class="maxmodal_overlay"></div>'); |
| 304 |
$('.maxmodal_overlay').on('click', $.proxy(this.close, this)); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
}); |
| 309 |
|