wrgem 0.0.43 → 0.0.44
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/wradmin/template/carrierwave.rb +15 -0
- data/lib/generators/wradmin/template/migrate/20140715192304_create_user.rb +5 -3
- data/lib/generators/wradmin/template/public/images/spritemap.png +0 -0
- data/lib/generators/wradmin/template/public/images/spritemap@2x.png +0 -0
- data/lib/generators/wradmin/template/views/layouts/admin.html.slim +1 -1
- data/lib/generators/wradmin/wradmin_generator.rb +3 -0
- data/lib/generators/wrstart/template/index.html.slim +1 -1
- data/lib/generators/wrstart/template/javascripts/requirements.js.coffee +5 -0
- data/lib/generators/wrstart/template/shared/_head.slim +1 -1
- data/lib/generators/wrstart/template/stylesheets/_requirements.sass +6 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/.bower.json +32 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/.editorconfig +11 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/.gitignore +5 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/LICENSE +22 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/README.md +94 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/bower.json +18 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/example.css +322 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/example.scss +412 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/logo_big.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/logo_big@2x.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/logo_small.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/logo_small@2x.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/te-logo-small.svg +12 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/thumbs-up.jpg +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/vs_icon.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/images/vs_icon@2x.png +0 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/example/index.html +433 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/gulpfile.js +48 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/ie9.css +23 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/sweet-alert.css +1 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/sweet-alert.html +37 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/sweet-alert.js +818 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/sweet-alert.min.js +1 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/lib/sweet-alert.scss +444 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/package.json +37 -0
- data/lib/generators/wrstart/template/vendor/assets/components/sweetalert/sweetalert.gif +0 -0
- data/lib/generators/wrstart/wrstart_generator.rb +2 -0
- data/lib/wrgem/version.rb +1 -1
- data/wrgem.gemspec +2 -2
- metadata +39 -12
@@ -0,0 +1,818 @@
|
|
1
|
+
// SweetAlert
|
2
|
+
// 2014 (c) - Tristan Edwards
|
3
|
+
// github.com/t4t5/sweetalert
|
4
|
+
;(function(window, document, undefined) {
|
5
|
+
|
6
|
+
var modalClass = '.sweet-alert',
|
7
|
+
overlayClass = '.sweet-overlay',
|
8
|
+
alertTypes = ['error', 'warning', 'info', 'success'],
|
9
|
+
defaultParams = {
|
10
|
+
title: '',
|
11
|
+
text: '',
|
12
|
+
type: null,
|
13
|
+
allowOutsideClick: false,
|
14
|
+
showCancelButton: false,
|
15
|
+
closeOnConfirm: true,
|
16
|
+
closeOnCancel: true,
|
17
|
+
confirmButtonText: 'OK',
|
18
|
+
confirmButtonColor: '#AEDEF4',
|
19
|
+
cancelButtonText: 'Cancel',
|
20
|
+
imageUrl: null,
|
21
|
+
imageSize: null,
|
22
|
+
timer: null,
|
23
|
+
customClass: '',
|
24
|
+
html: false,
|
25
|
+
animation: true,
|
26
|
+
allowEscapeKey: true
|
27
|
+
};
|
28
|
+
|
29
|
+
|
30
|
+
/*
|
31
|
+
* Manipulate DOM
|
32
|
+
*/
|
33
|
+
|
34
|
+
var getModal = function() {
|
35
|
+
var $modal = document.querySelector(modalClass);
|
36
|
+
|
37
|
+
if (!$modal) {
|
38
|
+
sweetAlertInitialize();
|
39
|
+
$modal = getModal();
|
40
|
+
}
|
41
|
+
|
42
|
+
return $modal;
|
43
|
+
},
|
44
|
+
getOverlay = function() {
|
45
|
+
return document.querySelector(overlayClass);
|
46
|
+
},
|
47
|
+
hasClass = function(elem, className) {
|
48
|
+
return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
|
49
|
+
},
|
50
|
+
addClass = function(elem, className) {
|
51
|
+
if (!hasClass(elem, className)) {
|
52
|
+
elem.className += ' ' + className;
|
53
|
+
}
|
54
|
+
},
|
55
|
+
removeClass = function(elem, className) {
|
56
|
+
var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
|
57
|
+
if (hasClass(elem, className)) {
|
58
|
+
while (newClass.indexOf(' ' + className + ' ') >= 0) {
|
59
|
+
newClass = newClass.replace(' ' + className + ' ', ' ');
|
60
|
+
}
|
61
|
+
elem.className = newClass.replace(/^\s+|\s+$/g, '');
|
62
|
+
}
|
63
|
+
},
|
64
|
+
escapeHtml = function(str) {
|
65
|
+
var div = document.createElement('div');
|
66
|
+
div.appendChild(document.createTextNode(str));
|
67
|
+
return div.innerHTML;
|
68
|
+
},
|
69
|
+
_show = function(elem) {
|
70
|
+
elem.style.opacity = '';
|
71
|
+
elem.style.display = 'block';
|
72
|
+
},
|
73
|
+
show = function(elems) {
|
74
|
+
if (elems && !elems.length) {
|
75
|
+
return _show(elems);
|
76
|
+
}
|
77
|
+
for (var i = 0; i < elems.length; ++i) {
|
78
|
+
_show(elems[i]);
|
79
|
+
}
|
80
|
+
},
|
81
|
+
_hide = function(elem) {
|
82
|
+
elem.style.opacity = '';
|
83
|
+
elem.style.display = 'none';
|
84
|
+
},
|
85
|
+
hide = function(elems) {
|
86
|
+
if (elems && !elems.length) {
|
87
|
+
return _hide(elems);
|
88
|
+
}
|
89
|
+
for (var i = 0; i < elems.length; ++i) {
|
90
|
+
_hide(elems[i]);
|
91
|
+
}
|
92
|
+
},
|
93
|
+
isDescendant = function(parent, child) {
|
94
|
+
var node = child.parentNode;
|
95
|
+
while (node !== null) {
|
96
|
+
if (node === parent) {
|
97
|
+
return true;
|
98
|
+
}
|
99
|
+
node = node.parentNode;
|
100
|
+
}
|
101
|
+
return false;
|
102
|
+
},
|
103
|
+
getTopMargin = function(elem) {
|
104
|
+
elem.style.left = '-9999px';
|
105
|
+
elem.style.display = 'block';
|
106
|
+
|
107
|
+
var height = elem.clientHeight,
|
108
|
+
padding;
|
109
|
+
if (typeof getComputedStyle !== "undefined") { /* IE 8 */
|
110
|
+
padding = parseInt(getComputedStyle(elem).getPropertyValue('padding'), 10);
|
111
|
+
} else {
|
112
|
+
padding = parseInt(elem.currentStyle.padding);
|
113
|
+
}
|
114
|
+
|
115
|
+
elem.style.left = '';
|
116
|
+
elem.style.display = 'none';
|
117
|
+
return ('-' + parseInt(height / 2 + padding) + 'px');
|
118
|
+
},
|
119
|
+
fadeIn = function(elem, interval) {
|
120
|
+
if (+elem.style.opacity < 1) {
|
121
|
+
interval = interval || 16;
|
122
|
+
elem.style.opacity = 0;
|
123
|
+
elem.style.display = 'block';
|
124
|
+
var last = +new Date();
|
125
|
+
var tick = function() {
|
126
|
+
elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100;
|
127
|
+
last = +new Date();
|
128
|
+
|
129
|
+
if (+elem.style.opacity < 1) {
|
130
|
+
setTimeout(tick, interval);
|
131
|
+
}
|
132
|
+
};
|
133
|
+
tick();
|
134
|
+
}
|
135
|
+
elem.style.display = 'block'; //fallback IE8
|
136
|
+
},
|
137
|
+
fadeOut = function(elem, interval) {
|
138
|
+
interval = interval || 16;
|
139
|
+
elem.style.opacity = 1;
|
140
|
+
var last = +new Date();
|
141
|
+
var tick = function() {
|
142
|
+
elem.style.opacity = +elem.style.opacity - (new Date() - last) / 100;
|
143
|
+
last = +new Date();
|
144
|
+
|
145
|
+
if (+elem.style.opacity > 0) {
|
146
|
+
setTimeout(tick, interval);
|
147
|
+
} else {
|
148
|
+
elem.style.display = 'none';
|
149
|
+
}
|
150
|
+
};
|
151
|
+
tick();
|
152
|
+
},
|
153
|
+
fireClick = function(node) {
|
154
|
+
// Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
|
155
|
+
// Then fixed for today's Chrome browser.
|
156
|
+
if (typeof MouseEvent === 'function') {
|
157
|
+
// Up-to-date approach
|
158
|
+
var mevt = new MouseEvent('click', {
|
159
|
+
view: window,
|
160
|
+
bubbles: false,
|
161
|
+
cancelable: true
|
162
|
+
});
|
163
|
+
node.dispatchEvent(mevt);
|
164
|
+
} else if ( document.createEvent ) {
|
165
|
+
// Fallback
|
166
|
+
var evt = document.createEvent('MouseEvents');
|
167
|
+
evt.initEvent('click', false, false);
|
168
|
+
node.dispatchEvent(evt);
|
169
|
+
} else if( document.createEventObject ) {
|
170
|
+
node.fireEvent('onclick') ;
|
171
|
+
} else if (typeof node.onclick === 'function' ) {
|
172
|
+
node.onclick();
|
173
|
+
}
|
174
|
+
},
|
175
|
+
stopEventPropagation = function(e) {
|
176
|
+
// In particular, make sure the space bar doesn't scroll the main window.
|
177
|
+
if (typeof e.stopPropagation === 'function') {
|
178
|
+
e.stopPropagation();
|
179
|
+
e.preventDefault();
|
180
|
+
} else if (window.event && window.event.hasOwnProperty('cancelBubble')) {
|
181
|
+
window.event.cancelBubble = true;
|
182
|
+
}
|
183
|
+
};
|
184
|
+
|
185
|
+
// Remember state in cases where opening and handling a modal will fiddle with it.
|
186
|
+
var previousActiveElement,
|
187
|
+
previousDocumentClick,
|
188
|
+
previousWindowKeyDown,
|
189
|
+
lastFocusedButton;
|
190
|
+
|
191
|
+
|
192
|
+
/*
|
193
|
+
* Add modal + overlay to DOM
|
194
|
+
*/
|
195
|
+
|
196
|
+
window.sweetAlertInitialize = function() {
|
197
|
+
var sweetHTML = '<div class="sweet-overlay" tabIndex="-1"></div><div class="sweet-alert" tabIndex="-1"><div class="icon error"><span class="x-mark"><span class="line left"></span><span class="line right"></span></span></div><div class="icon warning"> <span class="body"></span> <span class="dot"></span> </div> <div class="icon info"></div> <div class="icon success"> <span class="line tip"></span> <span class="line long"></span> <div class="placeholder"></div> <div class="fix"></div> </div> <div class="icon custom"></div> <h2>Title</h2><p>Text</p><button class="cancel" tabIndex="2">Cancel</button><button class="confirm" tabIndex="1">OK</button></div>',
|
198
|
+
sweetWrap = document.createElement('div');
|
199
|
+
|
200
|
+
sweetWrap.innerHTML = sweetHTML;
|
201
|
+
|
202
|
+
// Append elements to body
|
203
|
+
while (sweetWrap.firstChild) {
|
204
|
+
document.body.appendChild(sweetWrap.firstChild);
|
205
|
+
}
|
206
|
+
};
|
207
|
+
|
208
|
+
|
209
|
+
/*
|
210
|
+
* Global sweetAlert function
|
211
|
+
*/
|
212
|
+
|
213
|
+
window.sweetAlert = window.swal = function() {
|
214
|
+
// Copy arguments to the local args variable
|
215
|
+
var args = arguments;
|
216
|
+
if (getModal() !== null) {
|
217
|
+
// If getModal returns values then continue
|
218
|
+
modalDependant.apply(this, args);
|
219
|
+
} else {
|
220
|
+
// If getModal returns null i.e. no matches, then set up a interval event to check the return value until it is not null
|
221
|
+
var modalCheckInterval = setInterval(function() {
|
222
|
+
if (getModal() !== null) {
|
223
|
+
clearInterval(modalCheckInterval);
|
224
|
+
modalDependant.apply(this, args);
|
225
|
+
}
|
226
|
+
}, 100);
|
227
|
+
}
|
228
|
+
};
|
229
|
+
|
230
|
+
function modalDependant() {
|
231
|
+
|
232
|
+
var customizations = arguments[0];
|
233
|
+
|
234
|
+
/*
|
235
|
+
* Use argument if defined or default value from params object otherwise.
|
236
|
+
* Supports the case where a default value is boolean true and should be
|
237
|
+
* overridden by a corresponding explicit argument which is boolean false.
|
238
|
+
*/
|
239
|
+
function argumentOrDefault(key) {
|
240
|
+
var args = customizations;
|
241
|
+
|
242
|
+
if (typeof args[key] !== 'undefined') {
|
243
|
+
return args[key];
|
244
|
+
} else {
|
245
|
+
return defaultParams[key];
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
if (arguments[0] === undefined) {
|
250
|
+
logStr('SweetAlert expects at least 1 attribute!');
|
251
|
+
return false;
|
252
|
+
}
|
253
|
+
|
254
|
+
var params = extend({}, defaultParams);
|
255
|
+
|
256
|
+
switch (typeof arguments[0]) {
|
257
|
+
|
258
|
+
// Ex: swal("Hello", "Just testing", "info");
|
259
|
+
case 'string':
|
260
|
+
params.title = arguments[0];
|
261
|
+
params.text = arguments[1] || '';
|
262
|
+
params.type = arguments[2] || '';
|
263
|
+
|
264
|
+
break;
|
265
|
+
|
266
|
+
// Ex: swal({title:"Hello", text: "Just testing", type: "info"});
|
267
|
+
case 'object':
|
268
|
+
if (arguments[0].title === undefined) {
|
269
|
+
logStr('Missing "title" argument!');
|
270
|
+
return false;
|
271
|
+
}
|
272
|
+
|
273
|
+
params.title = arguments[0].title;
|
274
|
+
|
275
|
+
var availableCustoms = [
|
276
|
+
'text',
|
277
|
+
'type',
|
278
|
+
'customClass',
|
279
|
+
'allowOutsideClick',
|
280
|
+
'showCancelButton',
|
281
|
+
'closeOnConfirm',
|
282
|
+
'closeOnCancel',
|
283
|
+
'timer',
|
284
|
+
'confirmButtonColor',
|
285
|
+
'cancelButtonText',
|
286
|
+
'imageUrl',
|
287
|
+
'imageSize',
|
288
|
+
'html',
|
289
|
+
'animation',
|
290
|
+
'allowEscapeKey'];
|
291
|
+
|
292
|
+
// It would be nice to just use .forEach here, but IE8... :(
|
293
|
+
var numCustoms = availableCustoms.length;
|
294
|
+
for (var customIndex = 0; customIndex < numCustoms; customIndex++) {
|
295
|
+
var customName = availableCustoms[customIndex];
|
296
|
+
params[customName] = argumentOrDefault(customName);
|
297
|
+
}
|
298
|
+
|
299
|
+
// Show "Confirm" instead of "OK" if cancel button is visible
|
300
|
+
params.confirmButtonText = (params.showCancelButton) ? 'Confirm' : defaultParams.confirmButtonText;
|
301
|
+
params.confirmButtonText = argumentOrDefault('confirmButtonText');
|
302
|
+
|
303
|
+
// Function to call when clicking on cancel/OK
|
304
|
+
params.doneFunction = arguments[1] || null;
|
305
|
+
|
306
|
+
break;
|
307
|
+
|
308
|
+
default:
|
309
|
+
logStr('Unexpected type of argument! Expected "string" or "object", got ' + typeof arguments[0]);
|
310
|
+
return false;
|
311
|
+
|
312
|
+
}
|
313
|
+
|
314
|
+
setParameters(params);
|
315
|
+
fixVerticalPosition();
|
316
|
+
openModal();
|
317
|
+
|
318
|
+
|
319
|
+
// Modal interactions
|
320
|
+
var modal = getModal();
|
321
|
+
|
322
|
+
// Mouse interactions
|
323
|
+
var onButtonEvent = function(event) {
|
324
|
+
var e = event || window.event;
|
325
|
+
var target = e.target || e.srcElement,
|
326
|
+
targetedConfirm = (target.className.indexOf("confirm") !== -1),
|
327
|
+
modalIsVisible = hasClass(modal, 'visible'),
|
328
|
+
doneFunctionExists = (params.doneFunction && modal.getAttribute('data-has-done-function') === 'true');
|
329
|
+
|
330
|
+
switch (e.type) {
|
331
|
+
case ("mouseover"):
|
332
|
+
if (targetedConfirm) {
|
333
|
+
target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.04);
|
334
|
+
}
|
335
|
+
break;
|
336
|
+
case ("mouseout"):
|
337
|
+
if (targetedConfirm) {
|
338
|
+
target.style.backgroundColor = params.confirmButtonColor;
|
339
|
+
}
|
340
|
+
break;
|
341
|
+
case ("mousedown"):
|
342
|
+
if (targetedConfirm) {
|
343
|
+
target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.14);
|
344
|
+
}
|
345
|
+
break;
|
346
|
+
case ("mouseup"):
|
347
|
+
if (targetedConfirm) {
|
348
|
+
target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.04);
|
349
|
+
}
|
350
|
+
break;
|
351
|
+
case ("focus"):
|
352
|
+
var $confirmButton = modal.querySelector('button.confirm'),
|
353
|
+
$cancelButton = modal.querySelector('button.cancel');
|
354
|
+
|
355
|
+
if (targetedConfirm) {
|
356
|
+
$cancelButton.style.boxShadow = 'none';
|
357
|
+
} else {
|
358
|
+
$confirmButton.style.boxShadow = 'none';
|
359
|
+
}
|
360
|
+
break;
|
361
|
+
case ("click"):
|
362
|
+
if (targetedConfirm && doneFunctionExists && modalIsVisible) { // Clicked "confirm"
|
363
|
+
|
364
|
+
params.doneFunction(true);
|
365
|
+
|
366
|
+
if (params.closeOnConfirm) {
|
367
|
+
window.sweetAlert.close();
|
368
|
+
}
|
369
|
+
} else if (doneFunctionExists && modalIsVisible) { // Clicked "cancel"
|
370
|
+
|
371
|
+
// Check if callback function expects a parameter (to track cancel actions)
|
372
|
+
var functionAsStr = String(params.doneFunction).replace(/\s/g, '');
|
373
|
+
var functionHandlesCancel = functionAsStr.substring(0, 9) === "function(" && functionAsStr.substring(9, 10) !== ")";
|
374
|
+
|
375
|
+
if (functionHandlesCancel) {
|
376
|
+
params.doneFunction(false);
|
377
|
+
}
|
378
|
+
|
379
|
+
if (params.closeOnCancel) {
|
380
|
+
window.sweetAlert.close();
|
381
|
+
}
|
382
|
+
} else {
|
383
|
+
window.sweetAlert.close();
|
384
|
+
}
|
385
|
+
|
386
|
+
break;
|
387
|
+
}
|
388
|
+
};
|
389
|
+
|
390
|
+
var $buttons = modal.querySelectorAll('button');
|
391
|
+
for (var i = 0; i < $buttons.length; i++) {
|
392
|
+
$buttons[i].onclick = onButtonEvent;
|
393
|
+
$buttons[i].onmouseover = onButtonEvent;
|
394
|
+
$buttons[i].onmouseout = onButtonEvent;
|
395
|
+
$buttons[i].onmousedown = onButtonEvent;
|
396
|
+
//$buttons[i].onmouseup = onButtonEvent;
|
397
|
+
$buttons[i].onfocus = onButtonEvent;
|
398
|
+
}
|
399
|
+
|
400
|
+
// Remember the current document.onclick event.
|
401
|
+
previousDocumentClick = document.onclick;
|
402
|
+
document.onclick = function(event) {
|
403
|
+
var e = event || window.event;
|
404
|
+
var target = e.target || e.srcElement;
|
405
|
+
|
406
|
+
var clickedOnModal = (modal === target),
|
407
|
+
clickedOnModalChild = isDescendant(modal, target),
|
408
|
+
modalIsVisible = hasClass(modal, 'visible'),
|
409
|
+
outsideClickIsAllowed = modal.getAttribute('data-allow-ouside-click') === 'true';
|
410
|
+
|
411
|
+
if (!clickedOnModal && !clickedOnModalChild && modalIsVisible && outsideClickIsAllowed) {
|
412
|
+
window.sweetAlert.close();
|
413
|
+
}
|
414
|
+
};
|
415
|
+
|
416
|
+
|
417
|
+
// Keyboard interactions
|
418
|
+
var $okButton = modal.querySelector('button.confirm'),
|
419
|
+
$cancelButton = modal.querySelector('button.cancel'),
|
420
|
+
$modalButtons = modal.querySelectorAll('button[tabindex]');
|
421
|
+
|
422
|
+
|
423
|
+
function handleKeyDown(event) {
|
424
|
+
var e = event || window.event;
|
425
|
+
var keyCode = e.keyCode || e.which;
|
426
|
+
|
427
|
+
if ([9,13,32,27].indexOf(keyCode) === -1) {
|
428
|
+
// Don't do work on keys we don't care about.
|
429
|
+
return;
|
430
|
+
}
|
431
|
+
|
432
|
+
var $targetElement = e.target || e.srcElement;
|
433
|
+
|
434
|
+
var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
|
435
|
+
for (var i = 0; i < $modalButtons.length; i++) {
|
436
|
+
if ($targetElement === $modalButtons[i]) {
|
437
|
+
btnIndex = i;
|
438
|
+
break;
|
439
|
+
}
|
440
|
+
}
|
441
|
+
|
442
|
+
if (keyCode === 9) {
|
443
|
+
// TAB
|
444
|
+
if (btnIndex === -1) {
|
445
|
+
// No button focused. Jump to the confirm button.
|
446
|
+
$targetElement = $okButton;
|
447
|
+
} else {
|
448
|
+
// Cycle to the next button
|
449
|
+
if (btnIndex === $modalButtons.length - 1) {
|
450
|
+
$targetElement = $modalButtons[0];
|
451
|
+
} else {
|
452
|
+
$targetElement = $modalButtons[btnIndex + 1];
|
453
|
+
}
|
454
|
+
}
|
455
|
+
|
456
|
+
stopEventPropagation(e);
|
457
|
+
$targetElement.focus();
|
458
|
+
setFocusStyle($targetElement, params.confirmButtonColor); // TODO
|
459
|
+
|
460
|
+
} else {
|
461
|
+
if (keyCode === 13 || keyCode === 32) {
|
462
|
+
if (btnIndex === -1) {
|
463
|
+
// ENTER/SPACE clicked outside of a button.
|
464
|
+
$targetElement = $okButton;
|
465
|
+
} else {
|
466
|
+
// Do nothing - let the browser handle it.
|
467
|
+
$targetElement = undefined;
|
468
|
+
}
|
469
|
+
} else if (keyCode === 27 && params.allowEscapeKey === true) {
|
470
|
+
$targetElement = $cancelButton;
|
471
|
+
} else {
|
472
|
+
// Fallback - let the browser handle it.
|
473
|
+
$targetElement = undefined;
|
474
|
+
}
|
475
|
+
|
476
|
+
if ($targetElement !== undefined) {
|
477
|
+
fireClick($targetElement, e);
|
478
|
+
}
|
479
|
+
}
|
480
|
+
}
|
481
|
+
|
482
|
+
previousWindowKeyDown = window.onkeydown;
|
483
|
+
|
484
|
+
window.onkeydown = handleKeyDown;
|
485
|
+
|
486
|
+
function handleOnBlur(event) {
|
487
|
+
var e = event || window.event;
|
488
|
+
var $targetElement = e.target || e.srcElement,
|
489
|
+
$focusElement = e.relatedTarget,
|
490
|
+
modalIsVisible = hasClass(modal, 'visible');
|
491
|
+
|
492
|
+
if (modalIsVisible) {
|
493
|
+
var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
|
494
|
+
|
495
|
+
if ($focusElement !== null) {
|
496
|
+
// If we picked something in the DOM to focus to, let's see if it was a button.
|
497
|
+
for (var i = 0; i < $modalButtons.length; i++) {
|
498
|
+
if ($focusElement === $modalButtons[i]) {
|
499
|
+
btnIndex = i;
|
500
|
+
break;
|
501
|
+
}
|
502
|
+
}
|
503
|
+
|
504
|
+
if (btnIndex === -1) {
|
505
|
+
// Something in the dom, but not a visible button. Focus back on the button.
|
506
|
+
$targetElement.focus();
|
507
|
+
}
|
508
|
+
} else {
|
509
|
+
// Exiting the DOM (e.g. clicked in the URL bar);
|
510
|
+
lastFocusedButton = $targetElement;
|
511
|
+
}
|
512
|
+
}
|
513
|
+
}
|
514
|
+
|
515
|
+
$okButton.onblur = handleOnBlur;
|
516
|
+
$cancelButton.onblur = handleOnBlur;
|
517
|
+
|
518
|
+
window.onfocus = function() {
|
519
|
+
// When the user has focused away and focused back from the whole window.
|
520
|
+
window.setTimeout(function() {
|
521
|
+
// Put in a timeout to jump out of the event sequence. Calling focus() in the event
|
522
|
+
// sequence confuses things.
|
523
|
+
if (lastFocusedButton !== undefined) {
|
524
|
+
lastFocusedButton.focus();
|
525
|
+
lastFocusedButton = undefined;
|
526
|
+
}
|
527
|
+
}, 0);
|
528
|
+
};
|
529
|
+
}
|
530
|
+
|
531
|
+
|
532
|
+
/*
|
533
|
+
* Set default params for each popup
|
534
|
+
* @param {Object} userParams
|
535
|
+
*/
|
536
|
+
window.sweetAlert.setDefaults = window.swal.setDefaults = function(userParams) {
|
537
|
+
if (!userParams) {
|
538
|
+
throw new Error('userParams is required');
|
539
|
+
}
|
540
|
+
if (typeof userParams !== 'object') {
|
541
|
+
throw new Error('userParams has to be a object');
|
542
|
+
}
|
543
|
+
|
544
|
+
extend(defaultParams, userParams);
|
545
|
+
};
|
546
|
+
|
547
|
+
|
548
|
+
/*
|
549
|
+
* Set type, text and actions on modal
|
550
|
+
*/
|
551
|
+
|
552
|
+
function setParameters(params) {
|
553
|
+
var modal = getModal();
|
554
|
+
|
555
|
+
var $title = modal.querySelector('h2'),
|
556
|
+
$text = modal.querySelector('p'),
|
557
|
+
$cancelBtn = modal.querySelector('button.cancel'),
|
558
|
+
$confirmBtn = modal.querySelector('button.confirm');
|
559
|
+
|
560
|
+
// Title
|
561
|
+
$title.innerHTML = (params.html) ? params.title : escapeHtml(params.title).split("\n").join("<br>");
|
562
|
+
|
563
|
+
// Text
|
564
|
+
$text.innerHTML = (params.html) ? params.text : escapeHtml(params.text || '').split("\n").join("<br>");
|
565
|
+
|
566
|
+
if (params.text) {
|
567
|
+
show($text);
|
568
|
+
}
|
569
|
+
|
570
|
+
//Custom Class
|
571
|
+
if (params.customClass) {
|
572
|
+
addClass(modal, params.customClass);
|
573
|
+
modal.setAttribute('data-custom-class', params.customClass);
|
574
|
+
} else {
|
575
|
+
// Find previously set classes and remove them
|
576
|
+
var customClass = modal.getAttribute('data-custom-class');
|
577
|
+
removeClass(modal, customClass);
|
578
|
+
modal.setAttribute('data-custom-class', "");
|
579
|
+
}
|
580
|
+
|
581
|
+
// Icon
|
582
|
+
hide(modal.querySelectorAll('.icon'));
|
583
|
+
if (params.type && !isIE8()) {
|
584
|
+
var validType = false;
|
585
|
+
for (var i = 0; i < alertTypes.length; i++) {
|
586
|
+
if (params.type === alertTypes[i]) {
|
587
|
+
validType = true;
|
588
|
+
break;
|
589
|
+
}
|
590
|
+
}
|
591
|
+
if (!validType) {
|
592
|
+
logStr('Unknown alert type: ' + params.type);
|
593
|
+
return false;
|
594
|
+
}
|
595
|
+
var $icon = modal.querySelector('.icon.' + params.type);
|
596
|
+
show($icon);
|
597
|
+
|
598
|
+
// Animate icon
|
599
|
+
switch (params.type) {
|
600
|
+
case "success":
|
601
|
+
addClass($icon, 'animate');
|
602
|
+
addClass($icon.querySelector('.tip'), 'animateSuccessTip');
|
603
|
+
addClass($icon.querySelector('.long'), 'animateSuccessLong');
|
604
|
+
break;
|
605
|
+
case "error":
|
606
|
+
addClass($icon, 'animateErrorIcon');
|
607
|
+
addClass($icon.querySelector('.x-mark'), 'animateXMark');
|
608
|
+
break;
|
609
|
+
case "warning":
|
610
|
+
addClass($icon, 'pulseWarning');
|
611
|
+
addClass($icon.querySelector('.body'), 'pulseWarningIns');
|
612
|
+
addClass($icon.querySelector('.dot'), 'pulseWarningIns');
|
613
|
+
break;
|
614
|
+
}
|
615
|
+
}
|
616
|
+
|
617
|
+
// Custom image
|
618
|
+
if (params.imageUrl) {
|
619
|
+
var $customIcon = modal.querySelector('.icon.custom');
|
620
|
+
|
621
|
+
$customIcon.style.backgroundImage = 'url(' + params.imageUrl + ')';
|
622
|
+
show($customIcon);
|
623
|
+
|
624
|
+
var _imgWidth = 80,
|
625
|
+
_imgHeight = 80;
|
626
|
+
|
627
|
+
if (params.imageSize) {
|
628
|
+
var dimensions = params.imageSize.toString().split('x');
|
629
|
+
var imgWidth = dimensions[0];
|
630
|
+
var imgHeight = dimensions[1];
|
631
|
+
|
632
|
+
if (!imgWidth || !imgHeight) {
|
633
|
+
logStr("Parameter imageSize expects value with format WIDTHxHEIGHT, got " + params.imageSize);
|
634
|
+
} else {
|
635
|
+
_imgWidth = imgWidth;
|
636
|
+
_imgHeight = imgHeight;
|
637
|
+
}
|
638
|
+
}
|
639
|
+
$customIcon.setAttribute('style', $customIcon.getAttribute('style') + 'width:' + _imgWidth + 'px; height:' + _imgHeight + 'px');
|
640
|
+
}
|
641
|
+
|
642
|
+
// Cancel button
|
643
|
+
modal.setAttribute('data-has-cancel-button', params.showCancelButton);
|
644
|
+
if (params.showCancelButton) {
|
645
|
+
$cancelBtn.style.display = 'inline-block';
|
646
|
+
} else {
|
647
|
+
hide($cancelBtn);
|
648
|
+
}
|
649
|
+
|
650
|
+
// Edit text on cancel and confirm buttons
|
651
|
+
if (params.cancelButtonText) {
|
652
|
+
$cancelBtn.innerHTML = escapeHtml(params.cancelButtonText);
|
653
|
+
}
|
654
|
+
if (params.confirmButtonText) {
|
655
|
+
$confirmBtn.innerHTML = escapeHtml(params.confirmButtonText);
|
656
|
+
}
|
657
|
+
|
658
|
+
// Set confirm button to selected background color
|
659
|
+
$confirmBtn.style.backgroundColor = params.confirmButtonColor;
|
660
|
+
|
661
|
+
// Set box-shadow to default focused button
|
662
|
+
setFocusStyle($confirmBtn, params.confirmButtonColor);
|
663
|
+
|
664
|
+
// Allow outside click?
|
665
|
+
modal.setAttribute('data-allow-ouside-click', params.allowOutsideClick);
|
666
|
+
|
667
|
+
// Done-function
|
668
|
+
var hasDoneFunction = (params.doneFunction) ? true : false;
|
669
|
+
modal.setAttribute('data-has-done-function', hasDoneFunction);
|
670
|
+
|
671
|
+
// Prevent modal from animating
|
672
|
+
if (!params.animation){
|
673
|
+
modal.setAttribute('data-animation', 'none');
|
674
|
+
} else{
|
675
|
+
modal.setAttribute('data-animation', 'pop');
|
676
|
+
}
|
677
|
+
|
678
|
+
// Close timer
|
679
|
+
modal.setAttribute('data-timer', params.timer);
|
680
|
+
}
|
681
|
+
|
682
|
+
|
683
|
+
/*
|
684
|
+
* Set hover, active and focus-states for buttons (source: http://www.sitepoint.com/javascript-generate-lighter-darker-color)
|
685
|
+
*/
|
686
|
+
|
687
|
+
function colorLuminance(hex, lum) {
|
688
|
+
// Validate hex string
|
689
|
+
hex = String(hex).replace(/[^0-9a-f]/gi, '');
|
690
|
+
if (hex.length < 6) {
|
691
|
+
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
|
692
|
+
}
|
693
|
+
lum = lum || 0;
|
694
|
+
|
695
|
+
// Convert to decimal and change luminosity
|
696
|
+
var rgb = "#", c, i;
|
697
|
+
for (i = 0; i < 3; i++) {
|
698
|
+
c = parseInt(hex.substr(i*2,2), 16);
|
699
|
+
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
|
700
|
+
rgb += ("00"+c).substr(c.length);
|
701
|
+
}
|
702
|
+
|
703
|
+
return rgb;
|
704
|
+
}
|
705
|
+
|
706
|
+
function extend(a, b){
|
707
|
+
for (var key in b) {
|
708
|
+
if (b.hasOwnProperty(key)) {
|
709
|
+
a[key] = b[key];
|
710
|
+
}
|
711
|
+
}
|
712
|
+
|
713
|
+
return a;
|
714
|
+
}
|
715
|
+
|
716
|
+
function hexToRgb(hex) {
|
717
|
+
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
718
|
+
return result ? parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) : null;
|
719
|
+
}
|
720
|
+
|
721
|
+
// Add box-shadow style to button (depending on its chosen bg-color)
|
722
|
+
function setFocusStyle($button, bgColor) {
|
723
|
+
var rgbColor = hexToRgb(bgColor);
|
724
|
+
$button.style.boxShadow = '0 0 2px rgba(' + rgbColor +', 0.8), inset 0 0 0 1px rgba(0, 0, 0, 0.05)';
|
725
|
+
}
|
726
|
+
|
727
|
+
|
728
|
+
// Animation when opening modal
|
729
|
+
function openModal() {
|
730
|
+
var modal = getModal();
|
731
|
+
fadeIn(getOverlay(), 10);
|
732
|
+
show(modal);
|
733
|
+
addClass(modal, 'showSweetAlert');
|
734
|
+
removeClass(modal, 'hideSweetAlert');
|
735
|
+
|
736
|
+
previousActiveElement = document.activeElement;
|
737
|
+
var $okButton = modal.querySelector('button.confirm');
|
738
|
+
$okButton.focus();
|
739
|
+
|
740
|
+
setTimeout(function() {
|
741
|
+
addClass(modal, 'visible');
|
742
|
+
}, 500);
|
743
|
+
|
744
|
+
var timer = modal.getAttribute('data-timer');
|
745
|
+
|
746
|
+
if (timer !== "null" && timer !== "") {
|
747
|
+
modal.timeout = setTimeout(function() {
|
748
|
+
window.sweetAlert.close();
|
749
|
+
}, timer);
|
750
|
+
}
|
751
|
+
}
|
752
|
+
|
753
|
+
|
754
|
+
// Aninmation when closing modal
|
755
|
+
window.sweetAlert.close = window.swal.close = function() {
|
756
|
+
var modal = getModal();
|
757
|
+
fadeOut(getOverlay(), 5);
|
758
|
+
fadeOut(modal, 5);
|
759
|
+
removeClass(modal, 'showSweetAlert');
|
760
|
+
addClass(modal, 'hideSweetAlert');
|
761
|
+
removeClass(modal, 'visible');
|
762
|
+
|
763
|
+
|
764
|
+
// Reset icon animations
|
765
|
+
|
766
|
+
var $successIcon = modal.querySelector('.icon.success');
|
767
|
+
removeClass($successIcon, 'animate');
|
768
|
+
removeClass($successIcon.querySelector('.tip'), 'animateSuccessTip');
|
769
|
+
removeClass($successIcon.querySelector('.long'), 'animateSuccessLong');
|
770
|
+
|
771
|
+
var $errorIcon = modal.querySelector('.icon.error');
|
772
|
+
removeClass($errorIcon, 'animateErrorIcon');
|
773
|
+
removeClass($errorIcon.querySelector('.x-mark'), 'animateXMark');
|
774
|
+
|
775
|
+
var $warningIcon = modal.querySelector('.icon.warning');
|
776
|
+
removeClass($warningIcon, 'pulseWarning');
|
777
|
+
removeClass($warningIcon.querySelector('.body'), 'pulseWarningIns');
|
778
|
+
removeClass($warningIcon.querySelector('.dot'), 'pulseWarningIns');
|
779
|
+
|
780
|
+
|
781
|
+
// Reset the page to its previous state
|
782
|
+
window.onkeydown = previousWindowKeyDown;
|
783
|
+
document.onclick = previousDocumentClick;
|
784
|
+
if (previousActiveElement) {
|
785
|
+
previousActiveElement.focus();
|
786
|
+
}
|
787
|
+
lastFocusedButton = undefined;
|
788
|
+
clearTimeout(modal.timeout);
|
789
|
+
};
|
790
|
+
|
791
|
+
|
792
|
+
/*
|
793
|
+
* Set "margin-top"-property on modal based on its computed height
|
794
|
+
*/
|
795
|
+
|
796
|
+
function fixVerticalPosition() {
|
797
|
+
var modal = getModal();
|
798
|
+
|
799
|
+
modal.style.marginTop = getTopMargin(getModal());
|
800
|
+
}
|
801
|
+
|
802
|
+
// If browser is Internet Explorer 8
|
803
|
+
function isIE8() {
|
804
|
+
if (window.attachEvent && !window.addEventListener) {
|
805
|
+
return true;
|
806
|
+
} else {
|
807
|
+
return false;
|
808
|
+
}
|
809
|
+
}
|
810
|
+
|
811
|
+
// Error messages for developers
|
812
|
+
function logStr(string) {
|
813
|
+
if (window.console) { // IE...
|
814
|
+
window.console.log("SweetAlert: " + string);
|
815
|
+
}
|
816
|
+
}
|
817
|
+
|
818
|
+
})(window, document);
|