-
Notifications
You must be signed in to change notification settings - Fork 6
/
o.js
208 lines (184 loc) · 4.08 KB
/
o.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
/*
alphaama
mod o
options using localStorage key:value
*/
aa.o =
{
def:
{
id:'o',
ls:
{
'ns':'.aa', // used as the prefix for actions
'pagination': '100', // number of root events displayed
'reaction':'\uD83E\uDD18', // '🤘' default reaction emoji
'team':'dark', // 'light'
'trust':'11', // user score needed for loading media
'pow':'0', // proof of work difficulty
},
// todo
// 'nav_keys':
// {
// 'parent':'j',
// 'next':'k',
// 'child':'l',
// 'previous':'i',
// 'top':'h',
// 'bottom':'b',
// 'fetch':'f',
// 'replies':'\\',
// 'exit':'Escape'
// }
},
};
// on load
aa.o.load =()=>
{
const mod = aa.o;
const id = mod.def.id;
// ensure default options are set
for (const k in mod.def.ls)
{
if (!localStorage[k]) localStorage[k] = mod.def.ls[k];
}
aa.actions.push(
{
action:[id,'set'],
required:['key','value'],
description:'set option value',
exe:mod.set
},
{
action:[id,'reset'],
optional:['key'],
description:'reset all or just a single option',
exe:mod.reset
},
{
action:[id,'rm'],
optional:['key'],
description:'remove option',
exe:mod.rm
}
);
mod.o = {id:id,ls:localStorage};
aa.mod_load(mod).then(aa.mk.mod).then(e=>
{
let add_butt = aa.mk.butt_action(`${id} set `,'+','set');
mod.l.insertBefore(add_butt,document.getElementById(id));
});
// detect when changes happen on other tabs
window.addEventListener('storage',e=> { console.log('storage',e); });
};
aa.fx.team =team=>
{
if (team === 'dark') return 'light';
else return 'dark'
};
// makes a mod option item, to use with aa.mk.mod()
aa.o.mk =(k,v)=>
{
const id = aa.o.def.id;
const l = aa.mk.l('li',{id:id+'_'+k,cla:'item'});
// update
switch (k)
{
case 'team':
if (aa.l.dataset.team !== v) aa.l.dataset.team = v;
l.append(
aa.mk.butt_action(id+' set '+k+' '+aa.fx.team(v),k,'key'),
' ',
aa.mk.l('span',{cla:'val',con:v})
);
break;
// case 'pagination':
// let style = aa.mk.l('style',
// {con:'.note:not(:nth-child(-n+'+k+')):not(.in_path){display:none;'});
// document.head.append(style);
// aa.l.dataset.pagination = localStorage.pagination;
// break;
default:
l.append(
aa.mk.butt_action(id+' set '+k+' '+v,k,'key'),
' ',
aa.mk.l('span',{cla:'val',con:v})
);
}
return l
};
// reset one, multiple or all values
aa.o.reset =s=>
{
aa.cli.clear();
s.trim();
if (s)
{
const work =a=>
{
const v = a[0];
if (aa.o.def.ls[v])
{
localStorage[v] = aa.o.def.ls[v];
aa.mod_ui(aa.o,k);
}
}
aa.fx.loop(work,s);
aa.log(aa.mk.butt_action(aa.o.def.id+' reset '+s));
}
else if (window.confirm('reset all options?'))
{
localStorage.clear();
for (const k in aa.o.def.ls) localStorage[k] = aa.o.def.ls[k];
}
};
// remove option (if not default)
aa.o.rm =s=>
{
aa.cli.clear();
const work =a=>
{
const dis = localStorage.ns+' '+aa.o.def.id+' rm ';
const k = a.shift().trim();
if (k && localStorage[k])
{
if (!aa.o.def.hasOwnProperty(k))
{
localStorage.removeItem(k);
document.getElementById(aa.o.def.id+'_'+k).remove();
aa.log(dis+k);
}
else aa.log(dis+'key cannot be removed');
}
else aa.log(dis+'key not found');
};
aa.fx.loop(work,s)
};
// saves
aa.o.save =()=>
{
aa.o.o = {id:'o',ls:localStorage};
aa.mk.mod(aa.o);
};
// set key as value
aa.o.set =s=>
{
// aa.cli.fuck_off();
aa.cli.clear();
const work =a=>
{
let [k,v] = a;
if (k && v)
{
let old_v = localStorage[k];
localStorage[k] = v;
aa.mod_ui(aa.o,k);
let log = aa.mk.l('p',{con:aa.o.def.id+' set '+k+' '+v});
let undo = aa.mk.butt_action(aa.o.def.id+' set '+k+' '+old_v,'undo');
log.append(' ',undo);
aa.log(log);
}
};
aa.fx.loop(work,s)
};
window.addEventListener('load',aa.o.load);