forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-container.coffee
175 lines (132 loc) · 4.43 KB
/
pane-container.coffee
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
{find, flatten} = require 'underscore-plus'
{Model} = require 'theorist'
{Emitter, CompositeDisposable} = require 'event-kit'
Serializable = require 'serializable'
Pane = require './pane'
module.exports =
class PaneContainer extends Model
atom.deserializers.add(this)
Serializable.includeInto(this)
@version: 1
@properties
activePane: null
root: null
@behavior 'activePaneItem', ->
@$activePane
.switch((activePane) -> activePane?.$activeItem)
.distinctUntilChanged()
constructor: (params) ->
super
@emitter = new Emitter
@subscriptions = new CompositeDisposable
@setRoot(params?.root ? new Pane)
@destroyEmptyPanes() if params?.destroyEmptyPanes
@monitorActivePaneItem()
@monitorPaneItems()
deserializeParams: (params) ->
params.root = atom.deserializers.deserialize(params.root, container: this)
params.destroyEmptyPanes = atom.config.get('core.destroyEmptyPanes')
params.activePane = params.root.getPanes().find (pane) -> pane.id is params.activePaneId
params
serializeParams: (params) ->
root: @root?.serialize()
activePaneId: @activePane.id
onDidChangeRoot: (fn) ->
@emitter.on 'did-change-root', fn
observeRoot: (fn) ->
fn(@getRoot())
@onDidChangeRoot(fn)
onDidAddPane: (fn) ->
@emitter.on 'did-add-pane', fn
observePanes: (fn) ->
fn(pane) for pane in @getPanes()
@onDidAddPane ({pane}) -> fn(pane)
onDidChangeActivePane: (fn) ->
@emitter.on 'did-change-active-pane', fn
observeActivePane: (fn) ->
fn(@getActivePane())
@onDidChangeActivePane(fn)
onDidAddPaneItem: (fn) ->
@emitter.on 'did-add-pane-item', fn
observePaneItems: (fn) ->
fn(item) for item in @getPaneItems()
@onDidAddPaneItem ({item}) -> fn(item)
onDidChangeActivePaneItem: (fn) ->
@emitter.on 'did-change-active-pane-item', fn
observeActivePaneItem: (fn) ->
fn(@getActivePaneItem())
@onDidChangeActivePaneItem(fn)
onDidDestroyPaneItem: (fn) ->
@emitter.on 'did-destroy-pane-item', fn
getRoot: -> @root
setRoot: (@root) ->
@root.setParent(this)
@root.setContainer(this)
@emitter.emit 'did-change-root', @root
if not @getActivePane()? and @root instanceof Pane
@setActivePane(@root)
replaceChild: (oldChild, newChild) ->
throw new Error("Replacing non-existent child") if oldChild isnt @root
@setRoot(newChild)
getPanes: ->
@getRoot().getPanes()
getPaneItems: ->
@getRoot().getItems()
getActivePane: ->
@activePane
setActivePane: (activePane) ->
if activePane isnt @activePane
@activePane = activePane
@emitter.emit 'did-change-active-pane', @activePane
@activePane
getActivePaneItem: ->
@getActivePane().getActiveItem()
paneForUri: (uri) ->
find @getPanes(), (pane) -> pane.itemForUri(uri)?
saveAll: ->
pane.saveItems() for pane in @getPanes()
activateNextPane: ->
panes = @getPanes()
if panes.length > 1
currentIndex = panes.indexOf(@activePane)
nextIndex = (currentIndex + 1) % panes.length
panes[nextIndex].activate()
true
else
false
activatePreviousPane: ->
panes = @getPanes()
if panes.length > 1
currentIndex = panes.indexOf(@activePane)
previousIndex = currentIndex - 1
previousIndex = panes.length - 1 if previousIndex < 0
panes[previousIndex].activate()
true
else
false
destroyEmptyPanes: ->
pane.destroy() for pane in @getPanes() when pane.items.length is 0
paneItemDestroyed: (item) ->
@emitter.emit 'did-destroy-pane-item', item
didAddPane: (pane) ->
@emitter.emit 'did-add-pane', pane
# Called by Model superclass when destroyed
destroyed: ->
pane.destroy() for pane in @getPanes()
@subscriptions.dispose()
@emitter.dispose()
monitorActivePaneItem: ->
childSubscription = null
@subscriptions.add @observeActivePane (activePane) =>
if childSubscription?
@subscriptions.remove(childSubscription)
childSubscription.dispose()
childSubscription = activePane.observeActiveItem (activeItem) =>
@emitter.emit 'did-change-active-pane-item', activeItem
@subscriptions.add(childSubscription)
monitorPaneItems: ->
@subscriptions.add @observePanes (pane) =>
for item, index in pane.getItems()
@emitter.emit 'did-add-pane-item', {item, pane, index}
pane.onDidAddItem ({item, index}) =>
@emitter.emit 'did-add-pane-item', {item, pane, index}