stateful.js 3.8 KB

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