Skip to content

Commit 0ca8a88

Browse files
committed
convert tabs array to a class
1 parent 211bd67 commit 0ca8a88

File tree

8 files changed

+85
-78
lines changed

8 files changed

+85
-78
lines changed

js/browserUI.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ options
3636
options.enterEditMode - whether to enter editing mode when the tab is created. Defaults to true.
3737
options.openInBackground - whether to open the tab without switching to it. Defaults to false.
3838
*/
39-
function addTab (tabId = tabs.add(), options = {}) {
39+
function addTab (tabId = tabs.add() , options = {}) {
4040
tabBar.addTab(tabId)
4141
webviews.add(tabId)
4242

@@ -92,8 +92,7 @@ function closeTask (taskId) {
9292
})
9393

9494
const mostRecent = recentTaskList.reduce(
95-
(latest, current) =>
96-
current.lastActivity > latest.lastActivity ? current : latest
95+
(latest, current) => current.lastActivity > latest.lastActivity ? current : latest
9796
)
9897

9998
return switchToTask(mostRecent.id)
@@ -112,7 +111,7 @@ function closeTab (tabId) {
112111
if (tabId === tabs.getSelected()) {
113112
var currentIndex = tabs.getIndex(tabs.getSelected())
114113
var nextTab =
115-
tabs.getAtIndex(currentIndex - 1) || tabs.getAtIndex(currentIndex + 1)
114+
tabs.getAtIndex(currentIndex - 1) || tabs.getAtIndex(currentIndex + 1)
116115

117116
destroyTab(tabId)
118117

@@ -135,7 +134,7 @@ function switchToTask (id) {
135134

136135
var taskData = tasks.get(id)
137136

138-
if (taskData.tabs.length > 0) {
137+
if (taskData.tabs.count() > 0) {
139138
var selectedTab = taskData.tabs.getSelected()
140139

141140
// if the task has no tab that is selected, switch to the most recent one
@@ -195,5 +194,4 @@ module.exports = {
195194
closeTask,
196195
closeTab,
197196
switchToTask,
198-
switchToTab
199-
}
197+
switchToTab}

js/navbar/tabBar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ window.tabBar = {
114114
rerenderAll: function () {
115115
empty(tabBar.container)
116116
tabBar.tabElementMap = {}
117-
for (var i = 0; i < tabs.length; i++) {
118-
var el = tabBar.createElement(tabs[i])
117+
for (var i = 0; i < tabs.count(); i++) {
118+
var el = tabBar.createElement(tabs.getAtIndex(i))
119119
tabBar.container.appendChild(el)
120-
tabBar.tabElementMap[tabs[i].id] = el
120+
tabBar.tabElementMap[tabs.getAtIndex(i).id] = el
121121
}
122122
if (tabs.getSelected()) {
123123
tabBar.setActiveTab(tabs.getSelected())

js/searchbar/customBangs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ registerCustomBang({
133133
tabs.destroy(currentTab.id)
134134

135135
// make sure the task has at least one tab in it
136-
if (tabs.get().length === 0) {
136+
if (tabs.count() === 0) {
137137
tabs.add()
138138
}
139139

js/searchbar/openTabsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var searchOpenTabs = function (text, input, event, container) {
1414
var currentTab = currentTask.tabs.getSelected()
1515

1616
tasks.forEach(function (task) {
17-
task.tabs.get().forEach(function (tab) {
17+
task.tabs.forEach(function (tab) {
1818
if (tab.id === currentTab || !tab.title || tab.url === 'about:blank') {
1919
return
2020
}

js/tabState/tab.js

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
var tabPrototype = {
2-
add: function (tab = {}, index) {
1+
class TabList {
2+
constructor (tabs) {
3+
this.tabs = tabs || []
4+
}
5+
add (tab = {} , index) {
36
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.
47

58
var newTab = {
@@ -16,103 +19,113 @@ var tabPrototype = {
1619
}
1720

1821
if (index) {
19-
this.splice(index, 0, newTab)
22+
this.tabs.splice(index, 0, newTab)
2023
} else {
21-
this.push(newTab)
24+
this.tabs.push(newTab)
2225
}
2326

2427
return tabId
25-
},
26-
update: function (id, data) {
28+
}
29+
update (id, data) {
2730
if (!this.has(id)) {
2831
throw new ReferenceError('Attempted to update a tab that does not exist.')
2932
}
3033
const index = this.getIndex(id)
31-
34+
3235
for (var key in data) {
3336
if (data[key] === undefined) {
3437
throw new ReferenceError('Key ' + key + ' is undefined.')
3538
}
36-
this[index][key] = data[key]
39+
this.tabs[index][key] = data[key]
3740
}
38-
},
39-
destroy: function (id) {
41+
}
42+
destroy (id) {
4043
const index = this.getIndex(id)
41-
if(index < 0) return false
44+
if (index < 0) return false
4245

43-
tasks.getTaskContainingTab(id).tabHistory.push(this[index])
44-
this.splice(index, 1)
46+
tasks.getTaskContainingTab(id).tabHistory.push(this.tabs[index])
47+
this.tabs.splice(index, 1)
4548

4649
return index
47-
},
48-
destroyAll: function () {
50+
}
51+
destroyAll () {
4952
// this = [] doesn't work, so set the length of the array to 0 to remove all of the itemss
50-
this.length = 0
51-
},
52-
get: function (id) {
53+
this.tabs.length = 0
54+
}
55+
get (id) {
5356
if (!id) { // no id provided, return an array of all tabs
5457
// it is important to deep-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).
5558
var tabsToReturn = []
56-
for (var i = 0; i < this.length; i++) {
57-
tabsToReturn.push(JSON.parse(JSON.stringify(this[i])))
59+
for (var i = 0; i < this.tabs.length; i++) {
60+
tabsToReturn.push(JSON.parse(JSON.stringify(this.tabs[i])))
5861
}
5962
return tabsToReturn
6063
}
61-
for (var i = 0; i < this.length; i++) {
62-
if (this[i].id === id) {
63-
return JSON.parse(JSON.stringify(this[i]))
64+
for (var i = 0; i < this.tabs.length; i++) {
65+
if (this.tabs[i].id === id) {
66+
return JSON.parse(JSON.stringify(this.tabs[i]))
6467
}
6568
}
6669
return undefined
67-
},
68-
has: function (id) {
70+
}
71+
has (id) {
6972
return this.getIndex(id) > -1
70-
},
71-
getIndex: function (id) {
72-
for (var i = 0; i < this.length; i++) {
73-
if (this[i].id === id) {
73+
}
74+
75+
getIndex (id) {
76+
for (var i = 0; i < this.tabs.length; i++) {
77+
if (this.tabs[i].id === id) {
7478
return i
7579
}
7680
}
7781
return -1
78-
},
79-
getSelected: function () {
80-
for (var i = 0; i < this.length; i++) {
81-
if (this[i].selected) {
82-
return this[i].id
82+
}
83+
getSelected () {
84+
for (var i = 0; i < this.tabs.length; i++) {
85+
if (this.tabs[i].selected) {
86+
return this.tabs[i].id
8387
}
8488
}
8589
return null
86-
},
87-
getAtIndex: function (index) {
88-
return this[index] || undefined
89-
},
90-
setSelected: function (id) {
90+
}
91+
getAtIndex (index) {
92+
return this.tabs[index] || undefined
93+
}
94+
setSelected (id) {
9195
if (!this.has(id)) {
9296
throw new ReferenceError('Attempted to select a tab that does not exist.')
9397
}
94-
for (var i = 0; i < this.length; i++) {
95-
if (this[i].id === id) {
96-
this[i].selected = true
98+
for (var i = 0; i < this.tabs.length; i++) {
99+
if (this.tabs[i].id === id) {
100+
this.tabs[i].selected = true
97101
} else {
98-
this[i].selected = false
102+
this.tabs[i].selected = false
99103
}
100104
}
101-
},
102-
count: function () {
103-
return this.length
104-
},
105-
isEmpty: function () {
106-
if (!this || this.length === 0) {
105+
}
106+
count () {
107+
return this.tabs.length
108+
}
109+
isEmpty () {
110+
if (!this.tabs || this.tabs.length === 0) {
107111
return true
108112
}
109113

110-
if (this.length === 1 && (!this[0].url || this[0].url === 'about:blank')) {
114+
if (this.tabs.length === 1 && (!this.tabs[0].url || this.tabs[0].url === 'about:blank')) {
111115
return true
112116
}
113117

114118
return false
115119
}
120+
forEach (fun) {
121+
return this.tabs.forEach(fun)
122+
}
123+
splice (...args) {
124+
return this.tabs.splice.apply(this.tabs, args)
125+
}
126+
getStringifyableState () {
127+
return this.tabs
128+
}
116129
}
117130

118-
module.exports = tabPrototype
131+
module.exports = TabList

js/tabState/task.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const tabPrototype = require('tabState/tab.js')
1+
const TabList = require('tabState/tab.js')
22
const TabStack = require('tabRestore.js')
33

44
class TaskList {
@@ -10,15 +10,11 @@ class TaskList {
1010
add (task = {} , index) {
1111
const newTask = {
1212
name: task.name || null,
13-
tabs: task.tabs || [],
13+
tabs: new TabList(task.tabs),
1414
tabHistory: new TabStack(task.tabHistory),
1515
id: task.id || String(TaskList.getRandomId())
1616
}
1717

18-
for (var key in tabPrototype) {
19-
newTask.tabs[key] = tabPrototype[key]
20-
}
21-
2218
if (index) {
2319
this.tasks.splice(index, 0, newTask)
2420
} else {
@@ -30,7 +26,7 @@ class TaskList {
3026

3127
getStringifyableState () {
3228
return {
33-
tasks: this.tasks,
29+
tasks: this.tasks.map(task => Object.assign({}, task, {tabs: task.tabs.getStringifyableState()})),
3430
selectedTask: this.selected
3531
}
3632
}
@@ -83,9 +79,9 @@ class TaskList {
8379
var tabs = this.get(id).tabs
8480
var lastActivity = 0
8581

86-
for (var i = 0; i < tabs.length; i++) {
87-
if (tabs[i].lastActivity > lastActivity) {
88-
lastActivity = tabs[i].lastActivity
82+
for (var i = 0; i < tabs.count(); i++) {
83+
if (tabs.getAtIndex(i).lastActivity > lastActivity) {
84+
lastActivity = tabs.getAtIndex(i).lastActivity
8985
}
9086
}
9187

js/taskOverlay/taskOverlay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ taskOverlay.tabDragula.on('drop', function (el, target, source, sibling) { // se
163163

164164
// if the old task has no tabs left in it, destroy it
165165

166-
if (previousTask.tabs.length === 0) {
166+
if (previousTask.tabs.count() === 0) {
167167
browserUI.closeTask(previousTask.id)
168168
getTaskContainer(previousTask.id).remove()
169169
}
@@ -173,7 +173,7 @@ taskOverlay.tabDragula.on('drop', function (el, target, source, sibling) { // se
173173
var newIdx = newTask.tabs.getIndex(adjacentTadId)
174174
} else {
175175
// tab was inserted at end
176-
var newIdx = newTask.tabs.length
176+
var newIdx = newTask.tabs.count()
177177
}
178178

179179
// insert the tab at the correct spot

js/taskOverlay/taskOverlayBuilder.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ var TaskOverlayBuilder = {
130130
tabContainer.setAttribute('data-task', task.id)
131131

132132
if (task.tabs) {
133-
for (var i = 0; i < task.tabs.length; i++) {
134-
var el = this.element(tabContainer, task, task.tabs[i])
133+
for (var i = 0; i < task.tabs.count(); i++) {
134+
var el = this.element(tabContainer, task, task.tabs.getAtIndex(i))
135135
tabContainer.appendChild(el)
136136
}
137137
}
@@ -160,7 +160,7 @@ var TaskOverlayBuilder = {
160160
}
161161
}
162162
}
163-
// extend with other helper functions?
163+
// extend with other helper functions?
164164
}
165165

166166
module.exports = function createTaskContainer (task, index) {

0 commit comments

Comments
 (0)