Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
153 changes: 153 additions & 0 deletions src/extensions/default/DebugCommands/ErrorNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, document */

define(function (require, exports, module) {
"use strict";

var _ = brackets.getModule("thirdparty/lodash"),
Strings = brackets.getModule("strings");

var $span = null,
errorCount = 0,
_attached = false,
_windowOnError,
_consoleError,
_consoleClear;

function showDeveloperTools() {
try {
brackets.app.showDeveloperTools();
} catch (err) {
console.error(err);
}
}

function refreshIcon() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an "icon" anymore, so maybe change this name to refreshIndicator() ?

// never show 0 errors
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to do anything here if icon is toggled off, so maybe a quick check:

        if (!_attached) {
            if ($span) {
                $span.parent().hide();
            }
            return;
        }

if (errorCount === 0) {
// hide notifier if it was attached previously but errorCount was cleared
if ($span) {
$span.parent().hide();
}
return;
}

// update span if it was created before
if ($span) {
$span.parent().toggle(_attached);
$span.text(errorCount);
return;
}

// create the span
$span = $("<span>").text(errorCount);
$("<div>")
.addClass("error")
.css("cursor", "pointer")
.text(Strings.ERRORS + ": ")
.append($span)
.on("click", showDeveloperTools)
.prependTo("#status-bar .dynamic-indicators");
}

var blink = _.debounce(function () {
var times = 3,
speed = 200;

function blinkOnce() {
$span.css("visibility", "hidden");
setTimeout(function () {
$span.css("visibility", "visible");
times--;
if (times > 0) {
setTimeout(function () {
blinkOnce();
}, speed);
}
}, speed);
}

blinkOnce();
}, 100);

function incErrorCount() {
errorCount++;
blink();
refreshIcon();
}

function clearErrorCount() {
errorCount = 0;
refreshIcon();
}

function attachFunctions() {
if (_attached) { return; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format for this should be:

        if (_attached) {
            return;
        }

Also line 137.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by b081a1a


_attached = true;
_windowOnError = window.onerror;
_consoleError = window.console.error;
_consoleClear = window.console.clear;

// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror
window.onerror = function (errorMsg, url, lineNumber) {
incErrorCount();
if (_windowOnError) {
return _windowOnError(errorMsg, url, lineNumber);
}
// return false means that we didn't handle this error and it should run the default handler
return false;
};

window.console.error = function () {
incErrorCount();
return _consoleError.apply(window.console, arguments);
};

window.console.clear = function () {
clearErrorCount();
return _consoleClear.apply(window.console, arguments);
};
}

function detachFunctions() {
if (!_attached) { return; }

_attached = false;
window.onerror = _windowOnError;
window.console.error = _consoleError;
window.console.clear = _consoleClear;
}

function toggle(bool) {
if (bool) { attachFunctions(); } else { detachFunctions(); }
refreshIcon();
}

// Public API
exports.toggle = toggle;

});
29 changes: 26 additions & 3 deletions src/extensions/default/DebugCommands/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ define(function (require, exports, module) {
Dialogs = brackets.getModule("widgets/Dialogs"),
Strings = brackets.getModule("strings"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
ErrorNotification = require("ErrorNotification"),
NodeDebugUtils = require("NodeDebugUtils"),
PerfDialogTemplate = require("text!htmlContent/perf-dialog.html"),
LanguageDialogTemplate = require("text!htmlContent/language-dialog.html");
Expand All @@ -59,7 +60,10 @@ define(function (require, exports, module) {
DEBUG_SWITCH_LANGUAGE = "debug.switchLanguage",
DEBUG_ENABLE_NODE_DEBUGGER = "debug.enableNodeDebugger",
DEBUG_LOG_NODE_STATE = "debug.logNodeState",
DEBUG_RESTART_NODE = "debug.restartNode";
DEBUG_RESTART_NODE = "debug.restartNode",
DEBUG_SHOW_ERRORS_IN_STATUS_BAR = "debug.showErrorsInStatusBar";

PreferencesManager.definePreference(DEBUG_SHOW_ERRORS_IN_STATUS_BAR, "boolean", false);

function handleShowDeveloperTools() {
brackets.app.showDeveloperTools();
Expand Down Expand Up @@ -230,6 +234,22 @@ define(function (require, exports, module) {
});
}

function toggleErrorNotification(bool) {
var val;

if (typeof bool === "undefined") {
val = !PreferencesManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR);
} else {
val = !!bool;
}

ErrorNotification.toggle(val);

// update menu
CommandManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR).setChecked(val);
PreferencesManager.set(DEBUG_SHOW_ERRORS_IN_STATUS_BAR, val);
}

/* Register all the command handlers */

// Show Developer Tools (optionally enabled)
Expand All @@ -243,15 +263,17 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_RUN_UNIT_TESTS, DEBUG_RUN_UNIT_TESTS, _runUnitTests)
.setEnabled(false);

CommandManager.register(Strings.CMD_SHOW_PERF_DATA, DEBUG_SHOW_PERF_DATA, handleShowPerfData);
CommandManager.register(Strings.CMD_SWITCH_LANGUAGE, DEBUG_SWITCH_LANGUAGE, handleSwitchLanguage);
CommandManager.register(Strings.CMD_SHOW_PERF_DATA, DEBUG_SHOW_PERF_DATA, handleShowPerfData);
CommandManager.register(Strings.CMD_SWITCH_LANGUAGE, DEBUG_SWITCH_LANGUAGE, handleSwitchLanguage);
CommandManager.register(Strings.CMD_SHOW_ERRORS_IN_STATUS_BAR, DEBUG_SHOW_ERRORS_IN_STATUS_BAR, toggleErrorNotification);

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

enableRunTestsMenuItem();
toggleErrorNotification(PreferencesManager.get(DEBUG_SHOW_ERRORS_IN_STATUS_BAR));

/*
* Debug menu
Expand All @@ -270,6 +292,7 @@ define(function (require, exports, module) {
menu.addMenuItem(DEBUG_ENABLE_NODE_DEBUGGER);
menu.addMenuItem(DEBUG_LOG_NODE_STATE);
menu.addMenuItem(DEBUG_RESTART_NODE);
menu.addMenuItem(DEBUG_SHOW_ERRORS_IN_STATUS_BAR);
menu.addMenuItem(Commands.FILE_OPEN_PREFERENCES); // this command is defined in core, but exposed only in Debug menu for now

// exposed for convenience, but not official API
Expand Down
2 changes: 2 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ define({

// extensions/default/DebugCommands
"DEBUG_MENU" : "Debug",
"ERRORS" : "Errors",
"CMD_SHOW_DEV_TOOLS" : "Show Developer Tools",
"CMD_REFRESH_WINDOW" : "Reload With Extensions",
"CMD_RELOAD_WITHOUT_USER_EXTS" : "Reload Without Extensions",
Expand All @@ -503,6 +504,7 @@ define({
"CMD_ENABLE_NODE_DEBUGGER" : "Enable Node Debugger",
"CMD_LOG_NODE_STATE" : "Log Node State to Console",
"CMD_RESTART_NODE" : "Restart Node",
"CMD_SHOW_ERRORS_IN_STATUS_BAR" : "Show Errors in Status Bar",

"LANGUAGE_TITLE" : "Switch Language",
"LANGUAGE_MESSAGE" : "Language:",
Expand Down
4 changes: 4 additions & 0 deletions src/styles/brackets.less
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ a, img {
line-height: 25px;
height: 26px;
overflow: hidden;
.error {
font-weight: @font-weight-semibold;
color: @tc-error-text;
}
}

#status-info {
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/StatusBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
</div>
<div id="status-language"></div>
<div id="status-overwrite">{{STATUSBAR_INSERT}}</div>
<div class="dynamic-indicators">
<!-- container for on demand generated indicators so they are before the spinner -->
</div>
<div class="spinner"></div>
</div>
</div>
</div>