autofloat.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ///import core
  2. ///commands 悬浮工具栏
  3. ///commandsName AutoFloat,autoFloatEnabled
  4. ///commandsTitle 悬浮工具栏
  5. /**
  6. * modified by chengchao01
  7. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  8. */
  9. UE.plugins["autofloat"] = function() {
  10. var me = this,
  11. lang = me.getLang();
  12. me.setOpt({
  13. topOffset: 0
  14. });
  15. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  16. topOffset = me.options.topOffset;
  17. //如果不固定toolbar的位置,则直接退出
  18. if (!optsAutoFloatEnabled) {
  19. return;
  20. }
  21. var uiUtils = UE.ui.uiUtils,
  22. LteIE6 = browser.ie && browser.version <= 6,
  23. quirks = browser.quirks;
  24. function checkHasUI() {
  25. if (!UE.ui) {
  26. alert(lang.autofloatMsg);
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. function fixIE6FixedPos() {
  32. var docStyle = document.body.style;
  33. docStyle.backgroundImage = 'url("about:blank")';
  34. docStyle.backgroundAttachment = "fixed";
  35. }
  36. var bakCssText,
  37. placeHolder = document.createElement("div"),
  38. toolbarBox,
  39. orgTop,
  40. getPosition,
  41. flag = true; //ie7模式下需要偏移
  42. function setFloating() {
  43. var toobarBoxPos = domUtils.getXY(toolbarBox),
  44. origalFloat = domUtils.getComputedStyle(toolbarBox, "position"),
  45. origalLeft = domUtils.getComputedStyle(toolbarBox, "left");
  46. toolbarBox.style.width = toolbarBox.offsetWidth + "px";
  47. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  48. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  49. if (LteIE6 || (quirks && browser.ie)) {
  50. if (toolbarBox.style.position != "absolute") {
  51. toolbarBox.style.position = "absolute";
  52. }
  53. toolbarBox.style.top =
  54. (document.body.scrollTop || document.documentElement.scrollTop) -
  55. orgTop +
  56. topOffset +
  57. "px";
  58. } else {
  59. if (browser.ie7Compat && flag) {
  60. flag = false;
  61. toolbarBox.style.left =
  62. domUtils.getXY(toolbarBox).x -
  63. document.documentElement.getBoundingClientRect().left +
  64. 2 +
  65. "px";
  66. }
  67. if (toolbarBox.style.position != "fixed") {
  68. toolbarBox.style.position = "fixed";
  69. toolbarBox.style.top = topOffset + "px";
  70. (origalFloat == "absolute" || origalFloat == "relative") &&
  71. parseFloat(origalLeft) &&
  72. (toolbarBox.style.left = toobarBoxPos.x + "px");
  73. }
  74. }
  75. }
  76. function unsetFloating() {
  77. flag = true;
  78. if (placeHolder.parentNode) {
  79. placeHolder.parentNode.removeChild(placeHolder);
  80. }
  81. toolbarBox.style.cssText = bakCssText;
  82. }
  83. function updateFloating() {
  84. var rect3 = getPosition(me.container);
  85. var offset = me.options.toolbarTopOffset || 0;
  86. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  87. setFloating();
  88. } else {
  89. unsetFloating();
  90. }
  91. }
  92. var defer_updateFloating = utils.defer(
  93. function() {
  94. updateFloating();
  95. },
  96. browser.ie ? 200 : 100,
  97. true
  98. );
  99. me.addListener("destroy", function() {
  100. domUtils.un(window, ["scroll", "resize"], updateFloating);
  101. me.removeListener("keydown", defer_updateFloating);
  102. });
  103. me.addListener("ready", function() {
  104. if (checkHasUI(me)) {
  105. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  106. if (!me.ui) {
  107. return;
  108. }
  109. getPosition = uiUtils.getClientRect;
  110. toolbarBox = me.ui.getDom("toolbarbox");
  111. orgTop = getPosition(toolbarBox).top;
  112. bakCssText = toolbarBox.style.cssText;
  113. placeHolder.style.height = toolbarBox.offsetHeight + "px";
  114. if (LteIE6) {
  115. fixIE6FixedPos();
  116. }
  117. domUtils.on(window, ["scroll", "resize"], updateFloating);
  118. me.addListener("keydown", defer_updateFloating);
  119. me.addListener("beforefullscreenchange", function(t, enabled) {
  120. if (enabled) {
  121. unsetFloating();
  122. }
  123. });
  124. me.addListener("fullscreenchanged", function(t, enabled) {
  125. if (!enabled) {
  126. updateFloating();
  127. }
  128. });
  129. me.addListener("sourcemodechanged", function(t, enabled) {
  130. setTimeout(function() {
  131. updateFloating();
  132. }, 0);
  133. });
  134. me.addListener("clearDoc", function() {
  135. setTimeout(function() {
  136. updateFloating();
  137. }, 0);
  138. });
  139. }
  140. });
  141. };