;
// jquery toast plugin created by kamran ahmed copyright mit license 2015
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'],
init: function (options, elem) {
this.prepareoptions(options, $.toast.options);
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 = '';
this._toastel = this._toastel || $('
', {
class : 'jq-toast-single'
});
// for the loader on top
_toastcontent += '';
if ( this.options.allowtoastclose ) {
_toastcontent += '×';
};
if ( this.options.text instanceof array ) {
if ( this.options.heading ) {
_toastcontent +='' + this.options.heading + '
';
};
_toastcontent += '';
for (var i = 0; i < this.options.text.length; i++) {
_toastcontent += '- ' + this.options.text[i] + '
';
}
_toastcontent += '
';
} else {
if ( this.options.heading ) {
_toastcontent +='' + this.options.heading + '
';
};
_toastcontent += this.options.text;
}
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);
}
if ( this.options.icon !== false ) {
this._toastel.addclass('jq-has-icon');
if ( $.inarray(this.options.icon, this._defaulticons) !== -1 ) {
this._toastel.addclass('jq-icon-' + this.options.icon);
};
};
},
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,
bottom: 20
});
} else if ( this.options.position === 'top-center' ) {
this._container.css({
left: ( $(window).outerwidth() / 2 ) - this._container.outerwidth()/2,
top: 20
});
} 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.on('aftershown', function () {
that.processloader();
});
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');
});
} else if ( that.options.showhidetransition === 'slide' ) {
that._toastel.trigger('beforehide');
that._toastel.slideup(function () {
that._toastel.trigger('afterhidden');
});
} else {
that._toastel.trigger('beforehide');
that._toastel.hide(function () {
that._toastel.trigger('afterhidden');
});
}
});
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');
if ( _container.length === 0 ) {
_container = $('',{
class: "jq-toast-wrap"
});
$('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;
},
canautohide: function () {
return ( this.options.hideafter !== false ) && !isnan( parseint( this.options.hideafter, 10 ) );
},
processloader: function () {
// show the loader only, if auto-hide is on and loader is demanded
if (!this.canautohide() || this.options.loader === false) {
return false;
}
var loader = this._toastel.find('.jq-toast-loader');
// 400 is the default time that jquery uses for fade/slide
// divide by 1000 for milliseconds to seconds conversion
var transitiontime = (this.options.hideafter - 400) / 1000 + 's';
var loaderbg = this.options.loaderbg;
var style = loader.attr('style') || '';
style = style.substring(0, style.indexof('-webkit-transition')); // remove the last transition definition
style += '-webkit-transition: width ' + transitiontime + ' ease-in; \
-o-transition: width ' + transitiontime + ' ease-in; \
transition: width ' + transitiontime + ' ease-in; \
background-color: ' + loaderbg + ';';
loader.attr('style', style).addclass('jq-toast-loaded');
},
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.canautohide()) {
var that = this;
window.settimeout(function(){
if ( that.options.showhidetransition.tolowercase() === 'fade' ) {
that._toastel.trigger('beforehide');
that._toastel.fadeout(function () {
that._toastel.trigger('afterhidden');
});
} else if ( that.options.showhidetransition.tolowercase() === 'slide' ) {
that._toastel.trigger('beforehide');
that._toastel.slideup(function () {
that._toastel.trigger('afterhidden');
});
} else {
that._toastel.trigger('beforehide');
that._toastel.hide(function () {
that._toastel.trigger('afterhidden');
});
}
}, 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();
}
};
$.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 );
}
}
};
$.toast.options = {
text: '',
heading: '',
showhidetransition: 'fade',
allowtoastclose: true,
hideafter: 6000,
loader: true,
loaderbg: '#9ec600',
stack: 5,
position: 'bottom-left',
bgcolor: false,
textcolor: false,
textalign: 'left',
icon: false,
beforeshow: function () {},
aftershown: function () {},
beforehide: function () {},
afterhidden: function () {}
};
})( jquery, window, document );