Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 996899f

Browse files
committed
Merge pull request #7639 from adobe/zaggino/error-icon
console error indicator for status bar
2 parents ce7ade1 + c25357d commit 996899f

File tree

5 files changed

+188
-4
lines changed

5 files changed

+188
-4
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
25+
/*global define, $, brackets, window, document */
26+
27+
define(function (require, exports, module) {
28+
"use strict";
29+
30+
var _ = brackets.getModule("thirdparty/lodash"),
31+
AnimationUtils = brackets.getModule("utils/AnimationUtils"),
32+
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
33+
Strings = brackets.getModule("strings");
34+
35+
var $span = null,
36+
errorCount = 0,
37+
_attached = false,
38+
_windowOnError,
39+
_consoleError,
40+
_consoleClear;
41+
42+
ExtensionUtils.loadStyleSheet(module, "styles.css");
43+
44+
function showDeveloperTools() {
45+
try {
46+
brackets.app.showDeveloperTools();
47+
} catch (err) {
48+
console.error(err);
49+
}
50+
}
51+
52+
function refreshIndicator() {
53+
// never show 0 errors
54+
if (!_attached || errorCount === 0) {
55+
// hide notifier if it was attached previously
56+
// but errorCount was cleared or it was disabled
57+
if ($span) {
58+
$span.parent().hide();
59+
}
60+
return;
61+
}
62+
63+
// update span if it was created before
64+
if ($span) {
65+
$span.text(errorCount).parent().show();
66+
return;
67+
}
68+
69+
// create the span
70+
$span = $("<span>").text(errorCount);
71+
$("<div>")
72+
.attr("id", "error-counter")
73+
.attr("title", Strings.CMD_SHOW_DEV_TOOLS + "\u2026")
74+
.text(Strings.ERRORS + ": ")
75+
.append($span)
76+
.on("click", showDeveloperTools)
77+
.insertBefore("#status-bar .spinner");
78+
}
79+
80+
var blink = _.debounce(function () {
81+
AnimationUtils.animateUsingClass($span.parent()[0], "flash");
82+
}, 100);
83+
84+
function incErrorCount() {
85+
errorCount++;
86+
blink();
87+
refreshIndicator();
88+
}
89+
90+
function clearErrorCount() {
91+
errorCount = 0;
92+
refreshIndicator();
93+
}
94+
95+
function attachFunctions() {
96+
if (_attached) {
97+
return;
98+
}
99+
100+
_attached = true;
101+
_windowOnError = window.onerror;
102+
_consoleError = window.console.error;
103+
_consoleClear = window.console.clear;
104+
105+
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror
106+
window.onerror = function (errorMsg, url, lineNumber) {
107+
incErrorCount();
108+
if (_windowOnError) {
109+
return _windowOnError(errorMsg, url, lineNumber);
110+
}
111+
// return false means that we didn't handle this error and it should run the default handler
112+
return false;
113+
};
114+
115+
window.console.error = function () {
116+
incErrorCount();
117+
return _consoleError.apply(window.console, arguments);
118+
};
119+
120+
window.console.clear = function () {
121+
clearErrorCount();
122+
return _consoleClear.apply(window.console, arguments);
123+
};
124+
}
125+
126+
function detachFunctions() {
127+
if (!_attached) {
128+
return;
129+
}
130+
131+
_attached = false;
132+
window.onerror = _windowOnError;
133+
window.console.error = _consoleError;
134+
window.console.clear = _consoleClear;
135+
}
136+
137+
function toggle(bool) {
138+
if (bool) {
139+
attachFunctions();
140+
} else {
141+
detachFunctions();
142+
}
143+
refreshIndicator();
144+
}
145+
146+
// Public API
147+
exports.toggle = toggle;
148+
149+
});

src/extensions/default/DebugCommands/main.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ define(function (require, exports, module) {
4040
Dialogs = brackets.getModule("widgets/Dialogs"),
4141
Strings = brackets.getModule("strings"),
4242
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
43+
ErrorNotification = require("ErrorNotification"),
4344
NodeDebugUtils = require("NodeDebugUtils"),
4445
PerfDialogTemplate = require("text!htmlContent/perf-dialog.html"),
4546
LanguageDialogTemplate = require("text!htmlContent/language-dialog.html");
@@ -59,7 +60,10 @@ define(function (require, exports, module) {
5960
DEBUG_SWITCH_LANGUAGE = "debug.switchLanguage",
6061
DEBUG_ENABLE_NODE_DEBUGGER = "debug.enableNodeDebugger",
6162
DEBUG_LOG_NODE_STATE = "debug.logNodeState",
62-
DEBUG_RESTART_NODE = "debug.restartNode";
63+
DEBUG_RESTART_NODE = "debug.restartNode",
64+
DEBUG_SHOW_ERRORS_IN_STATUS_BAR = "debug.showErrorsInStatusBar";
65+
66+
PreferencesManager.definePreference(DEBUG_SHOW_ERRORS_IN_STATUS_BAR, "boolean", false);
6367

6468
function handleShowDeveloperTools() {
6569
brackets.app.showDeveloperTools();
@@ -233,6 +237,22 @@ define(function (require, exports, module) {
233237
});
234238
}
235239

240+
function toggleErrorNotification(bool) {
241+
var val;
242+
243+
if (typeof bool === "undefined") {
244+
val = !PreferencesManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR);
245+
} else {
246+
val = !!bool;
247+
}
248+
249+
ErrorNotification.toggle(val);
250+
251+
// update menu
252+
CommandManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR).setChecked(val);
253+
PreferencesManager.set(DEBUG_SHOW_ERRORS_IN_STATUS_BAR, val);
254+
}
255+
236256
/* Register all the command handlers */
237257

238258
// Show Developer Tools (optionally enabled)
@@ -246,15 +266,17 @@ define(function (require, exports, module) {
246266
CommandManager.register(Strings.CMD_RUN_UNIT_TESTS, DEBUG_RUN_UNIT_TESTS, _runUnitTests)
247267
.setEnabled(false);
248268

249-
CommandManager.register(Strings.CMD_SHOW_PERF_DATA, DEBUG_SHOW_PERF_DATA, handleShowPerfData);
250-
CommandManager.register(Strings.CMD_SWITCH_LANGUAGE, DEBUG_SWITCH_LANGUAGE, handleSwitchLanguage);
269+
CommandManager.register(Strings.CMD_SHOW_PERF_DATA, DEBUG_SHOW_PERF_DATA, handleShowPerfData);
270+
CommandManager.register(Strings.CMD_SWITCH_LANGUAGE, DEBUG_SWITCH_LANGUAGE, handleSwitchLanguage);
271+
CommandManager.register(Strings.CMD_SHOW_ERRORS_IN_STATUS_BAR, DEBUG_SHOW_ERRORS_IN_STATUS_BAR, toggleErrorNotification);
251272

252273
// Node-related Commands
253274
CommandManager.register(Strings.CMD_ENABLE_NODE_DEBUGGER, DEBUG_ENABLE_NODE_DEBUGGER, NodeDebugUtils.enableDebugger);
254275
CommandManager.register(Strings.CMD_LOG_NODE_STATE, DEBUG_LOG_NODE_STATE, NodeDebugUtils.logNodeState);
255276
CommandManager.register(Strings.CMD_RESTART_NODE, DEBUG_RESTART_NODE, NodeDebugUtils.restartNode);
256277

257278
enableRunTestsMenuItem();
279+
toggleErrorNotification(PreferencesManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR));
258280

259281
/*
260282
* Debug menu
@@ -273,6 +295,7 @@ define(function (require, exports, module) {
273295
menu.addMenuItem(DEBUG_ENABLE_NODE_DEBUGGER);
274296
menu.addMenuItem(DEBUG_LOG_NODE_STATE);
275297
menu.addMenuItem(DEBUG_RESTART_NODE);
298+
menu.addMenuItem(DEBUG_SHOW_ERRORS_IN_STATUS_BAR);
276299
menu.addMenuItem(Commands.FILE_OPEN_PREFERENCES); // this command is defined in core, but exposed only in Debug menu for now
277300

278301
// exposed for convenience, but not official API
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#error-counter {
2+
cursor: pointer;
3+
transition: all 3s;
4+
color: #f74687;
5+
background-color: transparent;
6+
}
7+
#error-counter.flash {
8+
transition: all 1s;
9+
background-color: #ffb0cd;
10+
}

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ define({
493493

494494
// extensions/default/DebugCommands
495495
"DEBUG_MENU" : "Debug",
496+
"ERRORS" : "Errors",
496497
"CMD_SHOW_DEV_TOOLS" : "Show Developer Tools",
497498
"CMD_REFRESH_WINDOW" : "Reload With Extensions",
498499
"CMD_RELOAD_WITHOUT_USER_EXTS" : "Reload Without Extensions",
@@ -503,6 +504,7 @@ define({
503504
"CMD_ENABLE_NODE_DEBUGGER" : "Enable Node Debugger",
504505
"CMD_LOG_NODE_STATE" : "Log Node State to Console",
505506
"CMD_RESTART_NODE" : "Restart Node",
507+
"CMD_SHOW_ERRORS_IN_STATUS_BAR" : "Show Errors in Status Bar",
506508

507509
"LANGUAGE_TITLE" : "Switch Language",
508510
"LANGUAGE_MESSAGE" : "Language:",

src/widgets/StatusBar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
<div id="status-overwrite">{{STATUSBAR_INSERT}}</div>
1414
<div class="spinner"></div>
1515
</div>
16-
</div>
16+
</div>

0 commit comments

Comments
 (0)