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

Commit ae3fc3f

Browse files
committed
Merge pull request #1807 from adobe/jasonsanjose/statusbar-refactor
Fix unit tests. Refactor StatusManager and EditorManager init
2 parents 73c92ff + 3acc237 commit ae3fc3f

10 files changed

Lines changed: 153 additions & 81 deletions

src/brackets.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ define(function (require, exports, module) {
9191
UrlParams = require("utils/UrlParams").UrlParams,
9292
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
9393
PreferencesManager = require("preferences/PreferencesManager"),
94-
Resizer = require("utils/Resizer"),
95-
StatusBar = require("widgets/Statusbar");
94+
Resizer = require("utils/Resizer");
9695

9796
// Local variables
9897
var params = new UrlParams(),

src/editor/EditorManager.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,25 @@ define(function (require, exports, module) {
642642
}
643643
}
644644

645+
function _init() {
646+
StatusBar.init($(".main-view .content"));
647+
648+
$modeInfo = $("#status-mode");
649+
$cursorInfo = $("#status-cursor");
650+
$fileInfo = $("#status-file");
651+
$indentType = $("#indent-type");
652+
$indentWidth = $("#indent-width");
653+
$indentDecrement = $("#indent-decrement");
654+
$indentIncrement = $("#indent-increment");
655+
656+
// indentation event handlers
657+
$indentType.on("click", _toggleIndentType);
658+
$indentDecrement.on("click", function () { _changeIndentSize(-1); });
659+
$indentIncrement.on("click", function () { _changeIndentSize(1); });
660+
661+
_onFocusedEditorChange(null, getFocusedEditor(), null);
662+
}
663+
645664
// Initialize: command handlers
646665
CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit);
647666

@@ -657,25 +676,10 @@ define(function (require, exports, module) {
657676
// Initialize: status bar focused listener
658677
$(exports).on("focusedEditorChange", _onFocusedEditorChange);
659678

660-
AppInit.htmlReady(function () {
661-
$modeInfo = $("#status-mode");
662-
$cursorInfo = $("#status-cursor");
663-
$fileInfo = $("#status-file");
664-
$indentType = $("#indent-type");
665-
$indentWidth = $("#indent-width");
666-
$indentDecrement = $("#indent-decrement");
667-
$indentIncrement = $("#indent-increment");
668-
669-
// indentation event handlers
670-
$indentType.on("click", _toggleIndentType);
671-
$indentDecrement.on("click", function () { _changeIndentSize(-1); });
672-
$indentIncrement.on("click", function () { _changeIndentSize(1); });
673-
674-
StatusBar.hide();
675-
_onFocusedEditorChange(null, getFocusedEditor(), null);
676-
});
679+
AppInit.htmlReady(_init);
677680

678681
// For unit tests and internal use only
682+
exports._init = _init;
679683
exports._openInlineWidget = _openInlineWidget;
680684
exports._doFocusedEditorChanged = _doFocusedEditorChanged;
681685
exports._createFullEditorForDocument = _createFullEditorForDocument;

src/htmlContent/main-view.html

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,6 @@
106106
<div class="table-container resizable-content"></div>
107107
</div>
108108

109-
<div id="status-bar" class="statusbar">
110-
<div id="status-info" class="info" >
111-
<div id="status-cursor"></div>
112-
</div>
113-
<div id="status-indicators" class="indicators">
114-
<div id="gold-star" title="{{JSLINT_NO_ERRORS}}">&#9733;</div>
115-
<div id="status-file"></div>
116-
<div id="status-mode"></div>
117-
<div id="status-indent">
118-
<div id="indent-type"></div>
119-
<div id="indent-width"></div>
120-
<div id="indent-decrement" class="indent-step"></div>
121-
<div id="indent-increment" class="indent-step"></div>
122-
</div>
123-
<div id="busy-indicator">&#9719;</div>
124-
</div>
125-
</div>
126109
</div>
127110

128111
<!-- Hack to ensure that the code editor's web font is loaded early. -->

src/widgets/StatusBar.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div id="status-bar" class="statusbar">
2+
<div id="status-info" class="info" >
3+
<div id="status-cursor"></div>
4+
</div>
5+
<div id="status-indicators" class="indicators">
6+
<div id="gold-star" title="{{JSLINT_NO_ERRORS}}">&#9733;</div>
7+
<div id="status-file"></div>
8+
<div id="status-mode"></div>
9+
<div id="status-indent">
10+
<div id="indent-type"></div>
11+
<div id="indent-width"></div>
12+
<div id="indent-decrement" class="indent-step"></div>
13+
<div id="indent-increment" class="indent-step"></div>
14+
</div>
15+
<div id="busy-indicator">&#9719;</div>
16+
</div>
17+
</div>

src/widgets/StatusBar.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
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
*/
77
define(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;

test/SpecRunner.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
<!-- Pre-load third party scripts that cannot be async loaded. -->
3737
<script src="../src/thirdparty/jquery-1.7.min.js"></script>
3838
<script src="../src/thirdparty/CodeMirror2/lib/codemirror.js"></script>
39-
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>
39+
<script src="../src/thirdparty/mustache/mustache.js"></script>
40+
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>
4041

4142
<!-- All other scripts are loaded through require. -->
4243
<script src="../src/thirdparty/require.js" data-main="SpecRunner"></script>

test/spec/Editor-test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ define(function (require, exports, module) {
2929
'use strict';
3030

3131
var Editor = require("editor/Editor").Editor,
32+
EditorManager = require("editor/EditorManager"),
3233
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
3334
EditorUtils = require("editor/EditorUtils");
3435

@@ -37,23 +38,19 @@ define(function (require, exports, module) {
3738
var myDocument, myEditor;
3839

3940
function createTestEditor(content, mode) {
40-
// create dummy Document for the Editor
41-
myDocument = SpecRunnerUtils.createMockDocument(content);
42-
43-
// create Editor instance (containing a CodeMirror instance)
44-
$("body").append("<div id='editor'/>");
45-
myEditor = new Editor(myDocument, true, mode, $("#editor").get(0), {});
41+
// create dummy Document and Editor
42+
var mocks = SpecRunnerUtils.createMockEditor(content, mode);
43+
myDocument = mocks.doc;
44+
myEditor = mocks.editor;
4645
}
4746

4847
afterEach(function () {
4948
if (myEditor) {
50-
myEditor.destroy();
49+
SpecRunnerUtils.destroyMockEditor(myDocument);
5150
myEditor = null;
52-
$("#editor").remove();
5351
myDocument = null;
5452
}
5553
});
56-
5754

5855
describe("Editor wrapper", function () {
5956
beforeEach(function () {

test/spec/EditorCommandHandlers-test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ define(function (require, exports, module) {
2929

3030
var Editor = require("editor/Editor").Editor,
3131
EditorCommandHandlers = require("editor/EditorCommandHandlers"),
32+
EditorManager = require("editor/EditorManager"),
3233
Commands = require("command/Commands"),
3334
CommandManager = require("command/CommandManager"),
3435
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
@@ -47,21 +48,17 @@ define(function (require, exports, module) {
4748

4849
var myDocument, myEditor;
4950
beforeEach(function () {
50-
// create dummy Document for the Editor
51-
myDocument = SpecRunnerUtils.createMockDocument(defaultContent);
51+
// create dummy Document and Editor
52+
var mocks = SpecRunnerUtils.createMockEditor(defaultContent, "javascript");
53+
myDocument = mocks.doc;
54+
myEditor = mocks.editor;
5255

53-
// create Editor instance (containing a CodeMirror instance)
54-
$("body").append("<div id='editor'/>");
55-
myEditor = new Editor(myDocument, true, "javascript", $("#editor").get(0), {});
56-
57-
// Must be focused so editor commands target it
5856
myEditor.focus();
5957
});
6058

6159
afterEach(function () {
62-
myEditor.destroy();
60+
SpecRunnerUtils.destroyMockEditor(myDocument);
6361
myEditor = null;
64-
$("#editor").remove();
6562
myDocument = null;
6663
});
6764

test/spec/MultiRangeInlineEditor-test.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,19 @@ define(function (require, exports, module) {
3939

4040
var inlineEditor,
4141
$editorHolder,
42-
hostEditor;
42+
hostEditor,
43+
doc;
4344

4445
beforeEach(function () {
45-
// init Editor instance (containing a CodeMirror instance)
46-
$("body").append("<div id='editor-holder'/>");
47-
$editorHolder = $("#editor-holder");
48-
EditorManager.setEditorHolder(this.$editorHolder);
49-
50-
var doc = SpecRunnerUtils.createMockDocument("hostEditor");
51-
hostEditor = new Editor(doc, true, "", $editorHolder.get(0), {});
46+
// create dummy Document and Editor
47+
var mocks = SpecRunnerUtils.createMockEditor("hostEditor", "");
48+
doc = mocks.doc;
49+
hostEditor = mocks.editor;
5250
});
5351

5452
afterEach(function () {
55-
hostEditor.destroy();
56-
$editorHolder.remove();
53+
SpecRunnerUtils.destroyMockEditor(doc);
54+
hostEditor = null;
5755
});
5856

5957
it("should initialize to a default state", function () {

0 commit comments

Comments
 (0)