保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use strict';
 
var cssSelect = require('css-select');
 
var svgoCssSelectAdapter = require('./css-select-adapter');
var cssSelectOpts = {
  xmlMode: true,
  adapter: svgoCssSelectAdapter
};
 
var JSAPI = module.exports = function(data, parentNode) {
    Object.assign(this, data);
    if (parentNode) {
        Object.defineProperty(this, 'parentNode', {
            writable: true,
            value: parentNode
        });
    }
};
 
/**
 * Perform a deep clone of this node.
 *
 * @return {Object} element
 */
JSAPI.prototype.clone = function() {
    var node = this;
    var nodeData = {};
 
    Object.keys(node).forEach(function(key) {
        if (key !== 'class' && key !== 'style' && key !== 'content') {
            nodeData[key] = node[key];
        }
    });
 
    // Deep-clone node data.
    nodeData = JSON.parse(JSON.stringify(nodeData));
 
    // parentNode gets set to a proper object by the parent clone,
    // but it needs to be true/false now to do the right thing
    // in the constructor.
    var clonedNode = new JSAPI(nodeData, !!node.parentNode);
 
    if (node.class) {
        clonedNode.class = node.class.clone(clonedNode);
    }
    if (node.style) {
        clonedNode.style = node.style.clone(clonedNode);
    }
    if (node.content) {
        clonedNode.content = node.content.map(function(childNode) {
            var clonedChild = childNode.clone();
            clonedChild.parentNode = clonedNode;
            return clonedChild;
        });
    }
 
    return clonedNode;
};
 
/**
 * Determine if item is an element
 * (any, with a specific name or in a names array).
 *
 * @param {String|Array} [param] element name or names arrays
 * @return {Boolean}
 */
JSAPI.prototype.isElem = function(param) {
 
    if (!param) return !!this.elem;
 
    if (Array.isArray(param)) return !!this.elem && (param.indexOf(this.elem) > -1);
 
    return !!this.elem && this.elem === param;
 
};
 
/**
 * Renames an element
 *
 * @param {String} name new element name
 * @return {Object} element
 */
JSAPI.prototype.renameElem = function(name) {
 
    if (name && typeof name === 'string')
        this.elem = this.local = name;
 
    return this;
 
};
 
/**
 * Determine if element is empty.
 *
 * @return {Boolean}
 */
 JSAPI.prototype.isEmpty = function() {
 
    return !this.content || !this.content.length;
 
};
 
/**
 * Find the closest ancestor of the current element.
 * @param elemName
 *
 * @return {?Object}
 */
 JSAPI.prototype.closestElem = function(elemName) {
    var elem = this;
 
    while ((elem = elem.parentNode) && !elem.isElem(elemName));
 
    return elem;
};
 
/**
 * Changes content by removing elements and/or adding new elements.
 *
 * @param {Number} start Index at which to start changing the content.
 * @param {Number} n Number of elements to remove.
 * @param {Array|Object} [insertion] Elements to add to the content.
 * @return {Array} Removed elements.
 */
 JSAPI.prototype.spliceContent = function(start, n, insertion) {
 
    if (arguments.length < 2) return [];
 
    if (!Array.isArray(insertion))
        insertion = Array.apply(null, arguments).slice(2);
 
    insertion.forEach(function(inner) { inner.parentNode = this }, this);
 
    return this.content.splice.apply(this.content, [start, n].concat(insertion));
 
 
};
 
/**
 * Determine if element has an attribute
 * (any, or by name or by name + value).
 *
 * @param {String} [name] attribute name
 * @param {String} [val] attribute value (will be toString()'ed)
 * @return {Boolean}
 */
 JSAPI.prototype.hasAttr = function(name, val) {
 
    if (!this.attrs || !Object.keys(this.attrs).length) return false;
 
    if (!arguments.length) return !!this.attrs;
 
    if (val !== undefined) return !!this.attrs[name] && this.attrs[name].value === val.toString();
 
    return !!this.attrs[name];
 
};
 
/**
 * Determine if element has an attribute by local name
 * (any, or by name or by name + value).
 *
 * @param {String} [localName] local attribute name
 * @param {Number|String|RegExp|Function} [val] attribute value (will be toString()'ed or executed, otherwise ignored)
 * @return {Boolean}
 */
 JSAPI.prototype.hasAttrLocal = function(localName, val) {
 
    if (!this.attrs || !Object.keys(this.attrs).length) return false;
 
    if (!arguments.length) return !!this.attrs;
 
    var callback;
 
    switch (val != null && val.constructor && val.constructor.name) {
        case 'Number':   // same as String
        case 'String':   callback = stringValueTest; break;
        case 'RegExp':   callback = regexpValueTest; break;
        case 'Function': callback = funcValueTest; break;
        default:         callback = nameTest;
    }
    return this.someAttr(callback);
 
    function nameTest(attr) {
        return attr.local === localName;
    }
 
    function stringValueTest(attr) {
        return attr.local === localName && val == attr.value;
    }
 
    function regexpValueTest(attr) {
        return attr.local === localName && val.test(attr.value);
    }
 
    function funcValueTest(attr) {
        return attr.local === localName && val(attr.value);
    }
 
};
 
/**
 * Get a specific attribute from an element
 * (by name or name + value).
 *
 * @param {String} name attribute name
 * @param {String} [val] attribute value (will be toString()'ed)
 * @return {Object|Undefined}
 */
 JSAPI.prototype.attr = function(name, val) {
 
    if (!this.hasAttr() || !arguments.length) return undefined;
 
    if (val !== undefined) return this.hasAttr(name, val) ? this.attrs[name] : undefined;
 
    return this.attrs[name];
 
};
 
/**
 * Get computed attribute value from an element
 *
 * @param {String} name attribute name
 * @return {Object|Undefined}
 */
 JSAPI.prototype.computedAttr = function(name, val) {
    /* jshint eqnull: true */
    if (!arguments.length) return;
 
    for (var elem = this; elem && (!elem.hasAttr(name) || !elem.attr(name).value); elem = elem.parentNode);
 
    if (val != null) {
        return elem ? elem.hasAttr(name, val) : false;
    } else if (elem && elem.hasAttr(name)) {
        return elem.attrs[name].value;
    }
 
};
 
/**
 * Remove a specific attribute.
 *
 * @param {String|Array} name attribute name
 * @param {String} [val] attribute value
 * @return {Boolean}
 */
 JSAPI.prototype.removeAttr = function(name, val, recursive) {
 
    if (!arguments.length) return false;
 
    if (Array.isArray(name)) {
        name.forEach(this.removeAttr, this);
        return false;
    }
 
    if (!this.hasAttr(name)) return false;
 
    if (!recursive && val && this.attrs[name].value !== val) return false;
 
    delete this.attrs[name];
 
    if (!Object.keys(this.attrs).length) delete this.attrs;
 
    return true;
 
};
 
/**
 * Add attribute.
 *
 * @param {Object} [attr={}] attribute object
 * @return {Object|Boolean} created attribute or false if no attr was passed in
 */
 JSAPI.prototype.addAttr = function(attr) {
    attr = attr || {};
 
    if (attr.name === undefined ||
        attr.prefix === undefined ||
        attr.local === undefined
    ) return false;
 
    this.attrs = this.attrs || {};
    this.attrs[attr.name] = attr;
 
    if(attr.name === 'class') { // newly added class attribute
        this.class.hasClass();
    }
 
    if(attr.name === 'style') { // newly added style attribute
        this.style.hasStyle();
    }
 
    return this.attrs[attr.name];
 
};
 
/**
 * Iterates over all attributes.
 *
 * @param {Function} callback callback
 * @param {Object} [context] callback context
 * @return {Boolean} false if there are no any attributes
 */
 JSAPI.prototype.eachAttr = function(callback, context) {
 
    if (!this.hasAttr()) return false;
 
    for (var name in this.attrs) {
        callback.call(context, this.attrs[name]);
    }
 
    return true;
 
};
 
/**
 * Tests whether some attribute passes the test.
 *
 * @param {Function} callback callback
 * @param {Object} [context] callback context
 * @return {Boolean} false if there are no any attributes
 */
 JSAPI.prototype.someAttr = function(callback, context) {
 
    if (!this.hasAttr()) return false;
 
    for (var name in this.attrs) {
        if (callback.call(context, this.attrs[name])) return true;
    }
 
    return false;
 
};
 
/**
 * Evaluate a string of CSS selectors against the element and returns matched elements.
 *
 * @param {String} selectors CSS selector(s) string
 * @return {Array} null if no elements matched
 */
 JSAPI.prototype.querySelectorAll = function(selectors) {
 
   var matchedEls = cssSelect(selectors, this, cssSelectOpts);
 
   return matchedEls.length > 0 ? matchedEls : null;
 
};
 
/**
 * Evaluate a string of CSS selectors against the element and returns only the first matched element.
 *
 * @param {String} selectors CSS selector(s) string
 * @return {Array} null if no element matched
 */
 JSAPI.prototype.querySelector = function(selectors) {
 
   return cssSelect.selectOne(selectors, this, cssSelectOpts);
 
};
 
/**
 * Test if a selector matches a given element.
 *
 * @param {String} selector CSS selector string
 * @return {Boolean} true if element would be selected by selector string, false if it does not
 */
 JSAPI.prototype.matches = function(selector) {
 
   return cssSelect.is(this, selector, cssSelectOpts);
 
};