-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-element.coffee
132 lines (105 loc) · 4.27 KB
/
pane-element.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
{CompositeDisposable} = require 'event-kit'
{$, callAttachHooks, callRemoveHooks} = require './space-pen-extensions'
PaneView = require './pane-view'
class PaneElement extends HTMLElement
attached: false
createdCallback: ->
@attached = false
@subscriptions = new CompositeDisposable
@inlineDisplayStyles = new WeakMap
@initializeContent()
@subscribeToDOMEvents()
@createSpacePenShim()
attachedCallback: ->
@attached = true
@focus() if @model.isFocused()
detachedCallback: ->
@attached = false
initializeContent: ->
@setAttribute 'class', 'pane'
@setAttribute 'tabindex', -1
@appendChild @itemViews = document.createElement('div')
@itemViews.setAttribute 'class', 'item-views'
subscribeToDOMEvents: ->
handleFocus = (event) =>
@model.focus()
if event.target is this and view = @getActiveView()
view.focus()
event.stopPropagation()
handleBlur = (event) =>
@model.blur() unless @contains(event.relatedTarget)
@addEventListener 'focus', handleFocus, true
@addEventListener 'blur', handleBlur, true
createSpacePenShim: ->
@__spacePenView = new PaneView(this)
initialize: (@model) ->
@subscriptions.add @model.onDidActivate(@activated.bind(this))
@subscriptions.add @model.observeActive(@activeStatusChanged.bind(this))
@subscriptions.add @model.observeActiveItem(@activeItemChanged.bind(this))
@subscriptions.add @model.onDidRemoveItem(@itemRemoved.bind(this))
@subscriptions.add @model.onDidDestroy(@paneDestroyed.bind(this))
@__spacePenView.setModel(@model)
this
getModel: -> @model
activated: ->
@focus()
activeStatusChanged: (active) ->
if active
@classList.add('active')
else
@classList.remove('active')
activeItemChanged: (item) ->
return unless item?
hasFocus = @hasFocus()
itemView = atom.views.getView(item)
unless @itemViews.contains(itemView)
@itemViews.appendChild(itemView)
callAttachHooks(itemView)
for child in @itemViews.children
if child is itemView
@showItemView(child) if @attached
else
@hideItemView(child)
itemView.focus() if hasFocus
showItemView: (itemView) ->
inlineDisplayStyle = @inlineDisplayStyles.get(itemView)
if inlineDisplayStyle?
itemView.style.display = inlineDisplayStyle
else
itemView.style.display = ''
hideItemView: (itemView) ->
inlineDisplayStyle = itemView.style.display
unless inlineDisplayStyle is 'none'
@inlineDisplayStyles.set(itemView, inlineDisplayStyle) if inlineDisplayStyle?
itemView.style.display = 'none'
itemRemoved: ({item, index, destroyed}) ->
if viewToRemove = atom.views.getView(item)
callRemoveHooks(viewToRemove) if destroyed
viewToRemove.remove()
paneDestroyed: ->
@subscriptions.dispose()
getActiveView: -> atom.views.getView(@model.getActiveItem())
hasFocus: ->
this is document.activeElement or @contains(document.activeElement)
atom.commands.add 'atom-pane',
'pane:save-items': -> @getModel().saveItems()
'pane:show-next-item': -> @getModel().activateNextItem()
'pane:show-previous-item': -> @getModel().activatePreviousItem()
'pane:show-item-1': -> @getModel().activateItemAtIndex(0)
'pane:show-item-2': -> @getModel().activateItemAtIndex(1)
'pane:show-item-3': -> @getModel().activateItemAtIndex(2)
'pane:show-item-4': -> @getModel().activateItemAtIndex(3)
'pane:show-item-5': -> @getModel().activateItemAtIndex(4)
'pane:show-item-6': -> @getModel().activateItemAtIndex(5)
'pane:show-item-7': -> @getModel().activateItemAtIndex(6)
'pane:show-item-8': -> @getModel().activateItemAtIndex(7)
'pane:show-item-9': -> @getModel().activateItemAtIndex(8)
'pane:move-item-right': -> @getModel().moveItemRight()
'pane:move-item-left': -> @getModel().moveItemLeft()
'pane:split-left': -> @getModel().splitLeft(copyActiveItem: true)
'pane:split-right': -> @getModel().splitRight(copyActiveItem: true)
'pane:split-up': -> @getModel().splitUp(copyActiveItem: true)
'pane:split-down': -> @getModel().splitDown(copyActiveItem: true)
'pane:close': -> @getModel().close()
'pane:close-other-items': -> @getModel().destroyInactiveItems()
module.exports = PaneElement = document.registerElement 'atom-pane', prototype: PaneElement.prototype