nv-facutil-def-bw-attr 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +132 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -63,14 +63,14 @@ const def_num_accessor = (self,attr)=>{
|
|
|
63
63
|
} else {
|
|
64
64
|
let val = Number(v);
|
|
65
65
|
if(!isNaN(val)) {
|
|
66
|
-
|
|
66
|
+
return val;
|
|
67
67
|
} else {
|
|
68
68
|
let rgx = /^([0-9]+[\.]?[0-9]*)([a-zA-Z]+)$/;
|
|
69
69
|
if(rgx.test(String(val))) {
|
|
70
|
-
|
|
70
|
+
return val;
|
|
71
71
|
} else {
|
|
72
72
|
return self?.defaults?.[accessor_name];
|
|
73
|
-
|
|
73
|
+
}
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
},
|
|
@@ -113,14 +113,140 @@ const def_json_accessor = (self,attr)=>{
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
})
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const load_script = (src, libName) => {
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const script = document.createElement('script');
|
|
121
|
+
script.src = src;
|
|
122
|
+
script.onload = () => resolve(true);
|
|
123
|
+
script.onerror = () => resolve(false);
|
|
124
|
+
script.async = true;
|
|
125
|
+
document.head.appendChild(script);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const is_script_loaded = (impt_name) =>{return Boolean(window[impt_name]!== undefined);}
|
|
129
|
+
|
|
130
|
+
const set_style_prop_cb = (self,attr,type,val,accessor_name,extra={})=>{
|
|
131
|
+
if(type==="num") {
|
|
132
|
+
let v = Number(val);
|
|
133
|
+
if(!isNaN(v)) {
|
|
134
|
+
self.style.setProperty(`--${attr}`, String(v)+'px');
|
|
135
|
+
} else {
|
|
136
|
+
let rgx = /^([0-9]+[\.]?[0-9]*)([a-zA-Z]+|%)$/;
|
|
137
|
+
if(rgx.test(String(v))) {
|
|
138
|
+
self.style.setProperty(`--${attr}`, String(v));
|
|
139
|
+
} else {
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
self.style.setProperty(`--${attr}`, v);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const load_script_cb = async (self,attr,type,val,accessor_name,extra)=>{
|
|
148
|
+
let {impt_name} = extra;
|
|
149
|
+
if(is_script_loaded(impt_name)) {
|
|
150
|
+
return [true,window[impt_name]];
|
|
151
|
+
} else {
|
|
152
|
+
let cond = await load_script(val,impt_name);
|
|
153
|
+
return cond?[true,window[impt_name]]:[false,impt_name,self[accessor_name]]
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const load_all_libs = async(self,ATTRS)=>{
|
|
158
|
+
let ps = [];
|
|
159
|
+
for(let attr in ATTRS) {
|
|
160
|
+
let d = ATTRS[attr];
|
|
161
|
+
if(d.action==="load_script") {
|
|
162
|
+
let impt_name = d.extra.impt_name;
|
|
163
|
+
let accessor_name = d.accessor_name;
|
|
164
|
+
if(is_script_loaded(impt_name)) {
|
|
165
|
+
ps.push([true,window[impt_name]]);
|
|
166
|
+
} else {
|
|
167
|
+
ps.push(
|
|
168
|
+
load_script(self[accessor_name],impt_name).then(cond=>{
|
|
169
|
+
return cond?[true,window[impt_name]]:[false,impt_name,self[accessor_name]];
|
|
170
|
+
}).catch(e=>[false,impt_name,self[accessor_name]])
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return await Promise.all(ps);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const DFLT_ACTS = {
|
|
180
|
+
none: (self,attr,type,val,accessor_name,extra={})=>{},
|
|
181
|
+
auto: (self,attr,type,val,accessor_name,extra={})=>{}, // done by css
|
|
182
|
+
extn: (self,attr,type,val,accessor_name,extra={})=>{}, // 外部组件自己控制
|
|
183
|
+
set_style_prop : set_style_prop_cb,
|
|
184
|
+
load_script : load_script_cb
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const init_dflts = (self,ATTRS)=>{
|
|
188
|
+
for(let attr in ATTRS) {
|
|
189
|
+
let accessor_name = attr_to_accessor_name(attr);
|
|
190
|
+
let cfg = ATTRS[attr];
|
|
191
|
+
if(cfg.type === "bool") {
|
|
192
|
+
def_bool_accessor(self,attr);
|
|
193
|
+
self.defaults[accessor_name] = cfg.dflt;
|
|
194
|
+
} else if(cfg.type==="json") {
|
|
195
|
+
def_json_accessor(self,attr);
|
|
196
|
+
self.defaults[accessor_name] = cfg.dflt;
|
|
197
|
+
} else if(cfg.type === "num") {
|
|
198
|
+
def_num_accessor(self,attr);
|
|
199
|
+
self.defaults[accessor_name] = cfg.dflt;
|
|
200
|
+
} else {
|
|
201
|
+
def_str_accessor(self,attr);
|
|
202
|
+
self.defaults[accessor_name] = cfg.dflt;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
116
205
|
}
|
|
117
206
|
|
|
207
|
+
const exec_when_attr_change = async (ATTRS,ACTIONS,self,attr,val)=>{
|
|
208
|
+
let {action,type,accessor_name,extra} = ATTRS[attr];
|
|
209
|
+
if(typeof(action) === "string") {
|
|
210
|
+
return ACTIONS[action](self,attr,type,val,accessor_name,extra);
|
|
211
|
+
} else if(typeof(action)=== "function") {
|
|
212
|
+
return action(self,attr,type,val,accessor_name,extra);
|
|
213
|
+
} else {
|
|
214
|
+
}
|
|
215
|
+
}
|
|
118
216
|
|
|
217
|
+
module.exports = (ACTIONS,ATTRS)=>{
|
|
218
|
+
let ACTS = creat_actions();
|
|
219
|
+
for(let k in DFLT_ACTS) {
|
|
220
|
+
if(ACTIONS[k] === undefined) {
|
|
221
|
+
ACTIONS[k] = DFLT_ACTS[k]
|
|
222
|
+
} else {}
|
|
223
|
+
}
|
|
224
|
+
for(let attr in ATTRS) {
|
|
225
|
+
ATTRS[attr].attr = attr
|
|
226
|
+
ATTRS[attr].accessor_name = attr_to_accessor_name(attr);
|
|
227
|
+
if(ATTRS[attr].extra) {} else {ATTRS[attr].extra={}}
|
|
228
|
+
}
|
|
229
|
+
////
|
|
230
|
+
let init_dflts_and_rtrn_attr_change_executor = (self)=>{
|
|
231
|
+
init_dflts(self,ATTRS);
|
|
232
|
+
return (attr,val)=>exec_when_attr_change(ACTIONS,ATTRS,self,attr,val);
|
|
233
|
+
}
|
|
234
|
+
////
|
|
235
|
+
return ({
|
|
236
|
+
init_dflts_and_rtrn_attr_change_executor,
|
|
237
|
+
creat_load_libs : (self)=>()=>load_all_libs(self,ATTRS)
|
|
238
|
+
})
|
|
239
|
+
}
|
|
119
240
|
|
|
120
|
-
|
|
241
|
+
[
|
|
121
242
|
attr_to_accessor_name,
|
|
122
243
|
def_bool_accessor,
|
|
123
244
|
def_str_accessor,
|
|
124
245
|
def_num_accessor,
|
|
125
|
-
def_json_accessor
|
|
126
|
-
|
|
246
|
+
def_json_accessor,
|
|
247
|
+
is_script_loaded,
|
|
248
|
+
load_script,
|
|
249
|
+
init_dflts,
|
|
250
|
+
exec_when_attr_change,
|
|
251
|
+
load_all_libs
|
|
252
|
+
].forEach(r=>{module.exports[r.name]=r});
|