autofloat.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ///import core
  2. ///commands 悬浮工具栏
  3. ///commandsName AutoFloat,autoFloatEnabled
  4. ///commandsTitle 悬浮工具栏
  5. /*
  6. * modified by chengchao01
  7. *
  8. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  9. */
  10. UE.plugins['autofloat'] = function() {
  11. var me = this,
  12. lang = me.getLang();
  13. me.setOpt({
  14. topOffset:0
  15. });
  16. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  17. topOffset = me.options.topOffset;
  18. //如果不固定toolbar的位置,则直接退出
  19. if(!optsAutoFloatEnabled){
  20. return;
  21. }
  22. var uiUtils = UE.ui.uiUtils,
  23. LteIE6 = browser.ie && browser.version <= 6,
  24. quirks = browser.quirks;
  25. function checkHasUI(editor){
  26. if(!editor.ui){
  27. alert(lang.autofloatMsg);
  28. return 0;
  29. }
  30. return 1;
  31. }
  32. function fixIE6FixedPos(){
  33. var docStyle = document.body.style;
  34. docStyle.backgroundImage = 'url("about:blank")';
  35. docStyle.backgroundAttachment = 'fixed';
  36. }
  37. var bakCssText,
  38. placeHolder = document.createElement('div'),
  39. toolbarBox,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 = (document.body.scrollTop||document.documentElement.scrollTop) - orgTop + topOffset + 'px';
  54. } else {
  55. if (browser.ie7Compat && flag) {
  56. flag = false;
  57. toolbarBox.style.left = domUtils.getXY(toolbarBox).x - document.documentElement.getBoundingClientRect().left+2 + 'px';
  58. }
  59. if(toolbarBox.style.position != 'fixed'){
  60. toolbarBox.style.position = 'fixed';
  61. toolbarBox.style.top = topOffset +"px";
  62. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  63. }
  64. }
  65. }
  66. function unsetFloating(){
  67. flag = true;
  68. if(placeHolder.parentNode){
  69. placeHolder.parentNode.removeChild(placeHolder);
  70. }
  71. toolbarBox.style.cssText = bakCssText;
  72. }
  73. function updateFloating(){
  74. var rect3 = getPosition(me.container);
  75. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > 0) {
  76. setFloating();
  77. }else{
  78. unsetFloating();
  79. }
  80. }
  81. var defer_updateFloating = utils.defer(function(){
  82. updateFloating();
  83. },browser.ie ? 200 : 100,true);
  84. me.addListener('destroy',function(){
  85. domUtils.un(window, ['scroll','resize'], updateFloating);
  86. me.removeListener('keydown', defer_updateFloating);
  87. });
  88. me.addListener('ready', function(){
  89. if(checkHasUI(me)){
  90. getPosition = uiUtils.getClientRect;
  91. toolbarBox = me.ui.getDom('toolbarbox');
  92. orgTop = getPosition(toolbarBox).top;
  93. bakCssText = toolbarBox.style.cssText;
  94. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  95. if(LteIE6){
  96. fixIE6FixedPos();
  97. }
  98. domUtils.on(window, ['scroll','resize'], updateFloating);
  99. me.addListener('keydown', defer_updateFloating);
  100. me.addListener('beforefullscreenchange', function (t, enabled){
  101. if (enabled) {
  102. unsetFloating();
  103. }
  104. });
  105. me.addListener('fullscreenchanged', function (t, enabled){
  106. if (!enabled) {
  107. updateFloating();
  108. }
  109. });
  110. me.addListener('sourcemodechanged', function (t, enabled){
  111. setTimeout(function (){
  112. updateFloating();
  113. },0);
  114. });
  115. me.addListener("clearDoc",function(){
  116. setTimeout(function(){
  117. updateFloating();
  118. },0);
  119. })
  120. }
  121. });
  122. };