8b660d2d1ca8b3052b60990730813978208443aa.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * jQuery EasyUI 1.3.6
  3. *
  4. * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
  7. * To use it on other terms please contact us at info@jeasyui.com
  8. *
  9. */
  10. /**
  11. * linkbutton - jQuery EasyUI
  12. *
  13. */
  14. (function($){
  15. function createButton(target) {
  16. var opts = $.data(target, 'linkbutton').options;
  17. var t = $(target).empty();
  18. t.addClass('l-btn').removeClass('l-btn-plain l-btn-selected l-btn-plain-selected');
  19. t.removeClass('l-btn-small l-btn-medium l-btn-large').addClass('l-btn-'+opts.size);
  20. if (opts.plain){t.addClass('l-btn-plain')}
  21. if (opts.selected){
  22. t.addClass(opts.plain ? 'l-btn-selected l-btn-plain-selected' : 'l-btn-selected');
  23. }
  24. t.attr('group', opts.group || '');
  25. t.attr('id', opts.id || '');
  26. var inner = $('<span class="l-btn-left"></span>').appendTo(t);
  27. if (opts.text){
  28. $('<span class="l-btn-text"></span>').html(opts.text).appendTo(inner);
  29. } else {
  30. $('<span class="l-btn-text l-btn-empty">&nbsp;</span>').appendTo(inner);
  31. }
  32. if (opts.iconCls){
  33. $('<span class="l-btn-icon">&nbsp;</span>').addClass(opts.iconCls).appendTo(inner);
  34. inner.addClass('l-btn-icon-'+opts.iconAlign);
  35. }
  36. t.unbind('.linkbutton').bind('focus.linkbutton',function(){
  37. if (!opts.disabled){
  38. $(this).addClass('l-btn-focus');
  39. }
  40. }).bind('blur.linkbutton',function(){
  41. $(this).removeClass('l-btn-focus');
  42. }).bind('click.linkbutton',function(){
  43. if (!opts.disabled){
  44. if (opts.toggle){
  45. if (opts.selected){
  46. $(this).linkbutton('unselect');
  47. } else {
  48. $(this).linkbutton('select');
  49. }
  50. }
  51. opts.onClick.call(this);
  52. }
  53. return false;
  54. });
  55. // if (opts.toggle && !opts.disabled){
  56. // t.bind('click.linkbutton', function(){
  57. // if (opts.selected){
  58. // $(this).linkbutton('unselect');
  59. // } else {
  60. // $(this).linkbutton('select');
  61. // }
  62. // });
  63. // }
  64. setSelected(target, opts.selected)
  65. setDisabled(target, opts.disabled);
  66. }
  67. function setSelected(target, selected){
  68. var opts = $.data(target, 'linkbutton').options;
  69. if (selected){
  70. if (opts.group){
  71. $('a.l-btn[group="'+opts.group+'"]').each(function(){
  72. var o = $(this).linkbutton('options');
  73. if (o.toggle){
  74. $(this).removeClass('l-btn-selected l-btn-plain-selected');
  75. o.selected = false;
  76. }
  77. });
  78. }
  79. $(target).addClass(opts.plain ? 'l-btn-selected l-btn-plain-selected' : 'l-btn-selected');
  80. opts.selected = true;
  81. } else {
  82. if (!opts.group){
  83. $(target).removeClass('l-btn-selected l-btn-plain-selected');
  84. opts.selected = false;
  85. }
  86. }
  87. }
  88. function setDisabled(target, disabled){
  89. var state = $.data(target, 'linkbutton');
  90. var opts = state.options;
  91. $(target).removeClass('l-btn-disabled l-btn-plain-disabled');
  92. if (disabled){
  93. opts.disabled = true;
  94. var href = $(target).attr('href');
  95. if (href){
  96. state.href = href;
  97. $(target).attr('href', 'javascript:void(0)');
  98. }
  99. if (target.onclick){
  100. state.onclick = target.onclick;
  101. target.onclick = null;
  102. }
  103. opts.plain ? $(target).addClass('l-btn-disabled l-btn-plain-disabled') : $(target).addClass('l-btn-disabled');
  104. } else {
  105. opts.disabled = false;
  106. if (state.href) {
  107. $(target).attr('href', state.href);
  108. }
  109. if (state.onclick) {
  110. target.onclick = state.onclick;
  111. }
  112. }
  113. }
  114. $.fn.linkbutton = function(options, param){
  115. if (typeof options == 'string'){
  116. return $.fn.linkbutton.methods[options](this, param);
  117. }
  118. options = options || {};
  119. return this.each(function(){
  120. var state = $.data(this, 'linkbutton');
  121. if (state){
  122. $.extend(state.options, options);
  123. } else {
  124. $.data(this, 'linkbutton', {
  125. options: $.extend({}, $.fn.linkbutton.defaults, $.fn.linkbutton.parseOptions(this), options)
  126. });
  127. $(this).removeAttr('disabled');
  128. }
  129. createButton(this);
  130. });
  131. };
  132. $.fn.linkbutton.methods = {
  133. options: function(jq){
  134. return $.data(jq[0], 'linkbutton').options;
  135. },
  136. enable: function(jq){
  137. return jq.each(function(){
  138. setDisabled(this, false);
  139. });
  140. },
  141. disable: function(jq){
  142. return jq.each(function(){
  143. setDisabled(this, true);
  144. });
  145. },
  146. select: function(jq){
  147. return jq.each(function(){
  148. setSelected(this, true);
  149. });
  150. },
  151. unselect: function(jq){
  152. return jq.each(function(){
  153. setSelected(this, false);
  154. });
  155. }
  156. };
  157. $.fn.linkbutton.parseOptions = function(target){
  158. var t = $(target);
  159. return $.extend({}, $.parser.parseOptions(target,
  160. ['id','iconCls','iconAlign','group','size',{plain:'boolean',toggle:'boolean',selected:'boolean'}]
  161. ), {
  162. disabled: (t.attr('disabled') ? true : undefined),
  163. text: $.trim(t.html()),
  164. iconCls: (t.attr('icon') || t.attr('iconCls'))
  165. });
  166. };
  167. $.fn.linkbutton.defaults = {
  168. id: null,
  169. disabled: false,
  170. toggle: false,
  171. selected: false,
  172. group: null,
  173. plain: false,
  174. text: '',
  175. iconCls: null,
  176. iconAlign: 'left',
  177. size: 'small', // small,large
  178. onClick: function(){}
  179. };
  180. })(jQuery);