mui.button.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Button
  3. * @param {type} $
  4. * @param {type} window
  5. * @param {type} document
  6. * @returns {undefined}
  7. */
  8. (function($, window, document) {
  9. var CLASS_ICON = $.className('icon');
  10. var CLASS_DISABLED = $.className('disabled');
  11. var STATE_RESET = 'reset';
  12. var STATE_LOADING = 'loading';
  13. var defaultOptions = {
  14. loadingText: 'Loading...', //文案
  15. loadingIcon: $.className('spinner') + ' ' + $.className('spinner-white'), //图标,可为空
  16. loadingIconPosition: 'left' //图标所处位置,仅支持left|right
  17. };
  18. var Button = function(element, options) {
  19. this.element = element;
  20. this.options = $.extend({}, defaultOptions, options);
  21. if (!this.options.loadingText) {
  22. this.options.loadingText = defaultOptions.loadingText;
  23. }
  24. if (this.options.loadingIcon === null) {
  25. this.options.loadingIcon = $.className('spinner');
  26. if ($.getStyles(this.element, 'color') === 'rgb(255, 255, 255)') {
  27. this.options.loadingIcon += ' ' + $.className('spinner-white');
  28. }
  29. }
  30. this.isInput = this.element.tagName === 'INPUT';
  31. this.resetHTML = this.isInput ? this.element.value : this.element.innerHTML;
  32. this.state = '';
  33. };
  34. Button.prototype.loading = function() {
  35. this.setState(STATE_LOADING);
  36. };
  37. Button.prototype.reset = function() {
  38. this.setState(STATE_RESET);
  39. };
  40. Button.prototype.setState = function(state) {
  41. if (this.state === state) {
  42. return false;
  43. }
  44. this.state = state;
  45. if (state === STATE_RESET) {
  46. this.element.disabled = false;
  47. this.element.classList.remove(CLASS_DISABLED);
  48. this.setHtml(this.resetHTML);
  49. } else if (state === STATE_LOADING) {
  50. this.element.disabled = true;
  51. this.element.classList.add(CLASS_DISABLED);
  52. var html = this.isInput ? this.options.loadingText : ('<span>' + this.options.loadingText + '</span>');
  53. if (this.options.loadingIcon && !this.isInput) {
  54. if (this.options.loadingIconPosition === 'right') {
  55. html += '&nbsp;<span class="' + this.options.loadingIcon + '"></span>';
  56. } else {
  57. html = '<span class="' + this.options.loadingIcon + '"></span>&nbsp;' + html;
  58. }
  59. }
  60. this.setHtml(html);
  61. }
  62. };
  63. Button.prototype.setHtml = function(html) {
  64. if (this.isInput) {
  65. this.element.value = html;
  66. } else {
  67. this.element.innerHTML = html;
  68. }
  69. }
  70. $.fn.button = function(state) {
  71. var buttonApis = [];
  72. this.each(function() {
  73. var buttonApi = this.mui_plugin_button;
  74. if (!buttonApi) {
  75. var loadingText = this.getAttribute('data-loading-text');
  76. var loadingIcon = this.getAttribute('data-loading-icon');
  77. var loadingIconPosition = this.getAttribute('data-loading-icon-position');
  78. this.mui_plugin_button = buttonApi = new Button(this, {
  79. loadingText: loadingText,
  80. loadingIcon: loadingIcon,
  81. loadingIconPosition: loadingIconPosition
  82. });
  83. }
  84. if (state === STATE_LOADING || state === STATE_RESET) {
  85. buttonApi.setState(state);
  86. }
  87. buttonApis.push(buttonApi);
  88. });
  89. return buttonApis.length === 1 ? buttonApis[0] : buttonApis;
  90. };
  91. })(mui, window, document);