AlwaysShowToolbar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit._editor.plugins.AlwaysShowToolbar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit._editor.plugins.AlwaysShowToolbar"] = true;
  8. dojo.provide("dijit._editor.plugins.AlwaysShowToolbar");
  9. dojo.require("dijit._editor._Plugin");
  10. dojo.declare("dijit._editor.plugins.AlwaysShowToolbar", dijit._editor._Plugin,
  11. {
  12. // summary:
  13. // This plugin is required for Editors in auto-expand mode.
  14. // It handles the auto-expansion as the user adds/deletes text,
  15. // and keeps the editor's toolbar visible even when the top of the editor
  16. // has scrolled off the top of the viewport (usually when editing a long
  17. // document).
  18. // description:
  19. // Specify this in extraPlugins (or plugins) parameter and also set
  20. // height to "".
  21. // example:
  22. // | <div dojoType="dijit.Editor" height=""
  23. // | extraPlugins="['dijit._editor.plugins.AlwaysShowToolbar']">
  24. // _handleScroll: Boolean
  25. // Enables/disables the handler for scroll events
  26. _handleScroll: true,
  27. setEditor: function(e){
  28. // Overrides _Plugin.setEditor().
  29. if(!e.iframe){
  30. console.log('Port AlwaysShowToolbar plugin to work with Editor without iframe');
  31. return;
  32. }
  33. this.editor = e;
  34. e.onLoadDeferred.addCallback(dojo.hitch(this, this.enable));
  35. },
  36. enable: function(d){
  37. // summary:
  38. // Enable plugin. Called when Editor has finished initializing.
  39. // tags:
  40. // private
  41. this._updateHeight();
  42. this.connect(window, 'onscroll', "globalOnScrollHandler");
  43. this.connect(this.editor, 'onNormalizedDisplayChanged', "_updateHeight");
  44. return d;
  45. },
  46. _updateHeight: function(){
  47. // summary:
  48. // Updates the height of the editor area to fit the contents.
  49. var e = this.editor;
  50. if(!e.isLoaded){ return; }
  51. if(e.height){ return; }
  52. var height = dojo._getMarginSize(e.editNode).h;
  53. if(dojo.isOpera){
  54. height = e.editNode.scrollHeight;
  55. }
  56. // console.debug('height',height);
  57. // alert(this.editNode);
  58. //height maybe zero in some cases even though the content is not empty,
  59. //we try the height of body instead
  60. if(!height){
  61. height = dojo._getMarginSize(e.document.body).h;
  62. }
  63. if(height == 0){
  64. console.debug("Can not figure out the height of the editing area!");
  65. return; //prevent setting height to 0
  66. }
  67. if(dojo.isIE <= 7 && this.editor.minHeight){
  68. var min = parseInt(this.editor.minHeight);
  69. if(height < min){ height = min; }
  70. }
  71. if(height != this._lastHeight){
  72. this._lastHeight = height;
  73. // this.editorObject.style.height = this._lastHeight + "px";
  74. dojo.marginBox(e.iframe, { h: this._lastHeight });
  75. }
  76. },
  77. // _lastHeight: Integer
  78. // Height in px of the editor at the last time we did sizing
  79. _lastHeight: 0,
  80. globalOnScrollHandler: function(){
  81. // summary:
  82. // Handler for scroll events that bubbled up to <html>
  83. // tags:
  84. // private
  85. var isIE6 = dojo.isIE < 7;
  86. if(!this._handleScroll){ return; }
  87. var tdn = this.editor.header;
  88. var db = dojo.body;
  89. if(!this._scrollSetUp){
  90. this._scrollSetUp = true;
  91. this._scrollThreshold = dojo.position(tdn, true).y;
  92. // console.log("threshold:", this._scrollThreshold);
  93. //what's this for?? comment out for now
  94. // if((isIE6)&&(db)&&(dojo.style(db, "backgroundIimage")=="none")){
  95. // db.style.backgroundImage = "url(" + dojo.uri.moduleUri("dijit", "templates/blank.gif") + ")";
  96. // db.style.backgroundAttachment = "fixed";
  97. // }
  98. }
  99. var scrollPos = dojo._docScroll().y;
  100. var s = tdn.style;
  101. if(scrollPos > this._scrollThreshold && scrollPos < this._scrollThreshold+this._lastHeight){
  102. // dojo.debug(scrollPos);
  103. if(!this._fixEnabled){
  104. var tdnbox = dojo._getMarginSize(tdn);
  105. this.editor.iframe.style.marginTop = tdnbox.h+"px";
  106. if(isIE6){
  107. s.left = dojo.position(tdn).x;
  108. if(tdn.previousSibling){
  109. this._IEOriginalPos = ['after',tdn.previousSibling];
  110. }else if(tdn.nextSibling){
  111. this._IEOriginalPos = ['before',tdn.nextSibling];
  112. }else{
  113. this._IEOriginalPos = ['last',tdn.parentNode];
  114. }
  115. dojo.body().appendChild(tdn);
  116. dojo.addClass(tdn,'dijitIEFixedToolbar');
  117. }else{
  118. s.position = "fixed";
  119. s.top = "0px";
  120. }
  121. dojo.marginBox(tdn, { w: tdnbox.w });
  122. s.zIndex = 2000;
  123. this._fixEnabled = true;
  124. }
  125. // if we're showing the floating toolbar, make sure that if
  126. // we've scrolled past the bottom of the editor that we hide
  127. // the toolbar for this instance of the editor.
  128. // TODO: when we get multiple editor toolbar support working
  129. // correctly, ensure that we check this against the scroll
  130. // position of the bottom-most editor instance.
  131. var eHeight = (this.height) ? parseInt(this.editor.height) : this.editor._lastHeight;
  132. s.display = (scrollPos > this._scrollThreshold+eHeight) ? "none" : "";
  133. }else if(this._fixEnabled){
  134. this.editor.iframe.style.marginTop = '';
  135. s.position = "";
  136. s.top = "";
  137. s.zIndex = "";
  138. s.display = "";
  139. if(isIE6){
  140. s.left = "";
  141. dojo.removeClass(tdn,'dijitIEFixedToolbar');
  142. if(this._IEOriginalPos){
  143. dojo.place(tdn, this._IEOriginalPos[1], this._IEOriginalPos[0]);
  144. this._IEOriginalPos = null;
  145. }else{
  146. dojo.place(tdn, this.editor.iframe, 'before');
  147. }
  148. }
  149. s.width = "";
  150. this._fixEnabled = false;
  151. }
  152. },
  153. destroy: function(){
  154. // Overrides _Plugin.destroy(). TODO: call this.inherited() rather than repeating code.
  155. this._IEOriginalPos = null;
  156. this._handleScroll = false;
  157. dojo.forEach(this._connects, dojo.disconnect);
  158. // clearInterval(this.scrollInterval);
  159. if(dojo.isIE < 7){
  160. dojo.removeClass(this.editor.header, 'dijitIEFixedToolbar');
  161. }
  162. }
  163. });
  164. }