forked from pulsar-edit/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pane.js
1430 lines (1285 loc) · 43.1 KB
/
pane.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
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
const Grim = require('grim');
const { CompositeDisposable, Emitter } = require('event-kit');
const PaneAxis = require('./pane-axis');
const TextEditor = require('./text-editor');
const { createPaneElement } = require('./pane-element');
let nextInstanceId = 1;
class SaveCancelledError extends Error {}
// Extended: A container for presenting content in the center of the workspace.
// Panes can contain multiple items, one of which is *active* at a given time.
// The view corresponding to the active item is displayed in the interface. In
// the default configuration, tabs are also displayed for each item.
//
// Each pane may also contain one *pending* item. When a pending item is added
// to a pane, it will replace the currently pending item, if any, instead of
// simply being added. In the default configuration, the text in the tab for
// pending items is shown in italics.
module.exports = class Pane {
inspect() {
return `Pane ${this.id}`;
}
static deserialize(
state,
{ deserializers, applicationDelegate, config, notifications, views }
) {
const { activeItemIndex } = state;
const activeItemURI = state.activeItemURI || state.activeItemUri;
const items = [];
for (const itemState of state.items) {
const item = deserializers.deserialize(itemState);
if (item) items.push(item);
}
state.items = items;
state.activeItem = items[activeItemIndex];
if (!state.activeItem && activeItemURI) {
state.activeItem = state.items.find(
item =>
typeof item.getURI === 'function' && item.getURI() === activeItemURI
);
}
return new Pane(
Object.assign(
{
deserializerManager: deserializers,
notificationManager: notifications,
viewRegistry: views,
config,
applicationDelegate
},
state
)
);
}
constructor(params = {}) {
this.setPendingItem = this.setPendingItem.bind(this);
this.getPendingItem = this.getPendingItem.bind(this);
this.clearPendingItem = this.clearPendingItem.bind(this);
this.onItemDidTerminatePendingState = this.onItemDidTerminatePendingState.bind(
this
);
this.saveItem = this.saveItem.bind(this);
this.saveItemAs = this.saveItemAs.bind(this);
this.id = params.id;
if (this.id != null) {
nextInstanceId = Math.max(nextInstanceId, this.id + 1);
} else {
this.id = nextInstanceId++;
}
this.activeItem = params.activeItem;
this.focused = params.focused != null ? params.focused : false;
this.applicationDelegate = params.applicationDelegate;
this.notificationManager = params.notificationManager;
this.config = params.config;
this.deserializerManager = params.deserializerManager;
this.viewRegistry = params.viewRegistry;
this.emitter = new Emitter();
this.alive = true;
this.subscriptionsPerItem = new WeakMap();
this.items = [];
this.itemStack = [];
this.container = null;
this.addItems((params.items || []).filter(item => item));
if (!this.getActiveItem()) this.setActiveItem(this.items[0]);
this.addItemsToStack(params.itemStackIndices || []);
this.setFlexScale(params.flexScale || 1);
}
getElement() {
if (!this.element) {
this.element = createPaneElement().initialize(this, {
views: this.viewRegistry,
applicationDelegate: this.applicationDelegate
});
}
return this.element;
}
serialize() {
const itemsToBeSerialized = this.items.filter(
item => item && typeof item.serialize === 'function'
);
const itemStackIndices = [];
for (const item of this.itemStack) {
if (typeof item.serialize === 'function') {
itemStackIndices.push(itemsToBeSerialized.indexOf(item));
}
}
const activeItemIndex = itemsToBeSerialized.indexOf(this.activeItem);
return {
deserializer: 'Pane',
id: this.id,
items: itemsToBeSerialized.map(item => item.serialize()),
itemStackIndices,
activeItemIndex,
focused: this.focused,
flexScale: this.flexScale
};
}
getParent() {
return this.parent;
}
setParent(parent) {
this.parent = parent;
}
getContainer() {
return this.container;
}
setContainer(container) {
if (container && container !== this.container) {
this.container = container;
container.didAddPane({ pane: this });
}
}
// Private: Determine whether the given item is allowed to exist in this pane.
//
// * `item` the Item
//
// Returns a {Boolean}.
isItemAllowed(item) {
if (typeof item.getAllowedLocations !== 'function') {
return true;
} else {
return item
.getAllowedLocations()
.includes(this.getContainer().getLocation());
}
}
setFlexScale(flexScale) {
this.flexScale = flexScale;
this.emitter.emit('did-change-flex-scale', this.flexScale);
return this.flexScale;
}
getFlexScale() {
return this.flexScale;
}
increaseSize() {
if (this.getContainer().getPanes().length > 1) {
this.setFlexScale(this.getFlexScale() * 1.1);
}
}
decreaseSize() {
if (this.getContainer().getPanes().length > 1) {
this.setFlexScale(this.getFlexScale() / 1.1);
}
}
/*
Section: Event Subscription
*/
// Public: Invoke the given callback when the pane resizes
//
// The callback will be invoked when pane's flexScale property changes.
// Use {::getFlexScale} to get the current value.
//
// * `callback` {Function} to be called when the pane is resized
// * `flexScale` {Number} representing the panes `flex-grow`; ability for a
// flex item to grow if necessary.
//
// Returns a {Disposable} on which '.dispose()' can be called to unsubscribe.
onDidChangeFlexScale(callback) {
return this.emitter.on('did-change-flex-scale', callback);
}
// Public: Invoke the given callback with the current and future values of
// {::getFlexScale}.
//
// * `callback` {Function} to be called with the current and future values of
// the {::getFlexScale} property.
// * `flexScale` {Number} representing the panes `flex-grow`; ability for a
// flex item to grow if necessary.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeFlexScale(callback) {
callback(this.flexScale);
return this.onDidChangeFlexScale(callback);
}
// Public: Invoke the given callback when the pane is activated.
//
// The given callback will be invoked whenever {::activate} is called on the
// pane, even if it is already active at the time.
//
// * `callback` {Function} to be called when the pane is activated.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidActivate(callback) {
return this.emitter.on('did-activate', callback);
}
// Public: Invoke the given callback before the pane is destroyed.
//
// * `callback` {Function} to be called before the pane is destroyed.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onWillDestroy(callback) {
return this.emitter.on('will-destroy', callback);
}
// Public: Invoke the given callback when the pane is destroyed.
//
// * `callback` {Function} to be called when the pane is destroyed.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidDestroy(callback) {
return this.emitter.once('did-destroy', callback);
}
// Public: Invoke the given callback when the value of the {::isActive}
// property changes.
//
// * `callback` {Function} to be called when the value of the {::isActive}
// property changes.
// * `active` {Boolean} indicating whether the pane is active.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActive(callback) {
return this.container.onDidChangeActivePane(activePane => {
const isActive = this === activePane;
callback(isActive);
});
}
// Public: Invoke the given callback with the current and future values of the
// {::isActive} property.
//
// * `callback` {Function} to be called with the current and future values of
// the {::isActive} property.
// * `active` {Boolean} indicating whether the pane is active.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActive(callback) {
callback(this.isActive());
return this.onDidChangeActive(callback);
}
// Public: Invoke the given callback when an item is added to the pane.
//
// * `callback` {Function} to be called with when items are added.
// * `event` {Object} with the following keys:
// * `item` The added pane item.
// * `index` {Number} indicating where the item is located.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddItem(callback) {
return this.emitter.on('did-add-item', callback);
}
// Public: Invoke the given callback when an item is removed from the pane.
//
// * `callback` {Function} to be called with when items are removed.
// * `event` {Object} with the following keys:
// * `item` The removed pane item.
// * `index` {Number} indicating where the item was located.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidRemoveItem(callback) {
return this.emitter.on('did-remove-item', callback);
}
// Public: Invoke the given callback before an item is removed from the pane.
//
// * `callback` {Function} to be called with when items are removed.
// * `event` {Object} with the following keys:
// * `item` The pane item to be removed.
// * `index` {Number} indicating where the item is located.
onWillRemoveItem(callback) {
return this.emitter.on('will-remove-item', callback);
}
// Public: Invoke the given callback when an item is moved within the pane.
//
// * `callback` {Function} to be called with when items are moved.
// * `event` {Object} with the following keys:
// * `item` The removed pane item.
// * `oldIndex` {Number} indicating where the item was located.
// * `newIndex` {Number} indicating where the item is now located.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidMoveItem(callback) {
return this.emitter.on('did-move-item', callback);
}
// Public: Invoke the given callback with all current and future items.
//
// * `callback` {Function} to be called with current and future items.
// * `item` An item that is present in {::getItems} at the time of
// subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeItems(callback) {
for (let item of this.getItems()) {
callback(item);
}
return this.onDidAddItem(({ item }) => callback(item));
}
// Public: Invoke the given callback when the value of {::getActiveItem}
// changes.
//
// * `callback` {Function} to be called with when the active item changes.
// * `activeItem` The current active item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActiveItem(callback) {
return this.emitter.on('did-change-active-item', callback);
}
// Public: Invoke the given callback when {::activateNextRecentlyUsedItem}
// has been called, either initiating or continuing a forward MRU traversal of
// pane items.
//
// * `callback` {Function} to be called with when the active item changes.
// * `nextRecentlyUsedItem` The next MRU item, now being set active
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onChooseNextMRUItem(callback) {
return this.emitter.on('choose-next-mru-item', callback);
}
// Public: Invoke the given callback when {::activatePreviousRecentlyUsedItem}
// has been called, either initiating or continuing a reverse MRU traversal of
// pane items.
//
// * `callback` {Function} to be called with when the active item changes.
// * `previousRecentlyUsedItem` The previous MRU item, now being set active
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onChooseLastMRUItem(callback) {
return this.emitter.on('choose-last-mru-item', callback);
}
// Public: Invoke the given callback when {::moveActiveItemToTopOfStack}
// has been called, terminating an MRU traversal of pane items and moving the
// current active item to the top of the stack. Typically bound to a modifier
// (e.g. CTRL) key up event.
//
// * `callback` {Function} to be called with when the MRU traversal is done.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDoneChoosingMRUItem(callback) {
return this.emitter.on('done-choosing-mru-item', callback);
}
// Public: Invoke the given callback with the current and future values of
// {::getActiveItem}.
//
// * `callback` {Function} to be called with the current and future active
// items.
// * `activeItem` The current active item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActiveItem(callback) {
callback(this.getActiveItem());
return this.onDidChangeActiveItem(callback);
}
// Public: Invoke the given callback before items are destroyed.
//
// * `callback` {Function} to be called before items are destroyed.
// * `event` {Object} with the following keys:
// * `item` The item that will be destroyed.
// * `index` The location of the item.
//
// Returns a {Disposable} on which `.dispose()` can be called to
// unsubscribe.
onWillDestroyItem(callback) {
return this.emitter.on('will-destroy-item', callback);
}
// Called by the view layer to indicate that the pane has gained focus.
focus() {
return this.activate();
}
// Called by the view layer to indicate that the pane has lost focus.
blur() {
this.focused = false;
return true; // if this is called from an event handler, don't cancel it
}
isFocused() {
return this.focused;
}
getPanes() {
return [this];
}
unsubscribeFromItem(item) {
const subscription = this.subscriptionsPerItem.get(item);
if (subscription) {
subscription.dispose();
this.subscriptionsPerItem.delete(item);
}
}
/*
Section: Items
*/
// Public: Get the items in this pane.
//
// Returns an {Array} of items.
getItems() {
return this.items.slice();
}
// Public: Get the active pane item in this pane.
//
// Returns a pane item.
getActiveItem() {
return this.activeItem;
}
setActiveItem(activeItem, options) {
const modifyStack = options && options.modifyStack;
if (activeItem !== this.activeItem) {
if (modifyStack !== false) this.addItemToStack(activeItem);
this.activeItem = activeItem;
this.emitter.emit('did-change-active-item', this.activeItem);
if (this.container)
this.container.didChangeActiveItemOnPane(this, this.activeItem);
}
return this.activeItem;
}
// Build the itemStack after deserializing
addItemsToStack(itemStackIndices) {
if (this.items.length > 0) {
if (
itemStackIndices.length !== this.items.length ||
itemStackIndices.includes(-1)
) {
itemStackIndices = this.items.map((item, i) => i);
}
for (let itemIndex of itemStackIndices) {
this.addItemToStack(this.items[itemIndex]);
}
}
}
// Add item (or move item) to the end of the itemStack
addItemToStack(newItem) {
if (newItem == null) {
return;
}
const index = this.itemStack.indexOf(newItem);
if (index !== -1) this.itemStack.splice(index, 1);
return this.itemStack.push(newItem);
}
// Return an {TextEditor} if the pane item is an {TextEditor}, or null otherwise.
getActiveEditor() {
if (this.activeItem instanceof TextEditor) return this.activeItem;
}
// Public: Return the item at the given index.
//
// * `index` {Number}
//
// Returns an item or `null` if no item exists at the given index.
itemAtIndex(index) {
return this.items[index];
}
// Makes the next item in the itemStack active.
activateNextRecentlyUsedItem() {
if (this.items.length > 1) {
if (this.itemStackIndex == null)
this.itemStackIndex = this.itemStack.length - 1;
if (this.itemStackIndex === 0)
this.itemStackIndex = this.itemStack.length;
this.itemStackIndex--;
const nextRecentlyUsedItem = this.itemStack[this.itemStackIndex];
this.emitter.emit('choose-next-mru-item', nextRecentlyUsedItem);
this.setActiveItem(nextRecentlyUsedItem, { modifyStack: false });
}
}
// Makes the previous item in the itemStack active.
activatePreviousRecentlyUsedItem() {
if (this.items.length > 1) {
if (
this.itemStackIndex + 1 === this.itemStack.length ||
this.itemStackIndex == null
) {
this.itemStackIndex = -1;
}
this.itemStackIndex++;
const previousRecentlyUsedItem = this.itemStack[this.itemStackIndex];
this.emitter.emit('choose-last-mru-item', previousRecentlyUsedItem);
this.setActiveItem(previousRecentlyUsedItem, { modifyStack: false });
}
}
// Moves the active item to the end of the itemStack once the ctrl key is lifted
moveActiveItemToTopOfStack() {
delete this.itemStackIndex;
this.addItemToStack(this.activeItem);
this.emitter.emit('done-choosing-mru-item');
}
// Public: Makes the next item active.
activateNextItem() {
const index = this.getActiveItemIndex();
if (index < this.items.length - 1) {
this.activateItemAtIndex(index + 1);
} else {
this.activateItemAtIndex(0);
}
}
// Public: Makes the previous item active.
activatePreviousItem() {
const index = this.getActiveItemIndex();
if (index > 0) {
this.activateItemAtIndex(index - 1);
} else {
this.activateItemAtIndex(this.items.length - 1);
}
}
activateLastItem() {
this.activateItemAtIndex(this.items.length - 1);
}
// Public: Move the active tab to the right.
moveItemRight() {
const index = this.getActiveItemIndex();
const rightItemIndex = index + 1;
if (rightItemIndex <= this.items.length - 1)
this.moveItem(this.getActiveItem(), rightItemIndex);
}
// Public: Move the active tab to the left
moveItemLeft() {
const index = this.getActiveItemIndex();
const leftItemIndex = index - 1;
if (leftItemIndex >= 0)
return this.moveItem(this.getActiveItem(), leftItemIndex);
}
// Public: Get the index of the active item.
//
// Returns a {Number}.
getActiveItemIndex() {
return this.items.indexOf(this.activeItem);
}
// Public: Activate the item at the given index.
//
// * `index` {Number}
activateItemAtIndex(index) {
const item = this.itemAtIndex(index) || this.getActiveItem();
return this.setActiveItem(item);
}
// Public: Make the given item *active*, causing it to be displayed by
// the pane's view.
//
// * `item` The item to activate
// * `options` (optional) {Object}
// * `pending` (optional) {Boolean} indicating that the item should be added
// in a pending state if it does not yet exist in the pane. Existing pending
// items in a pane are replaced with new pending items when they are opened.
activateItem(item, options = {}) {
if (item) {
const index =
this.getPendingItem() === this.activeItem
? this.getActiveItemIndex()
: this.getActiveItemIndex() + 1;
this.addItem(item, Object.assign({}, options, { index }));
this.setActiveItem(item);
}
}
// Public: Add the given item to the pane.
//
// * `item` The item to add. It can be a model with an associated view or a
// view.
// * `options` (optional) {Object}
// * `index` (optional) {Number} indicating the index at which to add the item.
// If omitted, the item is added after the current active item.
// * `pending` (optional) {Boolean} indicating that the item should be
// added in a pending state. Existing pending items in a pane are replaced with
// new pending items when they are opened.
//
// Returns the added item.
addItem(item, options = {}) {
// Backward compat with old API:
// addItem(item, index=@getActiveItemIndex() + 1)
if (typeof options === 'number') {
Grim.deprecate(
`Pane::addItem(item, ${options}) is deprecated in favor of Pane::addItem(item, {index: ${options}})`
);
options = { index: options };
}
const index =
options.index != null ? options.index : this.getActiveItemIndex() + 1;
const moved = options.moved != null ? options.moved : false;
const pending = options.pending != null ? options.pending : false;
if (!item || typeof item !== 'object') {
throw new Error(
`Pane items must be objects. Attempted to add item ${item}.`
);
}
if (typeof item.isDestroyed === 'function' && item.isDestroyed()) {
throw new Error(
`Adding a pane item with URI '${typeof item.getURI === 'function' &&
item.getURI()}' that has already been destroyed`
);
}
if (this.items.includes(item)) return;
const itemSubscriptions = new CompositeDisposable();
this.subscriptionsPerItem.set(item, itemSubscriptions);
if (typeof item.onDidDestroy === 'function') {
itemSubscriptions.add(
item.onDidDestroy(() => this.removeItem(item, false))
);
}
if (typeof item.onDidTerminatePendingState === 'function') {
itemSubscriptions.add(
item.onDidTerminatePendingState(() => {
if (this.getPendingItem() === item) this.clearPendingItem();
})
);
}
this.items.splice(index, 0, item);
const lastPendingItem = this.getPendingItem();
const replacingPendingItem = lastPendingItem != null && !moved;
if (replacingPendingItem) this.pendingItem = null;
if (pending) this.setPendingItem(item);
this.emitter.emit('did-add-item', { item, index, moved });
if (!moved) {
if (this.container) this.container.didAddPaneItem(item, this, index);
}
if (replacingPendingItem) this.destroyItem(lastPendingItem);
if (!this.getActiveItem()) this.setActiveItem(item);
return item;
}
setPendingItem(item) {
if (this.pendingItem !== item) {
const mostRecentPendingItem = this.pendingItem;
this.pendingItem = item;
if (mostRecentPendingItem) {
this.emitter.emit(
'item-did-terminate-pending-state',
mostRecentPendingItem
);
}
}
}
getPendingItem() {
return this.pendingItem || null;
}
clearPendingItem() {
this.setPendingItem(null);
}
onItemDidTerminatePendingState(callback) {
return this.emitter.on('item-did-terminate-pending-state', callback);
}
// Public: Add the given items to the pane.
//
// * `items` An {Array} of items to add. Items can be views or models with
// associated views. Any objects that are already present in the pane's
// current items will not be added again.
// * `index` (optional) {Number} index at which to add the items. If omitted,
// the item is # added after the current active item.
//
// Returns an {Array} of added items.
addItems(items, index = this.getActiveItemIndex() + 1) {
items = items.filter(item => !this.items.includes(item));
for (let i = 0; i < items.length; i++) {
const item = items[i];
this.addItem(item, { index: index + i });
}
return items;
}
removeItem(item, moved) {
const index = this.items.indexOf(item);
if (index === -1) return;
if (this.getPendingItem() === item) this.pendingItem = null;
this.removeItemFromStack(item);
this.emitter.emit('will-remove-item', {
item,
index,
destroyed: !moved,
moved
});
this.unsubscribeFromItem(item);
if (item === this.activeItem) {
if (this.items.length === 1) {
this.setActiveItem(undefined);
} else if (index === 0) {
this.activateNextItem();
} else {
this.activatePreviousItem();
}
}
this.items.splice(index, 1);
this.emitter.emit('did-remove-item', {
item,
index,
destroyed: !moved,
moved
});
if (!moved && this.container)
this.container.didDestroyPaneItem({ item, index, pane: this });
if (this.items.length === 0 && this.config.get('core.destroyEmptyPanes'))
this.destroy();
}
// Remove the given item from the itemStack.
//
// * `item` The item to remove.
// * `index` {Number} indicating the index to which to remove the item from the itemStack.
removeItemFromStack(item) {
const index = this.itemStack.indexOf(item);
if (index !== -1) this.itemStack.splice(index, 1);
}
// Public: Move the given item to the given index.
//
// * `item` The item to move.
// * `index` {Number} indicating the index to which to move the item.
moveItem(item, newIndex) {
const oldIndex = this.items.indexOf(item);
this.items.splice(oldIndex, 1);
this.items.splice(newIndex, 0, item);
this.emitter.emit('did-move-item', { item, oldIndex, newIndex });
}
// Public: Move the given item to the given index on another pane.
//
// * `item` The item to move.
// * `pane` {Pane} to which to move the item.
// * `index` {Number} indicating the index to which to move the item in the
// given pane.
moveItemToPane(item, pane, index) {
this.removeItem(item, true);
return pane.addItem(item, { index, moved: true });
}
// Public: Destroy the active item and activate the next item.
//
// Returns a {Promise} that resolves when the item is destroyed.
destroyActiveItem() {
return this.destroyItem(this.activeItem);
}
// Public: Destroy the given item.
//
// If the item is active, the next item will be activated. If the item is the
// last item, the pane will be destroyed if the `core.destroyEmptyPanes` config
// setting is `true`.
//
// This action can be prevented by onWillDestroyPaneItem callbacks in which
// case nothing happens.
//
// * `item` Item to destroy
// * `force` (optional) {Boolean} Destroy the item without prompting to save
// it, even if the item's `isPermanentDockItem` method returns true.
//
// Returns a {Promise} that resolves with a {Boolean} indicating whether or not
// the item was destroyed.
async destroyItem(item, force) {
const index = this.items.indexOf(item);
if (index === -1) return false;
if (
!force &&
typeof item.isPermanentDockItem === 'function' &&
item.isPermanentDockItem() &&
(!this.container || this.container.getLocation() !== 'center')
) {
return false;
}
// In the case where there are no `onWillDestroyPaneItem` listeners, preserve the old behavior
// where `Pane.destroyItem` and callers such as `Pane.close` take effect synchronously.
if (this.emitter.listenerCountForEventName('will-destroy-item') > 0) {
await this.emitter.emitAsync('will-destroy-item', { item, index });
}
if (
this.container &&
this.container.emitter.listenerCountForEventName(
'will-destroy-pane-item'
) > 0
) {
let preventClosing = false;
await this.container.willDestroyPaneItem({
item,
index,
pane: this,
prevent: () => {
preventClosing = true;
}
});
if (preventClosing) return false;
}
if (
!force &&
typeof item.shouldPromptToSave === 'function' &&
item.shouldPromptToSave()
) {
if (!(await this.promptToSaveItem(item))) return false;
}
this.removeItem(item, false);
if (typeof item.destroy === 'function') item.destroy();
return true;
}
// Public: Destroy all items.
destroyItems() {
return Promise.all(this.getItems().map(item => this.destroyItem(item)));
}
// Public: Destroy all items except for the active item.
destroyInactiveItems() {
return Promise.all(
this.getItems()
.filter(item => item !== this.activeItem)
.map(item => this.destroyItem(item))
);
}
promptToSaveItem(item, options = {}) {
return new Promise((resolve, reject) => {
if (
typeof item.shouldPromptToSave !== 'function' ||
!item.shouldPromptToSave(options)
) {
return resolve(true);
}
let uri;
if (typeof item.getURI === 'function') {
uri = item.getURI();
} else if (typeof item.getUri === 'function') {
uri = item.getUri();
} else {
return resolve(true);
}
const title =
(typeof item.getTitle === 'function' && item.getTitle()) || uri;
const saveDialog = (saveButtonText, saveFn, message) => {
this.applicationDelegate.confirm(
{
message,
detail:
'Your changes will be lost if you close this item without saving.',
buttons: [saveButtonText, 'Cancel', "&Don't Save"]
},
response => {
switch (response) {
case 0:
return saveFn(item, error => {
if (error instanceof SaveCancelledError) {
resolve(false);
} else if (error) {
saveDialog(
'Save as',
this.saveItemAs,
`'${title}' could not be saved.\nError: ${this.getMessageForErrorCode(
error.code
)}`
);
} else {
resolve(true);
}
});
case 1:
return resolve(false);
case 2:
return resolve(true);
}
}
);
};
saveDialog(
'Save',
this.saveItem,
`'${title}' has changes, do you want to save them?`
);
});
}
// Public: Save the active item.
saveActiveItem(nextAction) {
return this.saveItem(this.getActiveItem(), nextAction);
}
// Public: Prompt the user for a location and save the active item with the
// path they select.
//
// * `nextAction` (optional) {Function} which will be called after the item is
// successfully saved.
//
// Returns a {Promise} that resolves when the save is complete
saveActiveItemAs(nextAction) {
return this.saveItemAs(this.getActiveItem(), nextAction);
}
// Public: Save the given item.
//
// * `item` The item to save.
// * `nextAction` (optional) {Function} which will be called with no argument
// after the item is successfully saved, or with the error if it failed.
// The return value will be that of `nextAction` or `undefined` if it was not
// provided
//
// Returns a {Promise} that resolves when the save is complete
saveItem(item, nextAction) {
if (!item) return Promise.resolve();
let itemURI;
if (typeof item.getURI === 'function') {
itemURI = item.getURI();
} else if (typeof item.getUri === 'function') {
itemURI = item.getUri();
}
if (itemURI != null) {
if (typeof item.save === 'function') {
return promisify(() => item.save())
.then(() => {
if (nextAction) nextAction();
})
.catch(error => {
if (nextAction) {
nextAction(error);
} else {
this.handleSaveError(error, item);
}
});
} else if (nextAction) {