@jamesrock/rockjs 1.25.0 → 1.26.0

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.
Files changed (2) hide show
  1. package/index.js +82 -52
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -92,16 +92,20 @@ export const shuffle = (a) => {
92
92
  export const createNode = (type, className) => {
93
93
  const node = document.createElement(type);
94
94
  if(className) {
95
- node.classList.add(className);
96
- };
95
+ className.split('.').forEach((a) => {
96
+ node.classList.add(a);
97
+ });
98
+ };
97
99
  return node;
98
100
  };
99
101
 
100
102
  export const createSVGNode = (type = 'svg', className) => {
101
103
  const node = document.createElementNS('http://www.w3.org/2000/svg', type);
102
104
  if(className) {
103
- node.classList.add(className);
104
- };
105
+ className.split('.').forEach((a) => {
106
+ node.classList.add(a);
107
+ });
108
+ };
105
109
  return node;
106
110
  };
107
111
 
@@ -124,9 +128,7 @@ export const createOutput = (rows = 7) => {
124
128
  return textarea;
125
129
  };
126
130
 
127
- export const createContainer = (className) => {
128
- return createNode('div', className);
129
- };
131
+ export const createContainer = (className) => createNode('div', className);
130
132
 
131
133
  export const createSelect = (options) => {
132
134
  const node = createNode('select');
@@ -150,6 +152,34 @@ export const createHeading = (level = 1, label = '{label}') => {
150
152
  return node;
151
153
  };
152
154
 
155
+ export const createRadio = (value = 0, name = '{name}', id = '{id}', checked = false) => {
156
+ const node = createInput(value, 'radio');
157
+ node.name = name;
158
+ node.id = id;
159
+ node.checked = checked;
160
+ return node;
161
+ };
162
+
163
+ export const createLabel = (label = '{label}', id = '{id}', className) => {
164
+ const node = createNode('label', className);
165
+ node.innerHTML = label;
166
+ node.setAttribute('for', id);
167
+ return node;
168
+ };
169
+
170
+ export const createToggle = (options, id, defaultValue) => {
171
+ const node = createNode('div', 'toggle');
172
+ options.forEach((option) => {
173
+ const optionNode = createNode('div', 'toggle-item');
174
+ const radio = createRadio(option, id, `${option}-${id}`, option===defaultValue);
175
+ const label = createLabel(option, radio.id);
176
+ optionNode.append(radio);
177
+ optionNode.append(label);
178
+ node.append(optionNode);
179
+ });
180
+ return node;
181
+ };
182
+
153
183
  export const makeBitArray = (size) => {
154
184
  let bob = 2**size;
155
185
  return makeArray(size, () => {
@@ -249,7 +279,7 @@ export class Storage {
249
279
 
250
280
  export class GUID {
251
281
  constructor() {
252
-
282
+
253
283
  this.chars = [...this.uppercase, ...this.lowercase, ...this.numeric];
254
284
 
255
285
  };
@@ -257,14 +287,14 @@ export class GUID {
257
287
  lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
258
288
  numeric = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
259
289
  segment() {
260
-
290
+
261
291
  return makeArray(4, (a, i) => getRandom(i > 0 ? this.chars : this.uppercase)).join('');
262
292
 
263
293
  };
264
294
  get() {
265
-
295
+
266
296
  return makeArray(this.segments, () => this.segment()).join('-');
267
-
297
+
268
298
  };
269
299
  segments = 3;
270
300
  };
@@ -304,7 +334,7 @@ export class DisplayObject {
304
334
 
305
335
  };
306
336
  setStyle(key, value) {
307
-
337
+
308
338
  this.node.style[key] = value;
309
339
  return this;
310
340
 
@@ -316,13 +346,13 @@ export class DisplayObject {
316
346
 
317
347
  };
318
348
  addEventListener(event, handler, passive = true) {
319
-
349
+
320
350
  this.node.addEventListener(event, handler, {passive});
321
351
  return this;
322
-
352
+
323
353
  };
324
354
  dispatchEvent(event) {
325
-
355
+
326
356
  this.node.dispatchEvent(new Event(event));
327
357
  return this;
328
358
 
@@ -333,19 +363,19 @@ export class GameBase extends DisplayObject {
333
363
  constructor(name) {
334
364
 
335
365
  super();
336
-
366
+
337
367
  this.node = createNode('div', name);
338
368
  this.gameOverNode = createNode('div', 'game-over');
339
369
  this.canvas = createNode('canvas', 'game-canvas');
340
370
  this.ctx = this.canvas.getContext('2d');
341
371
  this.storage = new Storage(`me.jamesrock.${name}`);
342
-
372
+
343
373
  };
344
374
  showGameOverScreen() {
345
375
 
346
376
  const best = this.storage.get('best') || 0;
347
377
  this.storage.set('best', this.score > best ? this.score : best);
348
-
378
+
349
379
  this.gameOverNode.innerHTML = `\
350
380
  <div class="game-over-body">\
351
381
  <h1>Game over!</h1>\
@@ -417,7 +447,7 @@ export class BrickMaker extends DisplayObject {
417
447
 
418
448
  };
419
449
  calculate() {
420
-
450
+
421
451
  let total = null;
422
452
  switch(this.type) {
423
453
  case 'binary':
@@ -476,7 +506,7 @@ export class BrickMaker extends DisplayObject {
476
506
 
477
507
  };
478
508
  setColor(color) {
479
-
509
+
480
510
  this.color = color;
481
511
  this.node.style.setProperty('--color', this.color);
482
512
  return this;
@@ -487,80 +517,80 @@ export class BrickMaker extends DisplayObject {
487
517
 
488
518
  export class Collection extends Array {
489
519
  constructor() {
490
-
520
+
491
521
  super();
492
522
 
493
523
  };
494
524
  getItemByKeyValue(key, value) {
495
525
 
496
526
  return this.filter((item) => item[key]===value)[0];
497
-
527
+
498
528
  };
499
529
  getItemsByKeyValue(key, value) {
500
530
 
501
531
  return this.filter((item) => item[key]===value);
502
-
532
+
503
533
  };
504
534
  append(item) {
505
-
535
+
506
536
  this.push(item);
507
537
  return item;
508
-
538
+
509
539
  };
510
540
  prepend(item) {
511
-
541
+
512
542
  this.unshift(item);
513
543
  return item;
514
-
544
+
515
545
  };
516
546
  exists(value) {
517
547
 
518
548
  return (this.indexOf(value)>-1);
519
-
549
+
520
550
  };
521
551
  random() {
522
-
552
+
523
553
  return getRandom(this);
524
-
554
+
525
555
  };
526
556
  remove(item) {
527
-
557
+
528
558
  return this.removeAt(this.getIndexOf(item));
529
-
559
+
530
560
  };
531
561
  removeAt(index) {
532
-
562
+
533
563
  this.splice(index, 1);
534
564
  return this;
535
-
565
+
536
566
  };
537
567
  addAt(item, index) {
538
-
568
+
539
569
  this.splice(index, 0, item);
540
570
  return item;
541
-
571
+
542
572
  };
543
573
  first() {
544
-
574
+
545
575
  return this[0];
546
-
576
+
547
577
  };
548
578
  last() {
549
-
579
+
550
580
  return this[this.length-1];
551
-
581
+
552
582
  };
553
583
  swap(aIndex, bIndex) {
554
-
555
- var
584
+
585
+ var
556
586
  aProp = this[aIndex],
557
587
  bProp = this[bIndex];
558
-
588
+
559
589
  this[aIndex] = bProp;
560
590
  this[bIndex] = aProp;
561
-
591
+
562
592
  return this;
563
-
593
+
564
594
  };
565
595
  pushift() {
566
596
 
@@ -569,7 +599,7 @@ export class Collection extends Array {
569
599
 
570
600
  };
571
601
  shuffle() {
572
-
602
+
573
603
  return shuffle(this);
574
604
 
575
605
  };
@@ -681,7 +711,7 @@ export class DeckOfPlayingCards {
681
711
 
682
712
  };
683
713
  makeMap() {
684
-
714
+
685
715
  var out = {};
686
716
  this.cards.forEach((card) => {
687
717
  out[card.id] = card;
@@ -692,7 +722,7 @@ export class DeckOfPlayingCards {
692
722
  makeShuffledMap() {
693
723
 
694
724
  return this.cards.map((card) => card.id);
695
-
725
+
696
726
  };
697
727
  makeSaveMap() {
698
728
 
@@ -714,7 +744,7 @@ export class DeckOfPlayingCards {
714
744
 
715
745
  };
716
746
  destroy() {
717
-
747
+
718
748
  this.shuffledMap.forEach((id) => {
719
749
  this.map[id].destroy();
720
750
  });
@@ -756,17 +786,17 @@ export class DeckOfPlayingCards {
756
786
 
757
787
  };
758
788
  getCard(index = 0) {
759
-
789
+
760
790
  return this.cards[index];
761
791
 
762
792
  };
763
793
  getSuitIcon(suit) {
764
-
794
+
765
795
  return this.suitIcons[suit];
766
796
 
767
797
  };
768
798
  getSuitColor(suit) {
769
-
799
+
770
800
  return this.suitColors[suit];
771
801
 
772
802
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.25.0",
3
+ "version": "1.26.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",