-
Notifications
You must be signed in to change notification settings - Fork 35
/
Backbone.ModalDialog.js
387 lines (337 loc) · 14 KB
/
Backbone.ModalDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Backbone.ModalDialog.js v0.3.2
//
// Copyright (C)2012 Gareth Elms
// Distributed under MIT License
//
// Documentation and full license availabe at:
// https://github.com/GarethElms/BackboneJSModalView
Backbone.ModalView =
Backbone.View.extend(
{
name: "ModalView",
modalBlanket: null,
modalContainer: null,
defaultOptions:
{
fadeInDuration:150,
fadeOutDuration:150,
showCloseButton:true,
bodyOverflowHidden:false,
setFocusOnFirstFormControl:true,
targetContainer: document.body,
slideFromAbove: false,
slideFromBelow: false,
slideDistance: 150,
closeImageUrl: "close-modal.png",
closeImageHoverUrl: "close-modal-hover.png",
showModalAtScrollPosition: true,
permanentlyVisible: false,
backgroundClickClosesModal: true,
pressingEscapeClosesModal: true,
css:
{
"border": "2px solid #111",
"background-color": "#fff",
"-webkit-box-shadow": "0px 0px 15px 4px rgba(0, 0, 0, 0.5)",
"-moz-box-shadow": "0px 0px 15px 4px rgba(0, 0, 0, 0.5)",
"box-shadow": "0px 0px 15px 4px rgba(0, 0, 0, 0.5)",
"-webkit-border-radius": "10px",
"-moz-border-radius": "10px",
"border-radius": "10px"
}
},
initialize:
function()
{
},
events:
{
},
showModalBlanket:
function()
{
return this.ensureModalBlanket().fadeIn( this.options.fadeInDuration);
},
hideModalBlanket:
function()
{
return this.modalBlanket.fadeOut( this.options.fadeOutDuration);
},
ensureModalContainer:
function( target)
{
if( target != null)
{
// A target is passed in, we need to re-render the modal container into the target.
if( this.modalContainer != null)
{
this.modalContainer.remove();
this.modalContainer = null;
}
}
if( this.modalContainer == null)
{
this.modalContainer =
$("<div id='modalContainer'>")
.css({
"z-index":"99999",
"position":"relative",
"-webkit-border-radius": "6px",
"-moz-border-radius": "6px",
"border-radius": "6px"
})
.appendTo( target);
}
return this.modalContainer;
},
ensureModalBlanket:
function()
{
this.modalBlanket = $("#modal-blanket");
if( this.modalBlanket.length == 0)
{
this.modalBlanket =
$("<div id='modal-blanket'>")
.css(
{
position: "absolute",
top: 0,
left: 0,
height: $(document).height(), // Span the full document height...
width: "100%", // ...and full width
opacity: 0.5, // Make it slightly transparent
backgroundColor: "#000",
"z-index": 99900
})
.appendTo( document.body)
.hide();
}
else
{
// Ensure the blanket spans the whole document, screen may have been updated.
this.modalBlanket.css(
{
height: $(document).height(), // Span the full document height...
width: "100%" // ...and full width
});
}
return this.modalBlanket;
},
keyup:
function( event)
{
if( event.keyCode == 27 && this.options.pressingEscapeClosesModal)
{
this.hideModal();
}
},
click:
function( event)
{
if( event.target.id == "modal-blanket" && this.options.backgroundClickClosesModal)
{
this.hideModal();
}
},
setFocusOnFirstFormControl:
function()
{
var controls = $("input, select, email, url, number, range, date, month, week, time, datetime, datetime-local, search, color", $(this.el));
if( controls.length > 0)
{
$(controls[0]).focus();
}
},
hideModal:
function()
{
this.trigger( "closeModalWindow");
this.hideModalBlanket();
$(document.body).unbind( "keyup", this.keyup);
this.modalBlanket.unbind( "click", this.click);
if( this.options.bodyOverflowHidden === true)
{
$(document.body).css( "overflow", this.originalBodyOverflowValue);
}
var container = this.modalContainer;
$(this.modalContainer)
.fadeOut(
this.options.fadeOutDuration,
function()
{
container.remove();
});
},
getCoordinate:
function( coordinate, css)
{
if( typeof( css[coordinate]) !== "undefined")
{
var value = css[coordinate];
delete css[coordinate]; // Don't apply positioning to the $el, we apply it to the modal container. Remove it from options.css
return value;
}
},
recenter:
function()
{
return this.recentre();
},
recentre: // Re-centre the modal dialog after it has been displayed. Useful if the height changes after initial rendering eg; jquery ui tabs will hide tab sections
function()
{
var $el = $(this.el);
var coords = {
top: this.getCoordinate( "top", this.options.css),
left: this.getCoordinate( "left", this.options.css),
right: this.getCoordinate( "right", this.options.css),
bottom: this.getCoordinate( "bottom", this.options.css),
isEmpty: function(){return (this.top == null && this.left == null && this.right == null && this.bottom == null);}
};
var offsets = this.getOffsets();
var centreY = $(window).height() / 2;
var centreX = $(window).width() / 2;
var modalContainer = this.modalContainer;
var positionY = centreY - ($el.outerHeight() / 2);
modalContainer.css({"top": (positionY + offsets.y) + "px"});
var positionX = centreX - ($el.outerWidth() / 2);
modalContainer.css({"left": (positionX + offsets.x) + "px"});
return this;
},
getOffsets:
function()
{
var offsetY = 0, offsetX = 0;
if( this.options.showModalAtScrollPosition)
{
offsetY = $(document).scrollTop(),
offsetX = $(document).scrollLeft()
}
return {x:offsetX, y:offsetY};
},
showModal:
function( options)
{
this.defaultOptions.targetContainer = document.body;
this.options = $.extend( true, {}, this.defaultOptions, options, this.options);
if( this.options.permanentlyVisible)
{
this.options.showCloseButton = false;
this.options.backgroundClickClosesModal = false;
this.options.pressingEscapeClosesModal = false;
}
//Set the center alignment padding + border see css style
var $el = $(this.el);
var centreY = $(window).height() / 2;
var centreX = $(window).width() / 2;
var modalContainer = this.ensureModalContainer( this.options.targetContainer).empty();
$el.addClass( "modal");
var coords = {
top: this.getCoordinate( "top", this.options.css),
left: this.getCoordinate( "left", this.options.css),
right: this.getCoordinate( "right", this.options.css),
bottom: this.getCoordinate( "bottom", this.options.css),
isEmpty: function(){return (this.top == null && this.left == null && this.right == null && this.bottom == null);}
};
$el.css( this.options.css);
this.showModalBlanket();
this.keyup = _.bind( this.keyup, this);
this.click = _.bind( this.click, this);
$(document.body).keyup( this.keyup); // This handler is unbound in hideModal()
this.modalBlanket.click( this.click); // This handler is unbound in hideModal()
if( this.options.bodyOverflowHidden === true)
{
this.originalBodyOverflowValue = $(document.body).css( "overflow");
$(document.body).css( "overflow", "hidden");
}
modalContainer
.append( $el);
modalContainer.css({
"opacity": 0,
"position": "absolute",
"z-index": 999999});
var offsets = this.getOffsets();
// Only apply default centre coordinates if no css positions have been supplied
if( coords.isEmpty())
{
var positionY = centreY - ($el.outerHeight() / 2);
if( positionY < 10) positionY = 10;
// Overriding the coordinates with explicit values if they are passed in
if( typeof( this.options.y) !== "undefined")
{
positionY = this.options.y;
}
else
{
positionY += offsets.y;
}
modalContainer.css({"top": positionY + "px"});
var positionX = centreX - ($el.outerWidth() / 2);
// Overriding the coordinates with explicit values if they are passed in
if( typeof( this.options.x) !== "undefined")
{
positionX = this.options.x;
}
else
{
positionX += offsets.x;
}
modalContainer.css({"left": positionX + "px"});
}
else
{
if( coords.top != null) modalContainer.css({"top": coords.top + offsets.y});
if( coords.left != null) modalContainer.css({"left": coords.left + offsets.x});
if( coords.right != null) modalContainer.css({"right": coords.right});
if( coords.bottom != null) modalContainer.css({"bottom": coords.bottom});
}
if( this.options.setFocusOnFirstFormControl)
{
this.setFocusOnFirstFormControl();
}
if( this.options.showCloseButton)
{
var view = this;
var image =
$("<a href='#' id='modalCloseButton'> </a>")
.css({
"position":"absolute",
"top":"-10px",
"right":"-10px",
"width":"32px",
"height":"32px",
"background":"transparent url(" + view.options.closeImageUrl + ") top left no-repeat",
"text-decoration":"none"})
.appendTo( this.modalContainer)
.hover(
function()
{
$(this).css( "background-image", "url(" + view.options.closeImageHoverUrl + ") !important");
},
function()
{
$(this).css( "background-image", "url(" + view.options.closeImageUrl + ") !important");
})
.click(
function( event)
{
event.preventDefault();
view.hideModal();
});
}
var animateProperties = {opacity:1};
var modalOffset = modalContainer.offset();
if( this.options.slideFromAbove)
{
modalContainer.css({"top": (modalOffset.top - this.options.slideDistance) + "px"});
animateProperties.top = coords.top;
}
if( this.options.slideFromBelow)
{
modalContainer.css({"top": (modalOffset.top + this.options.slideDistance) + "px"});
animateProperties.top = coords.top;
}
this.modalContainer.animate( animateProperties, this.options.fadeInDuration);
return this;
}
});