alerts-sweet-rails 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2afdc89c2d763de3a32f31e7904baf872567caaa
4
+ data.tar.gz: b9cab76756cfb1136617c2c24ee8e689e18cfdbb
5
+ SHA512:
6
+ metadata.gz: 03904c0e6f0a41af44dfc3ae9030f65482524e647bf178b85b32feb0eadc3c4fc1ec1b839ba7f6dc9d3cbcc8a3dad2b522893e615778de1ea5525ad3235f4b2e
7
+ data.tar.gz: fb07f5cc6f6a4cc283175db4bc135351f1e6f4ffa18d8fd91cea313468b37a4429acd39f0dbb010aea7e63faa2d8449c915b4b2799a89c340eaaa686697bb833
@@ -0,0 +1,11 @@
1
+ require "alerts/sweet/rails/version"
2
+
3
+ module Alerts
4
+ module Sweet
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ # Do nothing
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Alerts
2
+ module Sweet
3
+ module Rails
4
+ VERSION = "0.0.2"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,714 @@
1
+ // SweetAlert
2
+ // 2014 (c) - Tristan Edwards
3
+ // github.com/t4t5/sweetalert
4
+ (function(window, document) {
5
+
6
+ var modalClass = '.sweet-alert',
7
+ overlayClass = '.sweet-overlay',
8
+ alertTypes = ['error', 'warning', 'info', 'success'];
9
+
10
+
11
+ /*
12
+ * Manipulate DOM
13
+ */
14
+
15
+ var getModal = function() {
16
+ return document.querySelector(modalClass);
17
+ },
18
+ getOverlay = function() {
19
+ return document.querySelector(overlayClass);
20
+ },
21
+ hasClass = function(elem, className) {
22
+ return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
23
+ },
24
+ addClass = function(elem, className) {
25
+ if (!hasClass(elem, className)) {
26
+ elem.className += ' ' + className;
27
+ }
28
+ },
29
+ removeClass = function(elem, className) {
30
+ var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
31
+ if (hasClass(elem, className)) {
32
+ while (newClass.indexOf(' ' + className + ' ') >= 0) {
33
+ newClass = newClass.replace(' ' + className + ' ', ' ');
34
+ }
35
+ elem.className = newClass.replace(/^\s+|\s+$/g, '');
36
+ }
37
+ },
38
+ escapeHtml = function(str) {
39
+ var div = document.createElement('div');
40
+ div.appendChild(document.createTextNode(str));
41
+ return div.innerHTML;
42
+ },
43
+ _show = function(elem) {
44
+ elem.style.opacity = '';
45
+ elem.style.display = 'block';
46
+ },
47
+ show = function(elems) {
48
+ if (elems && !elems.length) {
49
+ return _show(elems);
50
+ }
51
+ for (var i = 0; i < elems.length; ++i) {
52
+ _show(elems[i]);
53
+ }
54
+ },
55
+ _hide = function(elem) {
56
+ elem.style.opacity = '';
57
+ elem.style.display = 'none';
58
+ },
59
+ hide = function(elems) {
60
+ if (elems && !elems.length) {
61
+ return _hide(elems);
62
+ }
63
+ for (var i = 0; i < elems.length; ++i) {
64
+ _hide(elems[i]);
65
+ }
66
+ },
67
+ isDescendant = function(parent, child) {
68
+ var node = child.parentNode;
69
+ while (node !== null) {
70
+ if (node === parent) {
71
+ return true;
72
+ }
73
+ node = node.parentNode;
74
+ }
75
+ return false;
76
+ },
77
+ getTopMargin = function(elem) {
78
+ elem.style.left = '-9999px';
79
+ elem.style.display = 'block';
80
+
81
+ var height = elem.clientHeight;
82
+ var padding = parseInt(getComputedStyle(elem).getPropertyValue('padding'), 10);
83
+
84
+ elem.style.left = '';
85
+ elem.style.display = 'none';
86
+ return ('-' + parseInt(height / 2 + padding) + 'px');
87
+ },
88
+ fadeIn = function(elem, interval) {
89
+ if(+elem.style.opacity < 1) {
90
+ interval = interval || 16;
91
+ elem.style.opacity = 0;
92
+ elem.style.display = 'block';
93
+ var last = +new Date();
94
+ var tick = function() {
95
+ elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100;
96
+ last = +new Date();
97
+
98
+ if (+elem.style.opacity < 1) {
99
+ setTimeout(tick, interval);
100
+ }
101
+ };
102
+ tick();
103
+ }
104
+ },
105
+ fadeOut = function(elem, interval) {
106
+ interval = interval || 16;
107
+ elem.style.opacity = 1;
108
+ var last = +new Date();
109
+ var tick = function() {
110
+ elem.style.opacity = +elem.style.opacity - (new Date() - last) / 100;
111
+ last = +new Date();
112
+
113
+ if (+elem.style.opacity > 0) {
114
+ setTimeout(tick, interval);
115
+ } else {
116
+ elem.style.display = 'none';
117
+ }
118
+ };
119
+ tick();
120
+ },
121
+ fireClick = function(node) {
122
+ // Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
123
+ // Then fixed for today's Chrome browser.
124
+ if (MouseEvent) {
125
+ // Up-to-date approach
126
+ var mevt = new MouseEvent('click', {
127
+ view: window,
128
+ bubbles: false,
129
+ cancelable: true
130
+ });
131
+ node.dispatchEvent(mevt);
132
+ } else if ( document.createEvent ) {
133
+ // Fallback
134
+ var evt = document.createEvent('MouseEvents');
135
+ evt.initEvent('click', false, false);
136
+ node.dispatchEvent(evt);
137
+ } else if( document.createEventObject ) {
138
+ node.fireEvent('onclick') ;
139
+ } else if (typeof node.onclick === 'function' ) {
140
+ node.onclick();
141
+ }
142
+ },
143
+ stopEventPropagation = function(e) {
144
+ // In particular, make sure the space bar doesn't scroll the main window.
145
+ if (typeof e.stopPropagation === 'function') {
146
+ e.stopPropagation();
147
+ e.preventDefault();
148
+ } else if (window.event && window.event.hasOwnProperty('cancelBubble')) {
149
+ window.event.cancelBubble = true;
150
+ }
151
+ };
152
+
153
+ // Remember state in cases where opening and handling a modal will fiddle with it.
154
+ var previousActiveElement,
155
+ previousDocumentClick,
156
+ previousWindowKeyDown,
157
+ lastFocusedButton;
158
+
159
+ /*
160
+ * Add modal + overlay to DOM
161
+ */
162
+
163
+ function initialize() {
164
+ 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>',
165
+ sweetWrap = document.createElement('div');
166
+
167
+ sweetWrap.innerHTML = sweetHTML;
168
+
169
+ // For readability: check sweet-alert.html
170
+ document.body.appendChild(sweetWrap);
171
+
172
+ // For development use only!
173
+ /*jQuery.ajax({
174
+ url: '../lib/sweet-alert.html', // Change path depending on file location
175
+ dataType: 'html'
176
+ })
177
+ .done(function(html) {
178
+ jQuery('body').append(html);
179
+ });*/
180
+ }
181
+
182
+
183
+
184
+ /*
185
+ * Global sweetAlert function
186
+ */
187
+
188
+ window.sweetAlert = window.swal = function() {
189
+
190
+ // Default parameters
191
+ var params = {
192
+ title: '',
193
+ text: '',
194
+ type: null,
195
+ allowOutsideClick: false,
196
+ showCancelButton: false,
197
+ closeOnConfirm: true,
198
+ closeOnCancel: true,
199
+ confirmButtonText: 'OK',
200
+ confirmButtonColor: '#AEDEF4',
201
+ cancelButtonText: 'Cancel',
202
+ imageUrl: null,
203
+ imageSize: null
204
+ };
205
+
206
+ if (arguments[0] === undefined) {
207
+ window.console.error('sweetAlert expects at least 1 attribute!');
208
+ return false;
209
+ }
210
+
211
+
212
+ switch (typeof arguments[0]) {
213
+
214
+ case 'string':
215
+ params.title = arguments[0];
216
+ params.text = arguments[1] || '';
217
+ params.type = arguments[2] || '';
218
+
219
+ break;
220
+
221
+ case 'object':
222
+ if (arguments[0].title === undefined) {
223
+ window.console.error('Missing "title" argument!');
224
+ return false;
225
+ }
226
+
227
+ params.title = arguments[0].title;
228
+ params.text = arguments[0].text || params.text;
229
+ params.type = arguments[0].type || params.type;
230
+ params.allowOutsideClick = arguments[0].allowOutsideClick || params.allowOutsideClick;
231
+ params.showCancelButton = arguments[0].showCancelButton !== undefined ? arguments[0].showCancelButton : params.showCancelButton;
232
+ params.closeOnConfirm = arguments[0].closeOnConfirm !== undefined ? arguments[0].closeOnConfirm : params.closeOnConfirm;
233
+ params.closeOnCancel = arguments[0].closeOnCancel !== undefined ? arguments[0].closeOnCancel : params.closeOnCancel;
234
+
235
+ // Show "Confirm" instead of "OK" if cancel button is visible
236
+ params.confirmButtonText = (params.showCancelButton) ? 'Confirm' : params.confirmButtonText;
237
+
238
+ params.confirmButtonText = arguments[0].confirmButtonText || params.confirmButtonText;
239
+ params.confirmButtonColor = arguments[0].confirmButtonColor || params.confirmButtonColor;
240
+ params.cancelButtonText = arguments[0].cancelButtonText || params.cancelButtonText;
241
+ params.imageUrl = arguments[0].imageUrl || params.imageUrl;
242
+ params.imageSize = arguments[0].imageSize || params.imageSize;
243
+ params.doneFunction = arguments[1] || null;
244
+
245
+ break;
246
+
247
+ default:
248
+ window.console.error('Unexpected type of argument! Expected "string" or "object", got ' + typeof arguments[0]);
249
+ return false;
250
+
251
+ }
252
+
253
+ setParameters(params);
254
+ fixVerticalPosition();
255
+ openModal();
256
+
257
+
258
+ // Modal interactions
259
+ var modal = getModal();
260
+
261
+ // Mouse interactions
262
+ var onButtonEvent = function(e) {
263
+
264
+ var target = e.target || e.srcElement,
265
+ targetedConfirm = (target.className === 'confirm'),
266
+ modalIsVisible = hasClass(modal, 'visible'),
267
+ doneFunctionExists = (params.doneFunction && modal.getAttribute('data-has-done-function') === 'true');
268
+
269
+ switch (e.type) {
270
+ case ("mouseover"):
271
+ if (targetedConfirm) {
272
+ e.target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.04);
273
+ }
274
+ break;
275
+ case ("mouseout"):
276
+ if (targetedConfirm) {
277
+ e.target.style.backgroundColor = params.confirmButtonColor;
278
+ }
279
+ break;
280
+ case ("mousedown"):
281
+ if (targetedConfirm) {
282
+ e.target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.14);
283
+ }
284
+ break;
285
+ case ("mouseup"):
286
+ if (targetedConfirm) {
287
+ e.target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.04);
288
+ }
289
+ break;
290
+ case ("focus"):
291
+ var $confirmButton = modal.querySelector('button.confirm'),
292
+ $cancelButton = modal.querySelector('button.cancel');
293
+
294
+ if (targetedConfirm) {
295
+ $cancelButton.style.boxShadow = 'none';
296
+ } else {
297
+ $confirmButton.style.boxShadow = 'none';
298
+ }
299
+ break;
300
+ case ("click"):
301
+ if (targetedConfirm && doneFunctionExists && modalIsVisible) { // Clicked "confirm"
302
+
303
+ params.doneFunction(true);
304
+
305
+ if (params.closeOnConfirm) {
306
+ closeModal();
307
+ }
308
+ } else if (doneFunctionExists && modalIsVisible) { // Clicked "cancel"
309
+
310
+ // Check if callback function expects a parameter (to track cancel actions)
311
+ var functionAsStr = String(params.doneFunction).replace(/\s/g, '');
312
+ var functionHandlesCancel = functionAsStr.substring(0, 9) === "function(" && functionAsStr.substring(9, 10) !== ")";
313
+
314
+ if (functionHandlesCancel) {
315
+ params.doneFunction(false);
316
+ }
317
+
318
+ if (params.closeOnCancel) {
319
+ closeModal();
320
+ }
321
+ } else {
322
+ closeModal();
323
+ }
324
+
325
+ break;
326
+ }
327
+ };
328
+
329
+ var $buttons = modal.querySelectorAll('button');
330
+ for (var i = 0; i < $buttons.length; i++) {
331
+ $buttons[i].onclick = onButtonEvent;
332
+ $buttons[i].onmouseover = onButtonEvent;
333
+ $buttons[i].onmouseout = onButtonEvent;
334
+ $buttons[i].onmousedown = onButtonEvent;
335
+ //$buttons[i].onmouseup = onButtonEvent;
336
+ $buttons[i].onfocus = onButtonEvent;
337
+ }
338
+
339
+ // Remember the current document.onclick event.
340
+ previousDocumentClick = document.onclick;
341
+ document.onclick = function(e) {
342
+ var target = e.target || e.srcElement;
343
+
344
+ var clickedOnModal = (modal === target),
345
+ clickedOnModalChild = isDescendant(modal, e.target),
346
+ modalIsVisible = hasClass(modal, 'visible'),
347
+ outsideClickIsAllowed = modal.getAttribute('data-allow-ouside-click') === 'true';
348
+
349
+ if (!clickedOnModal && !clickedOnModalChild && modalIsVisible && outsideClickIsAllowed) {
350
+ closeModal();
351
+ }
352
+ };
353
+
354
+
355
+ // Keyboard interactions
356
+ var $okButton = modal.querySelector('button.confirm'),
357
+ $cancelButton = modal.querySelector('button.cancel'),
358
+ $modalButtons = modal.querySelectorAll('button:not([type=hidden])');
359
+
360
+
361
+ function handleKeyDown(e) {
362
+ var keyCode = e.keyCode || e.which;
363
+
364
+ if ([9,13,32,27].indexOf(keyCode) === -1) {
365
+ // Don't do work on keys we don't care about.
366
+ return;
367
+ }
368
+
369
+ var $targetElement = e.target || e.srcElement;
370
+
371
+ var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
372
+ for (var i = 0; i < $modalButtons.length; i++) {
373
+ if ($targetElement === $modalButtons[i]) {
374
+ btnIndex = i;
375
+ break;
376
+ }
377
+ }
378
+
379
+ if (keyCode === 9) {
380
+ // TAB
381
+ if (btnIndex === -1) {
382
+ // No button focused. Jump to the confirm button.
383
+ $targetElement = $okButton;
384
+ } else {
385
+ // Cycle to the next button
386
+ if (btnIndex === $modalButtons.length - 1) {
387
+ $targetElement = $modalButtons[0];
388
+ } else {
389
+ $targetElement = $modalButtons[btnIndex + 1];
390
+ }
391
+ }
392
+
393
+ stopEventPropagation(e);
394
+ $targetElement.focus();
395
+ setFocusStyle($targetElement, params.confirmButtonColor); // TODO
396
+
397
+ } else {
398
+ if (keyCode === 13 || keyCode === 32) {
399
+ if (btnIndex === -1) {
400
+ // ENTER/SPACE clicked outside of a button.
401
+ $targetElement = $okButton;
402
+ } else {
403
+ // Do nothing - let the browser handle it.
404
+ $targetElement = undefined;
405
+ }
406
+ } else if (keyCode === 27 && !($cancelButton.hidden || $cancelButton.style.display === 'none')) {
407
+ // ESC to cancel only if there's a cancel button displayed (like the alert() window).
408
+ $targetElement = $cancelButton;
409
+ } else {
410
+ // Fallback - let the browser handle it.
411
+ $targetElement = undefined;
412
+ }
413
+
414
+ if ($targetElement !== undefined) {
415
+ fireClick($targetElement, e);
416
+ }
417
+ }
418
+ }
419
+
420
+ previousWindowKeyDown = window.onkeydown;
421
+ window.onkeydown = handleKeyDown;
422
+
423
+ function handleOnBlur(e) {
424
+ var $targetElement = e.target || e.srcElement,
425
+ $focusElement = e.relatedTarget,
426
+ modalIsVisible = hasClass(modal, 'visible');
427
+
428
+ if (modalIsVisible) {
429
+ var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
430
+
431
+ if ($focusElement !== null) {
432
+ // If we picked something in the DOM to focus to, let's see if it was a button.
433
+ for (var i = 0; i < $modalButtons.length; i++) {
434
+ if ($focusElement === $modalButtons[i]) {
435
+ btnIndex = i;
436
+ break;
437
+ }
438
+ }
439
+
440
+ if (btnIndex === -1) {
441
+ // Something in the dom, but not a visible button. Focus back on the button.
442
+ $targetElement.focus();
443
+ }
444
+ } else {
445
+ // Exiting the DOM (e.g. clicked in the URL bar);
446
+ lastFocusedButton = $targetElement;
447
+ }
448
+ }
449
+ }
450
+
451
+ $okButton.onblur = handleOnBlur;
452
+ $cancelButton.onblur = handleOnBlur;
453
+
454
+ window.onfocus = function() {
455
+ // When the user has focused away and focused back from the whole window.
456
+ window.setTimeout(function() {
457
+ // Put in a timeout to jump out of the event sequence. Calling focus() in the event
458
+ // sequence confuses things.
459
+ if (lastFocusedButton !== undefined) {
460
+ lastFocusedButton.focus();
461
+ lastFocusedButton = undefined;
462
+ }
463
+ }, 0);
464
+ };
465
+ };
466
+
467
+
468
+ /*
469
+ * Set type, text and actions on modal
470
+ */
471
+
472
+ function setParameters(params) {
473
+ var modal = getModal();
474
+
475
+ var $title = modal.querySelector('h2'),
476
+ $text = modal.querySelector('p'),
477
+ $cancelBtn = modal.querySelector('button.cancel'),
478
+ $confirmBtn = modal.querySelector('button.confirm');
479
+
480
+ // Title
481
+ $title.innerHTML = escapeHtml(params.title).split("\n").join("<br>");
482
+
483
+ // Text
484
+ $text.innerHTML = escapeHtml(params.text || '').split("\n").join("<br>");
485
+ if (params.text) {
486
+ show($text);
487
+ }
488
+
489
+ // Icon
490
+ hide(modal.querySelectorAll('.icon'));
491
+ if (params.type) {
492
+ var validType = false;
493
+ for (var i = 0; i < alertTypes.length; i++) {
494
+ if (params.type === alertTypes[i]) {
495
+ validType = true;
496
+ break;
497
+ }
498
+ }
499
+ if (!validType) {
500
+ window.console.error('Unknown alert type: ' + params.type);
501
+ return false;
502
+ }
503
+ var $icon = modal.querySelector('.icon.' + params.type);
504
+ show($icon);
505
+
506
+ // Animate icon
507
+ switch (params.type) {
508
+ case "success":
509
+ addClass($icon, 'animate');
510
+ addClass($icon.querySelector('.tip'), 'animateSuccessTip');
511
+ addClass($icon.querySelector('.long'), 'animateSuccessLong');
512
+ break;
513
+ case "error":
514
+ addClass($icon, 'animateErrorIcon');
515
+ addClass($icon.querySelector('.x-mark'), 'animateXMark');
516
+ break;
517
+ case "warning":
518
+ addClass($icon, 'pulseWarning');
519
+ addClass($icon.querySelector('.body'), 'pulseWarningIns');
520
+ addClass($icon.querySelector('.dot'), 'pulseWarningIns');
521
+ break;
522
+ }
523
+
524
+ }
525
+
526
+ // Custom image
527
+ if (params.imageUrl) {
528
+ var $customIcon = modal.querySelector('.icon.custom');
529
+
530
+ $customIcon.style.backgroundImage = 'url(' + params.imageUrl + ')';
531
+ show($customIcon);
532
+
533
+ var _imgWidth = 80,
534
+ _imgHeight = 80;
535
+
536
+ if (params.imageSize) {
537
+ var imgWidth = params.imageSize.split('x')[0];
538
+ var imgHeight = params.imageSize.split('x')[1];
539
+
540
+ if (!imgWidth || !imgHeight) {
541
+ window.console.error("Parameter imageSize expects value with format WIDTHxHEIGHT, got " + params.imageSize);
542
+ } else {
543
+ _imgWidth = imgWidth;
544
+ _imgHeight = imgHeight;
545
+
546
+ $customIcon.css({
547
+ 'width': imgWidth + 'px',
548
+ 'height': imgHeight + 'px'
549
+ });
550
+ }
551
+ }
552
+ $customIcon.setAttribute('style', $customIcon.getAttribute('style') + 'width:' + _imgWidth + 'px; height:' + _imgHeight + 'px');
553
+ }
554
+
555
+ // Cancel button
556
+ modal.setAttribute('data-has-cancel-button', params.showCancelButton);
557
+ if (params.showCancelButton) {
558
+ $cancelBtn.style.display = 'inline-block';
559
+ } else {
560
+ hide($cancelBtn);
561
+ }
562
+
563
+ // Edit text on cancel and confirm buttons
564
+ if (params.cancelButtonText) {
565
+ $cancelBtn.innerHTML = escapeHtml(params.cancelButtonText);
566
+ }
567
+ if (params.confirmButtonText) {
568
+ $confirmBtn.innerHTML = escapeHtml(params.confirmButtonText);
569
+ }
570
+
571
+ // Set confirm button to selected background color
572
+ $confirmBtn.style.backgroundColor = params.confirmButtonColor;
573
+
574
+ // Set box-shadow to default focused button
575
+ setFocusStyle($confirmBtn, params.confirmButtonColor);
576
+
577
+ // Allow outside click?
578
+ modal.setAttribute('data-allow-ouside-click', params.allowOutsideClick);
579
+
580
+ // Done-function
581
+ var hasDoneFunction = (params.doneFunction) ? true : false;
582
+ modal.setAttribute('data-has-done-function', hasDoneFunction);
583
+ }
584
+
585
+
586
+ /*
587
+ * Set hover, active and focus-states for buttons (source: http://www.sitepoint.com/javascript-generate-lighter-darker-color)
588
+ */
589
+
590
+ function colorLuminance(hex, lum) {
591
+ // Validate hex string
592
+ hex = String(hex).replace(/[^0-9a-f]/gi, '');
593
+ if (hex.length < 6) {
594
+ hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
595
+ }
596
+ lum = lum || 0;
597
+
598
+ // Convert to decimal and change luminosity
599
+ var rgb = "#", c, i;
600
+ for (i = 0; i < 3; i++) {
601
+ c = parseInt(hex.substr(i*2,2), 16);
602
+ c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
603
+ rgb += ("00"+c).substr(c.length);
604
+ }
605
+
606
+ return rgb;
607
+ }
608
+
609
+ function hexToRgb(hex) {
610
+ var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
611
+ return result ? parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) : null;
612
+ }
613
+
614
+ // Add box-shadow style to button (depending on its chosen bg-color)
615
+ function setFocusStyle($button, bgColor) {
616
+ var rgbColor = hexToRgb(bgColor);
617
+ $button.style.boxShadow = '0 0 2px rgba(' + rgbColor +', 0.8), inset 0 0 0 1px rgba(0, 0, 0, 0.05)';
618
+ }
619
+
620
+
621
+
622
+ /*
623
+ * Animations
624
+ */
625
+
626
+ function openModal() {
627
+ var modal = getModal();
628
+ fadeIn(getOverlay(), 10);
629
+ show(modal);
630
+ addClass(modal, 'showSweetAlert');
631
+ removeClass(modal, 'hideSweetAlert');
632
+
633
+ previousActiveElement = document.activeElement;
634
+ var $okButton = modal.querySelector('button.confirm');
635
+ $okButton.focus();
636
+
637
+ setTimeout(function() {
638
+ addClass(modal, 'visible');
639
+ }, 500);
640
+ }
641
+
642
+ function closeModal() {
643
+ var modal = getModal();
644
+ fadeOut(getOverlay(), 5);
645
+ fadeOut(modal, 5);
646
+ removeClass(modal, 'showSweetAlert');
647
+ addClass(modal, 'hideSweetAlert');
648
+ removeClass(modal, 'visible');
649
+
650
+
651
+ // Reset icon animations
652
+
653
+ var $successIcon = modal.querySelector('.icon.success');
654
+ removeClass($successIcon, 'animate');
655
+ removeClass($successIcon.querySelector('.tip'), 'animateSuccessTip');
656
+ removeClass($successIcon.querySelector('.long'), 'animateSuccessLong');
657
+
658
+ var $errorIcon = modal.querySelector('.icon.error');
659
+ removeClass($errorIcon, 'animateErrorIcon');
660
+ removeClass($errorIcon.querySelector('.x-mark'), 'animateXMark');
661
+
662
+ var $warningIcon = modal.querySelector('.icon.warning');
663
+ removeClass($warningIcon, 'pulseWarning');
664
+ removeClass($warningIcon.querySelector('.body'), 'pulseWarningIns');
665
+ removeClass($warningIcon.querySelector('.dot'), 'pulseWarningIns');
666
+
667
+
668
+ // Reset the page to its previous state
669
+ window.onkeydown = previousWindowKeyDown;
670
+ document.onclick = previousDocumentClick;
671
+ if (previousActiveElement) {
672
+ previousActiveElement.focus();
673
+ }
674
+ lastFocusedButton = undefined;
675
+ }
676
+
677
+
678
+ /*
679
+ * Set "margin-top"-property on modal based on its computed height
680
+ */
681
+
682
+ function fixVerticalPosition() {
683
+ var modal = getModal();
684
+
685
+ modal.style.marginTop = getTopMargin(getModal());
686
+ }
687
+
688
+
689
+
690
+ /*
691
+ * If library is injected after page has loaded
692
+ */
693
+
694
+ (function () {
695
+ if (document.readyState === "complete" || document.readyState === "interactive") {
696
+ initialize();
697
+ } else {
698
+ if (document.addEventListener) {
699
+ document.addEventListener('DOMContentLoaded', function factorial() {
700
+ document.removeEventListener('DOMContentLoaded', arguments.callee, false);
701
+ initialize();
702
+ }, false);
703
+ } else if (document.attachEvent) {
704
+ document.attachEvent('onreadystatechange', function() {
705
+ if (document.readyState === 'complete') {
706
+ document.detachEvent('onreadystatechange', arguments.callee);
707
+ initialize();
708
+ }
709
+ });
710
+ }
711
+ }
712
+ })();
713
+
714
+ })(window, document);