2014-11-19 09:23:10 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ngModule = angular.module('woUtil');
|
|
|
|
ngModule.service('dialog', Dialog);
|
2014-11-19 11:19:55 -05:00
|
|
|
module.exports = Dialog;
|
2014-11-19 09:23:10 -05:00
|
|
|
|
2014-11-21 06:13:06 -05:00
|
|
|
/**
|
|
|
|
* A central service to display messages to the user in a dialog
|
|
|
|
*/
|
|
|
|
function Dialog($q, axe) {
|
|
|
|
this._q = $q;
|
|
|
|
this._axe = axe;
|
2014-11-25 11:46:33 -05:00
|
|
|
|
|
|
|
// binds the methods to the instance of the dialog service so that we can e.g.
|
|
|
|
// pass dialog.error as a callback to asynchronous functions without having to
|
|
|
|
// do dialog.error.bind(dialog) every time
|
|
|
|
this.info = this.info.bind(this);
|
|
|
|
this.error = this.error.bind(this);
|
|
|
|
this.confirm = this.confirm.bind(this);
|
2014-11-21 06:13:06 -05:00
|
|
|
}
|
2014-11-19 09:23:10 -05:00
|
|
|
|
2014-11-21 06:13:06 -05:00
|
|
|
/**
|
|
|
|
* Show an information dialog
|
|
|
|
* @param {String} options.title The title of the displayed dialog
|
|
|
|
* @param {String} options.message The message to be displayed
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2014-11-20 09:14:39 -05:00
|
|
|
Dialog.prototype.info = function(options) {
|
2014-11-26 06:50:16 -05:00
|
|
|
return this._handle(options, this.displayInfo, 'displayInfo');
|
2014-11-20 09:14:39 -05:00
|
|
|
};
|
2014-11-19 09:23:10 -05:00
|
|
|
|
2014-11-21 06:13:06 -05:00
|
|
|
/**
|
|
|
|
* Show an error dialog
|
|
|
|
* @param {String} options.title (optional) The title of the displayed dialog
|
|
|
|
* @param {String} options.message The message to be displayed
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2014-11-20 09:14:39 -05:00
|
|
|
Dialog.prototype.error = function(options) {
|
2014-11-21 06:13:06 -05:00
|
|
|
// log the error
|
2014-11-25 11:46:33 -05:00
|
|
|
if (options) {
|
|
|
|
this._axe.error((options.errMsg || options.message) + (options.stack ? ('\n' + options.stack) : ''));
|
|
|
|
}
|
2014-11-26 06:50:16 -05:00
|
|
|
return this._handle(options, this.displayError, 'displayError');
|
2014-11-20 09:14:39 -05:00
|
|
|
};
|
2014-11-19 09:23:10 -05:00
|
|
|
|
2014-11-21 06:13:06 -05:00
|
|
|
/**
|
|
|
|
* Show an confirm dialog
|
|
|
|
* @param {String} options.title The title of the displayed dialog
|
|
|
|
* @param {String} options.message The message to be displayed
|
|
|
|
* @param {String} options.callback The callback that is called after the confirmation has been granted or denied
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2014-11-20 09:14:39 -05:00
|
|
|
Dialog.prototype.confirm = function(options) {
|
2014-11-26 06:50:16 -05:00
|
|
|
return this._handle(options, this.displayConfirm, 'displayConfirm');
|
2014-11-21 06:13:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function which returns a promise
|
|
|
|
*/
|
|
|
|
Dialog.prototype._handle = function(options, fn, errMsg) {
|
|
|
|
return this._q(function(resolve, reject) {
|
|
|
|
if (fn) {
|
2014-11-26 11:57:14 -05:00
|
|
|
return resolve(fn(options));
|
2014-11-21 06:13:06 -05:00
|
|
|
} else {
|
|
|
|
reject(new Error('Dialog service ' + errMsg + ' not set!'));
|
|
|
|
}
|
|
|
|
});
|
2014-11-20 09:14:39 -05:00
|
|
|
};
|