-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathmain.js
More file actions
875 lines (739 loc) · 32.2 KB
/
main.js
File metadata and controls
875 lines (739 loc) · 32.2 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2016 - 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.
*
*/
define(function (require, exports, module) {
var _ = require("thirdparty/lodash"),
AppInit = require("utils/AppInit"),
Async = require("utils/Async"),
Strings = require("strings"),
MainViewManager = require("view/MainViewManager"),
DocumentManager = require("document/DocumentManager"),
EditorManager = require("editor/EditorManager"),
ProjectManager = require("project/ProjectManager"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
Menus = require("command/Menus"),
FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
ViewUtils = require("utils/ViewUtils"),
KeyEvent = require("utils/KeyEvent"),
WorkingSetView = require("project/WorkingSetView"),
PreferencesManager = require("preferences/PreferencesManager"),
KeyBindingManager = require("command/KeyBindingManager"),
Mustache = require("thirdparty/mustache/mustache"),
NavigationProvider = require("./NavigationProvider"),
FileRecovery = require("./FileRecovery");
var KeyboardPrefs = JSON.parse(require("text!./keyboard.json"));
// Command constants for recent files
var PREFS_RECENT_FILES = "recent-files.navigation",
SHOW_RECENT_FILES = "recent-files.show",
NEXT_IN_RECENT_FILES = "recent-files.next",
PREV_IN_RECENT_FILES = "recent-files.prev",
OPEN_FILES_VIEW_STATE = "openFiles";
var htmlTemplate = require("text!./html/recentfiles-template.html"),
dirtyDotTemplate = "<div class='file-status-icon dirty' style='position: absolute;margin-left: -2px;'></div>";
var MAX_ENTRY_COUNT = 50;
var isRecentFilesNavEnabled = true;
/*
* Contains list of most recently opened files and their last known cursor position
* @private
* @type {Array.<Object>}
*/
var _mrofList = [],
$mrofContainer = null;
var $currentContext,
activeEditor;
var _hideMROFList;
PreferencesManager.definePreference(PREFS_RECENT_FILES, "boolean", true, {
description: Strings.DESCRIPTION_RECENT_FILES_NAV
});
/**
* Opens a full editor for the given context
* @private
* @param {Object.<path, paneId, cursor>} contextData - wrapper to provide the information required to open a full editor
* @return {$.Promise} - from the commandmanager
*/
function _openEditorForContext(contextData) {
// Open the file in the current active pane to prevent unwanted scenarios if we are not in split view, fallback
// to the persisted paneId when specified and we are in split view or unable to determine the paneid
var activePaneId = MainViewManager.getActivePaneId(),
targetPaneId = contextData.paneId; // Assume we are going to use the last associated paneID
// Detect if we are not in split mode
if (MainViewManager.getPaneCount() === 1) {
// Override the targetPaneId with activePaneId as we are not yet in split mode
targetPaneId = activePaneId;
}
// If hide of MROF list is a context parameter, hide the MROF list on successful file open
if (contextData.hideOnOpenFile) {
_hideMROFList();
}
return CommandManager
.execute(Commands.FILE_OPEN,
{ fullPath: contextData.path,
paneId: targetPaneId
}
)
.done(function () {
if (contextData.cursor) {
activeEditor = EditorManager.getActiveEditor();
activeEditor.setCursorPos(contextData.cursor);
activeEditor.centerOnCursor();
}
});
}
/**
* Creates an entry for MROF list
* @private
* @param {String} path - full path of a doc
* @param {String} pane - the pane holding the editor for the doc
* @param {Object} cursorPos - current cursor position
* @return {Object} a frame containing file path, pane and last known cursor
*/
function _makeMROFListEntry(path, pane, cursorPos) {
return {
file: path,
paneId: pane,
cursor: cursorPos
};
}
/**
* Determines if a file is dirty
* @private
* @param {!File} file - file to test
* @return {boolean} true if the file is dirty, false otherwise
*/
function _isOpenAndDirty(file) {
// working set item might never have been opened; if so, then it's definitely not dirty
var docIfOpen = DocumentManager.getOpenDocumentForPath(file.fullPath);
return (docIfOpen && docIfOpen.isDirty);
}
function _checkExt(entry, index) {
var deferred = new $.Deferred(),
fileEntry = FileSystem.getFileForPath(entry.file);
if (entry.inMem) {
var indxInWS = MainViewManager.findInWorkingSet(entry.paneId, entry.file);
// Remove entry if InMemoryFile is not found in Working set
if (indxInWS === -1) {
_mrofList[index] = null;
deferred.reject();
} else {
deferred.resolve();
}
} else {
fileEntry.exists(function (err, exists) {
if (!err && exists) {
deferred.resolve();
} else {
_mrofList[index] = null;
deferred.reject();
}
});
}
return deferred.promise();
}
/**
* Checks whether entries in MROF list actually exists in fileSystem to prevent access to deleted files
* @private
*/
function _syncWithFileSystem() {
_mrofList = _mrofList.filter(function (e) { return e; });
return Async.doSequentially(_mrofList, _checkExt, false);
}
function _getFileListForEntries(entries) {
return $.map(entries, function (value, index) {
return FileSystem.getFileForPath(value.file);
});
}
function _addDirectoriesForDuplicateBaseNames() {
var checked = {}, baseName;
// Find duplicates first
$.map(_mrofList, function (value, index) {
baseName = FileUtils.getBaseName(value.file);
if (!checked[baseName]) {
checked[baseName] = [];
}
checked[baseName].push(value);
});
// Go through the map and solve the arrays with length over 1. Ignore the rest.
_.forEach(checked, function (value) {
if (value.length > 1) {
var dirs = ViewUtils.getDirNamesForDuplicateFiles(_getFileListForEntries(value));
$.map(value, function (value, index) {
// Go through recent files and add directories to appropriate entries
$mrofContainer.find("#mrof-list > li").each(function () {
var $li = $(this);
if ($li.data("path") === value.file) {
var dirSplit = dirs[index].split("/");
if (dirSplit.length > 3) {
dirs[index] = dirSplit[0] + "/\u2026/" + dirSplit[dirSplit.length - 1];
}
var $dir = $("<span class='directory'/>").html(" — " + dirs[index]);
$li.children("a.mroitem").find("span.directory").remove();
$li.children("a.mroitem").append($dir);
}
});
});
}
});
}
function _createFileEntries($mrofList) {
var data, fileEntry, $link, $newItem, $removeBtn;
// Iterate over the MROF list and create the pop over UI items
// If we are in split view we might want to show the panes corresponding to the entries
var isPaneLabelReqd = MainViewManager.getPaneCount() > 1;
if (isPaneLabelReqd) {
$mrofContainer.addClass("split-mode");
$(".first.pane-label", $mrofContainer).text(MainViewManager.getPaneTitle("first-pane"));
$(".second.pane-label", $mrofContainer).text(MainViewManager.getPaneTitle("second-pane"));
}
$.each(_mrofList, function (index, value) {
if (!isPaneLabelReqd && value.paneId !== MainViewManager.getActivePaneId()) {
// Try to see if we have same doc split
// Check existing list for this doc path and active pane entry
var entryIndex = _.findIndex(_mrofList, function (record) {
return (
record &&
record.file === value.file &&
record.paneId === MainViewManager.getActivePaneId()
);
});
// If found don't process this entry, as the document is already present in active pane
if (entryIndex >= 0) {
return true;
}
// Process this for active pane id
value.paneId = MainViewManager.getActivePaneId();
}
var indxInWS = MainViewManager.findInWorkingSet(value.paneId, value.file);
data = {
fullPath: value.file,
name: FileUtils.getBaseName(value.file),
isFile: true
};
fileEntry = FileSystem.getFileForPath(value.file);
// Create new list item with a link
$link = $("<a href='#' class='mroitem'></a>").html(
ViewUtils.getFileEntryDisplay({name: FileUtils.getBaseName(value.file)})
);
// Create remove button only for files not in working set
$removeBtn = null;
if (indxInWS === -1) {
$removeBtn = $("<span class='remove-file'>×</span>").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
// Find and remove the entry from _mrofList
var filePath = $(this).parent().data("path");
var paneId = $(this).parent().data("paneId");
_mrofList = _mrofList.filter(function(entry) {
return !(entry && entry.file === filePath && entry.paneId === paneId);
});
// Remove the list item from UI
$(this).parent().remove();
// Update preferences
PreferencesManager.setViewState(
OPEN_FILES_VIEW_STATE,
_mrofList,
PreferencesManager.STATE_PROJECT_CONTEXT
);
});
}
// Use the file icon providers
WorkingSetView.useIconProviders(data, $link);
// Create list item with link and conditionally add remove button
$newItem = $("<li></li>").append($link);
if ($removeBtn) {
$newItem.append($removeBtn);
}
if (indxInWS !== -1) { // in working set show differently
$newItem.addClass("working-set");
}
$newItem.data("path", value.file);
// convert the virtual path to display path
var displayPath = Phoenix.app.getDisplayPath(value.file);
$newItem.data("paneId", value.paneId);
$newItem.data("cursor", value.cursor);
$newItem.data("file", fileEntry);
$newItem.attr("title", displayPath); // this is for the tooltip
if (isPaneLabelReqd && value.paneId) {
$newItem.addClass(value.paneId);
$newItem.css('top', ($('.' + value.paneId, $mrofList).length * 22) + 'px');
}
// Use the class providers(git e.t.c)
WorkingSetView.useClassProviders(data, $newItem);
// If a file is dirty, mark it in the list
if (_isOpenAndDirty(fileEntry)) {
$(dirtyDotTemplate).prependTo($newItem);
}
$mrofList.append($newItem);
if (index === MAX_ENTRY_COUNT - 1) {
// We have reached the max number of entries we can display, break out
return false;
}
});
_addDirectoriesForDuplicateBaseNames();
}
/**
* This function is used to create mrof when a project is opened for the firt time with the recent files feature
* This routine acts as a logic to migrate existing viewlist to mrof structure
* @private
*/
function _createMROFList() {
var paneList = MainViewManager.getPaneIdList(),
mrofList = [],
fileList,
index;
var pane, file, mrofEntry, paneCount, fileCount;
// Iterate over the pane ID list
for (paneCount = 0; paneCount < paneList.length; paneCount++) {
pane = paneList[paneCount];
fileList = MainViewManager.getWorkingSet(pane);
// Iterate over the file list for this pane
for (fileCount = 0; fileCount < fileList.length; fileCount++) {
file = fileList[fileCount];
mrofEntry = _makeMROFListEntry(file.fullPath, pane, null);
// Add it in the MRU list order
index = MainViewManager.findInGlobalMRUList(pane, file);
mrofList[index] = mrofEntry;
}
}
return mrofList;
}
function _handleArrowKeys(event) {
var UP = 38,
DOWN = 40;
var $context, $nextContext;
if ($mrofContainer && (event.which === UP || event.which === DOWN)) {
$context = $currentContext || $("#mrof-container #mrof-list > li.highlight");
if ($context.length > 0) {
$nextContext = event.which === UP ? $context.prev() : $context.next();
if ($nextContext.length > 0) {
$currentContext = $nextContext;
//_resetOpenFileTimer();
$nextContext.find("a.mroitem").trigger("focus");
}
} else {
//WTF! (Worse than failure). We should not get here.
$("#mrof-container #mrof-list > li > a.mroitem:visited").last().trigger("focus");
}
// If we don't prevent this then scrolling happens by the browser(user agent behaviour)
// as well as a result of moving focus in the ul
event.preventDefault();
event.stopImmediatePropagation();
}
}
function _hideMROFListOnEscape(event) {
if ($mrofContainer && event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
_hideMROFList();
}
}
/**
* Shows the current MROF list
* @private
*/
function _createMROFDisplayList(refresh) {
var $def = $.Deferred();
var $mrofList, $link, $newItem;
/**
* Clears the MROF list in memory and pop over but retains the working set entries
* @private
*/
function _purgeAllExceptWorkingSet() {
_mrofList = _createMROFList();
$mrofList.empty();
_createMROFDisplayList(true);
$currentContext = null;
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
}
if (!refresh) {
// Call hide first to make sure we are not creating duplicate lists
_hideMROFList();
$mrofContainer = $(Mustache.render(htmlTemplate, {Strings: Strings})).appendTo('body');
$("#mrof-list-close").one("click", _hideMROFList);
// Attach clear list handler to the 'Clear All' button
$("#mrof-container .footer > div#clear-mrof-list").on("click", _purgeAllExceptWorkingSet);
$(window).on("keydown", _handleArrowKeys);
$(window).on("keyup", _hideMROFListOnEscape);
}
$mrofList = $mrofContainer.find("#mrof-list");
/**
* Focus handler for the link in list item
* @private
*/
function _onFocus(event) {
var $scope = $(event.target).parent();
$("#mrof-container #mrof-list > li.highlight").removeClass("highlight");
$(event.target).parent().addClass("highlight");
// convert the virtual path to display path
var displayPath = Phoenix.app.getDisplayPath($scope.data("path"));
$mrofContainer.find("#recent-file-path").text(displayPath);
$mrofContainer.find("#recent-file-path").attr('title', displayPath);
$currentContext = $scope;
}
/**
* Click handler for the link in list item
* @private
*/
function _onClick(event) {
var $scope = $(event.delegateTarget).parent();
_openEditorForContext({
path: $scope.data("path"),
paneId: $scope.data("paneId"),
cursor: $scope.data("cursor"),
hideOnOpenFile: true
});
}
var data, fileEntry;
_syncWithFileSystem().always(function () {
_mrofList = _mrofList.filter(function (e) { return e; });
_createFileEntries($mrofList);
var $fileLinks = $("#mrof-container #mrof-list > li > a.mroitem");
// Handlers for mouse events on the list items
$fileLinks.on("focus", _onFocus);
$fileLinks.on("click", _onClick);
$fileLinks.on("select", _onClick);
// Put focus on the Most recent file link in the list
$fileLinks.first().trigger("focus");
$def.resolve();
});
return $def.promise();
}
function _openFile() {
if ($currentContext) {
_openEditorForContext({
path: $currentContext.data("path"),
paneId: $currentContext.data("paneId"),
cursor: $currentContext.data("cursor")
});
}
}
function _hideMROFListOnNavigationEnd(event) {
if ($mrofContainer && event.keyCode === KeyEvent.DOM_VK_CONTROL) {
_openFile();
_hideMROFList();
}
}
/**
* Opens the next item in MROF list if pop over is visible else displays the pop over
* @private
*/
function _moveNext() {
var $context, $next;
$context = $currentContext || $("#mrof-container #mrof-list > li.highlight");
if ($context.length > 0) {
$next = $context.next();
if ($next.length === 0) {
$next = $("#mrof-container #mrof-list > li").first();
}
if ($next.length > 0) {
$currentContext = $next;
$next.find("a.mroitem").trigger("focus");
}
} else {
//WTF! (Worse than failure). We should not get here.
$("#mrof-container #mrof-list > li > a.mroitem:visited").last().trigger("focus");
}
}
function _cmdMoveNext() {
var $displayPromise;
if (!$mrofContainer) {
$displayPromise = _createMROFDisplayList();
$mrofContainer.addClass("confirmation-mode");
$(window).on("keyup", _hideMROFListOnNavigationEnd);
}
if ($displayPromise) {
$displayPromise.always(function () {
_moveNext();
});
} else {
_moveNext();
}
}
/**
* Opens the previous item in MROF list if pop over is visible else displays the pop over
* @private
*/
function _movePrev() {
var $context, $prev;
$context = $currentContext || $("#mrof-container #mrof-list > li.highlight");
if ($context.length > 0) {
$prev = $context.prev();
if ($prev.length === 0) {
$prev = $("#mrof-container #mrof-list > li").last();
}
if ($prev.length > 0) {
$currentContext = $prev;
$prev.find("a.mroitem").trigger("focus");
}
} else {
//WTF! (Worse than failure). We should not get here.
$("#mrof-container #mrof-list > li > a.mroitem:visited").last().trigger("focus");
}
}
function _cmdMovePrev() {
var $displayPromise;
if (!$mrofContainer) {
$displayPromise = _createMROFDisplayList();
$mrofContainer.addClass("confirmation-mode");
$(window).on("keyup", _hideMROFListOnNavigationEnd);
}
if ($displayPromise) {
$displayPromise.always(function () {
_movePrev();
});
} else {
_movePrev();
}
}
function _updateCursorPosition(filePath, paneId, cursorPos) {
if (!paneId) { // Don't handle this if not a full view/editor
return;
}
// Check existing list for this doc path and pane entry
var index = _.findIndex(_mrofList, function (record) {
return (record && record.file === filePath && record.paneId === paneId);
});
var entry;
if (index !== -1) {
_mrofList[index].cursor = cursorPos;
}
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
}
/**
* Adds an entry to MROF list
* @private
* @param {Editor} editor - editor to extract file information
*/
function _addToMROFList(file, paneId, cursorPos) {
var filePath = file.fullPath;
if (!paneId) { // Don't handle this if not a full view/editor
return;
}
// Check existing list for this doc path and pane entry
var index = _.findIndex(_mrofList, function (record) {
return (record && record.file === filePath && record.paneId === paneId);
});
var entry;
if (index !== -1) {
entry = _mrofList[index];
if (entry.cursor && !cursorPos) {
cursorPos = entry.cursor;
}
}
entry = _makeMROFListEntry(filePath, paneId, cursorPos);
// Check if the file is an InMemoryFile
if (file.constructor.name === "InMemoryFile") {
// Mark the entry as inMem, so that we can knock it off from the list when removed from working set
entry.inMem = true;
}
if (index !== -1) {
_mrofList.splice(index, 1);
}
// add it to the front of the list
_mrofList.unshift(entry);
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
}
// To update existing entry if a move has happened
function _handleWorkingSetMove(event, file, sourcePaneId, destinationPaneId) {
// Check existing list for this doc path and source pane entry
var index = _.findIndex(_mrofList, function (record) {
return (record && record.file === file.fullPath && record.paneId === sourcePaneId);
}), tIndex;
// If an entry is found update the pane info
if (index >= 0) {
// But an entry with the target pane Id should not exist
tIndex = _.findIndex(_mrofList, function (record) {
return (record && record.file === file.fullPath && record.paneId === destinationPaneId);
});
if (tIndex === -1) {
_mrofList[index].paneId = destinationPaneId;
} else {
// Remove this entry as it has been moved.
_mrofList.splice(index, 1);
}
}
}
// Handle project close or app close to set view state
function _handleAppClose() {
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
_mrofList = [];
}
function _initRecentFilesList() {
_mrofList = PreferencesManager.getViewState(OPEN_FILES_VIEW_STATE, PreferencesManager.STATE_PROJECT_CONTEXT) || [];
_mrofList = _mrofList.filter(function (entry) {
return entry;
});
// Have a check on the number of entries to fallback to working set if we detect corruption
if (_mrofList.length < MainViewManager.getWorkingSetSize(MainViewManager.ALL_PANES)) {
_mrofList = _createMROFList();
}
}
function _handleProjectOpen() {
_mrofList = [];
// We will do a late initialization once we get the first editor change or file open notification
}
ProjectManager.on("projectOpen", _handleProjectOpen);
function _showRecentFileList() {
if (isRecentFilesNavEnabled) {
_createMROFDisplayList();
}
}
/**
* Hides the current MROF list if visible
* @private
*/
_hideMROFList = function () {
if ($mrofContainer) {
$mrofContainer.remove();
$mrofContainer = null;
$currentContext = null;
activeEditor = EditorManager.getActiveEditor();
if (activeEditor) {
activeEditor.focus();
}
}
$(window).off("keydown", _handleArrowKeys);
$(window).off("keyup", _hideMROFListOnNavigationEnd);
$(window).off("keyup", _hideMROFListOnEscape);
};
// To take care of hiding the popover during app navigation in os using key board shortcuts
$(window).on("blur focus", function () {
_hideMROFList();
});
// Merges the entries to a single pane if split view have been merged
// Then purges duplicate entries in mrof list
function _handlePaneMerge(e, paneId) {
var index;
var targetPaneId = MainViewManager.FIRST_PANE;
$.each(_mrofList, function (itrIndex, value) {
if (value && value.paneId === paneId) { // We have got an entry which needs merge
// Before modifying the actual pane info check if an entry exists with same target pane
index = _.findIndex(_mrofList, function (record) {
return (record && record.file === value.file && record.paneId === targetPaneId);
});
if (index !== -1) { // A duplicate entry found, remove the current one instead of updating
_mrofList[index] = null;
} else { // Update with merged pane info
_mrofList[itrIndex].paneId = targetPaneId;
}
}
});
// Clean the null/undefined entries
_mrofList = _mrofList.filter(function (e) { return e; });
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
}
function _initRecentFileMenusAndCommands() {
// Command to show recent files list
if (!CommandManager.get(SHOW_RECENT_FILES)) {
CommandManager.register(Strings.CMD_RECENT_FILES_OPEN, SHOW_RECENT_FILES, _showRecentFileList);
KeyBindingManager.addBinding(SHOW_RECENT_FILES,
Phoenix.isNativeApp ? "Ctrl-R" : "Ctrl-Alt-Shift-O");
}
// Keyboard only - Navigate to the next doc in MROF list
if (!CommandManager.get(NEXT_IN_RECENT_FILES)) {
CommandManager.register(Strings.CMD_NEXT_DOC, NEXT_IN_RECENT_FILES, _cmdMoveNext);
}
KeyBindingManager.addBinding(NEXT_IN_RECENT_FILES, KeyboardPrefs[NEXT_IN_RECENT_FILES]);
// Keyboard only - Navigate to the prev doc in MROF list
if (!CommandManager.get(PREV_IN_RECENT_FILES)) {
CommandManager.register(Strings.CMD_PREV_DOC, PREV_IN_RECENT_FILES, _cmdMovePrev);
}
KeyBindingManager.addBinding(PREV_IN_RECENT_FILES, KeyboardPrefs[PREV_IN_RECENT_FILES]);
var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
menu.addMenuItem(SHOW_RECENT_FILES, "", Menus.AFTER, Commands.FILE_REOPEN_CLOSED);
}
function _initDefaultNavigationCommands() {
KeyBindingManager.addBinding(Commands.NAVIGATE_NEXT_DOC, KeyboardPrefs[NEXT_IN_RECENT_FILES]);
KeyBindingManager.addBinding(Commands.NAVIGATE_PREV_DOC, KeyboardPrefs[PREV_IN_RECENT_FILES]);
}
function _removeKeys(keys) {
_.forEach(keys, function (config) {
KeyBindingManager.removeBinding(config.key);
});
}
function _removeNavigationKeys() {
_removeKeys(KeyboardPrefs[NEXT_IN_RECENT_FILES]);
_removeKeys(KeyboardPrefs[PREV_IN_RECENT_FILES]);
}
function _deregisterSortcutsAndMenus() {
_removeNavigationKeys();
Menus.getMenu(Menus.AppMenuBar.FILE_MENU).removeMenuItem(SHOW_RECENT_FILES);
}
// Handle current file change
function handleCurrentFileChange(e, newFile, newPaneId, oldFile) {
if (newFile) {
if (_mrofList.length === 0) {
_initRecentFilesList();
}
_addToMROFList(newFile, newPaneId);
}
}
// Handle Active Editor change to update mrof information
function _handleActiveEditorChange(event, current, previous) {
if (current) { // Handle only full editors
if (_mrofList.length === 0) {
_initRecentFilesList();
}
var file = current.document.file;
var paneId = current._paneId;
_addToMROFList(file, paneId, current.getCursorPos(true, "first"));
}
if (previous) { // Capture the last know cursor position
_updateCursorPosition(previous.document.file.fullPath, previous._paneId, previous.getCursorPos(true, "first"));
}
}
function _attachListners() {
MainViewManager.on("workingSetMove.pane-first-pane", _handleWorkingSetMove);
MainViewManager.on("currentFileChange", handleCurrentFileChange);
MainViewManager.on("paneDestroy", _handlePaneMerge);
EditorManager.on("activeEditorChange", _handleActiveEditorChange);
ProjectManager.on("beforeProjectClose beforeAppClose", _handleAppClose);
}
function _detachListners() {
MainViewManager.off("workingSetMove.pane-first-pane", _handleWorkingSetMove);
MainViewManager.off("currentFileChange", handleCurrentFileChange);
MainViewManager.off("paneDestroy", _handlePaneMerge);
EditorManager.off("activeEditorChange", _handleActiveEditorChange);
ProjectManager.off("beforeProjectClose beforeAppClose", _handleAppClose);
}
PreferencesManager.on("change", PREFS_RECENT_FILES, function () {
if (PreferencesManager.get(PREFS_RECENT_FILES)) {
_removeNavigationKeys();
_initRecentFileMenusAndCommands();
_mrofList = [];
_initRecentFilesList();
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
_detachListners();
_attachListners();
isRecentFilesNavEnabled = true;
} else {
// Reset the view state to empty
_mrofList = [];
PreferencesManager.setViewState(OPEN_FILES_VIEW_STATE, _mrofList, PreferencesManager.STATE_PROJECT_CONTEXT);
_deregisterSortcutsAndMenus();
_initDefaultNavigationCommands();
_detachListners();
isRecentFilesNavEnabled = false;
}
});
AppInit.appReady(function () {
FileRecovery.init();
NavigationProvider.init();
});
});