11/*
2- * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
2+ * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
33 *
44 * Permission is hereby granted, free of charge, to any person obtaining a
55 * copy of this software and associated documentation files (the "Software"),
2727define ( function ( require , exports , module ) {
2828 "use strict" ;
2929
30- var ExtensionUtils = brackets . getModule ( "utils/ExtensionUtils " ) ;
30+ var Strings = brackets . getModule ( "strings " ) ;
3131
32- var $span = null ,
33- errorCount = 0 ,
34- _windowOnError = window . onerror ,
35- _consoleError = window . console . error ;
36-
37- ExtensionUtils . loadStyleSheet ( module , "style.css" ) ;
32+ var $span = null ,
33+ errorCount = 0 ,
34+ _attached = false ,
35+ _windowOnError ,
36+ _consoleError ;
3837
3938 function showDeveloperTools ( ) {
4039 try {
@@ -44,37 +43,71 @@ define(function (require, exports, module) {
4443 }
4544 }
4645
47- function showIcon ( ) {
46+ function refreshIcon ( ) {
47+ // never show 0 errors
48+ if ( errorCount === 0 ) {
49+ return ;
50+ }
51+
52+ // update span if it was created before
4853 if ( $span ) {
54+ $span . parent ( ) . toggle ( _attached ) ;
4955 $span . text ( errorCount ) ;
5056 return ;
5157 }
58+
59+ // create the span
5260 $span = $ ( "<span>" ) . text ( errorCount ) ;
53- $ ( "<a>" )
54- . addClass ( "consoleErrorIcon" )
61+ $ ( "<div>" )
62+ . addClass ( "error" )
63+ . text ( Strings . ERRORS + ":" )
5564 . append ( $span )
5665 . on ( "click" , showDeveloperTools )
57- . appendTo ( "#main-toolbar .buttons " ) ;
66+ . prependTo ( "#status-bar .indicators " ) ;
5867 }
5968
6069 function incErrorCount ( ) {
6170 errorCount ++ ;
62- showIcon ( ) ;
71+ refreshIcon ( ) ;
6372 }
6473
65- // https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror
66- window . onerror = function ( errorMsg , url , lineNumber ) {
67- incErrorCount ( ) ;
68- if ( _windowOnError ) {
69- return _windowOnError ( errorMsg , url , lineNumber ) ;
70- }
71- // return false means that we didn't handle this error and it should run the default handler
72- return false ;
73- } ;
74-
75- window . console . error = function ( ) {
76- incErrorCount ( ) ;
77- return _consoleError . apply ( window . console , arguments ) ;
78- } ;
74+ function attachFunctions ( ) {
75+ if ( _attached ) { return ; }
76+
77+ _attached = true ;
78+ _windowOnError = window . onerror ;
79+ _consoleError = window . console . error ;
80+
81+ // https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror
82+ window . onerror = function ( errorMsg , url , lineNumber ) {
83+ incErrorCount ( ) ;
84+ if ( _windowOnError ) {
85+ return _windowOnError ( errorMsg , url , lineNumber ) ;
86+ }
87+ // return false means that we didn't handle this error and it should run the default handler
88+ return false ;
89+ } ;
90+
91+ window . console . error = function ( ) {
92+ incErrorCount ( ) ;
93+ return _consoleError . apply ( window . console , arguments ) ;
94+ } ;
95+ }
96+
97+ function detachFunctions ( ) {
98+ if ( ! _attached ) { return ; }
99+
100+ _attached = false ;
101+ window . onerror = _windowOnError ;
102+ window . console . error = _consoleError ;
103+ }
104+
105+ function toggle ( bool ) {
106+ if ( bool ) { attachFunctions ( ) ; } else { detachFunctions ( ) ; }
107+ refreshIcon ( ) ;
108+ }
109+
110+ // Public API
111+ exports . toggle = toggle ;
79112
80113} ) ;
0 commit comments