alertifyjs
Version:
AlertifyJS is a javascript framework for developing pretty browser dialogs and notifications.
186 lines (180 loc) • 7.3 kB
JavaScript
/**
* Confirm dialog object
*
* alertify.confirm(message);
* alertify.confirm(message, onok);
* alertify.confirm(message, onok, oncancel);
* alertify.confirm(title, message, onok, oncancel);
*/
alertify.dialog('confirm', function () {
var autoConfirm = {
timer: null,
index: null,
text: null,
duration: null,
task: function (event, self) {
if (self.isOpen()) {
self.__internal.buttons[autoConfirm.index].element.innerHTML = autoConfirm.text + ' (‏' + autoConfirm.duration + '‏) ';
autoConfirm.duration -= 1;
if (autoConfirm.duration === -1) {
clearAutoConfirm(self);
var button = self.__internal.buttons[autoConfirm.index];
var closeEvent = createCloseEvent(autoConfirm.index, button);
if (typeof self.callback === 'function') {
self.callback.apply(self, [closeEvent]);
}
//close the dialog.
if (closeEvent.close !== false) {
self.close();
}
}
} else {
clearAutoConfirm(self);
}
}
};
function clearAutoConfirm(self) {
if (autoConfirm.timer !== null) {
clearInterval(autoConfirm.timer);
autoConfirm.timer = null;
self.__internal.buttons[autoConfirm.index].element.innerHTML = autoConfirm.text;
}
}
function startAutoConfirm(self, index, duration) {
clearAutoConfirm(self);
autoConfirm.duration = duration;
autoConfirm.index = index;
autoConfirm.text = self.__internal.buttons[index].element.innerHTML;
autoConfirm.timer = setInterval(delegate(self, autoConfirm.task), 1000);
autoConfirm.task(null, self);
}
return {
main: function (_title, _message, _onok, _oncancel) {
var title, message, onok, oncancel;
switch (arguments.length) {
case 1:
message = _title;
break;
case 2:
message = _title;
onok = _message;
break;
case 3:
message = _title;
onok = _message;
oncancel = _onok;
break;
case 4:
title = _title;
message = _message;
onok = _onok;
oncancel = _oncancel;
break;
}
this.set('title', title);
this.set('message', message);
this.set('onok', onok);
this.set('oncancel', oncancel);
return this;
},
setup: function () {
return {
buttons: [
{
text: alertify.defaults.glossary.ok,
key: keys.ENTER,
className: alertify.defaults.theme.ok,
},
{
text: alertify.defaults.glossary.cancel,
key: keys.ESC,
invokeOnClose: true,
className: alertify.defaults.theme.cancel,
}
],
focus: {
element: 0,
select: false
},
options: {
maximizable: false,
resizable: false
}
};
},
build: function () {
//nothing
},
prepare: function () {
//nothing
},
setMessage: function (message) {
this.setContent(message);
},
settings: {
message: null,
labels: null,
onok: null,
oncancel: null,
defaultFocus: null,
reverseButtons: null,
},
settingUpdated: function (key, oldValue, newValue) {
switch (key) {
case 'message':
this.setMessage(newValue);
break;
case 'labels':
if ('ok' in newValue && this.__internal.buttons[0].element) {
this.__internal.buttons[0].text = newValue.ok;
this.__internal.buttons[0].element.innerHTML = newValue.ok;
}
if ('cancel' in newValue && this.__internal.buttons[1].element) {
this.__internal.buttons[1].text = newValue.cancel;
this.__internal.buttons[1].element.innerHTML = newValue.cancel;
}
break;
case 'reverseButtons':
if (newValue === true) {
this.elements.buttons.primary.appendChild(this.__internal.buttons[0].element);
} else {
this.elements.buttons.primary.appendChild(this.__internal.buttons[1].element);
}
break;
case 'defaultFocus':
this.__internal.focus.element = newValue === 'ok' ? 0 : 1;
break;
}
},
callback: function (closeEvent) {
clearAutoConfirm(this);
var returnValue;
switch (closeEvent.index) {
case 0:
if (typeof this.get('onok') === 'function') {
returnValue = this.get('onok').call(this, closeEvent);
if (typeof returnValue !== 'undefined') {
closeEvent.cancel = !returnValue;
}
}
break;
case 1:
if (typeof this.get('oncancel') === 'function') {
returnValue = this.get('oncancel').call(this, closeEvent);
if (typeof returnValue !== 'undefined') {
closeEvent.cancel = !returnValue;
}
}
break;
}
},
autoOk: function (duration) {
startAutoConfirm(this, 0, duration);
return this;
},
autoCancel: function (duration) {
startAutoConfirm(this, 1, duration);
return this;
}
};
});