• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jclo / rview / 10808978281

11 Sep 2024 09:32AM UTC coverage: 62.305% (-0.02%) from 62.329%
10808978281

push

github

jclo
Fixed a few typos.

115 of 115 branches covered (100.0%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

2485 of 4058 relevant lines covered (61.24%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.43
/src/component/generic.js
1
/** ************************************************************************
1✔
2
 *
1✔
3
 * Defines the generic component. All the created components extend this
1✔
4
 * component.
1✔
5
 *
1✔
6
 * generic.js is built upon the Prototypal Instantiation pattern. It
1✔
7
 * returns an object by calling its constructor. It doesn't use the new
1✔
8
 * keyword.
3✔
9
 *
1✔
10
 * Private Functions:
1✔
11
 *  . none,
1✔
12
 *
1✔
13
 *
1✔
14
 * Constructor:
1✔
15
 *  . Construct                   constructor,
1✔
16
 *
3✔
17
 *
3✔
18
 * Private Variables
1✔
19
 *  . _tag                        constains the tag string of the component,
1✔
20
 *  . _cList                      contains the list of children objects,
1✔
21
 *  . _mess                       the messenger object,
1✔
22
 *
3✔
23
 * Private Methods:
1✔
24
 *  . _intInit                    checks if it needs to use $init or init method,
1✔
25
 *  . _intEvents                  applies the deprecated events method,
1✔
26
 *  . _intListen                  checks if it needs to use $listenDOM or listen method,
1✔
27
 *  . _intPostRender              checks if it needs to use $postRender or postRender method,
1✔
28
 *  . _intOnChange                checks if it needs to use $onChange or onChange method,
1✔
29
 *  . _intRender                  checks if it needs to use $render or render method,
1✔
30
 *  . _init                       executes the private init when the comp. is created,
1✔
31
 *  . _renderer                   renders the component an its children, return XML,
1✔
32
 *  . _render                     returns data returned by the public method render,
1✔
33
 *
1✔
34
 *
1✔
35
 * Public Variables:
1✔
36
 *  . id                          unique id of the component (read only),
1✔
37
 *  . children                    list of the children components,
1✔
38
 *  . state                       component state properties,
1✔
39
 *  . props                       component properties,
1✔
40
 *  . name                        name of the component,
1✔
41
 *
1✔
42
 *
1✔
43
 * Public Methods:
1✔
44
 *  . $                           returns an object to manipulate the comp. in the DOM,
1✔
45
 *  . $animate                    animates the component,
1✔
46
 *  . $abortAnimation             aborts the running animation,
1✔
47
 *  . $append                     appends a component as the last child,
1✔
48
 *  . $getChild                   returns a component object,
1✔
49
 *  . $removeChild                removes a child,
1✔
50
 *  . $getChildren                returns the list of the children,
1✔
51
 *  . $getIdAndName               returns the component's Id and name,
1✔
52
 *  . $hyperscript                returns an XML string of the hyperscript template,
1✔
53
 *  . $setState                   updates state value(s),
1✔
54
 *  . $listen                     listens a message,
1✔
55
 *  . $emit                       sends a message,
1✔
56
 *
1✔
57
 *
1✔
58
 * Empty Public Methods:
1✔
59
 *  . $init                       executes the public initializations,
1✔
60
 *  . events                      processes the DOM events (to be phased out),
1✔
61
 *  . $listenDOM                  listens for DOM events,
1✔
62
 *  . $postRender                 performs operations after component added to DOM,
1✔
63
 *  . $onChange                   performs operations after component updated in DOM,
1✔
64
 *  . $render                     returns the component XML string,
1✔
65
 *
1✔
66
 *
1✔
67
 *
1✔
68
 * @namespace    -
1✔
69
 * @dependencies none
1✔
70
 * @exports      -
1✔
71
 * @author       -
1✔
72
 * @since        0.0.0
1✔
73
 * @version      -
1✔
74
 * ********************************************************************** */
1✔
75
/* global */
1✔
76
/* eslint-disable one-var, semi-style, no-underscore-dangle */
1✔
77

1✔
78

1✔
79
// -- Vendor Modules
1✔
80
import KZlog from '@mobilabs/kzlog';
1✔
81

1✔
82

1✔
83
// -- Local Modules
1✔
84
import _ from '../lib/_';
1✔
85
import A from './animate';
1✔
86
import AD from './add';
1✔
87
import R from './render';
1✔
88
import H from './hyperscript';
1✔
89
import S from './setstate';
1✔
90
import Util from './util';
1✔
91
import C from './config';
1✔
92

1✔
93

1✔
94
// -- Local Constants
1✔
95
const { level } = C.logger
1✔
96
    , log       = KZlog('RView', level, false)
1✔
97
    ;
1✔
98

1✔
99

1✔
100
// -- Local Variables
1✔
101

1✔
102

1✔
103
// -- Public ---------------------------------------------------------------
1✔
104

1✔
105
/**
1✔
106
 * Defines the Generic Component constructor.
1✔
107
 *
1✔
108
 * @constructor (...args)
1✔
109
 * @public
1✔
110
 * @param {}                -,
1✔
111
 * @returns {}              -,
1✔
112
 * @since 0.0.0
1✔
113
 */
1✔
114
function Construct() {
1✔
115
  /* eslint-disable-next-line prefer-spread, prefer-rest-params */
1✔
116
  this._init.apply(this, arguments);
1✔
117
}
1✔
118

1✔
119

1✔
120
const methods = {
1✔
121

1✔
122

1✔
123
  // -- Private Methods ----------------------------------------------------
1✔
124

1✔
125
  /**
1✔
126
   * Checks if it needs to use $init or the deprecated init method.
1✔
127
   *
1✔
128
   * @method ()
1✔
129
   * @private
1✔
130
   * @param {}              -,
1✔
131
   * @returns {}            -,
1✔
132
   * @since 0.0.0
1✔
133
   */
1✔
134
  _intInit() {
1✔
135
    if (!/^init\(\)[^{]+\{\s*\}/m.test(this.init.toString())
1✔
136
    ) {
1✔
137
      log.warn('init method is deprecated, use $init instead!');
1✔
138
      this.init();
×
139
      return;
×
140
    }
×
141
    this.$init();
×
142
  },
×
143

×
144
  /**
1✔
145
   * Applies the deprecated events method.
1✔
146
   *
1✔
147
   * @method ()
1✔
148
   * @private
1✔
149
   * @param {}              -,
1✔
150
   * @returns {}            -,
1✔
151
   * @since 0.0.0
1✔
152
   */
1✔
153
  _intEvents() {
1✔
154
    if (!/^events\(\)[^{]+\{\s*\}/m.test(this.events.toString())) {
1✔
155
      log.warn('events method is deprecated, use $listenDOM or $postRender instead!');
1✔
156
      this.events();
×
157
    }
×
158
  },
×
159

×
160
  /**
×
161
   * Checks if it needs to use $listenDOM or the deprecated listen method.
×
162
   *
×
163
   * @method ()
×
164
   * @private
×
165
   * @param {}              -,
1✔
166
   * @returns {}            -,
1✔
167
   * @since 0.0.0
1✔
168
   */
1✔
169
  _intListen() {
1✔
170
    if (!/^listen\(\)[^{]+\{\s*\}/m.test(this.listen.toString())) {
1✔
171
      log.warn('listen method is deprecated, use $listenDOM instead!');
1✔
172
      this.listen();
×
173
      return;
×
174
    }
×
175
    this.$listenDOM();
×
176
  },
×
177

×
178
  /**
×
179
   * Checks if it needs to use $postRender or the deprecated postRender method.
1✔
180
   *
1✔
181
   * @method ()
1✔
182
   * @private
1✔
183
   * @param {}              -,
1✔
184
   * @returns {}            -,
1✔
185
   * @since 0.0.0
1✔
186
   */
1✔
187
  _intPostRender() {
1✔
188
    if (!/^postRender\(\)[^{]+\{\s*\}/m.test(this.postRender.toString())) {
1✔
189
      log.warn('postRender method is deprecated, use $postRender instead!');
×
190
      this.postRender();
×
191
      return;
×
192
    }
×
193
    this.$postRender();
×
194
  },
×
195

×
196
  /**
×
197
   * Checks if it needs to use $onChange or the deprecated onChange method.
1✔
198
   *
1✔
199
   * @method ()
1✔
200
   * @private
1✔
201
   * @param {}              -,
1✔
202
   * @returns {}            -,
1✔
203
   * @since 0.0.0
1✔
204
   */
1✔
205
  _intOnChange() {
1✔
206
    if (!/^onChange\(\)[^{]+\{\s*\}/m.test(this.onChange.toString())) {
1✔
207
      log.warn('onChange method is deprecated, use $onChange instead!');
×
208
      this.onChange();
×
209
      return;
×
210
    }
×
211
    this.$onChange();
×
212
  },
×
213

×
214
  /**
×
215
   * Checks if it needs to use $onChange or the deprecated onChange method.
1✔
216
   *
1✔
217
   * @method (...args)
1✔
218
   * @private
1✔
219
   * @param {...}           the arguments,
1✔
220
   * @returns {XMLString}   returns the rendered data,
1✔
221
   * @since 0.0.0
1✔
222
   */
1✔
223
  _intRender(...args) {
1✔
224
    if (!/^render\((.*)\)[^{]+\{\s*\}/m.test(this.render.toString())) {
1✔
225
      log.warn('render method is deprecated, use $render instead!');
×
226
      return this.render(...args);
×
227
    }
×
228
    return this.$render(...args);
×
229
  },
×
230

×
231
  /**
×
232
   * Does the initializations when the component is created.
1✔
233
   *
1✔
234
   * @method (arg1)
1✔
235
   * @private
1✔
236
   * @param {Object}        the options,
1✔
237
   * @returns {Object}      returns this,
1✔
238
   * @since 0.0.0
1✔
239
   */
1✔
240
  _init(...args) {
1✔
241
    // init private:
1✔
242
    this._tag = null;
1✔
243
    this._cList = null;
×
244
    this._mess = null;
×
245

×
246
    // init public:
×
247
    // Creates an unique id for this component:
×
248
    // this.id = `i${Math.random().toString(36).substr(2, 7)}`;
×
249
    this.id = _.makeid(C.idLength);
×
250
    this.children = null;
1✔
251

1✔
252
    const [state, props] = args;
1✔
253
    this.state = _.isLiteralObject(state) ? state : {};
1✔
254
    this.props = _.isLiteralObject(props) ? props : {};
1✔
255
    this.name = 'mynameisnobody';
1✔
256

1✔
257
    // Call the public init:
1✔
258
    this._intInit();
×
259
    return this;
×
260
  },
×
261

×
262
  /**
×
263
   * Renders the component and returns its XMLString.
×
264
   * (must not be overwritten)
×
265
   *
×
266
   * @method ()
×
267
   * @public
×
268
   * @param {}              -,
×
269
   * @returns {XMLString}   returns the component's XMLString,
×
270
   * @since 0.0.0
×
271
   */
×
272
  _renderer() {
×
273
    return R.render(this);
×
274
  },
×
275

×
276
  /**
×
277
   * Returns data returned by the public method render.
×
278
   * (must not be overwritten)
×
279
   *
×
280
   * @method ([arg1], [arg2])
×
281
   * @public
×
282
   * @param {Object}        the state properties,
×
283
   * @param {Object}        the optional properties,
1✔
284
   * @returns {XMLString}   returns the component's XMLString,
1✔
285
   * @since 0.0.0
1✔
286
   */
1✔
287
  _render(...args) {
1✔
288
    /*
1✔
289
    xml = xml.replace(/<!--(.*?)-->/g, '')    // remove comments
1✔
290
      .replace(/\n\s+</g, '\n<')              // remove leading spaces before a tag,
1✔
291
      .replace(/\n<\/div>/g, '</div>')        // remove unwanted `\n`,
1✔
292
      .replace(/\n<\/ul>/g, '</ul>')          // -
1✔
293
      .replace(/\n<\/li>/g, '</li>')          // -
1✔
294
      .replace(/\n<\/a>/g, '</a>')            // -
1✔
295
    ;
1✔
296
    */
1✔
297
    const xml = this._intRender(...args);
1✔
298
    if (_.isString(xml)) {
1✔
299
      return xml.trim();
1✔
300
    }
1✔
301
    return xml;
×
302
  },
×
303

×
304

×
305
  // -- Defined Public Methods ---------------------------------------------
×
306

×
307
  /**
×
308
   * Returns an object to manipulate the component in the DOM.
×
309
   * (must not be overwritten - see implementation in $.js)
×
310
   *
×
311
   * Nota: $ is filled when the View.Component is created
×
312
   * (see main.js constructor).
×
313
   *
×
314
   * @method (arg1)
×
315
   * @public
×
316
   * @param {String}        the node selector (id or class),
×
317
   * @returns {Object}      returns the $ object,
×
318
   * @since 0.0.0
×
319
   */
×
320
  $: null,
×
321

×
322
  /**
×
323
   * Animates the component.
1✔
324
   * (must not be overwritten)
1✔
325
   *
1✔
326
   * @method (...args)
1✔
327
   * @public
1✔
328
   * @param {...args}       properties [, duration ] [, easing ] [, callback ],
1✔
329
   * @returns {Object}      returns this,
1✔
330
   * @since 0.0.0
1✔
331
   */
1✔
332
  $animate(...args) {
1✔
333
    [this._anim_timer, this._anim_callback] = A.animate(this, ...args);
1✔
334
    return this;
1✔
335
  },
1✔
336

1✔
337
  /**
1✔
338
   * Aborts the running animation.
1✔
339
   * (must not be overwritten)
1✔
340
   *
1✔
341
   * @method ()
1✔
342
   * @public
1✔
343
   * @param {}              -,
1✔
344
   * @returns {Object}      returns this,
1✔
345
   * @since 0.0.0
1✔
346
   */
1✔
347
  $abortAnimation() {
1✔
348
    A.abortAnimation(this._anim_timer, this._anim_callback);
1✔
349
    return this;
1✔
350
  },
1✔
351

1✔
352
  /**
1✔
353
   * Appends a component as the last child to the selected component.
1✔
354
   * (must not be overwritten)
1✔
355
   *
×
356
   * @method (...args)
×
357
   * @public
×
358
   * @param {...args}       tag, component, [state], [props]
1✔
359
   * @returns {Object}      returns this,
1✔
360
   * @since 0.0.0
1✔
361
   */
1✔
362
  $append(...args) {
1✔
363
    AD.append(this, ...args);
1✔
364
    return this;
1✔
365
  },
1✔
366

1✔
367
  /**
1✔
368
   * Returns a component object.
1✔
369
   * (must not be overwritten)
1✔
370
   *
×
371
   * Nota:
×
372
   * the algorithm tests 'ident' in this order: tag, id, name. It
×
373
   * explores by parsing entirely the first child branch before parsing the
1✔
374
   * second child branch. I stops as soon as there is a match.
1✔
375
   *
1✔
376
   * So, if a child in the second child branch as the same tag or name as a
1✔
377
   * child in the first child branch, the algorithm returns the child in the
1✔
378
   * first branch only. The matching child on the second branch won't never be
1✔
379
   * retrieved.
1✔
380
   *
1✔
381
   * So, avoid duplicating tag or name.
1✔
382
   *
1✔
383
   * @method (arg1)
1✔
384
   * @public
1✔
385
   * @param {String}        the component identity (could be tag, id or name),
1✔
386
   * @returns {Object}      returns the component object or null,
1✔
387
   * @since 0.0.0
1✔
388
   */
1✔
389
  $getChild(ident) {
1✔
390
    return Util.getChild(this, ident);
1✔
391
  },
1✔
392

1✔
393
  /**
1✔
394
   * Remove a component's child.
1✔
395
   * (must not be overwritten)
1✔
396
   *
1✔
397
   * @method (arg1)
1✔
398
   * @public
1✔
399
   * @param {String}        the component identity (could be tag, id or name),
1✔
400
   * @returns {Boolean}     returns true or false,
1✔
401
   * @since 0.0.0
1✔
402
   */
1✔
403
  $removeChild(ident) {
1✔
404
    return Util.removeChild(this, ident);
1✔
405
  },
1✔
406

1✔
407
  /**
1✔
408
   * Returns the list of children.
1✔
409
   * (must not be overwritten)
1✔
410
   *
1✔
411
   * @method ()
1✔
412
   * @public
1✔
413
   * @param {}              -,
1✔
414
   * @returns {Array}       returns the children list or null,
1✔
415
   * @since 0.0.0
1✔
416
   */
1✔
417
  $getChildren() {
1✔
418
    return Util.getChildren(this);
1✔
419
  },
1✔
420

1✔
421
  /**
1✔
422
   * Returns the component's Id and name.
1✔
423
   * (must not be overwritten)
1✔
424
   *
1✔
425
   * @method ()
1✔
426
   * @public
1✔
427
   * @param {}              -,
1✔
428
   * @returns {Object}      returns the component's id and name,
1✔
429
   * @since 0.0.0
1✔
430
   */
1✔
431
  $getIdAndName() {
1✔
432
    return { id: this.id, name: this.name };
1✔
433
  },
1✔
434

1✔
435
  /**
1✔
436
   * Returns an XMLString representation of the hyperscript template.
1✔
437
   * (must not be overwritten)
1✔
438
   *
1✔
439
   * @method (...args)
1✔
440
   * @public
1✔
441
   * @param {...}           arguments like { tag, attributes, value },
1✔
442
   * @returns {Object}      returns an object containing the node, its attributes
1✔
443
   * @since 0.0.0           and the children,
1✔
444
   */
1✔
445
  $hyperscript(...args) {
1✔
446
    return H.format(...args);
1✔
447
  },
1✔
448

1✔
449
  /**
1✔
450
   * Updates state value(s).
1✔
451
   * (must not be overwritten)
×
452
   *
×
453
   * @method (arg1)
×
454
   * @public
1✔
455
   * @param {Object}        the state object,
1✔
456
   * @returns {Object}      returns this,
1✔
457
   * @since 0.0.0
1✔
458
   */
1✔
459
  $setState(params) {
1✔
460
    if (_.isLiteralObject(params)) {
1✔
461
      S.setState(this, params);
1✔
462
    }
1✔
463
    return this;
1✔
464
  },
1✔
465

1✔
466
  /**
1✔
467
   * Listens for a message from another component.
1✔
468
   * (must not be overwritten)
1✔
469
   *
1✔
470
   * @method (arg1, arg2)
1✔
471
   * @public
×
472
   * @param {String}        the event to listen,
1✔
473
   * @param {Function}      the listener to attach to this event,
1✔
474
   * @returns {Object}      returns this,
1✔
475
   * @since 0.0.0
1✔
476
   */
1✔
477
  $listen(event, listener) {
1✔
478
    if (this._mess) {
1✔
479
      this._mess.subscribe(event, listener);
1✔
480
      return this;
×
481
    }
×
482
    /* eslint-disable-next-line no-console */
×
483
    console.log('$listen: the plugin Messenger is not installed!');
1✔
484
    return this;
1✔
485
  },
1✔
486

1✔
487
  /**
1✔
488
   * sends for a message to another component.
1✔
489
   * (must not be overwritten)
1✔
490
   *
1✔
491
   * @method (arg1, arg2)
1✔
492
   * @public
1✔
493
   * @param {String}        the event to listen,
1✔
494
   * @param {Object}        the payload to send,
1✔
495
   * @returns {Object}      returns this,
1✔
496
   * @since 0.0.0
×
497
   */
×
498
  $emit(event, payload) {
×
499
    if (this._mess) {
×
500
      this._mess.publish(event, payload);
×
501
      return this;
×
502
    }
×
503
    /* eslint-disable-next-line no-console */
×
504
    console.log('$emit: the plugin Messenger is not installed!');
1✔
505
    return this;
1✔
506
  },
1✔
507

1✔
508

1✔
509
  // -- Empty Public Methods -----------------------------------------------
1✔
510

1✔
511
  /**
1✔
512
   * Does the initializations when the component is created.
1✔
513
   * (could be overwritten)
1✔
514
   *
1✔
515
   * Nota:
1✔
516
   * 'props.options' are initialized when the component is instantiated. Be
1✔
517
   * careful not to overwrite it.
×
518
   *
×
519
   * @method ()
×
520
   * @public
×
521
   * @param {}              -,
×
522
   * @returns {Object}      returns this,
×
523
   * @since 0.0.0
×
524
   */
×
525
  init() {},
×
526
  $init() {
×
527
    return this;
×
528
  },
×
529

×
530
  /**
×
531
   * Processes the DOM events.
1✔
532
   * (could be overwritten)
1✔
533
   *
1✔
534
   * @method ()
1✔
535
   * @public
1✔
536
   * @param {}              -,
1✔
537
   * @returns {Object}      returns this,
1✔
538
   * @since 0.0.0
1✔
539
   */
1✔
540
  events() {},
1✔
541

1✔
542
  /**
1✔
543
   * Listens for DOM events.
1✔
544
   * (could be overwritten)
1✔
545
   *
1✔
546
   * Nota:
1✔
547
   * This method is called after the component
1✔
548
   * has been attached to the DOM. It must be used
1✔
549
   * to listen to the DOM events generated by the component.
1✔
550
   *
1✔
551
   * @method ()
1✔
552
   * @public
1✔
553
   * @param {}              -,
1✔
554
   * @returns {Object}      returns this,
1✔
555
   * @since 0.0.0
1✔
556
   */
1✔
557
  listen() {},
1✔
558
  $listenDOM() {
1✔
559
    return this;
1✔
560
  },
1✔
561

1✔
562
  /**
1✔
563
   * Performs operations after the component is added to the DOM.
1✔
564
   * (could be overwritten)
1✔
565
   *
1✔
566
   * Nota:
1✔
567
   * This method is called after the component
1✔
568
   * has been attached to the DOM. It must be used
1✔
569
   * to perform a post-rendering process.
1✔
570
   *
1✔
571
   * @method ()
1✔
572
   * @public
1✔
573
   * @param {}              -,
1✔
574
   * @returns {Object}      returns this,
1✔
575
   * @since 0.0.0
1✔
576
   */
1✔
577
  postRender() {},
1✔
578
  $postRender() {
1✔
579
    return this;
1✔
580
  },
1✔
581

1✔
582
  /**
1✔
583
   * Performs operations after the component in the DOM has been updated.
1✔
584
   * (could be overwritten)
1✔
585
   *
1✔
586
   * Nota:
1✔
587
   * This method is called after the component, already
1✔
588
   * in the DOM, is updated through a $setState. It must be used
1✔
589
   * to perform a post-rendering process after each update.
1✔
590
   *
1✔
591
   * @method ()
1✔
592
   * @public
1✔
593
   * @param {}              -,
1✔
594
   * @returns {Object}      returns this,
1✔
595
   * @since 0.0.0
1✔
596
   */
1✔
597
  onChange() {},
1✔
598
  $onChange() {
1✔
599
    return this;
1✔
UNCOV
600
  },
×
601

×
602
  /**
×
603
   * Returns an XMLString.
1✔
604
   * (could be overwritten)
1✔
605
   *
1✔
606
   * @method ()
1✔
607
   * @public
1✔
608
   * @param {}              -,
1✔
609
   * @returns {String}      returns XMLString,
1✔
610
   * @since 0.0.0
1✔
611
   */
1✔
612
  render() {},
1✔
613
  $render() {
1✔
614
    return '<div></div>';
1✔
615
  },
1✔
616
};
1✔
617

1✔
618

1✔
619
// -- Export
1✔
620
export default { Construct, methods };
1✔
621

1✔
622
/* eslint-enable one-var, semi-style, no-underscore-dangle */
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc