linkbutton.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * linkbutton - jQuery EasyUI
  3. *
  4. * Licensed under the GPL:
  5. * http://www.gnu.org/licenses/gpl.txt
  6. *
  7. * Copyright 2010 stworthy [ stworthy@gmail.com ]
  8. */
  9. (function($){
  10. function createButton(target) {
  11. var opts = $.data(target, 'linkbutton').options;
  12. $(target).empty();
  13. $(target).addClass('l-btn');
  14. if (opts.id){
  15. $(target).attr('id', opts.id);
  16. } else {
  17. $(target).removeAttr('id');
  18. }
  19. if (opts.plain){
  20. $(target).addClass('l-btn-plain');
  21. } else {
  22. $(target).removeClass('l-btn-plain');
  23. }
  24. if (opts.text){
  25. $(target).html(opts.text).wrapInner(
  26. '<span class="l-btn-left">' +
  27. '<span class="l-btn-text">' +
  28. '</span>' +
  29. '</span>'
  30. );
  31. if (opts.iconCls){
  32. $(target).find('.l-btn-text').addClass(opts.iconCls).css('padding-left', '20px');
  33. }
  34. } else {
  35. $(target).html('&nbsp;').wrapInner(
  36. '<span class="l-btn-left">' +
  37. '<span class="l-btn-text">' +
  38. '<span class="l-btn-empty"></span>' +
  39. '</span>' +
  40. '</span>'
  41. );
  42. if (opts.iconCls){
  43. $(target).find('.l-btn-empty').addClass(opts.iconCls);
  44. }
  45. }
  46. setDisabled(target, opts.disabled);
  47. }
  48. function setDisabled(target, disabled){
  49. var state = $.data(target, 'linkbutton');
  50. if (disabled){
  51. if(state.options.disabled==false){
  52. state.options.disabled = true;
  53. var href = $(target).attr('href');
  54. if (href){
  55. state.href = href;
  56. $(target).attr('href', 'javascript:void(0)');
  57. }
  58. var onclick = $(target).attr('onclick');
  59. if (onclick) {
  60. state.onclick = onclick;
  61. $(target).attr('onclick', 'javascript:void(0)');
  62. }
  63. $(target).addClass('l-btn-disabled');
  64. }
  65. } else {
  66. if(state.options.disabled==true){
  67. state.options.disabled = false;
  68. if (state.href) {
  69. $(target).attr('href', state.href);
  70. }
  71. if (state.onclick) {
  72. target.onclick = state.onclick;
  73. }
  74. $(target).removeClass('l-btn-disabled');
  75. }
  76. }
  77. }
  78. $.fn.linkbutton = function(options){
  79. if (typeof options == 'string'){
  80. switch(options){
  81. case 'options':
  82. return $.data(this[0], 'linkbutton').options;
  83. case 'enable':
  84. return this.each(function(){
  85. setDisabled(this, false);
  86. });
  87. case 'disable':
  88. return this.each(function(){
  89. setDisabled(this, true);
  90. });
  91. }
  92. }
  93. options = options || {};
  94. return this.each(function(){
  95. var state = $.data(this, 'linkbutton');
  96. if (state){
  97. $.extend(state.options, options);
  98. } else {
  99. var t = $(this);
  100. $.data(this, 'linkbutton', {
  101. options: $.extend({}, $.fn.linkbutton.defaults, {
  102. id: t.attr('id'),
  103. disabled: (t.attr('disabled') ? true : undefined),
  104. plain: (t.attr('plain') ? t.attr('plain') == 'true' : undefined),
  105. text: $.trim(t.html()),
  106. iconCls: t.attr('icon')
  107. }, options)
  108. });
  109. t.removeAttr('disabled');
  110. }
  111. createButton(this);
  112. });
  113. };
  114. $.fn.linkbutton.defaults = {
  115. id: null,
  116. disabled: false,
  117. plain: false,
  118. text: '',
  119. iconCls: null
  120. };
  121. })(jQuery);