bootstrap.alerts.js 1.52 KB
/**
 * 显示一个告警框
 * 作者:黄平
 * 日期:2016-04-11
 */
(function($) {
	$.fn.alerts = function(options, param) {
		var self = this;
		if (typeof (options) == "string") {
			var method = $.fn.alerts.methods[options];
			if (method){
				return method.call(this, param);
			}
		}
		return this.each(function() {
			var opt = $.extend({}, $.fn.alerts.defaults, options);
			self.data("alerts", opt);
			if (opt.url) {
				$.ajax({
					url : opt.url,
					data : opt.queryParams,
					type : opt.method,
					dataType : opt.dataType,
					success : function(data) {
						
					}
				});
			} else {
				_createAlerts.call(self, opt);
			}
		});
	};

	function _createAlerts(opt) {
		var jq = this;
		jq.empty();
		jq.addClass("alert alert-"+ opt.type +" alert-dismissable");
		var close = $("<button type='button' class='close'>").html("&times;").appendTo(jq);
		close.click(function() {
			jq.alerts("hide", "slow");
		});
		if (opt.content) {
			jq.append(opt.content);
		}
		if (opt.showOnCreate === true) {
			jq.alerts("show", "slow");
		} else {
			jq.alerts("hide", "slow");
		}
	}
	
	$.fn.alerts.methods = {
		hide : function(speed) {
			var jq = this;
			return this.each(function() {
				jq.hide(speed);
				jq.removeClass();
				jq.empty();
			});
		},
		show : function(speed) {
			var jq = this;
			return this.each(function() {
				jq.show(speed);
			});
		}
	};
	
	$.fn.alerts.event = {
		
	};
	$.fn.alerts.defaults = $.extend({}, $.fn.alerts.event, {
		content : "",
		showOnCreate : true,
		type : "info"
	});
})(jQuery);