Skip to content

Instantly share code, notes, and snippets.

@serradura
Forked from dbalexandre/notify.js
Last active August 29, 2015 14:05
Show Gist options
  • Save serradura/cd125121168ed5704eed to your computer and use it in GitHub Desktop.
Save serradura/cd125121168ed5704eed to your computer and use it in GitHub Desktop.
/*************
CONSTRUCTOR:
*************
Notify.options = {"closeButton": true}; // Defines global options.
notify = new Notify({"closeButton": false}); // You can pass custom options
*/
/*******
USAGE
*******
notify.success('MSG', 'TITLE');
// The title is optional;
notify.info('TEXT');
// Available methods:
// info, success, warning, and error
*/
/******************
DEFAULT MESSAGES:
*****************
var notifyOptions = {
messages: {
success: 'DEFAULT SUCCESS MSG',
error: {
title: 'TITLE',
body: 'ERROR TEXT'
},
info: {
title: 'TITLE',
text: 'INFO TEXT' // You can use 'text' instead of 'body'.
},
}
};
notify = new Notify(notifyOptions);
notify.success(); // Will show 'DEFAULT SUCCESS MSG';
notify.success('another msg'); // Will show 'another msg';
notify.error(); // Title 'TITLE', Body: 'ERROR TEXT';
notify.info(); // Title 'TITLE', Body: 'INFO TEXT';
*/
/************
EXCEPTIONS:
************
notify.error(); // actions without a msg, or default message, will throw an error.
*/
/***********
UTILITIES:
***********
newNotify = notify.build(); // return a new instance, accepts the constructor options.
newNotify.error('YOUR ERROR MSG');
notify.set({"closeButton": true}) // Redefine the instance options.
*/
var Notify = (function() {
function Notify(options) {
this.set(options);
};
Notify.prototype = {
constructor: Notify,
set: function(options) {
setOptions.call(this, options);
return this;
},
info: function() {
notificator.apply(this, notifierArgs('info', arguments));
},
success: function() {
notificator.apply(this, notifierArgs('success', arguments));
},
warning: function() {
notificator.apply(this, notifierArgs('warning', arguments));
},
error: function() {
notificator.apply(this, notifierArgs('error', arguments));
},
clear: function() {
this._client().clear();
},
build: function(options) {
return new this.constructor(options);
},
includeTo: function(type, options) {
if ( !type || !/^(f|o)/.test(typeof type) ) {
throw "notify.includeTo(type, options): type must be an object";
}
type.prototype.notify = this.build(options);
},
_client: function() {
return getClient.call(this);
}
};
function notifierArgs(action, args) {
return [action, args[0], args[1]];
}
function setOptions(customOptions) {
var options = customOptions || {},
defaultOptions = this.constructor.options || {};
setMessages.call(this, options);
if( this._options ) delete this._options;
this._options = $.extend(defaultOptions, options);
};
function setMessages(options) {
if( this._messages ) delete this._messages;
this._messages = options.messages;
};
function getClient() {
toastr.options = this._options;
return toastr;
};
function notificator(action, body, title) {
var client = this._client(),
data = body || (this._messages && this._messages[action]);
if(!data) throw 'Notify: messages argument is required.';
if(typeof(data) === 'object') {
client[action](data.text || data.body, data.title || title);
} else {
client[action](data, title);
}
};
return Notify;
})();
@serradura
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment