-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathtab.js
More file actions
256 lines (217 loc) · 7.28 KB
/
Copy pathtab.js
File metadata and controls
256 lines (217 loc) · 7.28 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
class TabList {
constructor (tabs, parentTaskList) {
this.tabs = tabs || []
this.parentTaskList = parentTaskList
}
//tab properties that shouldn't be saved to disk
static temporaryProperties = ['hasAudio', 'previewImage', 'loaded', 'hasWebContents']
add (tab = {}, options = {}, emit=true) {
var tabId = String(tab.id || Math.round(Math.random() * 100000000000000000)) // you can pass an id that will be used, or a random one will be generated.
var newTab = {
url: tab.url || '',
title: tab.title || '',
id: tabId,
lastActivity: tab.lastActivity || Date.now(),
secure: tab.secure,
private: tab.private || false,
readerable: tab.readerable || false,
themeColor: tab.themeColor,
backgroundColor: tab.backgroundColor,
scrollPosition: tab.scrollPosition || 0,
selected: tab.selected || false,
muted: tab.muted || false,
loaded: tab.loaded || false,
hasAudio: false,
previewImage: '',
isFileView: false,
hasWebContents: false,
// Trail tree properties
parentId: tab.parentId || null, // ID of parent tab (null = root)
childIds: tab.childIds || [], // Array of child tab IDs
collapsed: tab.collapsed || false, // Whether children are hidden
trailName: tab.trailName || null, // Optional name for this branch
trailEmoji: tab.trailEmoji || null, // Optional emoji
depth: tab.depth || 0, // Nesting level for indentation
}
if (options.atEnd) {
this.tabs.push(newTab)
} else {
this.tabs.splice(this.getSelectedIndex() + 1, 0, newTab)
}
if (emit) {
this.parentTaskList.emit('tab-added', tabId, newTab, options, this.parentTaskList.getTaskContainingTab(tabId).id)
}
return tabId
}
update (id, data, emit=true) {
if (!this.has(id)) {
throw new ReferenceError('Attempted to update a tab that does not exist.')
}
const index = this.getIndex(id)
for (var key in data) {
if (data[key] === undefined) {
throw new ReferenceError('Key ' + key + ' is undefined.')
}
this.tabs[index][key] = data[key]
if (emit) {
this.parentTaskList.emit('tab-updated', id, key, data[key], this.parentTaskList.getTaskContainingTab(id).id)
}
// changing URL erases scroll position
if (key === 'url') {
this.tabs[index].scrollPosition = 0
if (emit) {
this.parentTaskList.emit('tab-updated', id, 'scrollPosition', 0, this.parentTaskList.getTaskContainingTab(id).id)
}
}
}
}
destroy (id, emit=true) {
const index = this.getIndex(id)
if (index < 0) return false
const containingTask = this.parentTaskList.getTaskContainingTab(id).id
const tab = this.tabs[index]
// Handle children: reparent them to the destroyed tab's parent (grandparent)
const childIds = tab.childIds || []
const parentId = tab.parentId || null
childIds.forEach(childId => {
const childIndex = this.getIndex(childId)
if (childIndex >= 0) {
// Update child's parentId to grandparent
this.tabs[childIndex].parentId = parentId
// Recalculate depth
this.tabs[childIndex].depth = this.parentTaskList.calculateDepth(childId)
if (emit) {
this.parentTaskList.emit('tab-reparented', childId, parentId, containingTask)
}
}
})
// If this tab had a parent, remove this tab from parent's childIds
if (parentId) {
const parentIndex = this.getIndex(parentId)
if (parentIndex >= 0) {
const parentChildIds = this.tabs[parentIndex].childIds || []
const childIdIndex = parentChildIds.indexOf(id)
if (childIdIndex >= 0) {
parentChildIds.splice(childIdIndex, 1)
}
// Add this tab's children to the parent's childIds
this.tabs[parentIndex].childIds = parentChildIds.concat(childIds)
}
}
tasks.getTaskContainingTab(id).tabHistory.push(this.toPermanentState(this.tabs[index]))
this.tabs.splice(index, 1)
if (emit) {
this.parentTaskList.emit('tab-destroyed', id, containingTask)
}
return index
}
get (id) {
if (!id) { // no id provided, return an array of all tabs
// it is important to copy the tab objects when returning them. Otherwise, the original tab objects get modified when the returned tabs are modified (such as when processing a url).
var tabsToReturn = []
for (var i = 0; i < this.tabs.length; i++) {
tabsToReturn.push(Object.assign({}, this.tabs[i]))
}
return tabsToReturn
}
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id === id) {
return Object.assign({}, this.tabs[i])
}
}
return undefined
}
has (id) {
return this.getIndex(id) > -1
}
getIndex (id) {
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id === id) {
return i
}
}
return -1
}
getSelected () {
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].selected) {
return this.tabs[i].id
}
}
return null
}
getSelectedIndex () {
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].selected) {
return i
}
}
return null
}
getAtIndex (index) {
return this.tabs[index] || undefined
}
setSelected (id, emit=true) {
if (!this.has(id)) {
throw new ReferenceError('Attempted to select a tab that does not exist.')
}
for (var i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id === id) {
this.tabs[i].selected = true
this.tabs[i].lastActivity = Date.now()
} else if (this.tabs[i].selected) {
this.tabs[i].selected = false
this.tabs[i].lastActivity = Date.now()
}
}
if (emit) {
this.parentTaskList.emit('tab-selected', id, this.parentTaskList.getTaskContainingTab(id).id)
}
}
moveBy (id, offset) {
var currentIndex = this.getIndex(id)
var newIndex = currentIndex + offset
var newIndexTab = this.getAtIndex(newIndex)
if (newIndexTab) {
var currentTab = this.getAtIndex(currentIndex)
this.splice(currentIndex, 1, newIndexTab)
this.splice(newIndex, 1, currentTab)
}
//This doesn't need to dispatch an event because splice will dispatch already
}
count () {
return this.tabs.length
}
isEmpty () {
if (!this.tabs || this.tabs.length === 0) {
return true
}
if (this.tabs.length === 1 && !this.tabs[0].url) {
return true
}
return false
}
forEach (fun) {
return this.tabs.forEach(fun)
}
splice (...args) {
const containingTask = this.parentTaskList.find(t => t.tabs === this).id
this.parentTaskList.emit('tab-splice', containingTask, ...args)
return this.tabs.splice.apply(this.tabs, args)
}
spliceNoEmit (...args) {
return this.tabs.splice.apply(this.tabs, args)
}
toPermanentState (tab) {
//removes temporary properties of the tab that are lost on page reload
let result = {}
Object.keys(tab)
.filter(key => !TabList.temporaryProperties.includes(key))
.forEach(key => result[key] = tab[key])
return result
}
getStringifyableState () {
return this.tabs.map(tab => this.toPermanentState(tab))
}
}
module.exports = TabList