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