autofloat.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. optsAutoFloatEnabled = me.options.autoFloatEnabled !== false;
  13. //如果不固定toolbar的位置,则直接退出
  14. if(!optsAutoFloatEnabled){
  15. return;
  16. }
  17. var uiUtils = UE.ui.uiUtils,
  18. LteIE6 = browser.ie && browser.version <= 6,
  19. quirks = browser.quirks;
  20. function checkHasUI(editor){
  21. if(!editor.ui){
  22. alert('autofloat插件功能依赖于UEditor UI\nautofloat定义位置: _src/plugins/autofloat.js');
  23. throw({
  24. name: '未包含UI文件',
  25. message: 'autofloat功能依赖于UEditor UI。autofloat定义位置: _src/plugins/autofloat.js'
  26. });
  27. }
  28. return 1;
  29. }
  30. function fixIE6FixedPos(){
  31. var docStyle = document.body.style;
  32. docStyle.backgroundImage = 'url("about:blank")';
  33. docStyle.backgroundAttachment = 'fixed';
  34. }
  35. var bakCssText,
  36. placeHolder = document.createElement('div'),
  37. toolbarBox,orgTop,
  38. getPosition,
  39. flag =true; //ie7模式下需要偏移
  40. function setFloating(){
  41. var toobarBoxPos = domUtils.getXY(toolbarBox),
  42. origalFloat = domUtils.getComputedStyle(toolbarBox,'position'),
  43. origalLeft = domUtils.getComputedStyle(toolbarBox,'left');
  44. toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
  45. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  46. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  47. if (LteIE6 || quirks) {
  48. if(toolbarBox.style.position != 'absolute'){
  49. toolbarBox.style.position = 'absolute';
  50. }
  51. toolbarBox.style.top = (document.body.scrollTop||document.documentElement.scrollTop) - orgTop + 'px';
  52. } else {
  53. if (browser.ie7Compat && flag) {
  54. flag = false;
  55. toolbarBox.style.left = domUtils.getXY(toolbarBox).x - document.documentElement.getBoundingClientRect().left+2 + 'px';
  56. }
  57. if(toolbarBox.style.position != 'fixed'){
  58. toolbarBox.style.position = 'fixed';
  59. toolbarBox.style.top = '0';
  60. ((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
  61. }
  62. }
  63. }
  64. function unsetFloating(){
  65. flag = true;
  66. if(placeHolder.parentNode)
  67. placeHolder.parentNode.removeChild(placeHolder);
  68. toolbarBox.style.cssText = bakCssText;
  69. }
  70. function updateFloating(){
  71. var rect3 = getPosition(me.container);
  72. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > 0) {
  73. setFloating();
  74. }else{
  75. unsetFloating();
  76. }
  77. }
  78. var defer_updateFloating = utils.defer(function(){
  79. updateFloating();
  80. },browser.ie ? 200 : 100,true);
  81. me.addListener('destroy',function(){
  82. domUtils.un(window, ['scroll','resize'], updateFloating);
  83. me.removeListener('keydown', defer_updateFloating);
  84. });
  85. me.addListener('ready', function(){
  86. if(checkHasUI(me)){
  87. getPosition = uiUtils.getClientRect;
  88. toolbarBox = me.ui.getDom('toolbarbox');
  89. orgTop = getPosition(toolbarBox).top;
  90. bakCssText = toolbarBox.style.cssText;
  91. placeHolder.style.height = toolbarBox.offsetHeight + 'px';
  92. if(LteIE6){
  93. fixIE6FixedPos();
  94. }
  95. me.addListener('autoheightchanged', function (t, enabled){
  96. if (enabled) {
  97. domUtils.on(window, ['scroll','resize'], updateFloating);
  98. me.addListener('keydown', defer_updateFloating);
  99. } else {
  100. domUtils.un(window, ['scroll','resize'], updateFloating);
  101. me.removeListener('keydown', defer_updateFloating);
  102. }
  103. });
  104. me.addListener('beforefullscreenchange', function (t, enabled){
  105. if (enabled) {
  106. unsetFloating();
  107. }
  108. });
  109. me.addListener('fullscreenchanged', function (t, enabled){
  110. if (!enabled) {
  111. updateFloating();
  112. }
  113. });
  114. me.addListener('sourcemodechanged', function (t, enabled){
  115. setTimeout(function (){
  116. updateFloating();
  117. });
  118. });
  119. }
  120. })
  121. };