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

Commit

Permalink
refactor StatusManager to allow EditorManager to control initializati…
Browse files Browse the repository at this point in the history
…on. Fixes unit test failures
  • Loading branch information
jasonsanjose committed Oct 9, 2012
1 parent 1c2b989 commit ef215ef
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 72 deletions.
3 changes: 1 addition & 2 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ define(function (require, exports, module) {
UrlParams = require("utils/UrlParams").UrlParams,
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
PreferencesManager = require("preferences/PreferencesManager"),
Resizer = require("utils/Resizer"),
StatusBar = require("widgets/Statusbar");
Resizer = require("utils/Resizer");

// Local variables
var params = new UrlParams(),
Expand Down
38 changes: 21 additions & 17 deletions src/editor/EditorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,25 @@ define(function (require, exports, module) {
}
}

function _init() {
StatusBar.init($(".main-view .content"));

$modeInfo = $("#status-mode");
$cursorInfo = $("#status-cursor");
$fileInfo = $("#status-file");
$indentType = $("#indent-type");
$indentWidth = $("#indent-width");
$indentDecrement = $("#indent-decrement");
$indentIncrement = $("#indent-increment");

// indentation event handlers
$indentType.on("click", _toggleIndentType);
$indentDecrement.on("click", function () { _changeIndentSize(-1); });
$indentIncrement.on("click", function () { _changeIndentSize(1); });

_onFocusedEditorChange(null, getFocusedEditor(), null);
}

// Initialize: command handlers
CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT, Commands.TOGGLE_QUICK_EDIT, _toggleQuickEdit);

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

AppInit.htmlReady(function () {
$modeInfo = $("#status-mode");
$cursorInfo = $("#status-cursor");
$fileInfo = $("#status-file");
$indentType = $("#indent-type");
$indentWidth = $("#indent-width");
$indentDecrement = $("#indent-decrement");
$indentIncrement = $("#indent-increment");

// indentation event handlers
$indentType.on("click", _toggleIndentType);
$indentDecrement.on("click", function () { _changeIndentSize(-1); });
$indentIncrement.on("click", function () { _changeIndentSize(1); });

StatusBar.hide();
_onFocusedEditorChange(null, getFocusedEditor(), null);
});
AppInit.htmlReady(_init);

// For unit tests and internal use only
exports._init = _init;
exports._openInlineWidget = _openInlineWidget;
exports._doFocusedEditorChanged = _doFocusedEditorChanged;
exports._createFullEditorForDocument = _createFullEditorForDocument;
Expand Down
17 changes: 0 additions & 17 deletions src/htmlContent/main-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@
<div class="table-container resizable-content"></div>
</div>

<div id="status-bar" class="statusbar">
<div id="status-info" class="info" >
<div id="status-cursor"></div>
</div>
<div id="status-indicators" class="indicators">
<div id="gold-star" title="{{JSLINT_NO_ERRORS}}">&#9733;</div>
<div id="status-file"></div>
<div id="status-mode"></div>
<div id="status-indent">
<div id="indent-type"></div>
<div id="indent-width"></div>
<div id="indent-decrement" class="indent-step"></div>
<div id="indent-increment" class="indent-step"></div>
</div>
<div id="busy-indicator">&#9719;</div>
</div>
</div>
</div>

<!-- Hack to ensure that the code editor's web font is loaded early. -->
Expand Down
17 changes: 17 additions & 0 deletions src/widgets/StatusBar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="status-bar" class="statusbar">
<div id="status-info" class="info" >
<div id="status-cursor"></div>
</div>
<div id="status-indicators" class="indicators">
<div id="gold-star" title="{{JSLINT_NO_ERRORS}}">&#9733;</div>
<div id="status-file"></div>
<div id="status-mode"></div>
<div id="status-indent">
<div id="indent-type"></div>
<div id="indent-width"></div>
<div id="indent-decrement" class="indent-step"></div>
<div id="indent-increment" class="indent-step"></div>
</div>
<div id="busy-indicator">&#9719;</div>
</div>
</div>
68 changes: 54 additions & 14 deletions src/widgets/StatusBar.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, document*/
/*global define, $, brackets, window, document, Mustache */

/**
* A status bar with support for file information and busy and status indicators.
*/
define(function (require, exports, module) {
'use strict';

var AppInit = require("utils/AppInit");
var AppInit = require("utils/AppInit"),
StatusBarHTML = require("text!widgets/StatusBar.html"),
Strings = require("strings");

var _init = false;

// Indicates if the busy cursor is active to avoid unnecesary operations
var busyCursor = false;
var _busyCursor = false;

// A simple regexp to sanitize indicator ids
var indicatorIDRegexp = new RegExp("[^a-zA-Z 0-9]+", "g");
var _indicatorIDRegexp = new RegExp("[^a-zA-Z 0-9]+", "g");

// These vars are initialized by the AppInit.htmlReady handler
// below since they refer to DOM elements
Expand All @@ -27,8 +31,12 @@ define(function (require, exports, module) {
* @param {boolean} updateCursor Sets the cursor to "wait"
*/
function showBusyIndicator(updateCursor) {
if (!_init) {
return;
}

if (updateCursor) {
busyCursor = true;
_busyCursor = true;
$("*").addClass("busyCursor");
}

Expand All @@ -39,10 +47,14 @@ define(function (require, exports, module) {
* Hides the 'busy' indicator
*/
function hideBusyIndicator() {
if (!_init) {
return;
}

// Check if we are using the busyCursor class to avoid
// unnecesary calls to $('*').removeClass()
if (busyCursor) {
busyCursor = false;
if (_busyCursor) {
_busyCursor = false;
$("*").removeClass("busyCursor");
}

Expand All @@ -60,11 +72,14 @@ define(function (require, exports, module) {
* TODO Unused command parameter. Include command functionality for statusbar indicators.
*/
function addIndicator(id, indicator, visible, style, tooltip, command) {

if (!_init) {
return;
}

indicator = indicator || document.createElement("div");
tooltip = tooltip || "";
style = style || "";
id = id.replace(indicatorIDRegexp, "-") || "";
id = id.replace(_indicatorIDRegexp, "-") || "";

var $indicator = $(indicator);

Expand All @@ -89,8 +104,11 @@ define(function (require, exports, module) {
* @param {string} command Optional command name to execute on the indicator click.
*/
function updateIndicator(id, visible, style, tooltip, command) {
if (!_init) {
return;
}

var $indicator = $("#" + id.replace(indicatorIDRegexp, "-"));
var $indicator = $("#" + id.replace(_indicatorIDRegexp, "-"));

if ($indicator) {

Expand Down Expand Up @@ -118,25 +136,47 @@ define(function (require, exports, module) {
* Hide the statusbar
*/
function hide() {
if (!_init) {
return;
}

$statusBar.hide();
}

/**
* Show the statusbar
*/
function show() {
if (!_init) {
return;
}

$statusBar.show();
}

// Initialize items dependent on HTML DOM
AppInit.htmlReady(function () {

function init($parent) {
// check if status bar already exists
if (_init) {
return;
}

$parent = $parent || $("body");
$parent.append(Mustache.render(StatusBarHTML, Strings));

// Initialize items dependent on HTML DOM
$statusBar = $("#status-bar");
$indicators = $("#status-indicators");
$busyIndicator = $("#busy-indicator");

$busyIndicator.hide();
});

_init = true;

// hide on init
hide();
}

exports.init = init;
exports.showBusyIndicator = showBusyIndicator;
exports.hideBusyIndicator = hideBusyIndicator;
exports.addIndicator = addIndicator;
Expand Down
3 changes: 2 additions & 1 deletion test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
<!-- Pre-load third party scripts that cannot be async loaded. -->
<script src="../src/thirdparty/jquery-1.7.min.js"></script>
<script src="../src/thirdparty/CodeMirror2/lib/codemirror.js"></script>
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>
<script src="../src/thirdparty/mustache/mustache.js"></script>
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>

<!-- All other scripts are loaded through require. -->
<script src="../src/thirdparty/require.js" data-main="SpecRunner"></script>
Expand Down
19 changes: 12 additions & 7 deletions test/spec/Editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,36 @@ define(function (require, exports, module) {
'use strict';

var Editor = require("editor/Editor").Editor,
EditorManager = require("editor/EditorManager"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
EditorUtils = require("editor/EditorUtils");

describe("Editor", function () {
var defaultContent = 'Brackets is going to be awesome!\n';
var myDocument, myEditor;
var myDocument, myEditor, $editorHolder;

function createTestEditor(content, mode) {
// Initialize EditorManager
$editorHolder = $("<div id='editor-holder'/>");
EditorManager._init();
EditorManager.setEditorHolder($editorHolder);
$("body").append($editorHolder);

// create dummy Document for the Editor
myDocument = SpecRunnerUtils.createMockDocument(content);

// create Editor instance (containing a CodeMirror instance)
$("body").append("<div id='editor'/>");
myEditor = new Editor(myDocument, true, mode, $("#editor").get(0), {});
// create Editor instance
myEditor = new Editor(myDocument, true, mode, $editorHolder.get(0), {});
}

afterEach(function () {
if (myEditor) {
myEditor.destroy();
EditorManager._destroyEditorIfUnneeded(myDocument);
$editorHolder.remove();
myEditor = null;
$("#editor").remove();
myDocument = null;
}
});


describe("Editor wrapper", function () {
beforeEach(function () {
Expand Down
20 changes: 12 additions & 8 deletions test/spec/EditorCommandHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ define(function (require, exports, module) {

var Editor = require("editor/Editor").Editor,
EditorCommandHandlers = require("editor/EditorCommandHandlers"),
EditorManager = require("editor/EditorManager"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
Expand All @@ -45,23 +46,26 @@ define(function (require, exports, module) {
"\n" +
"}";

var myDocument, myEditor;
var myDocument, myEditor, $editorHolder;
beforeEach(function () {
// Initialize EditorManager
$editorHolder = $("<div id='editor-holder'/>");
EditorManager._init();
EditorManager.setEditorHolder($editorHolder);
$("body").append($editorHolder);

// create dummy Document for the Editor
myDocument = SpecRunnerUtils.createMockDocument(defaultContent);

// create Editor instance (containing a CodeMirror instance)
$("body").append("<div id='editor'/>");
myEditor = new Editor(myDocument, true, "javascript", $("#editor").get(0), {});

// Must be focused so editor commands target it
myEditor = new Editor(myDocument, true, "javascript", $editorHolder.get(0), {});
myEditor.focus();
});

afterEach(function () {
myEditor.destroy();
EditorManager._destroyEditorIfUnneeded(myDocument);
$editorHolder.remove();
myEditor = null;
$("#editor").remove();
myDocument = null;
});

Expand Down
15 changes: 9 additions & 6 deletions test/spec/MultiRangeInlineEditor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,24 @@ define(function (require, exports, module) {

var inlineEditor,
$editorHolder,
hostEditor;
hostEditor,
doc;

beforeEach(function () {
// init Editor instance (containing a CodeMirror instance)
$("body").append("<div id='editor-holder'/>");
$editorHolder = $("#editor-holder");
EditorManager.setEditorHolder(this.$editorHolder);
$editorHolder = $("<div id='editor-holder'/>");
EditorManager._init();
EditorManager.setEditorHolder($editorHolder);
$("body").append($editorHolder);

var doc = SpecRunnerUtils.createMockDocument("hostEditor");
doc = SpecRunnerUtils.createMockDocument("hostEditor");
hostEditor = new Editor(doc, true, "", $editorHolder.get(0), {});
});

afterEach(function () {
hostEditor.destroy();
EditorManager._destroyEditorIfUnneeded(doc);
$editorHolder.remove();
hostEditor = null;
});

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

0 comments on commit ef215ef

Please sign in to comment.