Skip to content

Commit

Permalink
Merged with parent
Browse files Browse the repository at this point in the history
  • Loading branch information
lipis committed Oct 21, 2014
2 parents 5801437 + 5359ee6 commit 396f422
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 47 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions lib/sweet-alert.css
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,6 @@
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(238, 162, 54, 0.6);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(238, 162, 54, 0.6);
}
.sweet-alert button::-moz-focus-inner {
border: 0;
}
110 changes: 64 additions & 46 deletions lib/sweet-alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@

var modalClass = '.sweet-alert',
overlayClass = '.sweet-overlay',
alertTypes = ['error', 'warning', 'info', 'success'];
alertTypes = ['error', 'warning', 'info', 'success'],
defaultParams = {
title: '',
text: '',
type: null,
allowOutsideClick: false,
showCancelButton: false,
closeOnConfirm: true,
closeOnCancel: true,
confirmButtonText: 'OK',
confirmButtonClass: 'btn-primary',
cancelButtonText: 'Cancel',
imageUrl: null,
imageSize: null
};


/*
Expand Down Expand Up @@ -160,7 +174,7 @@
* Add modal + overlay to DOM
*/

function initialize() {
window.sweetAlertInitialize = function() {
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 class="lead text-muted">Text</p><p><button class="cancel btn btn-default btn-lg" tabIndex="2">Cancel</button> <button class="confirm btn btn-lg" tabIndex="1">OK</button></p></div>',
sweetWrap = document.createElement('div');

Expand All @@ -171,43 +185,26 @@

// For development use only!
/*jQuery.ajax({
url: '../lib/sweet-alert.html', // Change path depending on file location
dataType: 'html'
})
.done(function(html) {
jQuery('body').append(html);
});*/
url: '../lib/sweet-alert.html', // Change path depending on file location
dataType: 'html'
})
.done(function(html) {
jQuery('body').append(html);
});*/
}



/*
* Global sweetAlert function
*/

window.sweetAlert = window.swal = function() {

// Default parameters
var params = {
title: '',
text: '',
type: null,
allowOutsideClick: false,
showCancelButton: false,
closeOnConfirm: true,
closeOnCancel: true,
confirmButtonText: 'OK',
confirmButtonClass: 'btn-primary',
cancelButtonText: 'Cancel',
imageUrl: null,
imageSize: null
};

if (arguments[0] === undefined) {
window.console.error('sweetAlert expects at least 1 attribute!');
return false;
}

var params = extend({}, defaultParams);

switch (typeof arguments[0]) {

case 'string':
Expand All @@ -224,21 +221,21 @@
}

params.title = arguments[0].title;
params.text = arguments[0].text || params.text;
params.type = arguments[0].type || params.type;
params.allowOutsideClick = arguments[0].allowOutsideClick || params.allowOutsideClick;
params.showCancelButton = arguments[0].showCancelButton !== undefined ? arguments[0].showCancelButton : params.showCancelButton;
params.closeOnConfirm = arguments[0].closeOnConfirm !== undefined ? arguments[0].closeOnConfirm : params.closeOnConfirm;
params.closeOnCancel = arguments[0].closeOnCancel !== undefined ? arguments[0].closeOnCancel : params.closeOnCancel;
params.text = arguments[0].text || defaultParams.text;
params.type = arguments[0].type || defaultParams.type;
params.allowOutsideClick = arguments[0].allowOutsideClick || defaultParams.allowOutsideClick;
params.showCancelButton = arguments[0].showCancelButton !== undefined ? arguments[0].showCancelButton : defaultParams.showCancelButton;
params.closeOnConfirm = arguments[0].closeOnConfirm !== undefined ? arguments[0].closeOnConfirm : defaultParams.closeOnConfirm;
params.closeOnCancel = arguments[0].closeOnCancel !== undefined ? arguments[0].closeOnCancel : defaultParams.closeOnCancel;

// Show "Confirm" instead of "OK" if cancel button is visible
params.confirmButtonText = (params.showCancelButton) ? 'Confirm' : params.confirmButtonText;
params.confirmButtonText = (defaultParams.showCancelButton) ? 'Confirm' : defaultParams.confirmButtonText;

params.confirmButtonText = arguments[0].confirmButtonText || params.confirmButtonText;
params.confirmButtonClass = arguments[0].confirmButtonClass || params.confirmButtonClass;
params.cancelButtonText = arguments[0].cancelButtonText || params.cancelButtonText;
params.imageUrl = arguments[0].imageUrl || params.imageUrl;
params.imageSize = arguments[0].imageSize || params.imageSize;
params.confirmButtonText = arguments[0].confirmButtonText || defaultParams.confirmButtonText;
params.confirmButtonClass = arguments[0].confirmButtonClass || defaultParams.confirmButtonClass;
params.cancelButtonText = arguments[0].cancelButtonText || defaultParams.cancelButtonText;
params.imageUrl = arguments[0].imageUrl || defaultParams.imageUrl;
params.imageSize = arguments[0].imageSize || defaultParams.imageSize;
params.doneFunction = arguments[1] || null;

break;
Expand Down Expand Up @@ -427,6 +424,20 @@
};
};

/**
* Set default params for each popup
* @param {Object} userParams
*/
window.swal.setDefaults = function(userParams) {
if (!userParams) {
throw new Error('userParams is required');
}
if (typeof userParams !== 'object') {
throw new Error('userParams has to be a object');
}

extend(defaultParams, userParams);
};

/*
* Set type, text and actions on modal
Expand Down Expand Up @@ -569,6 +580,16 @@
return rgb;
}

function extend(a, b){
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}

return a;
}

function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) : null;
Expand All @@ -581,7 +602,6 @@
}



/*
* Animations
*/
Expand Down Expand Up @@ -644,30 +664,28 @@

function fixVerticalPosition() {
var modal = getModal();

modal.style.marginTop = getTopMargin(getModal());
}



/*
* If library is injected after page has loaded
*/

(function () {
if (document.readyState === "complete" || document.readyState === "interactive") {
initialize();
if (document.readyState === "complete" || document.readyState === "interactive" && document.body) {
sweetAlertInitialize();
} else {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', function factorial() {
document.removeEventListener('DOMContentLoaded', arguments.callee, false);
initialize();
sweetAlertInitialize();
}, false);
} else if (document.attachEvent) {
document.attachEvent('onreadystatechange', function() {
if (document.readyState === 'complete') {
document.detachEvent('onreadystatechange', arguments.callee);
initialize();
sweetAlertInitialize();
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions lib/sweet-alert.less
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@
.btn-warning {
.form-control-focus(@btn-warning-border);
}

button::-moz-focus-inner {
border: 0;
}
}
Loading

0 comments on commit 396f422

Please sign in to comment.