forked from t4t5/sweetalert
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ | |
*.DS_STORE | ||
node_modules | ||
bower_components | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var defaultParams = { | ||
title: '', | ||
text: '', | ||
type: null, | ||
allowOutsideClick: false, | ||
showConfirmButton: true, | ||
showCancelButton: false, | ||
closeOnConfirm: true, | ||
closeOnCancel: true, | ||
confirmButtonText: 'OK', | ||
confirmButtonColor: '#8CD4F5', | ||
cancelButtonText: 'Cancel', | ||
imageUrl: null, | ||
imageSize: null, | ||
timer: null, | ||
customClass: '', | ||
html: false, | ||
animation: true, | ||
allowEscapeKey: true, | ||
inputType: 'text', | ||
inputPlaceholder: '', | ||
inputValue: '', | ||
showLoaderOnConfirm: false | ||
}; | ||
|
||
exports['default'] = defaultParams; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
|
||
var _colorLuminance = require('./utils'); | ||
|
||
var _getModal = require('./handle-swal-dom'); | ||
|
||
var _hasClass$isDescendant = require('./handle-dom'); | ||
|
||
/* | ||
* User clicked on "Confirm"/"OK" or "Cancel" | ||
*/ | ||
var handleButton = function handleButton(event, params, modal) { | ||
var e = event || window.event; | ||
var target = e.target || e.srcElement; | ||
|
||
var targetedConfirm = target.className.indexOf('confirm') !== -1; | ||
var targetedOverlay = target.className.indexOf('sweet-overlay') !== -1; | ||
var modalIsVisible = _hasClass$isDescendant.hasClass(modal, 'visible'); | ||
var doneFunctionExists = params.doneFunction && modal.getAttribute('data-has-done-function') === 'true'; | ||
|
||
// Since the user can change the background-color of the confirm button programmatically, | ||
// we must calculate what the color should be on hover/active | ||
var normalColor, hoverColor, activeColor; | ||
if (targetedConfirm && params.confirmButtonColor) { | ||
normalColor = params.confirmButtonColor; | ||
hoverColor = _colorLuminance.colorLuminance(normalColor, -0.04); | ||
activeColor = _colorLuminance.colorLuminance(normalColor, -0.14); | ||
} | ||
|
||
function shouldSetConfirmButtonColor(color) { | ||
if (targetedConfirm && params.confirmButtonColor) { | ||
target.style.backgroundColor = color; | ||
} | ||
} | ||
|
||
switch (e.type) { | ||
case 'mouseover': | ||
shouldSetConfirmButtonColor(hoverColor); | ||
break; | ||
|
||
case 'mouseout': | ||
shouldSetConfirmButtonColor(normalColor); | ||
break; | ||
|
||
case 'mousedown': | ||
shouldSetConfirmButtonColor(activeColor); | ||
break; | ||
|
||
case 'mouseup': | ||
shouldSetConfirmButtonColor(hoverColor); | ||
break; | ||
|
||
case 'focus': | ||
var $confirmButton = modal.querySelector('button.confirm'); | ||
var $cancelButton = modal.querySelector('button.cancel'); | ||
|
||
if (targetedConfirm) { | ||
$cancelButton.style.boxShadow = 'none'; | ||
} else { | ||
$confirmButton.style.boxShadow = 'none'; | ||
} | ||
break; | ||
|
||
case 'click': | ||
var clickedOnModal = modal === target; | ||
var clickedOnModalChild = _hasClass$isDescendant.isDescendant(modal, target); | ||
|
||
// Ignore click outside if allowOutsideClick is false | ||
if (!clickedOnModal && !clickedOnModalChild && modalIsVisible && !params.allowOutsideClick) { | ||
break; | ||
} | ||
|
||
if (targetedConfirm && doneFunctionExists && modalIsVisible) { | ||
handleConfirm(modal, params); | ||
} else if (doneFunctionExists && modalIsVisible || targetedOverlay) { | ||
handleCancel(modal, params); | ||
} else if (_hasClass$isDescendant.isDescendant(modal, target) && target.tagName === 'BUTTON') { | ||
sweetAlert.close(); | ||
} | ||
break; | ||
} | ||
}; | ||
|
||
/* | ||
* User clicked on "Confirm"/"OK" | ||
*/ | ||
var handleConfirm = function handleConfirm(modal, params) { | ||
var callbackValue = true; | ||
|
||
if (_hasClass$isDescendant.hasClass(modal, 'show-input')) { | ||
callbackValue = modal.querySelector('input').value; | ||
|
||
if (!callbackValue) { | ||
callbackValue = ''; | ||
} | ||
} | ||
|
||
params.doneFunction(callbackValue); | ||
|
||
if (params.closeOnConfirm) { | ||
sweetAlert.close(); | ||
} | ||
// Disable cancel and confirm button if the parameter is true | ||
if (params.showLoaderOnConfirm) { | ||
sweetAlert.disableButtons(); | ||
} | ||
}; | ||
|
||
/* | ||
* User clicked on "Cancel" | ||
*/ | ||
var handleCancel = function handleCancel(modal, params) { | ||
// Check if callback function expects a parameter (to track cancel actions) | ||
var functionAsStr = String(params.doneFunction).replace(/\s/g, ''); | ||
var functionHandlesCancel = functionAsStr.substring(0, 9) === 'function(' && functionAsStr.substring(9, 10) !== ')'; | ||
|
||
if (functionHandlesCancel) { | ||
params.doneFunction(false); | ||
} | ||
|
||
if (params.closeOnCancel) { | ||
sweetAlert.close(); | ||
} | ||
}; | ||
|
||
exports['default'] = { | ||
handleButton: handleButton, | ||
handleConfirm: handleConfirm, | ||
handleCancel: handleCancel | ||
}; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var hasClass = function hasClass(elem, className) { | ||
return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' '); | ||
}; | ||
|
||
var addClass = function addClass(elem, className) { | ||
if (!hasClass(elem, className)) { | ||
elem.className += ' ' + className; | ||
} | ||
}; | ||
|
||
var removeClass = function removeClass(elem, className) { | ||
var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' '; | ||
if (hasClass(elem, className)) { | ||
while (newClass.indexOf(' ' + className + ' ') >= 0) { | ||
newClass = newClass.replace(' ' + className + ' ', ' '); | ||
} | ||
elem.className = newClass.replace(/^\s+|\s+$/g, ''); | ||
} | ||
}; | ||
|
||
var escapeHtml = function escapeHtml(str) { | ||
var div = document.createElement('div'); | ||
div.appendChild(document.createTextNode(str)); | ||
return div.innerHTML; | ||
}; | ||
|
||
var _show = function _show(elem) { | ||
elem.style.opacity = ''; | ||
elem.style.display = 'block'; | ||
}; | ||
|
||
var show = function show(elems) { | ||
if (elems && !elems.length) { | ||
return _show(elems); | ||
} | ||
for (var i = 0; i < elems.length; ++i) { | ||
_show(elems[i]); | ||
} | ||
}; | ||
|
||
var _hide = function _hide(elem) { | ||
elem.style.opacity = ''; | ||
elem.style.display = 'none'; | ||
}; | ||
|
||
var hide = function hide(elems) { | ||
if (elems && !elems.length) { | ||
return _hide(elems); | ||
} | ||
for (var i = 0; i < elems.length; ++i) { | ||
_hide(elems[i]); | ||
} | ||
}; | ||
|
||
var isDescendant = function isDescendant(parent, child) { | ||
var node = child.parentNode; | ||
while (node !== null) { | ||
if (node === parent) { | ||
return true; | ||
} | ||
node = node.parentNode; | ||
} | ||
return false; | ||
}; | ||
|
||
var getTopMargin = function getTopMargin(elem) { | ||
elem.style.left = '-9999px'; | ||
elem.style.display = 'block'; | ||
|
||
var height = elem.clientHeight, | ||
padding; | ||
if (typeof getComputedStyle !== 'undefined') { | ||
// IE 8 | ||
padding = parseInt(getComputedStyle(elem).getPropertyValue('padding-top'), 10); | ||
} else { | ||
padding = parseInt(elem.currentStyle.padding); | ||
} | ||
|
||
elem.style.left = ''; | ||
elem.style.display = 'none'; | ||
return '-' + parseInt((height + padding) / 2) + 'px'; | ||
}; | ||
|
||
var fadeIn = function fadeIn(elem, interval) { | ||
if (+elem.style.opacity < 1) { | ||
interval = interval || 16; | ||
elem.style.opacity = 0; | ||
elem.style.display = 'block'; | ||
var last = +new Date(); | ||
var tick = (function (_tick) { | ||
function tick() { | ||
return _tick.apply(this, arguments); | ||
} | ||
|
||
tick.toString = function () { | ||
return _tick.toString(); | ||
}; | ||
|
||
return tick; | ||
})(function () { | ||
elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100; | ||
last = +new Date(); | ||
|
||
if (+elem.style.opacity < 1) { | ||
setTimeout(tick, interval); | ||
} | ||
}); | ||
tick(); | ||
} | ||
elem.style.display = 'block'; //fallback IE8 | ||
}; | ||
|
||
var fadeOut = function fadeOut(elem, interval) { | ||
interval = interval || 16; | ||
elem.style.opacity = 1; | ||
var last = +new Date(); | ||
var tick = (function (_tick2) { | ||
function tick() { | ||
return _tick2.apply(this, arguments); | ||
} | ||
|
||
tick.toString = function () { | ||
return _tick2.toString(); | ||
}; | ||
|
||
return tick; | ||
})(function () { | ||
elem.style.opacity = +elem.style.opacity - (new Date() - last) / 100; | ||
last = +new Date(); | ||
|
||
if (+elem.style.opacity > 0) { | ||
setTimeout(tick, interval); | ||
} else { | ||
elem.style.display = 'none'; | ||
} | ||
}); | ||
tick(); | ||
}; | ||
|
||
var fireClick = function fireClick(node) { | ||
// Taken from https://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/ | ||
// Then fixed for today's Chrome browser. | ||
if (typeof MouseEvent === 'function') { | ||
// Up-to-date approach | ||
var mevt = new MouseEvent('click', { | ||
view: window, | ||
bubbles: false, | ||
cancelable: true | ||
}); | ||
node.dispatchEvent(mevt); | ||
} else if (document.createEvent) { | ||
// Fallback | ||
var evt = document.createEvent('MouseEvents'); | ||
evt.initEvent('click', false, false); | ||
node.dispatchEvent(evt); | ||
} else if (document.createEventObject) { | ||
node.fireEvent('onclick'); | ||
} else if (typeof node.onclick === 'function') { | ||
node.onclick(); | ||
} | ||
}; | ||
|
||
var stopEventPropagation = function stopEventPropagation(e) { | ||
// In particular, make sure the space bar doesn't scroll the main window. | ||
if (typeof e.stopPropagation === 'function') { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
} else if (window.event && window.event.hasOwnProperty('cancelBubble')) { | ||
window.event.cancelBubble = true; | ||
} | ||
}; | ||
|
||
exports.hasClass = hasClass; | ||
exports.addClass = addClass; | ||
exports.removeClass = removeClass; | ||
exports.escapeHtml = escapeHtml; | ||
exports._show = _show; | ||
exports.show = show; | ||
exports._hide = _hide; | ||
exports.hide = hide; | ||
exports.isDescendant = isDescendant; | ||
exports.getTopMargin = getTopMargin; | ||
exports.fadeIn = fadeIn; | ||
exports.fadeOut = fadeOut; | ||
exports.fireClick = fireClick; | ||
exports.stopEventPropagation = stopEventPropagation; |
Oops, something went wrong.