linkbutton.js

jquery.linkbutton.js

 

/**
 * linkbutton - jQuery EasyUI
 * 
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2010 stworthy [ stworthy@gmail.com ] 
 */
(function($){
	
	function createButton(target) {
		var opts = $.data(target, 'linkbutton').options;
		
		$(target).empty();
		$(target).addClass('l-btn');
		if (opts.id){
			$(target).attr('id', opts.id);
		} else {
			$(target).removeAttr('id');
		}
		if (opts.plain){
			$(target).addClass('l-btn-plain');
		} else {
			$(target).removeClass('l-btn-plain');
		}
		
		if (opts.text){
			$(target).html(opts.text).wrapInner(
					'<span class="l-btn-left">' +
					'<span class="l-btn-text">' +
					'</span>' +
					'</span>'
			);
			if (opts.iconCls){
				$(target).find('.l-btn-text').addClass(opts.iconCls).css('padding-left', '20px');
			}
		} else {
			$(target).html(' ').wrapInner(
					'<span class="l-btn-left">' +
					'<span class="l-btn-text">' +
					'<span class="l-btn-empty"></span>' +
					'</span>' +
					'</span>'
			);
			if (opts.iconCls){
				$(target).find('.l-btn-empty').addClass(opts.iconCls);
			}
		}
		
		setDisabled(target, opts.disabled);
	}
	
	function setDisabled(target, disabled){
		var state = $.data(target, 'linkbutton');
		if (disabled){
			state.options.disabled = true;
			var href = $(target).attr('href');
			if (href){
				state.href = href;
				$(target).attr('href', 'javascript:void(0)');
			}
			var onclick = $(target).attr('onclick');
			if (onclick) {
				state.onclick = onclick;
				$(target).attr('onclick', null);
			}
			$(target).addClass('l-btn-disabled');
		} else {
			state.options.disabled = false;
			if (state.href) {
				$(target).attr('href', state.href);
			}
			if (state.onclick) {
				target.onclick = state.onclick;
			}
			$(target).removeClass('l-btn-disabled');
		}
	}
	/**
	 * 扩展jQuery对象的属性方法
	 */
	$.fn.linkbutton = function(options){
		if (typeof options == 'string'){
			switch(options){
			case 'options':
				return $.data(this[0], 'linkbutton').options;
			case 'enable':
				return this.each(function(){
					setDisabled(this, false);
				});
			case 'disable':
				return this.each(function(){
					setDisabled(this, true);
				});
			}
		}
		
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'linkbutton');
			if (state){
				$.extend(state.options, options);
			} else {
				var t = $(this);
				$.data(this, 'linkbutton', {
					options: $.extend({}, $.fn.linkbutton.defaults, {
						id: t.attr('id'),
						disabled: (t.attr('disabled') ? true : undefined),
						plain: (t.attr('plain') ? t.attr('plain') == 'true' : undefined),
						text: $.trim(t.html()),
						iconCls: t.attr('icon')
					}, options)
				});
				t.removeAttr('disabled');
			}
			
			createButton(this);
		});
	};
	
	$.fn.linkbutton.defaults = {
			id: null,
			disabled: false,
			plain: false,
			text: '',
			iconCls: null
	};
	
})(jQuery);

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值