-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathMainViewManager.js
More file actions
1858 lines (1608 loc) · 65.3 KB
/
MainViewManager.js
File metadata and controls
1858 lines (1608 loc) · 65.3 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// jshint ignore: start
// @INCLUDE_IN_API_DOCS
/**
* MainViewManager manages the arrangement of all open panes as well as provides the controller
* logic behind all views in the MainView (e.g. ensuring that a file doesn't appear in 2 lists)
*
* Each pane contains one or more views wich are created by a view factory and inserted into a pane list.
* There may be several panes managed by the MainViewManager with each pane containing a list of views.
* The panes are always visible and the layout is determined by the MainViewManager and the user.
*
* Currently we support only 2 panes.
*
* All of the WorkingSet APIs take a paneId Argument. This can be an actual pane Id, ALL_PANES (in most cases)
* or ACTIVE_PANE. ALL_PANES may not be supported for some APIs. See the API for details.
*
* This module dispatches several events:
*
* - activePaneChange - When the active pane changes. There will always be an active pane.
* (e, newPaneId:string, oldPaneId:string)
* - currentFileChange -- When the user has switched to another pane, file, document. When the user closes a view
* and there are no other views to show the current file will be null.
* (e, newFile:File, newPaneId:string, oldFile:File, oldPaneId:string)
* - paneLayoutChange -- When Orientation changes.
* (e, orientation:string)
* - paneCreate -- When a pane is created
* (e, paneId:string)
* - paneDestroy -- When a pane is destroyed
* (e, paneId:string)
*
*
* To listen for working set changes, you must listen to *all* of these events:
* - workingSetAdd -- When a file is added to the working set
* (e, fileAdded:File, index:number, paneId:string)
* - workingSetAddList -- When multiple files are added to the working set
* (e, fileAdded:Array."File", paneId:string)
* - workingSetMove - When a File has moved to a different working set
* (e, File:FILE, sourcePaneId:string, destinationPaneId:string)
* - workingSetRemove -- When a file is removed from the working set
* (e, fileRemoved:File, suppressRedraw:boolean, paneId:string)
* - workingSetRemoveList -- When multiple files are removed from the working set
* (e, filesRemoved:Array."File", paneId:string)
* - workingSetSort -- When a pane's view array is reordered without additions or removals.
* (e, paneId:string)
* - workingSetUpdate -- When changes happen due to system events such as a file being deleted.
* listeners should discard all working set info and rebuilt it from the pane
* by calling getWorkingSet()
* (e, paneId:string)
* - _workingSetDisableAutoSort -- When the working set is reordered by manually dragging a file.
* (e, paneId:string) For Internal Use Only.
*
* To listen for events, do something like this: (see EventDispatcher for details on this pattern)
* `MainViewManager.on("eventname", handler);`
* @module view/MainViewManager
*/
define(function (require, exports, module) {
var _ = require("thirdparty/lodash"),
EventDispatcher = require("utils/EventDispatcher"),
Strings = require("strings"),
AppInit = require("utils/AppInit"),
CommandManager = require("command/CommandManager"),
MainViewFactory = require("view/MainViewFactory"),
ViewStateManager = require("view/ViewStateManager"),
Commands = require("command/Commands"),
EditorManager = require("editor/EditorManager"),
FileSystemError = require("filesystem/FileSystemError"),
DocumentManager = require("document/DocumentManager"),
PreferencesManager = require("preferences/PreferencesManager"),
ProjectManager = require("project/ProjectManager"),
WorkspaceManager = require("view/WorkspaceManager"),
AsyncUtils = require("utils/Async"),
ViewUtils = require("utils/ViewUtils"),
Resizer = require("utils/Resizer"),
Pane = require("view/Pane").Pane,
KeyBindingManager = brackets.getModule("command/KeyBindingManager");
/**
* Event current file change
* @const
* @type {string}
*/
const EVENT_CURRENT_FILE_CHANGE = "currentFileChange";
/**
* Preference setting name for the MainView Saved State
* @const
* @private
*/
var PREFS_NAME = "mainView.state";
/**
* Legacy Preference setting name used to migrate old preferences
* @const
* @private
*/
var OLD_PREFS_NAME = "project.files";
/**
* Special paneId shortcut that can be used to specify that
* all panes should be targeted by the API.
* Not all APIs support this constnant.
* Check the API documentation before use.
* @const
*/
var ALL_PANES = "ALL_PANES";
/**
* Special paneId shortcut that can be used to specify that
* the API should target the focused pane only.
* All APIs support this shortcut.
* @const
*/
var ACTIVE_PANE = "ACTIVE_PANE";
/**
* Internal pane id
* @const
* @private
*/
var FIRST_PANE = "first-pane";
/**
* Internal pane id
* @const
* @private
*/
var SECOND_PANE = "second-pane";
/*
* NOTE: The following commands and constants will change
* when implementing the UX UI Treatment @larz0
*/
/**
* Vertical layout state name
* @const
* @private
*/
var VERTICAL = "VERTICAL";
/**
* Horizontal layout state name
* @const
* @private
*/
var HORIZONTAL = "HORIZONTAL";
/**
* The minimum width or height that a pane can be
* @const
* @private
*/
var MIN_PANE_SIZE = 75;
/**
* current orientation (null, VERTICAL or HORIZONTAL)
* @type {string=}
* @private
*/
var _orientation = null;
/**
* current pane id. May not be null
* @type {!string}
* @private
*/
var _activePaneId = null;
/**
* DOM element hosting the Main View.
* @type {jQuery}
* @private
*/
var _$el;
/**
* Maps paneId to Pane objects
* @type {Object.<string, Pane>}
* @private
*/
var _panes = {};
/**
* map of pane scroll states
* @type {Object.map<string, *>}
* @private
*/
var _paneScrollStates = {};
/**
* flag indicating if traversing is currently taking place
* When True, changes the current pane's MRU list will not be updated.
* Useful for next/previous keyboard navigation (until Ctrl is released)
* or for incremental-search style document preview like Quick Open will eventually have.
* @type {!boolean}
* @private
*/
var _traversingFileList = false;
/**
* The global MRU list (for traversing)
* @type {Array.<file:File, paneId:string>}
* @private
*/
var _mruList = [];
/**
* Localized pane titles
* @type {Object.<"FIRST_PANE"|"SECOND_PANE", {VERTICAL: string, HORIZONTAL: string}>}
* Localized string for first and second panes in the current orientation.
* @see {@link #getPaneTitle} for more information
* @private
*/
var _paneTitles = {};
/*
* Initialize _paneTitles
*/
_paneTitles[FIRST_PANE] = {};
_paneTitles[SECOND_PANE] = {};
_paneTitles[FIRST_PANE][VERTICAL] = Strings.LEFT;
_paneTitles[FIRST_PANE][HORIZONTAL] = Strings.TOP;
_paneTitles[SECOND_PANE][VERTICAL] = Strings.RIGHT;
_paneTitles[SECOND_PANE][HORIZONTAL] = Strings.BOTTOM;
/**
* Makes a MRU List Entry
* @param {!File} File - the file
* @param {!string} paneId - the paneId
* @return {{file:File, paneId:string}}
* @private
*/
function _makeMRUListEntry(file, paneId) {
return { file: file, paneId: paneId };
}
/**
* Locates the first MRU entry of a file for the requested pane
* @param {!string} paneId - the paneId
* @param {!File} File - the file
* @return {{file:File, paneId:string}}
* @private
*/
function _findFileInMRUList(paneId, file) {
return _.findIndex(_mruList, function (record) {
return (record.file.fullPath === file.fullPath && record.paneId === paneId);
});
}
/**
* Checks whether a file is listed exclusively in the provided pane
* @param {!File} File - the file
* @return {{file:File, paneId:string}}
*/
function isExclusiveToPane(file, paneId) {
paneId = paneId === ACTIVE_PANE && _activePaneId ? _activePaneId : paneId;
var index = _.findIndex(_mruList, function (record) {
return (record.file.fullPath === file.fullPath && record.paneId !== paneId);
});
return index === -1;
}
/**
* Retrieves the currently active Pane Id
* @return {!string} Active Pane's ID.
*/
function getActivePaneId() {
return _activePaneId;
}
/**
* Resolve paneId to actual pane.
* @param {?string} paneId - id of the desired pane. May be symbolic or null (to indicate current pane)
* @return {string} id of the pane in which to open the document
* @private
*/
function _resolvePaneId(paneId) {
if (!paneId || paneId === ACTIVE_PANE) {
return getActivePaneId();
}
return paneId;
}
/**
* Retrieves the Pane object for the given paneId
* @param {!string} paneId - id of the pane to retrieve
* @return {?Pane} the Pane object or null if a pane object doesn't exist for the pane
* @private
*/
function _getPane(paneId) {
paneId = _resolvePaneId(paneId);
if (_panes[paneId]) {
return _panes[paneId];
}
return null;
}
/**
* Focuses the current pane. If the current pane has a current view, then the pane will focus the view.
*/
function focusActivePane() {
const activePane = _getPane(ACTIVE_PANE);
activePane && activePane.focus();
}
/**
* Determines if the pane id is a special pane id
* @param {!string} paneId - the id to test
* @return {boolean} true if the pane id is a special identifier, false if not
* @private
*/
function _isSpecialPaneId(paneId) {
return paneId === ACTIVE_PANE || paneId === ALL_PANES;
}
/**
* Makes the file the most recent for the pane and the global mru lists
* @param {!string} paneId - id of the pane to mae th file most recent or ACTIVE_PANE
* @param {!File} file - File object to make most recent
* @private
*/
function _makeFileMostRecent(paneId, file) {
var index,
entry,
pane = _getPane(paneId);
if (!_traversingFileList) {
pane.makeViewMostRecent(file);
index = _findFileInMRUList(pane.id, file);
entry = _makeMRUListEntry(file, pane.id);
if (index !== -1) {
_mruList.splice(index, 1);
}
if (_findFileInMRUList(pane.id, file) !== -1) {
console.log(file.fullPath + " duplicated in mru list");
}
// add it to the front of the list
_mruList.unshift(entry);
}
}
/**
* Makes the Pane's current file the most recent
* @param {!string} paneId - id of the pane to make the file most recent, or ACTIVE_PANE
* @param {!File} file - File object to make most recent
* @private
*/
function _makePaneMostRecent(paneId) {
var pane = _getPane(paneId);
if (pane.getCurrentlyViewedFile()) {
_makeFileMostRecent(paneId, pane.getCurrentlyViewedFile());
}
}
/**
* Switch active pane to the specified pane id (or ACTIVE_PANE/ALL_PANES, in which case this
* call does nothing).
* @param {!string} paneId - the id of the pane to activate
*/
function setActivePaneId(newPaneId) {
if (!_isSpecialPaneId(newPaneId) && newPaneId !== _activePaneId) {
var oldPaneId = _activePaneId,
oldPane = _getPane(ACTIVE_PANE),
newPane = _getPane(newPaneId);
if (!newPane) {
throw new Error("invalid pane id: " + newPaneId);
}
_activePaneId = newPaneId;
exports.trigger("activePaneChange", newPaneId, oldPaneId);
exports.trigger(EVENT_CURRENT_FILE_CHANGE, _getPane(ACTIVE_PANE).getCurrentlyViewedFile(),
newPaneId,
oldPane.getCurrentlyViewedFile(),
oldPaneId);
_makePaneMostRecent(_activePaneId);
focusActivePane();
}
}
/**
* Retrieves the Pane ID for the specified container
* @private
* @param {!jQuery} $el - the element of the pane to fetch
* @return {?string} the id of the pane that matches the container or undefined if a pane doesn't exist for that container
*/
function _getPaneFromElement($el) {
return _.find(_panes, function (pane) {
if (pane.$el[0] === $el[0]) {
return pane;
}
});
}
/**
* Retrieves the currently viewed file of the specified paneId
* @param {?string} paneId - the id of the pane in which to retrieve the currently viewed file
* @return {?File} File object of the currently viewed file, or null if there isn't one or there's no such pane
*/
function getCurrentlyViewedFile(paneId) {
var pane = _getPane(paneId);
return pane ? pane.getCurrentlyViewedFile() : null;
}
/**
* Retrieves the currently viewed editor of the specified paneId
* @param {?string} paneId - the id of the pane in which to retrieve the currently viewed editor
* @return {?Editor} currently editor, or null if there isn't one or there's no such pane
*/
function getCurrentlyViewedEditor(paneId) {
const pane = _getPane(paneId);
return pane ? pane.getCurrentlyViewedEditor() : null;
}
/**
* Gets an array of editors open in panes with their pane IDs.
* Can return an empty array if no editors are open.
*
* @return {{editor: Editor, paneId: string[]}} An array of objects, each containing an editor and its corresponding pane ID.
*/
function getAllViewedEditors() {
const editorList = [];
for (let paneId of getPaneIdList()) {
const editor = getCurrentlyViewedEditor(paneId);
if (editor) {
editorList.push({ editor, paneId });
}
}
return editorList;
}
/**
* Retrieves the currently viewed path of the pane specified by paneId
* @param {?string} paneId - the id of the pane in which to retrieve the currently viewed path
* @return {?string} the path of the currently viewed file or null if there isn't one
*/
function getCurrentlyViewedPath(paneId) {
var file = getCurrentlyViewedFile(paneId);
return file ? file.fullPath : null;
}
/**
* EditorManager.activeEditorChange handler
* This event is triggered when an visible editor gains focus
* Therefore we need to Activate the pane that the active editor belongs to
* @private
* @param {!jQuery.Event} e - jQuery Event object
* @param {Editor=} current - editor being made the current editor
*/
function _activeEditorChange(e, current) {
if (current) {
var $container = current.$el.parent().parent(),
pane = _getPaneFromElement($container);
if (pane) {
// Editor is a full editor
if (pane.id !== _activePaneId) {
// we just need to set the active pane in this case
// it will dispatch the currentFileChange message as well
// as dispatching other events when the active pane changes
setActivePaneId(pane.id);
}
} else {
// Editor is an inline editor, find the parent pane
var parents = $container.parents(".view-pane");
if (parents.length === 1) {
$container = $(parents[0]);
pane = _getPaneFromElement($container);
if (pane) {
if (pane.id !== _activePaneId) {
// activate the pane which will put focus in the pane's doc
setActivePaneId(pane.id);
// reset the focus to the inline editor
current.focus();
}
}
}
}
}
}
/**
* Iterates over the pane or ALL_PANES and calls the callback function for each.
* @param {!string} paneId - id of the pane in which to adjust the scroll state, ALL_PANES or ACTIVE_PANE
* @param {!function(!pane:Pane):boolean} callback - function to callback on to perform work.
* The callback will receive a Pane and should return false to stop iterating.
* @private
*/
function _forEachPaneOrPanes(paneId, callback) {
if (paneId === ALL_PANES) {
_.forEach(_panes, callback);
} else {
callback(_getPane(paneId));
}
}
/**
* Caches the specified pane's current scroll state
* If there was already cached state for the specified pane, it is discarded and overwritten
* @param {!string} paneId - id of the pane in which to cache the scroll state,
* ALL_PANES or ACTIVE_PANE
*/
function cacheScrollState(paneId) {
_forEachPaneOrPanes(paneId, function (pane) {
_paneScrollStates[pane.id] = pane.getScrollState();
});
}
/**
* Restores the scroll state from cache and applies the heightDelta
* The view implementation is responsible for applying or ignoring the heightDelta.
* This is used primarily when a modal bar opens to keep the editor from scrolling the current
* page out of view in order to maintain the appearance.
* The state is removed from the cache after calling this function.
* @param {!string} paneId - id of the pane in which to adjust the scroll state,
* ALL_PANES or ACTIVE_PANE
* @param {!number} heightDelta - delta H to apply to the scroll state
*/
function restoreAdjustedScrollState(paneId, heightDelta) {
_forEachPaneOrPanes(paneId, function (pane) {
pane.restoreAndAdjustScrollState(_paneScrollStates[pane.id], heightDelta);
delete _paneScrollStates[pane.id];
});
}
/**
* Retrieves the WorkingSet for the given paneId not including temporary views
* @param {!string} paneId - id of the pane in which to get the view list, ALL_PANES or ACTIVE_PANE
* @return {Array.<File>}
*/
function getWorkingSet(paneId) {
var result = [];
_forEachPaneOrPanes(paneId, function (pane) {
var viewList = pane.getViewList();
result = _.union(result, viewList);
});
return result;
}
/**
* Retrieves the list of all open files including temporary views
* @return {array.<File>} the list of all open files in all open panes
*/
function getAllOpenFiles() {
var result = getWorkingSet(ALL_PANES);
_.forEach(_panes, function (pane) {
var file = pane.getCurrentlyViewedFile();
if (file) {
result = _.union(result, [file]);
}
});
return result;
}
/**
* Retrieves the list of all open pane ids
* @return {array.<string>} the list of all open panes
*/
function getPaneIdList() {
return Object.keys(_panes);
}
/**
* Retrieves the size of the selected pane's view list
* @param {!string} paneId - id of the pane in which to get the workingset size.
* Can use `ALL_PANES` or `ACTIVE_PANE`
* @return {!number} the number of items in the specified pane
*/
function getWorkingSetSize(paneId) {
var result = 0;
_forEachPaneOrPanes(paneId, function (pane) {
result += pane.getViewListSize();
});
return result;
}
/**
* Retrieves the title to display in the workingset view
* @param {!string} paneId - id of the pane in which to get the title
* @return {?string} title
*/
function getPaneTitle(paneId) {
return _paneTitles[paneId][_orientation];
}
/**
* Retrieves the number of panes
* @return {number}
*/
function getPaneCount() {
return Object.keys(_panes).length;
}
/**
* Helper to abastract the common working set search functions
* @param {!string} paneId - id of the pane to search or ALL_PANES to search all panes
* @param {!string} fullPath - path of the file to locate
* @param {!string} method - name of the method to use for searching
* "findInViewList", "findInViewListAddedOrder" or "FindInViewListMRUOrder"
*
* @private
*/
function _doFindInWorkingSet(paneId, fullPath, method) {
var result = -1;
_forEachPaneOrPanes(paneId, function (pane) {
var index = pane[method].call(pane, fullPath);
if (index >= 0) {
result = index;
return false;
}
});
return result;
}
/**
* Finds all instances of the specified file in all working sets.
* If there is a temporary view of the file, it is not part of the result set
* @param {!string} fullPath - path of the file to find views of
* @return {{pane:string, index:number}} an array of paneId/index records
*/
function findInAllWorkingSets(fullPath) {
let index,
result = [];
_.forEach(_panes, function (pane) {
index = pane.findInViewList(fullPath);
if (index >= 0) {
result.push({ paneId: pane.id, index: index });
}
});
return result;
}
/**
* Returns the pane IDs and editors (if present) of the given file in any open and viewable pane.
* If the same file is open in multiple panes, all matching panes will be returned.
* If not found in any panes, an empty array will be returned.
* @param {string} fullPath - The full path of the file to search for.
* @return {{paneId: string, editor: ?Editor}} An array of objects, each containing the pane ID and the corresponding editor, if present.
*/
function findInOpenPane(fullPath) {
const paneList = [];
for (let paneId of getPaneIdList()) {
const file = getCurrentlyViewedFile(paneId);
const editor = getCurrentlyViewedEditor(paneId);
if (file && file.fullPath === fullPath) {
paneList.push({ paneId, editor });
}
}
return paneList;
}
/**
* Gets the index of the file matching fullPath in the workingset
* @param {!string} paneId - id of the pane in which to search or ALL_PANES or ACTIVE_PANE
* @param {!string} fullPath - full path of the file to search for
* @return {number} index, -1 if not found.
*/
function findInWorkingSet(paneId, fullPath) {
return _doFindInWorkingSet(paneId, fullPath, "findInViewList");
}
/**
* Gets the index of the file matching fullPath in the added order workingset
* @param {!string} paneId - id of the pane in which to search or ALL_PANES or ACTIVE_PANE
* @param {!string} fullPath - full path of the file to search for
* @return {number} index, -1 if not found.
*/
function findInWorkingSetByAddedOrder(paneId, fullPath) {
return _doFindInWorkingSet(paneId, fullPath, "findInViewListAddedOrder");
}
/**
* Gets the index of the file matching fullPath in the MRU order workingset
* @param {!string} paneId - id of the pane in which to search or ALL_PANES or ACTIVE_PANE
* @param {!string} fullPath - full path of the file to search for
* @return {number} index, -1 if not found.
*/
function findInWorkingSetByMRUOrder(paneId, fullPath) {
return _doFindInWorkingSet(paneId, fullPath, "findInViewListMRUOrder");
}
/**
* @private
* Retrieves pane id where the specified file has been opened. Used to ensure that a file
* is open in only one pane so this will change once support for multiple views is added
* The result includes panes with a temporary view of the file not just working set instances
* @param {!string} fullPath - full path of the file to search for
* @return {?string} pane id where the file has been opened or null if it wasn't found
*/
function _getPaneIdForPath(fullPath) {
// Search all working sets and pull off the first one
var info = findInAllWorkingSets(fullPath).shift();
// Look for a view that has not been added to a working set
if (!info) {
_.forEach(_panes, function (pane) {
if (pane.getCurrentlyViewedPath() === fullPath) {
info = { paneId: pane.id };
return false;
}
});
}
if (!info) {
return null;
}
return info.paneId;
}
/**
* View states are only saved once every second max.
* @type {boolean}
* @private
*/
let _viewStateSaveScheduled = false;
function _scheduleViewStateSave() {
function _saveViewStateAndResetScheduler() {
_saveViewState();
_viewStateSaveScheduled = false;
}
if (!_viewStateSaveScheduled) {
_viewStateSaveScheduled = true;
window.setTimeout(_saveViewStateAndResetScheduler, 1000);
}
}
/**
* Adds the given file to the end of the workingset, if it is not already there.
* This API does not create a view of the file, it just adds it to the working set
* Views of files in the working set are persisted and are not destroyed until the user
* closes the file using FILE_CLOSE; Views are created using FILE_OPEN and, when opened, are
* made the current view. If a File is already opened then the file is just made current
* and its view is shown.
* @param {!string} paneId - The id of the pane in which to add the file object to or ACTIVE_PANE
* @param {!File} file - The File object to add to the workingset
* @param {number=} index - Position to add to list (defaults to last); -1 is ignored
* @param {boolean=} forceRedraw - If true, a workingset change notification is always sent
* (useful if suppressRedraw was used with removeView() earlier)
*/
function addToWorkingSet(paneId, file, index, force) {
// look for the file to have already been added to another pane
var pane = _getPane(paneId);
if (!pane) {
throw new Error("invalid pane id: " + paneId);
}
var result = pane.reorderItem(file, index, force),
entry = _makeMRUListEntry(file, pane.id);
// handles the case of save as so that the file remains in the
// the same location in the working set as the file that was renamed
if (result === pane.ITEM_FOUND_NEEDS_SORT) {
console.warn("pane.reorderItem returned pane.ITEM_FOUND_NEEDS_SORT which shouldn't happen " + file);
exports.trigger("workingSetSort", pane.id);
} else if (result === pane.ITEM_NOT_FOUND) {
index = pane.addToViewList(file, index);
if (_findFileInMRUList(pane.id, file) === -1) {
// Add to or update the position in MRU
if (pane.getCurrentlyViewedFile() === file) {
_mruList.unshift(entry);
} else {
_mruList.push(entry);
}
}
exports.trigger("workingSetAdd", file, index, pane.id);
}
}
/**
* Adds the given file list to the end of the workingset.
* @param {!string} paneId - The id of the pane in which to add the file object to or ACTIVE_PANE
* @param {!Array.<File>} fileList - Array of files to add to the pane
*/
function addListToWorkingSet(paneId, fileList) {
var uniqueFileList,
pane = _getPane(paneId);
uniqueFileList = pane.addListToViewList(fileList);
uniqueFileList.forEach(function (file) {
if (_findFileInMRUList(pane.id, file) !== -1) {
console.log(file.fullPath + " duplicated in mru list");
}
_mruList.push(_makeMRUListEntry(file, pane.id));
});
exports.trigger("workingSetAddList", uniqueFileList, pane.id);
// find all of the files that could be added but were not
var unsolvedList = fileList.filter(function (item) {
// if the file open in another pane, then add it to the list of unsolvedList
return (pane.findInViewList(item.fullPath) === -1 && _getPaneIdForPath(item.fullPath));
});
// Use the pane id of the first one in the list for pane id and recurse
// if we add more panes, then this will recurse until all items in the list are satisified
if (unsolvedList.length) {
addListToWorkingSet(_getPaneIdForPath(unsolvedList[0].fullPath), unsolvedList);
}
}
/**
* Removes a file from the global MRU list. Future versions of this
* implementation may support the ALL_PANES constant but FOCUS_PANE is not allowed
* @param {!string} paneId - Must be a valid paneId (not a shortcut e.g. ALL_PANES)
@ @param {File} file The file object to remove.
* @private
*/
function _removeFileFromMRU(paneId, file) {
var index,
compare = function (record) {
return (record.file === file && record.paneId === paneId);
};
// find and remove all instances
do {
index = _.findIndex(_mruList, compare);
if (index !== -1) {
_mruList.splice(index, 1);
}
} while (index !== -1);
}
/**
* Removes a file the specified pane
* @param {!string} paneId - Must be a valid paneId (not a shortcut e.g. ALL_PANES)
* @param {!File} file - the File to remove
* @param {boolean=} suppressRedraw - true to tell listeners not to redraw
* Use the suppressRedraw flag when calling this function along with many changes to prevent flicker
* @private
*/
function _removeView(paneId, file, suppressRedraw) {
var pane = _getPane(paneId);
if (pane.removeView(file)) {
_removeFileFromMRU(pane.id, file);
exports.trigger("workingSetRemove", file, suppressRedraw, pane.id);
}
}
/**
* moves a view from one pane to another
* @param {!string} sourcePaneId - id of the source pane
* @param {!string} destinationPaneId - id of the destination pane
* @param {!File} file - the File to move
* @param {Number} destinationIndex - the working set index of the file in the destination pane
* @return {jQuery.Promise} a promise that resolves when the move has completed.
* @private
*/
function _moveView(sourcePaneId, destinationPaneId, file, destinationIndex) {
var result = new $.Deferred(),
sourcePane = _getPane(sourcePaneId),
destinationPane = _getPane(destinationPaneId);
sourcePane.moveView(file, destinationPane, destinationIndex)
.done(function () {
// remove existing entry from mrulist for the same document if present
_removeFileFromMRU(destinationPane.id, file);
// update the mru list
_mruList.every(function (record) {
if (record.file === file && record.paneId === sourcePane.id) {
record.paneId = destinationPane.id;
return false;
}
return true;
});
exports.trigger("workingSetMove", file, sourcePane.id, destinationPane.id);
result.resolve();
});
return result.promise();
}
/**
* Switch between panes
*/
function switchPaneFocus() {
var $firstPane = $('#first-pane'), $secondPane = $('#second-pane');
if ($firstPane.hasClass('active-pane')) {
$secondPane.click();
} else {
$firstPane.click();
}
}
/**
* DocumentManager.pathDeleted Event handler to remove a file
* from the MRU list
* @param {!jQuery.event} e -
* @param {!string} fullPath - path of the file to remove
* @private
*/
function _removeDeletedFileFromMRU(e, fullPath) {
var index,
compare = function (record) {
return (record.file.fullPath === fullPath);
};
// find and remove all instances
do {
index = _.findIndex(_mruList, compare);
if (index !== -1) {
_mruList.splice(index, 1);
}
} while (index !== -1);
}
/**
* sorts the pane's view list
* @param {!string} paneId - id of the pane to sort, ALL_PANES or ACTIVE_PANE
* @param {sortFunctionCallback} compareFn - callback to determine sort order (called on each item)
* @see {@link Pane#sortViewList} for more information
* @see {@link https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort|Sort Array - MDN}
* @private
*/
function _sortWorkingSet(paneId, compareFn) {
_forEachPaneOrPanes(paneId, function (pane) {
pane.sortViewList(compareFn);
exports.trigger("workingSetSort", pane.id);
});
}
/**
* moves a working set item from one index to another shifting the items
* after in the working set up and reinserting it at the desired location
* @param {!string} paneId - id of the pane to sort
* @param {!number} fromIndex - the index of the item to move
* @param {!number} toIndex - the index to move to
* @private
*/
function _moveWorkingSetItem(paneId, fromIndex, toIndex) {
var pane = _getPane(paneId);
pane.moveWorkingSetItem(fromIndex, toIndex);
exports.trigger("workingSetSort", pane.id);
exports.trigger("_workingSetDisableAutoSort", pane.id);
}
/**
* Mutually exchanges the files at the indexes passed by parameters.
* @param {!string} paneId - id of the pane to swap indices or ACTIVE_PANE
* @param {!number} index1 - the index on the left
* @param {!number} index2 - the index on the rigth
* @private
*/
function _swapWorkingSetListIndexes(paneId, index1, index2) {
var pane = _getPane(paneId);
pane.swapViewListIndexes(index1, index2);