-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
113 lines (104 loc) · 4.11 KB
/
options.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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
// assumes/uses chrome-extension-async.js
// assumes/uses VScommon.js
const errorHandler = error => {
console.warn(error)
}
const saveVal = async (name,value) => {
let o = {}
o[name]=value
return chrome.storage.sync.set(o)
.then((result)=>{
console.log('Saved %s : %o : %o', name, value, result)
return result
})
.catch(errorHandler)
}
// factory
const genSaveEventValue = ({name, kind = "value", convert}) => (event) => {
const o = {}
const value = typeof event === 'object'
? event.target[kind]
: event
const converted = !!convert
? typeof convert === 'function'
? convert(value)
: typeof window[convert] ==='function'
? window[convert](value)
: (()=>{throw `Unable to process convert = "${convert.toString()}"`})()
: value
saveVal(name,converted)
}
const checkBoxElement = ([property, description, selected],groupName='optSet1') => {
const elem=document.createElement('label')
elem.classList.add('checkBox')
elem.innerHTML=`${makeSpan(property,'itemTitle')} ${description}
<input type="checkbox" id="${property}" value="${property}" ${selected?'checked':''} name="${groupName}">
<span class="checkmark"></span>`
elem.querySelector('input').addEventListener('click', multiHandler)
return elem
}
const optStatusItems = document.getElementById('optStatusItems')
const getCheckGroup = (groupName='optSet1') => [...document.querySelectorAll(`input[name="${groupName}"]`)]
const getCheckGroupValues = () => getCheckGroup().reduce((a,e)=>e.checked?[...a,e.id]:a,[])
// this is a small domain, so we'll just generate the result set every time something changes
const multiHandler = event => {
optStatusItems.value = getCheckGroupValues()
saveVal('optStatusItems',optStatusItems.value)
}
let resetDefaultChecks = document.getElementById('resetDefaultChecks')
const resetDefaultChecksEvent = () => {
getCheckGroup().forEach(o=>{
o.checked = fetchList.initialDefault.indexOf(o.id)>-1
})
multiHandler()
}
resetDefaultChecks.addEventListener('click',resetDefaultChecksEvent)
const resetDefaults = document.getElementById('resetDefaults')
const resetDefaultsEvent = () => {
resetDefaultChecksEvent()
VSoptions.forEach(obj=>{
const elem = document.getElementById(obj.name)
if(elem){
elem[obj.kind||'value']=obj.origin
}
saveVal(obj.name,obj.origin)
})
}
resetDefaults.addEventListener('click',resetDefaultsEvent)
// create, init, and hook
const init = (options) => {
options.forEach(e => {
const elem = document.getElementById(e.name)
chrome.storage.sync.get(e.name)
.then(value => {
if(e.name==='optStatusItems'){
const selection = value.hasOwnProperty(e.name) && Array.isArray(value[e.name])
? value[e.name]
: e.origin
fetchList.list
// build params for checkBoxElement
.map((e,i)=>([e,fetchList.description[i],selection.indexOf(e)>-1]))
// make rows of labels
.map(a=>checkBoxElement(a))
// flip em so we can push them in from the front
.reverse()
// push them in from the front
.forEach(e=>{
optStatusItems.prepend(e)
// e.querySelector('input').addEventListener('click', multiHandler)
})
}else{
elem[e.kind||"value"] = value.hasOwnProperty(e.name)
? value[e.name]
: e.origin
elem.addEventListener('change', genSaveEventValue(e))
}
})
.catch(errorHandler)
})
}
init(VSoptions)