if (typeof Object.create !== 'function') { Object.create = function(obj) { function F() {} F.prototype = obj; return new F(); }; } (function($, window, document, undefined) { "use strict"; var Toast = { _positionClasses : ['bottom-left', 'bottom-right', 'top-right', 'top-left', 'bottom-center', 'top-center', 'mid-center'], _defaultIcons : ['success', 'error', 'info', 'warning', 'ok', 'confirm'], init: function(options, elem) { this.prepareOptions(options, $.toast.options); if (this.options.func !== '') { switch(this.options.func) { case 'resetbypage': this.resetbypage(this.options.page) break; case 'resetbyid': this.resetbyid(this.options.id) break; case 'resetallnonpersistent': this.resetallnonpersistent(this.options.page) break; } } else { if (this.cancelbyidunique(this.options.id, this.options.isUniqueId)) return; this.process(); } }, prepareOptions: function(options, options_to_extend) { var _options = {}; if ((typeof options === 'string') || (options instanceof Array)) { _options.text = options; } else { _options = options; } this.options = $.extend({}, options_to_extend, _options); }, process: function() { this.setup(); this.addToDom(); this.position(); this.bindToast(); this.animate(); }, setup: function() { var _toastContent = '', err = '', persistent = (this.options.persistent) ? ' persistent-toast' : '', shadow = (this.options.isShadow) ? ' shadow' : ''; this._toastEl = this._toastEl || $('
', { class : 'jq-toast-single ' + this.options.page + persistent + shadow }); if (this.options.id != '') { this._toastEl.attr('toast-id', this.options.id); } if (this.options.allowToastClose) { _toastContent += '×'; }; if (this.options.icon !== false) { this._toastEl.addClass('jq-has-icon'); this._toastEl.addClass('jq-action-' + this.options.icon); if ($.inArray(this.options.icon, this._defaultIcons) !== -1) { //this._toastEl.addClass('jq-icon-' + this.options.icon); _toastContent += '
'; err = ' jq-' + this.options.icon; }; }; if (this.options.text instanceof Array) { _toastContent += '
'; if (this.options.heading) { _toastContent += '
' + this.options.heading + '
'; }; _toastContent += '
'; } else { _toastContent += '
'; if (this.options.heading) { _toastContent += '
' + this.options.heading + '
'; }; _toastContent += '
' + this.options.text + '
'; } _toastContent += '
'; this._toastEl.html(_toastContent); if (this.options.bgColor !== false) { this._toastEl.css("background-color", this.options.bgColor); }; if (this.options.textColor !== false) { this._toastEl.css("color", this.options.textColor); }; if (this.options.textAlign) { this._toastEl.css('text-align', this.options.textAlign); }; }, position: function() { if ((typeof this.options.position === 'string') && ($.inArray(this.options.position, this._positionClasses) !== -1)) { if (this.options.position === 'bottom-center') { this._container.css({ left: ($(window).outerWidth() / 2) - this._container.outerWidth()/2 }); this._container.addClass('bottom-center'); } else if (this.options.position === 'top-center') { this._container.css({ left: ($(window).outerWidth() / 2) - this._container.outerWidth()/2 }); this._container.addClass('top-center'); } else if (this.options.position === 'mid-center') { this._container.css({ left: ($(window).outerWidth() / 2) - this._container.outerWidth()/2, top: ($(window).outerHeight() / 2) - this._container.outerHeight()/2 }); } else { this._container.addClass(this.options.position); } } else if (typeof this.options.position === 'object') { this._container.css({ top : this.options.position.top ? this.options.position.top : 'auto', bottom : this.options.position.bottom ? this.options.position.bottom : 'auto', left : this.options.position.left ? this.options.position.left : 'auto', right : this.options.position.right ? this.options.position.right : 'auto' }); } else { this._container.addClass('bottom-left'); } }, bindToast: function() { var that = this; this._toastEl.find('.close-jq-toast-single').on('click', function(e) { e.preventDefault(); if(that.options.showHideTransition === 'fade') { that._toastEl.trigger('beforeHide'); that._toastEl.fadeOut(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } else if (that.options.showHideTransition === 'slide') { that._toastEl.trigger('beforeHide'); that._toastEl.slideUp(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } else { that._toastEl.trigger('beforeHide'); that._toastEl.hide(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } }); if (typeof this.options.beforeShow == 'function') { this._toastEl.on('beforeShow', function() { that.options.beforeShow(); }); }; if (typeof this.options.afterShown == 'function') { this._toastEl.on('afterShown', function() { that.options.afterShown(); }); }; if (typeof this.options.beforeHide == 'function') { this._toastEl.on('beforeHide', function() { that.options.beforeHide(); }); }; if (typeof this.options.afterHidden == 'function') { this._toastEl.on('afterHidden', function() { that.options.afterHidden(); }); }; }, addToDom: function() { //var _container = $('.jq-toast-wrap'); var _container = $('.jq-toast-wrap-' + this.options.position); if (_container.length === 0) { _container = $('
',{ class: "jq-toast-wrap" + " jq-toast-wrap-" + this.options.position }); $('body').append(_container); } else if (!this.options.stack || isNaN(parseInt(this.options.stack, 10))) { _container.empty(); } _container.find('.jq-toast-single:hidden').remove(); _container.append(this._toastEl); if (this.options.stack && !isNaN(parseInt(this.options.stack), 10)) { var _prevToastCount = _container.find('.jq-toast-single').length, _extToastCount = _prevToastCount - this.options.stack; if (_extToastCount > 0) { $('.jq-toast-wrap').find('.jq-toast-single').slice(0, _extToastCount).remove(); }; } this._container = _container; }, animate: function() { var that = this; this._toastEl.hide(); this._toastEl.trigger('beforeShow'); if (this.options.showHideTransition.toLowerCase() === 'fade') { this._toastEl.fadeIn(function(){ that._toastEl.trigger('afterShown'); }); } else if (this.options.showHideTransition.toLowerCase() === 'slide') { this._toastEl.slideDown(function(){ that._toastEl.trigger('afterShown'); }); } else { this._toastEl.show(function(){ that._toastEl.trigger('afterShown'); }); } if ((this.options.hideAfter !== false) && !isNaN(parseInt(this.options.hideAfter, 10))) { var that = this; window.setTimeout(function(){ if (that.options.showHideTransition.toLowerCase() === 'fade') { that._toastEl.trigger('beforeHide'); that._toastEl.fadeOut(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } else if (that.options.showHideTransition.toLowerCase() === 'slide') { that._toastEl.trigger('beforeHide'); that._toastEl.slideUp(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } else { that._toastEl.trigger('beforeHide'); that._toastEl.hide(function() { that._toastEl.trigger('afterHidden'); that._toastEl.remove(); }); } }, this.options.hideAfter); }; }, reset: function(resetWhat) { if (resetWhat === 'all') $('.jq-toast-wrap').remove(); else this._toastEl.remove(); }, update: function(options) { this.prepareOptions(options, this.options); this.setup(); this.bindToast(); }, resetbypage: function(page) { //$('.' + page).remove(); $('.' + page).each(function() { if (!$(this).hasClass('persistent-toast')) $(this).remove(); }) }, resetbyid: function(toastid) { $('.jq-toast-single').each(function() { var attr = $(this).attr('toast-id'); if (typeof attr !== typeof undefined && attr !== false) { if (attr == toastid) $(this).remove(); } }) }, cancelbyidunique: function(toastid, toastunique) { if (!toastunique) return false; if (toastid == '') return false; var iscancel = false; $('.jq-toast-single').each(function() { var attr = $(this).attr('toast-id'); if (typeof attr !== typeof undefined && attr !== false) { if (attr == toastid) iscancel = true; } }) return iscancel; }, resetallnonpersistent: function(page) { $('.jq-toast-single').each(function() { if (!$(this).hasClass('persistent-toast')) $(this).remove(); }) }, }; $.toast = function(options) { var toast = Object.create(Toast); toast.init(options, this); return { reset: function(what) { toast.reset(what); }, update: function(options) { toast.update(options); }, resetbypage: function(page) { toast.resetbypage(page); }, resetbyid: function(toastid) { toast.resetbyid(toastid); }, cancelbyidunique: function(toastid, toastunique) { toast.cancelbyidunique(toastid, toastunique); }, resetallnonpersistent: function() { toast.resetallnonpersistent(); } } }; $.toast.options = { id: '', text: '', page: '', heading: '', toasttype: '', showHideTransition: 'slide', allowToastClose: true, hideAfter: 5000, stack: 25, position: 'bottom-right', bgColor: false, textColor: false, textAlign: 'left', icon: false, func: '', isShadow: true, persistent: false, isUniqueId: true, beforeShow: function() {}, afterShown: function() {}, beforeHide: function() {}, afterHidden: function() {} }; })(jQuery, window, document);