@jamesrock/rockjs 1.29.0 → 1.31.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.
- package/index.js +104 -60
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -10,8 +10,8 @@ export const getRandom = (a) => a[randomIndex(a)];
|
|
|
10
10
|
export const getFirst = (a) => a[0];
|
|
11
11
|
export const getLast = (a) => a[a.length-1];
|
|
12
12
|
export const isOrientation = (orientation) => matchMedia(`(orientation: ${orientation})`).matches;
|
|
13
|
-
export const maxWidth = (width) => matchMedia(`(max-width: ${width}px)`).matches;
|
|
14
13
|
export const minWidth = (width) => matchMedia(`(min-width: ${width}px)`).matches;
|
|
14
|
+
export const minHeight = (height) => matchMedia(`(min-height: ${height}px)`).matches;
|
|
15
15
|
export const isColorScheme = (colorScheme) => matchMedia(`(prefers-color-scheme: ${colorScheme})`).matches;
|
|
16
16
|
export const isLandscape = () => isOrientation('landscape');
|
|
17
17
|
export const isPortrait = () => isOrientation('portrait');
|
|
@@ -89,90 +89,89 @@ export const shuffle = (a) => {
|
|
|
89
89
|
return a;
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
node.classList.add(a);
|
|
97
|
-
});
|
|
98
|
-
};
|
|
92
|
+
const applyClassName = (node, className) => {
|
|
93
|
+
className?.split('.').forEach((a) => {
|
|
94
|
+
node.classList.add(a);
|
|
95
|
+
});
|
|
99
96
|
return node;
|
|
100
97
|
};
|
|
101
98
|
|
|
102
|
-
export const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
className.split('.').forEach((a) => {
|
|
106
|
-
node.classList.add(a);
|
|
107
|
-
});
|
|
108
|
-
};
|
|
109
|
-
return node;
|
|
110
|
-
};
|
|
99
|
+
export const makeNode = (type, className) => applyClassName(document.createElement(type), className);
|
|
100
|
+
|
|
101
|
+
export const makeSVGNode = (type = 'svg', className) => applyClassName(document.createElementNS('http://www.w3.org/2000/svg', type), className);
|
|
111
102
|
|
|
112
|
-
export const
|
|
113
|
-
const btn =
|
|
103
|
+
export const makeButton = (label = '{label}', className) => {
|
|
104
|
+
const btn = makeNode('button', className);
|
|
114
105
|
btn.innerText = label;
|
|
115
106
|
return btn;
|
|
116
107
|
};
|
|
117
108
|
|
|
118
|
-
export const
|
|
119
|
-
const input =
|
|
109
|
+
export const makeInput = (value = 0, type = 'number') => {
|
|
110
|
+
const input = makeNode('input');
|
|
120
111
|
input.type = type;
|
|
121
112
|
input.value = value;
|
|
122
113
|
return input;
|
|
123
114
|
};
|
|
124
115
|
|
|
125
|
-
export const
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
return
|
|
116
|
+
export const makeOutput = (rows = 7) => {
|
|
117
|
+
const node = makeNode('textarea');
|
|
118
|
+
node.rows = rows;
|
|
119
|
+
return node;
|
|
129
120
|
};
|
|
130
121
|
|
|
131
|
-
export const
|
|
122
|
+
export const makeContainer = (className) => makeNode('div', className);
|
|
132
123
|
|
|
133
|
-
export const
|
|
134
|
-
const node =
|
|
124
|
+
export const makeSelect = (options, defaultValue = options[0][1]) => {
|
|
125
|
+
const node = makeNode('select');
|
|
135
126
|
options.forEach(([label, value]) => {
|
|
136
|
-
const option =
|
|
127
|
+
const option = makeOption(label, value);
|
|
137
128
|
node.appendChild(option);
|
|
138
|
-
})
|
|
129
|
+
});
|
|
130
|
+
node.value = defaultValue;
|
|
139
131
|
return node;
|
|
140
132
|
};
|
|
141
133
|
|
|
142
|
-
export const
|
|
143
|
-
const node =
|
|
134
|
+
export const makeOption = (label = '{label}', value = 0) => {
|
|
135
|
+
const node = makeNode('option');
|
|
144
136
|
node.innerText = label;
|
|
145
137
|
node.value = value;
|
|
146
138
|
return node;
|
|
147
139
|
};
|
|
148
140
|
|
|
149
|
-
export const
|
|
150
|
-
const node =
|
|
141
|
+
export const makeHeading = (level = 1, label = '{label}') => {
|
|
142
|
+
const node = makeNode(`h${level}`);
|
|
151
143
|
node.innerText = label;
|
|
152
144
|
return node;
|
|
153
145
|
};
|
|
154
146
|
|
|
155
|
-
export const
|
|
156
|
-
const node =
|
|
147
|
+
export const makeLink = (href = '{href}', label = '{label}', className) => {
|
|
148
|
+
const node = makeNode('a', className);
|
|
149
|
+
node.href = href;
|
|
150
|
+
node.innerText = label;
|
|
151
|
+
return node;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export const makeRadio = (value = 0, name = '{name}', id = '{id}', checked = false) => {
|
|
155
|
+
const node = makeInput(value, 'radio');
|
|
157
156
|
node.name = name;
|
|
158
157
|
node.id = id;
|
|
159
158
|
node.checked = checked;
|
|
160
159
|
return node;
|
|
161
160
|
};
|
|
162
161
|
|
|
163
|
-
export const
|
|
164
|
-
const node =
|
|
162
|
+
export const makeLabel = (label = '{label}', id = '{id}', className) => {
|
|
163
|
+
const node = makeNode('label', className);
|
|
165
164
|
node.innerHTML = label;
|
|
166
165
|
node.setAttribute('for', id);
|
|
167
166
|
return node;
|
|
168
167
|
};
|
|
169
168
|
|
|
170
169
|
export const makeToggle = (options, name, defaultValue, className = 'toggle') => {
|
|
171
|
-
const node =
|
|
170
|
+
const node = makeNode('div', className);
|
|
172
171
|
options.forEach(([label, value, optionClassName]) => {
|
|
173
|
-
const optionNode =
|
|
174
|
-
const radioNode =
|
|
175
|
-
const labelNode =
|
|
172
|
+
const optionNode = makeNode('div', 'toggle-item');
|
|
173
|
+
const radioNode = makeRadio(value, name, `${name}-${value}`, value===defaultValue);
|
|
174
|
+
const labelNode = makeLabel(label, radioNode.id, optionClassName);
|
|
176
175
|
optionNode.appendChild(radioNode);
|
|
177
176
|
optionNode.appendChild(labelNode);
|
|
178
177
|
node.appendChild(optionNode);
|
|
@@ -222,6 +221,47 @@ export const makeHexMap = (full = true) => {
|
|
|
222
221
|
return out;
|
|
223
222
|
};
|
|
224
223
|
|
|
224
|
+
export const makeMap = (items, key = 'id') => {
|
|
225
|
+
const out = {};
|
|
226
|
+
items.forEach((item) => {
|
|
227
|
+
out[item[key]] = item;
|
|
228
|
+
});
|
|
229
|
+
return out;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const addDragListeners = (target, tolerance) => {
|
|
233
|
+
|
|
234
|
+
const rounder = new Rounder(tolerance);
|
|
235
|
+
let touch = null;
|
|
236
|
+
|
|
237
|
+
target.addEventListener('touchstart', (e) => {
|
|
238
|
+
|
|
239
|
+
touch = e.touches[0];
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
target.addEventListener('touchmove', (e) => {
|
|
245
|
+
|
|
246
|
+
const {clientX: originalClientX, clientY: originalClientY} = touch;
|
|
247
|
+
const {clientX, clientY} = e.touches[0];
|
|
248
|
+
const x = rounder.round(clientX - originalClientX);
|
|
249
|
+
const y = rounder.round(clientY - originalClientY);
|
|
250
|
+
|
|
251
|
+
if(x) {
|
|
252
|
+
touch = e.touches[0];
|
|
253
|
+
target.dispatchEvent(new Event(x > 0 ? 'drag-right' : 'drag-left'));
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
if(y) {
|
|
257
|
+
touch = e.touches[0];
|
|
258
|
+
target.dispatchEvent(new Event(y > 0 ? 'drag-down' : 'drag-up'));
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
};
|
|
264
|
+
|
|
225
265
|
export class Storage {
|
|
226
266
|
constructor(namespace) {
|
|
227
267
|
|
|
@@ -364,9 +404,9 @@ export class GameBase extends DisplayObject {
|
|
|
364
404
|
|
|
365
405
|
super();
|
|
366
406
|
|
|
367
|
-
this.node =
|
|
368
|
-
this.gameOverNode =
|
|
369
|
-
this.canvas =
|
|
407
|
+
this.node = makeNode('div', name);
|
|
408
|
+
this.gameOverNode = makeNode('div', 'game-over');
|
|
409
|
+
this.canvas = makeNode('canvas', 'game-canvas');
|
|
370
410
|
this.ctx = this.canvas.getContext('2d');
|
|
371
411
|
this.storage = new Storage(`me.jamesrock.${name}`);
|
|
372
412
|
|
|
@@ -417,7 +457,7 @@ export class BrickMaker extends DisplayObject {
|
|
|
417
457
|
'hex': makeBitArray(size)
|
|
418
458
|
};
|
|
419
459
|
|
|
420
|
-
const node = this.node =
|
|
460
|
+
const node = this.node = makeNode('div', 'brick-maker');
|
|
421
461
|
const bits = this.bits = [];
|
|
422
462
|
|
|
423
463
|
this.setColor(color);
|
|
@@ -427,7 +467,7 @@ export class BrickMaker extends DisplayObject {
|
|
|
427
467
|
makeArray(size).forEach((y) => {
|
|
428
468
|
makeArray(size).forEach((x) => {
|
|
429
469
|
|
|
430
|
-
const bit =
|
|
470
|
+
const bit = makeNode('div', 'brick-maker-bit');
|
|
431
471
|
bit.style.width = bit.style.height = `${scale}px`;
|
|
432
472
|
bit.dataset.x = x;
|
|
433
473
|
bit.dataset.y = y;
|
|
@@ -677,9 +717,9 @@ export class PlayingCard extends DisplayObject {
|
|
|
677
717
|
make() {
|
|
678
718
|
|
|
679
719
|
const
|
|
680
|
-
node =
|
|
681
|
-
svg =
|
|
682
|
-
use =
|
|
720
|
+
node = makeNode('div', 'card'),
|
|
721
|
+
svg = makeSVGNode('svg'),
|
|
722
|
+
use = makeSVGNode('use');
|
|
683
723
|
|
|
684
724
|
use.setAttribute('href', `${this.deck.sprite}#${this.suit}${this.value}`);
|
|
685
725
|
|
|
@@ -701,7 +741,7 @@ export class DeckOfPlayingCards {
|
|
|
701
741
|
this.sprite = sprite;
|
|
702
742
|
this.cardMaker = cardMaker;
|
|
703
743
|
this.cards = saved.length ? this.makeFromSaved(saved) : this.make();
|
|
704
|
-
this.map = this.
|
|
744
|
+
this.map = makeMap(this.cards);
|
|
705
745
|
this.shuffledMap = this.makeShuffledMap();
|
|
706
746
|
|
|
707
747
|
};
|
|
@@ -709,15 +749,6 @@ export class DeckOfPlayingCards {
|
|
|
709
749
|
|
|
710
750
|
return shuffle(shuffle(this.makeDeckValues().map(([value, suit]) => this.cardMaker(this, value, suit))));
|
|
711
751
|
|
|
712
|
-
};
|
|
713
|
-
makeMap() {
|
|
714
|
-
|
|
715
|
-
var out = {};
|
|
716
|
-
this.cards.forEach((card) => {
|
|
717
|
-
out[card.id] = card;
|
|
718
|
-
});
|
|
719
|
-
return out;
|
|
720
|
-
|
|
721
752
|
};
|
|
722
753
|
makeShuffledMap() {
|
|
723
754
|
|
|
@@ -834,3 +865,16 @@ export class DeckOfPlayingCards {
|
|
|
834
865
|
'S': 'black'
|
|
835
866
|
};
|
|
836
867
|
};
|
|
868
|
+
|
|
869
|
+
// temporary alias
|
|
870
|
+
export const createNode = makeNode;
|
|
871
|
+
export const createSVGNode = makeSVGNode;
|
|
872
|
+
export const createButton = makeButton;
|
|
873
|
+
export const createInput = makeInput;
|
|
874
|
+
export const createOutput = makeOutput;
|
|
875
|
+
export const createContainer = makeContainer;
|
|
876
|
+
export const createSelect = makeSelect;
|
|
877
|
+
export const createOption = makeOption;
|
|
878
|
+
export const createHeading = makeHeading;
|
|
879
|
+
export const createRadio = makeRadio;
|
|
880
|
+
export const createLabel = makeLabel;
|