-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOverviewManager.js
executable file
·220 lines (188 loc) · 7.63 KB
/
OverviewManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// ------------------------------------------------------------------------
/*
* Copyright (c) 2014 Thomas Valera. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/*
* OverviewManager
*
* Defines the main manager for the overview extension.
* This manager keeps the overview view updated with the latest editor.
*
*/
// ------------------------------------------------------------------------
/*global define, brackets, $, console, setTimeout */
/*jslint nomen: true, vars: true */
define(function (require, exports, module) {
'use strict';
// Load modules
var ViewManager = require("OverviewViewManager"),
EditorManger = brackets.getModule("editor/EditorManager"),
Editor = brackets.getModule("editor/Editor"),
DocumentManager = brackets.getModule("document/DocumentManager"),
_overviewEditor = null;
// ------------------------------------------------------------------------
/*
* HELPER FUNCTIONS
*/
// ------------------------------------------------------------------------
/*
* Returns the current full editor if exists,
* null otherwise
*/
function _getCurrentFullEditor() {
return EditorManger.getCurrentFullEditor();
}
/*
* Sets the listeners for the overviewEditor
*/
function _setOverviewEditorListeners() {
$(_overviewEditor).on("cursorActivity", function (obj) {
// If selected different line than the one already active
if (obj.target.getCursorPos().line !== _getCurrentFullEditor().getCursorPos().line) {
// Get new position
var cursorPos = _overviewEditor.getCursorPos();
// Set cursor to selected line and center view to position
_getCurrentFullEditor().setCursorPos(cursorPos.line, cursorPos.ch, true, false);
// Set timeout before giving focus back to master editor
// This fixes the bug where the overviewEditor still gets the final focus
// TODO: FIX THIS!
setTimeout(function () {
_getCurrentFullEditor().focus();
}, 100);
}
});
}
/*
* Sets the given document and overview element in overviewEditor.
* If document is null, destroy and disable overivew.
*/
function _setOverviewEditorWithDocument(document, overview) {
// If document exists
if (document !== null && overview !== undefined) {
// If overviewEdor does not exist or overvieweditor has different document
if (_overviewEditor === null || document !== _overviewEditor.document) {
// If overviewEditor exists, destroy
if (_overviewEditor !== null) {
_overviewEditor.destroy();
}
// Create overview editor
_overviewEditor = new Editor.Editor(document, false, overview.find("#code-overview-content").get(0));
// Set listeners
_setOverviewEditorListeners();
// Enable
ViewManager.enable();
}
} else if (document === null) {
// Destroy and disable
ViewManager.disable();
// If overviewEditor exists
if (_overviewEditor !== null) {
_overviewEditor.destroy();
_overviewEditor = null;
}
} else {
console.error("Cannot set overview editor, document or overview do not exist");
}
}
/*
* Reloads the overviewEditor if needed
*/
function _reloadOverviewEditor() {
// Get overview and fulleditor
var overview = ViewManager.getOverview();
// If overview exists
if (overview !== null && overview !== undefined) {
var fullEditor = _getCurrentFullEditor();
// If overview and fullEditor exist
if (fullEditor !== null) {
_setOverviewEditorWithDocument(fullEditor.document, overview);
} else {
console.error("Full editor does not exist!");
_setOverviewEditorWithDocument(null, undefined);
}
} else {
console.error("Overview element does not exist");
}
}
// ------------------------------------------------------------------------
/*
* LISTENERS FUNCTIONS
*/
// ------------------------------------------------------------------------
/*
* Sets the listeners for the viewmanager
*/
function _setViewManagerListeners() {
$(ViewManager).on("OverviewAttached", function () {
// If currentFullEditor exists, enable
if (_getCurrentFullEditor() !== null) {
ViewManager.enable();
}
});
$(ViewManager).on("ZoomApplied", function () {
_overviewEditor.refresh();
});
$(ViewManager).on("OverviewVisible", function () {
_reloadOverviewEditor();
});
$(ViewManager).on("OverviewHidden", function () {
_overviewEditor.destroy();
_overviewEditor = null;
});
}
/*
* Sets listener for panel manager
*/
function _setDocumentManagerListeners() {
$(DocumentManager).on("currentDocumentChange", function () {
// Reload only if overviewEditor exists
// This will handle the case where a new editor is available
// and the case where the last editor is removed
if (_overviewEditor !== null) {
_reloadOverviewEditor();
} else {
// Else, only enable
// The overviewEditor will be reloaded when the toolbar is clicked.
ViewManager.enable();
}
});
}
// ------------------------------------------------------------------------
/*
* API FUNCTIONS
*/
// ------------------------------------------------------------------------
/*
* Initializes the manager
*/
function init() {
// Set listeners
_setDocumentManagerListeners();
_setViewManagerListeners();
// Initialize viewmanager
ViewManager.init();
}
// API
exports.init = init;
});