]> git.donarmstrong.com Git - dactyl.git/blob - common/content/statusline.js
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / content / statusline.js
1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2011 by Kris Maglione <maglione.k@gmail.com>
4 //
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
7 /* use strict */
8
9 /** @scope modules */
10
11 var StatusLine = Module("statusline", {
12     init: function init() {
13         this._statusLine = document.getElementById("status-bar");
14         this.statusBar = document.getElementById("addon-bar") || this._statusLine;
15         this.baseGroup = this.statusBar == this._statusLine ? "StatusLine " : "";
16
17         if (this.statusBar.localName == "toolbar") {
18             styles.system.add("addon-bar", config.styleableChrome, <css><![CDATA[
19                 #status-bar { margin-top: 0 !important; }
20                 #addon-bar > statusbar { -moz-box-flex: 1 }
21                 #addon-bar > #addonbar-closebutton { visibility: collapse; }
22                 #addon-bar > xul|toolbarspring { visibility: collapse; }
23             ]]></css>);
24
25             overlay.overlayWindow(window, { append: <><statusbar id="status-bar" ordinal="0"/></> });
26
27             highlight.loadCSS(util.compileMacro(<![CDATA[
28                 !AddonBar;#addon-bar {
29                     /* The Add-on Bar */
30                     padding-left: 0 !important;
31                     min-height: 18px !important;
32                     -moz-appearance: none !important;
33                     <padding>
34                 }
35                 !AddonButton;#addon-bar xul|toolbarbutton {
36                     /* An Add-on Bar button */
37                     -moz-appearance: none !important;
38                     padding: 0 !important;
39                     border-width: 0px !important;
40                     min-width: 0 !important;
41                     color: inherit !important;
42                 }
43                 AddonButton:not(:hover)  background: transparent;
44             ]]>)({ padding: config.OS.isMacOSX ? "padding-right: 10px !important;" : "" }));
45
46             if (document.getElementById("appmenu-button"))
47                 highlight.loadCSS(<![CDATA[
48                     AppmenuButton       /* The app-menu button */ \
49                                         min-width: 0 !important; padding: 0 .5em !important;
50                 ]]>);
51         }
52
53         XML.ignoreWhitespace = true;
54         let _commandline = "if (window.dactyl) return dactyl.modules.commandline";
55         let prepend = <e4x xmlns={XUL} xmlns:dactyl={NS}>
56             <button id="appmenu-button" label="" image="chrome://branding/content/icon16.png" highlight="AppmenuButton" />
57             <toolbarbutton id="appmenu-toolbar-button" label="" image="chrome://branding/content/icon16.png" />
58             <statusbar id="status-bar" highlight="StatusLine">
59                 <!-- insertbefore="dactyl.statusBefore;" insertafter="dactyl.statusAfter;" -->
60                 <hbox key="container" hidden="false" align="center"  flex="1">
61                     <stack orient="horizontal"       align="stretch" flex="1" highlight="CmdLine StatusCmdLine" class="dactyl-container">
62                         <hbox                                                 highlight="CmdLine StatusCmdLine" class="dactyl-container">
63                             <label key="mode"          crop="end"                                               class="plain" collapsed="true"/>
64                             <stack  id="dactyl-statusline-stack"     flex="1" highlight="CmdLine StatusCmdLine" class="dactyl-container">
65                                 <textbox key="url"     crop="end"    flex="1" style="background: transparent;"  class="plain dactyl-status-field-url" readonly="true"/>
66                                 <textbox key="message" crop="end"    flex="1" highlight="Normal StatusNormal"   class="plain"                         readonly="true"/>
67                             </stack>
68                         </hbox>
69                     </stack>
70                     <label class="plain" key="inputbuffer"    flex="0"/>
71                     <label class="plain" key="progress"       flex="0"/>
72                     <label class="plain" key="tabcount"       flex="0"/>
73                     <label class="plain" key="bufferposition" flex="0"/>
74                     <label class="plain" key="zoomlevel"      flex="0"/>
75                 </hbox>
76                 <!-- just hide them since other elements expect them -->
77                 <statusbarpanel id="statusbar-display"       hidden="true"/>
78                 <statusbarpanel id="statusbar-progresspanel" hidden="true"/>
79             </statusbar>
80         </e4x>;
81
82         for each (let attr in prepend..@key)
83             attr.parent().@id = "dactyl-statusline-field-" + attr;
84
85         overlay.overlayWindow(window, {
86             objects: this.widgets = { get status() this.container },
87             prepend: prepend.elements()
88         });
89
90         try {
91             this.security = content.document.dactylSecurity || "insecure";
92         }
93         catch (e) {}
94     },
95
96     get visible() !this.statusBar.collapsed && !this.statusBar.hidden,
97
98     signals: {
99         "browser.locationChange": function (webProgress, request, uri) {
100             let win = webProgress.DOMWindow;
101             this.status = uri;
102             this.progress = uri && win && win.dactylProgress || "";
103
104             // if this is not delayed we get the position of the old buffer
105             this.timeout(function () {
106                 this.updateBufferPosition();
107                 this.updateZoomLevel();
108             }, 500);
109         },
110         "browser.overLink": function (link) {
111             switch (options["showstatuslinks"]) {
112             case "status":
113                 this.status = link ? _("status.link", link) : buffer.uri;
114                 break;
115             case "command":
116                 if (link)
117                     dactyl.echo(_("status.link", link), commandline.FORCE_SINGLELINE);
118                 else
119                     commandline.clear();
120                 break;
121             }
122         },
123         "browser.progressChange": function onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {
124             if (webProgress && webProgress.DOMWindow)
125                 webProgress.DOMWindow.dactylProgress = curTotalProgress / maxTotalProgress;
126             this.progress = curTotalProgress / maxTotalProgress;
127         },
128         "browser.securityChange": function onSecurityChange(webProgress, request, state) {
129
130             if (state & Ci.nsIWebProgressListener.STATE_IS_BROKEN)
131                 this.security = "broken";
132             else if (state & Ci.nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL)
133                 this.security = "extended";
134             else if (state & Ci.nsIWebProgressListener.STATE_SECURE_HIGH)
135                 this.security = "secure";
136             else // if (state & Ci.nsIWebProgressListener.STATE_IS_INSECURE)
137                 this.security = "insecure";
138
139             if (webProgress && webProgress.DOMWindow)
140                 webProgress.DOMWindow.document.dactylSecurity = this.security;
141         },
142         "browser.stateChange": function onStateChange(webProgress, request, flags, status) {
143             const L = Ci.nsIWebProgressListener;
144
145             if (flags & (L.STATE_IS_DOCUMENT | L.STATE_IS_WINDOW))
146                 if (flags & L.STATE_START)
147                     this.progress = 0;
148                 else if (flags & L.STATE_STOP)
149                     this.progress = "";
150
151             if (flags & L.STATE_STOP)
152                 this.updateStatus();
153         },
154         "browser.statusChange": function onStatusChange(webProgress, request, status, message) {
155             this.timeout(function () {
156                 this.status = message || buffer.uri;
157             });
158         }
159     },
160
161     /**
162      * Update the status bar to indicate how secure the website is:
163      * extended - Secure connection with Extended Validation(EV) certificate.
164      * secure -   Secure connection with valid certificate.
165      * broken -   Secure connection with invalid certificate, or
166      *            mixed content.
167      * insecure - Insecure connection.
168      *
169      * @param {'extended'|'secure'|'broken'|'insecure'} type
170      */
171     set security(type) {
172         this._security = type;
173         const highlightGroup = {
174             extended: "StatusLineExtended",
175             secure:   "StatusLineSecure",
176             broken:   "StatusLineBroken",
177             insecure: "StatusLineNormal"
178         };
179
180         highlight.highlightNode(this.statusBar, this.baseGroup + highlightGroup[type]);
181     },
182     get security() this._security,
183
184     // update all fields of the statusline
185     update: function update() {
186         this.updateStatus();
187         this.inputBuffer = "";
188         this.progress = "";
189         this.updateTabCount();
190         this.updateBufferPosition();
191         this.updateZoomLevel();
192     },
193
194     unsafeURI: deprecated("util.unsafeURI", { get: function unsafeURI() util.unsafeURI }),
195     losslessDecodeURI: deprecated("util.losslessDecodeURI", function losslessDecodeURI() util.losslessDecodeURI.apply(util, arguments)),
196
197     /**
198      * Update the URL displayed in the status line. Also displays status
199      * icons, [+-♥], when there are next and previous pages in the
200      * current tab's history, and when the current URL is bookmarked,
201      * respectively.
202      *
203      * @param {string} url The URL to display.
204      */
205     get status() this._uri,
206     set status(uri) {
207         let modified = "";
208         let url = uri;
209         if (isinstance(uri, Ci.nsIURI)) {
210             // when session information is available, add [+] when we can go
211             // backwards, [-] when we can go forwards
212             if (uri.equals(buffer.uri) && window.getWebNavigation) {
213                 let sh = window.getWebNavigation().sessionHistory;
214                 if (sh && sh.index > 0)
215                     modified += "-";
216                 if (sh && sh.index < sh.count - 1)
217                     modified += "+";
218                 if (this.bookmarked)
219                     modified += UTF8("❤");
220             }
221
222             if (modules.quickmarks)
223                 modified += quickmarks.find(uri.spec.replace(/#.*/, "")).join("");
224
225             url = util.losslessDecodeURI(uri.spec);
226         }
227
228         if (url == "about:blank") {
229             if (!buffer.title)
230                 url = _("buffer.noName");
231         }
232         else {
233             url = url.replace(RegExp("^dactyl://help/(\\S+)#(.*)"), function (m, n1, n2) n1 + " " + decodeURIComponent(n2) + " " + _("buffer.help"))
234                      .replace(RegExp("^dactyl://help/(\\S+)"), "$1 " + _("buffer.help"));
235         }
236
237         if (modified)
238             url += " [" + modified + "]";
239
240         this.widgets.url.value = url;
241         this._status = uri;
242     },
243
244     get bookmarked() this._bookmarked,
245     set bookmarked(val) {
246         this._bookmarked = val;
247         if (this.status)
248             this.status = this.status;
249     },
250
251     updateStatus: function updateStatus() {
252         this.timeout(function () {
253             this.status = buffer.uri;
254         });
255     },
256
257     updateUrl: deprecated("statusline.status", function updateUrl(url) { this.status = url || buffer.uri }),
258
259     /**
260      * Set the contents of the status line's input buffer to the given
261      * string. Used primarily when a key press requires further input
262      * before being processed, including mapping counts and arguments,
263      * along with multi-key mappings.
264      *
265      * @param {string} buffer
266      * @optional
267      */
268     get inputBuffer() this.widgets.inputbuffer.value,
269     set inputBuffer(val) this.widgets.inputbuffer.value = val == null ? "" : val,
270     updateInputBuffer: deprecated("statusline.inputBuffer", function updateInputBuffer(val) { this.inputBuffer = val; }),
271
272     /**
273      * Update the page load progress bar.
274      *
275      * @param {string|number} progress The current progress, as follows:
276      *    A string          - Displayed literally.
277      *    A ratio 0 < n < 1 - Displayed as a progress bar.
278      *    A number n <= 0   - Displayed as a "Loading" message.
279      *    Any other number  - The progress is cleared.
280      */
281     progress: Modes.boundProperty({
282         get: function progress() this._progress,
283         set: function progress(progress) {
284             this._progress = progress || "";
285
286             if (isinstance(progress, ["String", _]))
287                 this.widgets.progress.value = this._progress;
288             else if (typeof progress == "number") {
289                 let progressStr = "";
290                 if (this._progress <= 0)
291                     progressStr = /*L*/"[ Loading...         ]";
292                 else if (this._progress < 1) {
293                     let progress = Math.round(this._progress * 20);
294                     progressStr = "["
295                         + "===================>                    "
296                             .substr(20 - progress, 20)
297                         + "]";
298                 }
299                 this.widgets.progress.value = progressStr;
300             }
301         }
302     }),
303     updateProgress: deprecated("statusline.progress", function updateProgress(progress) {
304         this.progress = progress;
305     }),
306
307     /**
308      * Display the correct tabcount (e.g., [1/5]) on the status bar.
309      *
310      * @param {boolean} delayed When true, update count after a brief timeout.
311      *     Useful in the many cases when an event that triggers an update is
312      *     broadcast before the tab state is fully updated.
313      * @optional
314      */
315     updateTabCount: function updateTabCount(delayed) {
316         if (dactyl.has("tabs")) {
317             if (delayed) {
318                 this.timeout(function () this.updateTabCount(false), 0);
319                 return;
320             }
321
322             this.widgets.tabcount.value = "[" + (tabs.index(null, true) + 1) + "/" + tabs.visibleTabs.length + "]";
323         }
324     },
325
326     /**
327      * Display the main content's vertical scroll position in the status
328      * bar.
329      *
330      * @param {number} percent The position, as a percentage.
331      * @optional
332      */
333     updateBufferPosition: function updateBufferPosition(percent) {
334         if (percent == null) {
335             let win = document.commandDispatcher.focusedWindow;
336             if (!win)
337                 return;
338             win.scrollY; // intentional - see Kris
339             percent = win.scrollY    == 0 ?  0 : // This prevents a forced rendering
340                       win.scrollMaxY == 0 ? -1 : win.scrollY / win.scrollMaxY;
341         }
342
343         percent = Math.round(percent * 100);
344
345         if (percent < 0)
346             var position = "All";
347         else if (percent == 0)
348             position = "Top";
349         else if (percent >= 100)
350             position = "Bot";
351         else if (percent < 10)
352             position = " " + percent + "%";
353         else
354             position = percent + "%";
355
356         this.widgets.bufferposition.value = position;
357     },
358
359     /**
360      * Display the main content's zoom level.
361      *
362      * @param {number} percent The zoom level, as a percentage. @optional
363      * @param {boolean} full True if full zoom is in operation. @optional
364      */
365     updateZoomLevel: function updateZoomLevel(percent, full) {
366         if (arguments.length == 0)
367             [percent, full] = [buffer.zoomLevel, buffer.fullZoom];
368
369         if (percent == 100)
370             this.widgets.zoomlevel.value = "";
371         else {
372             percent = ("  " + Math.round(percent)).substr(-3);
373             if (full)
374                 this.widgets.zoomlevel.value = " [" + percent + "%]";
375             else
376                 this.widgets.zoomlevel.value = " (" + percent + "%)";
377         }
378     }
379 });
380
381 // vim: set fdm=marker sw=4 ts=4 et: