1
0

stateful.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function() {
  2. var browser = baidu.editor.browser,
  3. domUtils = baidu.editor.dom.domUtils,
  4. uiUtils = baidu.editor.ui.uiUtils;
  5. var TPL_STATEFUL =
  6. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  7. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  8. (browser.ie
  9. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  10. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  11. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  12. ' onmouseout="$$.Stateful_onMouseOut(event, this);"');
  13. baidu.editor.ui.Stateful = {
  14. alwalysHoverable: false,
  15. target: null, //目标元素和this指向dom不一样
  16. Stateful_init: function() {
  17. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  18. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  19. },
  20. Stateful_getHtmlTpl: function() {
  21. var tpl = this._Stateful_dGetHtmlTpl();
  22. // 使用function避免$转义
  23. return tpl.replace(/stateful/g, function() {
  24. return TPL_STATEFUL;
  25. });
  26. },
  27. Stateful_onMouseEnter: function(evt, el) {
  28. this.target = el;
  29. if (!this.isDisabled() || this.alwalysHoverable) {
  30. this.addState("hover");
  31. this.fireEvent("over");
  32. }
  33. },
  34. Stateful_onMouseLeave: function(evt, el) {
  35. if (!this.isDisabled() || this.alwalysHoverable) {
  36. this.removeState("hover");
  37. this.removeState("active");
  38. this.fireEvent("out");
  39. }
  40. },
  41. Stateful_onMouseOver: function(evt, el) {
  42. var rel = evt.relatedTarget;
  43. if (!uiUtils.contains(el, rel) && el !== rel) {
  44. this.Stateful_onMouseEnter(evt, el);
  45. }
  46. },
  47. Stateful_onMouseOut: function(evt, el) {
  48. var rel = evt.relatedTarget;
  49. if (!uiUtils.contains(el, rel) && el !== rel) {
  50. this.Stateful_onMouseLeave(evt, el);
  51. }
  52. },
  53. Stateful_onMouseDown: function(evt, el) {
  54. if (!this.isDisabled()) {
  55. this.addState("active");
  56. }
  57. },
  58. Stateful_onMouseUp: function(evt, el) {
  59. if (!this.isDisabled()) {
  60. this.removeState("active");
  61. }
  62. },
  63. Stateful_postRender: function() {
  64. if (this.disabled && !this.hasState("disabled")) {
  65. this.addState("disabled");
  66. }
  67. },
  68. hasState: function(state) {
  69. return domUtils.hasClass(this.getStateDom(), "edui-state-" + state);
  70. },
  71. addState: function(state) {
  72. if (!this.hasState(state)) {
  73. this.getStateDom().className += " edui-state-" + state;
  74. }
  75. },
  76. removeState: function(state) {
  77. if (this.hasState(state)) {
  78. domUtils.removeClasses(this.getStateDom(), ["edui-state-" + state]);
  79. }
  80. },
  81. getStateDom: function() {
  82. return this.getDom("state");
  83. },
  84. isChecked: function() {
  85. return this.hasState("checked");
  86. },
  87. setChecked: function(checked) {
  88. if (!this.isDisabled() && checked) {
  89. this.addState("checked");
  90. } else {
  91. this.removeState("checked");
  92. }
  93. },
  94. isDisabled: function() {
  95. return this.hasState("disabled");
  96. },
  97. setDisabled: function(disabled) {
  98. if (disabled) {
  99. this.removeState("hover");
  100. this.removeState("checked");
  101. this.removeState("active");
  102. this.addState("disabled");
  103. } else {
  104. this.removeState("disabled");
  105. }
  106. }
  107. };
  108. })();