This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 970
/
Copy pathframeStateUtil.js
311 lines (276 loc) · 9.31 KB
/
frameStateUtil.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
import Immutable from 'immutable'
import Config from '../constants/config.js'
const getFavicon = require('../lib/faviconUtil.js')
export function isFrameKeyActive (windowState, frameKey) {
return windowState.get('activeFrameKey') === frameKey
}
export function getActiveFrameIndex (windowState) {
return findIndexForFrameKey(windowState.get('frames'), windowState.get('activeFrameKey'))
}
export function getFrameByIndex (windowState, i) {
return windowState.getIn(['frames', i])
}
export function getFrameByKey (windowState, key) {
let i = findIndexForFrameKey(windowState.get('frames'), key)
return windowState.getIn(['frames', i])
}
export function getActiveFrame (windowState) {
const activeFrameIndex = getActiveFrameIndex(windowState)
return windowState.get('frames').get(activeFrameIndex)
}
export function setActiveFrameIndex (windowState, i) {
const frame = getFrameByIndex(windowState, i)
if (!frame) {
return windowState
}
return setActiveFrameKey(windowState, frame.get('key'))
}
export function setActiveFrameKey (windowState, activeFrameKey) {
return windowState.set('activeFrameKey', activeFrameKey)
}
export function makeNextFrameActive (windowState) {
const activeFrameIndex = getActiveFrameIndex(windowState)
return setActiveFrameIndex(windowState, (activeFrameIndex + 1) % windowState.get('frames').size)
}
export function makePrevFrameActive (windowState) {
const activeFrameIndex = getActiveFrameIndex(windowState)
return setActiveFrameIndex(windowState, (windowState.get('frames').size + activeFrameIndex - 1) % windowState.get('frames').size)
}
/**
* Obtains the index for the specified frame key
*/
export function findIndexForFrameKey (frames, key) {
return frames.findIndex(frame => frame.get('key') === key)
}
/**
* Obtains the frameProps index in the frames
*/
export function getFramePropsIndex (frames, frameProps) {
return frames.findIndex(found => found.get('key') === frameProps.get('key'))
}
/**
* Converts a feature string into an object.
* @param {String} featureStr A string like, arg=val,arg2=val2
*/
export function getFeatures (featureStr) {
return String(featureStr)
.split(',')
.reduce((acc, feature) => {
feature = feature
.split('=')
.map(featureElem => featureElem.trim())
if (feature.length !== 2) {
return acc
}
acc[decodeURIComponent(feature[0])] = decodeURIComponent(feature[1])
return acc
}, {})
}
/**
* Determines if the specified frame was opened from the specified
* ancestorFrameKey.
*
* For example you may go to google.com and open 3 links in new tabs:
* G g1 g2 g3
* Then you may change to g1 and open another tab:
* G g1 g1.1 g2 g3
* But then you may go back to google.com and open another tab.
* It should go like so:
* G g1 g1.1 g2 g3 g4
*/
function isAncestorFrameKey (frames, frame, parentFrameKey) {
if (!frame || !frame.get('parentFrameKey')) {
return false
}
if (frame.get('parentFrameKey') === parentFrameKey) {
return true
}
// So there is a parentFrameKey but it isn't the specified one.
// Check recursively for each of the parentFrame's ancestors to see
// if we have a match.
let parentFrameIndex = findIndexForFrameKey(frames, frame.get('parentFrameKey'))
let parentFrame = frames.get(parentFrameIndex)
if (parentFrameIndex === -1 || !parentFrame.get('parentFrameKey')) {
return false
}
return isAncestorFrameKey(frames, parentFrame, parentFrameKey)
}
/**
* Adds a frame specified by frameOpts and newKey and sets the activeFrameKey
* @return Immutable top level application state ready to merge back in
*/
export function addFrame (frames, frameOpts, newKey, activeFrameKey) {
var url = frameOpts.location || Config.defaultUrl
let frame = Immutable.fromJS({
audioMuted: false, // frame is muted
canGoBack: false,
canGoForward: false,
location: url, // page url
src: url, // what the iframe src should be
isPrivate: frameOpts.isPrivate || false,
element: frameOpts.element,
features: getFeatures(frameOpts.features),
key: newKey,
parentFrameKey: frameOpts.parentFrameKey,
parentWindowKey: frameOpts.parentWindowKey,
navbar: {
searchSuggestions: true,
focused: true,
urlbar: {
location: url,
urlPreview: '',
suggestions: {
selectedIndex: 0,
searchResults: [],
suggestionList: null
},
selected: true,
focused: true,
active: false
}
},
searchDetail: null,
findDetail: {
searchString: '',
caseSensitivity: false
}
})
// Find the closest index to the current frame's index which has
// a different ancestor frame key.
let insertionIndex = findIndexForFrameKey(frames, frameOpts.parentFrameKey)
if (insertionIndex === -1) {
insertionIndex = frames.size
}
while (insertionIndex < frames.size) {
++insertionIndex
if (!isAncestorFrameKey(frames, frames.get(insertionIndex), frameOpts.parentFrameKey)) {
break
}
}
return {
frames: frames.splice(insertionIndex, 0, frame),
activeFrameKey
}
}
/**
* Undoes a frame close and inserts it at the last index
* @return Immutable top level application state ready to merge back in
*/
export function undoCloseFrame (windowState, closedFrames) {
if (closedFrames.size === 0) {
return {}
}
var closedFrame = closedFrames.last()
let insertIndex = closedFrame.get('closedAtIndex')
return {
closedFrames: closedFrames.pop(),
frames: windowState.get('frames').splice(insertIndex, 0, closedFrame.remove('closedAtIndex')),
activeFrameKey: closedFrame.get('key')
}
}
/**
* Removes a frame specified by frameProps
* @return Immutable top level application state ready to merge back in
*/
export function removeFrame (frames, closedFrames, frameProps, activeFrameKey) {
if (!frameProps.get('isPrivate')) {
closedFrames = closedFrames.push(frameProps)
if (frameProps.get('thumbnailBlob')) {
window.URL.revokeObjectURL(frameProps.get('thumbnailBlob'))
}
if (closedFrames.size > Config.maxClosedFrames) {
closedFrames = closedFrames.shift()
}
}
const activeFrameIndex = findIndexForFrameKey(frames, activeFrameKey)
const framePropsIndex = getFramePropsIndex(frames, frameProps)
frames = frames.splice(framePropsIndex, 1)
return {
activeFrameKey: frameProps.get('key') === activeFrameKey && frames.size > 0
? Math.max(
frames.get(activeFrameIndex)
// Go to the next frame if it exists.
? frames.get(activeFrameIndex).get('key')
// Otherwise go to the frame right before the active tab.
: frames.get(activeFrameIndex - 1).get('key'),
0) : activeFrameKey,
closedFrames,
frames
}
}
/**
* Removes all but the specified frameProps
* @return Immutable top level application state ready to merge back in
*/
export function removeOtherFrames (frames, closedFrames, frameProps) {
closedFrames = closedFrames.concat(frames.filter(currentFrameProps => !currentFrameProps.get('isPrivate') && currentFrameProps.get('key') !== frameProps.get('key')))
.take(Config.maxClosedFrames)
closedFrames.forEach(currentFrameProps => {
if (currentFrameProps.get('thumbnailBlob')) {
window.URL.revokeObjectURL(currentFrameProps.get('thumbnailBlob'))
}
})
frames = Immutable.fromJS([frameProps])
return {
activeFrameKey: frameProps.get('key'),
closedFrames,
frames
}
}
/**
* Extracts theme-color from a favicon using vibrant.js.
*/
export function computeThemeColor (frameProps) {
return new Promise((resolve, reject) => {
var icon = getFavicon(frameProps)
var xhr = new window.XMLHttpRequest()
xhr.open('GET', icon, true)
xhr.responseType = 'blob'
xhr.send()
xhr.onload = function () {
var status = xhr.status
if (status !== 0 && status !== 200) {
reject(
new Error('Got HTTP status ' + status + ' trying to load ' + icon)
)
return
}
renderFromBlob(xhr.response)
}
xhr.onerror = xhr.ontimeout = function () {
reject(new Error('Error while fetching icon: ', icon))
}
function renderFromBlob (blob) {
var img = new window.Image()
img.src = window.URL.createObjectURL(blob)
img.onload = () => {
var vibrant = new window.Vibrant(img)
var swatches = vibrant.swatches()
window.URL.revokeObjectURL(img.src)
// Arbitrary selection ordering, which appears to give decent results.
const swatchOrder = ['Muted', 'LightMuted', 'DarkMuted', 'DarkVibrant', 'Vibrant', 'LightVibrant']
for (var i = 0; i < swatchOrder.length; i++) {
var swatch = swatchOrder[i]
if (swatches[swatch]) {
resolve(swatches[swatch].getHex())
break
}
}
}
img.onerror = () => {
window.URL.revokeObjectURL(img.src)
reject(new Error('Could not render image from blob.'))
}
}
})
}
export function getFrameTabPageIndex (frames, frameProps) {
const index = getFramePropsIndex(frames, frameProps)
if (index === -1) {
return -1
}
return Math.floor(index / Config.tabs.tabsPerPage)
}