source: Dev/trunk/src/client/dijit/_editor/plugins/AlwaysShowToolbar.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 6.1 KB
Line 
1define([
2        "dojo/_base/declare", // declare
3        "dojo/dom-class", // domClass.add domClass.remove
4        "dojo/dom-construct", // domConstruct.place
5        "dojo/dom-geometry",
6        "dojo/_base/lang", // lang.hitch
7        "dojo/on",
8        "dojo/sniff", // has("ie") has("opera")
9        "dojo/_base/window", // win.body
10        "../_Plugin"
11], function(declare, domClass, domConstruct, domGeometry, lang, on, has, win, _Plugin){
12
13        // module:
14        //              dijit/_editor/plugins/AlwaysShowToolbar
15
16        return declare("dijit._editor.plugins.AlwaysShowToolbar", _Plugin, {
17                // summary:
18                //              This plugin is required for Editors in auto-expand mode.
19                //              It handles the auto-expansion as the user adds/deletes text,
20                //              and keeps the editor's toolbar visible even when the top of the editor
21                //              has scrolled off the top of the viewport (usually when editing a long
22                //              document).
23                // description:
24                //              Specify this in extraPlugins (or plugins) parameter and also set
25                //              height to "".
26                // example:
27                //      |       <script type="dojo/require">
28                //      |               AlwaysShowToolbar: "dijit/_editor/plugins/AlwaysShowToolbar"
29                //      |       </script>
30                //      |       <div data-dojo-type="dijit/Editor" height=""
31                //      |                       data-dojo-props="extraPlugins: [AlwaysShowToolbar]">
32
33                // _handleScroll: Boolean
34                //              Enables/disables the handler for scroll events
35                _handleScroll: true,
36
37                setEditor: function(e){
38                        // Overrides _Plugin.setEditor().
39                        if(!e.iframe){
40                                console.log('Port AlwaysShowToolbar plugin to work with Editor without iframe');
41                                return;
42                        }
43
44                        this.editor = e;
45
46                        e.onLoadDeferred.then(lang.hitch(this, this.enable));
47                },
48
49                enable: function(d){
50                        // summary:
51                        //              Enable plugin.  Called when Editor has finished initializing.
52                        // tags:
53                        //              private
54
55                        this._updateHeight();
56                        this.own(
57                                on(window, 'scroll', lang.hitch(this, "globalOnScrollHandler")),
58                                this.editor.on('NormalizedDisplayChanged', lang.hitch(this, "_updateHeight"))
59                        );
60                        return d;
61                },
62
63                _updateHeight: function(){
64                        // summary:
65                        //              Updates the height of the editor area to fit the contents.
66                        var e = this.editor;
67                        if(!e.isLoaded){
68                                return;
69                        }
70                        if(e.height){
71                                return;
72                        }
73
74                        var height = domGeometry.getMarginSize(e.editNode).h;
75                        if(has("opera")){
76                                height = e.editNode.scrollHeight;
77                        }
78                        // console.debug('height',height);
79                        // alert(this.editNode);
80
81                        //height maybe zero in some cases even though the content is not empty,
82                        //we try the height of body instead
83                        if(!height){
84                                height = domGeometry.getMarginSize(e.document.body).h;
85                        }
86
87                        if(this._fixEnabled){
88                                // #16204: add toolbar height when it is fixed aka "stuck to the top of the screen" to prevent content from cutting off during autosizing.
89                                // Seems like _updateHeight should be taking the intitial margin height from a more appropriate node that includes the marginTop set in globalOnScrollHandler.
90                                height += domGeometry.getMarginSize(this.editor.header).h;
91                        }
92
93                        if(height == 0){
94                                console.debug("Can not figure out the height of the editing area!");
95                                return; //prevent setting height to 0
96                        }
97                        if(has("ie") <= 7 && this.editor.minHeight){
98                                var min = parseInt(this.editor.minHeight);
99                                if(height < min){
100                                        height = min;
101                                }
102                        }
103                        if(height != this._lastHeight){
104                                this._lastHeight = height;
105                                // this.editorObject.style.height = this._lastHeight + "px";
106                                domGeometry.setMarginBox(e.iframe, { h: this._lastHeight });
107                        }
108                },
109
110                // _lastHeight: Integer
111                //              Height in px of the editor at the last time we did sizing
112                _lastHeight: 0,
113
114                globalOnScrollHandler: function(){
115                        // summary:
116                        //              Handler for scroll events that bubbled up to `<html>`
117                        // tags:
118                        //              private
119
120                        var isIE6 = has("ie") < 7;
121                        if(!this._handleScroll){
122                                return;
123                        }
124                        var tdn = this.editor.header;
125                        if(!this._scrollSetUp){
126                                this._scrollSetUp = true;
127                                this._scrollThreshold = domGeometry.position(tdn, true).y;
128                        }
129
130                        var scrollPos = domGeometry.docScroll(this.editor.ownerDocument).y;
131                        var s = tdn.style;
132
133                        if(scrollPos > this._scrollThreshold && scrollPos < this._scrollThreshold + this._lastHeight){
134                                // console.debug(scrollPos);
135                                if(!this._fixEnabled){
136                                        var tdnbox = domGeometry.getMarginSize(tdn);
137                                        this.editor.iframe.style.marginTop = tdnbox.h + "px";
138
139                                        if(isIE6){
140                                                s.left = domGeometry.position(tdn).x;
141                                                if(tdn.previousSibling){
142                                                        this._IEOriginalPos = ['after', tdn.previousSibling];
143                                                }else if(tdn.nextSibling){
144                                                        this._IEOriginalPos = ['before', tdn.nextSibling];
145                                                }else{
146                                                        this._IEOriginalPos = ['last', tdn.parentNode];
147                                                }
148                                                this.editor.ownerDocumentBody.appendChild(tdn);
149                                                domClass.add(tdn, 'dijitIEFixedToolbar');
150                                        }else{
151                                                s.position = "fixed";
152                                                s.top = "0px";
153                                        }
154
155                                        domGeometry.setMarginBox(tdn, { w: tdnbox.w });
156                                        s.zIndex = 2000;
157                                        this._fixEnabled = true;
158                                }
159                                // if we're showing the floating toolbar, make sure that if
160                                // we've scrolled past the bottom of the editor that we hide
161                                // the toolbar for this instance of the editor.
162
163                                // TODO: when we get multiple editor toolbar support working
164                                // correctly, ensure that we check this against the scroll
165                                // position of the bottom-most editor instance.
166                                var eHeight = (this.height) ? parseInt(this.editor.height) : this.editor._lastHeight;
167                                s.display = (scrollPos > this._scrollThreshold + eHeight) ? "none" : "";
168                        }else if(this._fixEnabled){
169                                this.editor.iframe.style.marginTop = '';
170                                s.position = "";
171                                s.top = "";
172                                s.zIndex = "";
173                                s.display = "";
174                                if(isIE6){
175                                        s.left = "";
176                                        domClass.remove(tdn, 'dijitIEFixedToolbar');
177                                        if(this._IEOriginalPos){
178                                                domConstruct.place(tdn, this._IEOriginalPos[1], this._IEOriginalPos[0]);
179                                                this._IEOriginalPos = null;
180                                        }else{
181                                                domConstruct.place(tdn, this.editor.iframe, 'before');
182                                        }
183                                }
184                                s.width = "";
185                                this._fixEnabled = false;
186                        }
187                },
188
189                destroy: function(){
190                        // Overrides _Plugin.destroy().   TODO: call this.inherited() rather than repeating code.
191                        this._IEOriginalPos = null;
192                        this._handleScroll = false;
193                        this.inherited(arguments);
194
195                        if(has("ie") < 7){
196                                domClass.remove(this.editor.header, 'dijitIEFixedToolbar');
197                        }
198                }
199        });
200
201});
Note: See TracBrowser for help on using the repository browser.