-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiff-diff.ts
211 lines (177 loc) · 5.52 KB
/
iff-diff.ts
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
import {xc, PropAction, ReactiveSurface, PropDef, PropDefMap, IReactor} from 'xtal-element/lib/XtalCore.js';
/**
* @element iff-diff
*/
export class IffDiff extends HTMLElement implements ReactiveSurface{
static is = 'iff-diff';
disabled: boolean | undefined;
//boilerplate
propActions = propActions;
reactor: IReactor = new xc.Rx(this); self=this;
sysOfRec: WeakRef<IffDiff> | undefined;
sysOfRecHandler(e: Event){
const key = slicedPropDefs.propLookup.value!.alias!;
(<any>this)[key] = this.syncWith !== undefined ? (<any>e).detail.value : !(<any>e).detail.value;
}
disconnectedCallback(){
this.disconnect();
}
disconnect(){
const sys = this.sysOfRec?.deref();
if(sys === undefined) return;
sys.removeEventListener('value-changed' as keyof HTMLElementEventMap, this.sysOfRecHandler);
}
/**
* Boolean property / attribute -- must be true to pass test(s)
* @attr
*/
iff: boolean | undefined;
/**
* LHS Operand.
* @attr
*/
lhs!: boolean | string | number | object;
/**
* RHS Operand.
* @attr
*/
rhs!: boolean | string | number | object;
/**
* lhs must equal rhs to pass tests.
* @attr
*/
equals!: boolean;
/**
* lhs must not equal rhs to pass tests.
* @attr not-equals
*/
notEquals: boolean | undefined;
/**
* For strings, this means lhs.indexOf(rhs) > -1
* For arrays, this means lhs intersect rhs = rhs
* For numbers, this means lhs >= rhs
* For objects, this means all the properties of rhs match the same properties of lhs
* @attr includes
*/
includes!: boolean;
setAttr: string | undefined;
setClass: string | undefined;
setPart: string | undefined;
syncWith: string | undefined;
antiSyncWith: string | undefined;
evaluatedValue: boolean = false;
/**
* Computed based on values of if / equals / not_equals / includes
*/
value: boolean | undefined;
onPropChange(n: string, propDef: PropDef, newVal: any){
this.reactor.addToQueue(propDef, newVal);
}
connectedCallback(){
this.style.display = 'none';
xc.mergeProps(this, slicedPropDefs);
}
}
const linkValue = ({iff, lhs, rhs, includes, equals, notEquals, self, disabled}: IffDiff) => {
if((self.syncWith || self.antiSyncWith) !== undefined) return; // don't compute our own value
const key = slicedPropDefs.propLookup.value!.alias!;
const aSelf = self as any;
let val = self.iff;
if(val){
if(equals || notEquals){
let eq = false;
if(typeof lhs === 'object' && typeof rhs === 'object'){
import('./compare.js').then(({compare}) => {
eq = compare(lhs, rhs);
aSelf[key] = equals ? eq : !eq;
});
}else{
eq = lhs === rhs;
aSelf[key] = equals ? eq : !eq;
}
}else if(includes){
import('./includes.js').then(({includes}) => {
aSelf[key] = includes(lhs, rhs);
});
}else{
aSelf[key] = !!val;
}
}else{
aSelf[key] = !!val;
}
self.evaluatedValue = true;;
};
const linkValueFromReference = ({syncWith, antiSyncWith, self}: IffDiff) => {
const union = syncWith || antiSyncWith;
if(union === undefined) return;
const sourceOfTruth = (self.getRootNode() as HTMLElement).querySelector('#' + union) as IffDiff;
if(sourceOfTruth == null) throw syncWith + " not found.";
self.disconnect();
self.sysOfRec = new WeakRef<IffDiff>(sourceOfTruth);
const key = slicedPropDefs.propLookup.value!.alias!;
(<any>self)[key] = sourceOfTruth.value;
sourceOfTruth.addEventListener('value-changed', self.sysOfRecHandler.bind(self));
}
const affectNextSibling = ({self, value, setAttr, setClass, setPart, evaluatedValue}: IffDiff) => {
if(!evaluatedValue) return;
const ns = self.nextElementSibling;
if(ns === null){
setTimeout(() => affectNextSibling(self), 50);
return;
}
if(value){
if(setAttr !== undefined) ns.setAttribute(setAttr, '');
if(setClass !== undefined) ns.classList.add(setClass);
if(setPart !== undefined) (<any>ns).part.add(setPart);
}else{
if(setAttr !== undefined) ns.removeAttribute(setAttr);
if(setClass !== undefined) ns.classList.remove(setClass);
if(setPart !== undefined) (<any>ns).part.remove(setPart);
}
}
const propActions = [
linkValue,
affectNextSibling,
linkValueFromReference,
] as PropAction[];
const bool1: PropDef = {
type: Boolean,
dry: true,
async: true,
};
const obj1: PropDef = {
type: Object,
dry: true,
async: true,
};
const str1: PropDef = {
type: String,
dry: true,
async: true,
};
const propDefMap: PropDefMap<IffDiff> = {
iff: bool1, notEquals: bool1, includes: bool1, evaluatedValue: bool1,
disabled: {
type: Boolean,
reflect: true,
},
lhs: obj1, rhs: obj1,
setAttr: str1, setClass: str1, setPart: str1,
value:{
type: Boolean,
dry: true,
notify: true,
reflect: true,
async: true,
obfuscate: true,
},
syncWith: str1, antiSyncWith: str1
};
const slicedPropDefs = xc.getSlicedPropDefs(propDefMap);
xc.letThereBeProps(IffDiff, slicedPropDefs, 'onPropChange');
xc.define(IffDiff);
declare global {
interface HTMLElementTagNameMap {
"iff-diff": IffDiff,
}
}