|
| 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 | +}); |
0 commit comments