11/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
2- /*global define, $, brackets, window, document*/
2+ /*global define, $, brackets, window, document, Mustache */
33
44/**
55 * A status bar with support for file information and busy and status indicators.
66 */
77define ( function ( require , exports , module ) {
88 'use strict' ;
99
10- var AppInit = require ( "utils/AppInit" ) ;
10+ var AppInit = require ( "utils/AppInit" ) ,
11+ StatusBarHTML = require ( "text!widgets/StatusBar.html" ) ,
12+ Strings = require ( "strings" ) ;
13+
14+ var _init = false ;
1115
1216 // Indicates if the busy cursor is active to avoid unnecesary operations
13- var busyCursor = false ;
17+ var _busyCursor = false ;
1418
1519 // A simple regexp to sanitize indicator ids
16- var indicatorIDRegexp = new RegExp ( "[^a-zA-Z 0-9]+" , "g" ) ;
20+ var _indicatorIDRegexp = new RegExp ( "[^a-zA-Z 0-9]+" , "g" ) ;
1721
1822 // These vars are initialized by the AppInit.htmlReady handler
1923 // below since they refer to DOM elements
@@ -27,8 +31,12 @@ define(function (require, exports, module) {
2731 * @param {boolean } updateCursor Sets the cursor to "wait"
2832 */
2933 function showBusyIndicator ( updateCursor ) {
34+ if ( ! _init ) {
35+ return ;
36+ }
37+
3038 if ( updateCursor ) {
31- busyCursor = true ;
39+ _busyCursor = true ;
3240 $ ( "*" ) . addClass ( "busyCursor" ) ;
3341 }
3442
@@ -39,10 +47,14 @@ define(function (require, exports, module) {
3947 * Hides the 'busy' indicator
4048 */
4149 function hideBusyIndicator ( ) {
50+ if ( ! _init ) {
51+ return ;
52+ }
53+
4254 // Check if we are using the busyCursor class to avoid
4355 // unnecesary calls to $('*').removeClass()
44- if ( busyCursor ) {
45- busyCursor = false ;
56+ if ( _busyCursor ) {
57+ _busyCursor = false ;
4658 $ ( "*" ) . removeClass ( "busyCursor" ) ;
4759 }
4860
@@ -60,11 +72,14 @@ define(function (require, exports, module) {
6072 * TODO Unused command parameter. Include command functionality for statusbar indicators.
6173 */
6274 function addIndicator ( id , indicator , visible , style , tooltip , command ) {
63-
75+ if ( ! _init ) {
76+ return ;
77+ }
78+
6479 indicator = indicator || document . createElement ( "div" ) ;
6580 tooltip = tooltip || "" ;
6681 style = style || "" ;
67- id = id . replace ( indicatorIDRegexp , "-" ) || "" ;
82+ id = id . replace ( _indicatorIDRegexp , "-" ) || "" ;
6883
6984 var $indicator = $ ( indicator ) ;
7085
@@ -89,8 +104,11 @@ define(function (require, exports, module) {
89104 * @param {string } command Optional command name to execute on the indicator click.
90105 */
91106 function updateIndicator ( id , visible , style , tooltip , command ) {
107+ if ( ! _init ) {
108+ return ;
109+ }
92110
93- var $indicator = $ ( "#" + id . replace ( indicatorIDRegexp , "-" ) ) ;
111+ var $indicator = $ ( "#" + id . replace ( _indicatorIDRegexp , "-" ) ) ;
94112
95113 if ( $indicator ) {
96114
@@ -118,25 +136,47 @@ define(function (require, exports, module) {
118136 * Hide the statusbar
119137 */
120138 function hide ( ) {
139+ if ( ! _init ) {
140+ return ;
141+ }
142+
121143 $statusBar . hide ( ) ;
122144 }
123145
124146 /**
125147 * Show the statusbar
126148 */
127149 function show ( ) {
150+ if ( ! _init ) {
151+ return ;
152+ }
153+
128154 $statusBar . show ( ) ;
129155 }
130-
131- // Initialize items dependent on HTML DOM
132- AppInit . htmlReady ( function ( ) {
156+
157+ function init ( $parent ) {
158+ // check if status bar already exists
159+ if ( _init ) {
160+ return ;
161+ }
162+
163+ $parent = $parent || $ ( "body" ) ;
164+ $parent . append ( Mustache . render ( StatusBarHTML , Strings ) ) ;
165+
166+ // Initialize items dependent on HTML DOM
133167 $statusBar = $ ( "#status-bar" ) ;
134168 $indicators = $ ( "#status-indicators" ) ;
135169 $busyIndicator = $ ( "#busy-indicator" ) ;
136170
137171 $busyIndicator . hide ( ) ;
138- } ) ;
172+
173+ _init = true ;
174+
175+ // hide on init
176+ hide ( ) ;
177+ }
139178
179+ exports . init = init ;
140180 exports . showBusyIndicator = showBusyIndicator ;
141181 exports . hideBusyIndicator = hideBusyIndicator ;
142182 exports . addIndicator = addIndicator ;
0 commit comments