-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiflow.js
62 lines (60 loc) · 1.94 KB
/
multiflow.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
/**
* Interoperability API for plugin compatibility
*
* Feel free to copy/paste verbatim, or pull out functions as required
*/
export const MultiFlow = {
/**
* Get the currently focused frame's Window or the main Window if MultiFlow is not active
*
* @example MultiFlow.getWindow().document.body.addEventListener( ... )
* @returns {Window}
*/
getWindow () {
const body = document.body
const mode = body.getAttribute('data-mode')
if (mode === 'multiflow') {
const frames = document.querySelectorAll('#multiflow iframe')
const index = body.getAttribute('data-focused')
return frames[Number(index) || 0].contentWindow
}
return window
},
/**
* Hook into MultiFlow state changes
*
* Available attributes are:
*
* - mode <string> : either "workflowy" or "multiflow" depending if columns are visible
* - loading <boolean> : whether one or more frames are loading
* - frames <number> : the number of frames created (note: closed frames may be hidden!)
* - focused <number> : the currently focused frame index
*
* @example MultiFlow.onChange((attr, value, oldValue) => if (attr === 'mode') { ... })
* @param callback
*/
onChange (callback) {
const observer = new MutationObserver(function (mutations, observer) {
for (const mutation of mutations) {
const { attributeName, target, oldValue } = mutation
const attr = attributeName.substring(5)
const value = target.getAttribute(attributeName)
if (oldValue !== value) {
callback(attr, parse(value), parse(oldValue))
}
}
})
observer.observe(document.body, {
attributes: true,
attributeOldValue: true,
attributeFilter: 'data-mode data-frames data-loading data-focused'.split(' '),
})
},
}
function parse (value) {
return /\d/.test(value)
? parseInt(value)
: /true|false/.test(value)
? value === 'true'
: value
}