Skip to content

Commit 0227866

Browse files
committed
shuffle
1 parent 6d89fb2 commit 0227866

File tree

9 files changed

+447
-216
lines changed

9 files changed

+447
-216
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ machine.visible; //Returns boolean
8585
Change spin result, if the returned value is out of bounds, the element will be randomly choosen:
8686

8787
```javascript
88-
machine.setRandomize(foo); //foo must be a function (should return int) or an int
88+
machine.randomize = foo; //foo must be a function (should return int) or an int
8989
```
9090

9191
Change spin direction, machine must not be running:

dist/slotmachine.js

Lines changed: 37 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/slotmachine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/slot-machine.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ module.exports = class SlotMachine {
5151
this.running = false;
5252
// Machine is stopping?
5353
this.stopping = false;
54+
// Randomize Function|Number
55+
this.randomize = this.settings.randomize;
5456
// Current active element
5557
this.settings.active = Number(this.settings.active);
5658
if (isNaN(this.settings.active) ||
@@ -69,7 +71,7 @@ module.exports = class SlotMachine {
6971
// Initialize spin direction [up, down]
7072
this._initDirection();
7173
// Show active element
72-
this.resetPosition();
74+
this._resetPosition();
7375
// Start auto animation
7476
if (this.settings.auto !== false) {
7577
if (this.settings.auto === true) {
@@ -157,8 +159,8 @@ module.exports = class SlotMachine {
157159
get custom () {
158160
let choosen;
159161

160-
if (typeof this.settings.randomize === 'function') {
161-
let index = this.settings.randomize.call(this, this.active);
162+
if (typeof this._randomize === 'function') {
163+
let index = this._randomize(this.active);
162164
if (index < 0 || index >= this.tiles.length) {
163165
index = 0;
164166
}
@@ -248,6 +250,18 @@ module.exports = class SlotMachine {
248250
}
249251
}
250252

253+
/**
254+
* @desc PUBLIC - Changes randomize function
255+
* @param function|Number - Set new randomize function
256+
*/
257+
set randomize (rnd) {
258+
this._randomize = rnd;
259+
260+
if (typeof rnd === 'number') {
261+
this._randomize = () => rnd;
262+
}
263+
}
264+
251265
/**
252266
* @desc PRIVATE - Set CSS speed cclass
253267
* @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP]
@@ -349,26 +363,14 @@ module.exports = class SlotMachine {
349363
/**
350364
* @desc PRIVATE - Reset active element position
351365
*/
352-
resetPosition (margin) {
366+
_resetPosition (margin) {
353367
this.container.classList.toggle(FX_NO_TRANSITION);
354368
this._animate(margin === undefined ? this.direction.initial : margin);
355369
// Force reflow, flushing the CSS changes
356370
this.container.offsetHeight;
357371
this.container.classList.toggle(FX_NO_TRANSITION);
358372
}
359373

360-
/**
361-
* @desc PUBLIC - Changes randomize function
362-
* @param function|Number - Set new randomize function
363-
*/
364-
setRandomize (rnd) {
365-
this.settings.randomize = rnd;
366-
367-
if (typeof rnd === 'number') {
368-
this.settings.randomize = () => rnd;
369-
}
370-
}
371-
372374
/**
373375
* @desc PUBLIC - SELECT previous element relative to the current active element
374376
* @return {Number} - Returns result index
@@ -394,11 +396,11 @@ module.exports = class SlotMachine {
394396
}
395397

396398
/**
397-
* @desc PUBLIC - Starts shuffling the elements
399+
* @desc PRIVATE - Starts shuffling the elements
398400
* @param {Number} repeations - Number of shuffles (undefined to make infinite animation
399401
* @return {Number} - Returns result index
400402
*/
401-
getDelayFromSpins (spins) {
403+
_getDelayFromSpins (spins) {
402404
let delay = this.settings.delay;
403405
this._transition = 'linear';
404406

@@ -443,14 +445,14 @@ module.exports = class SlotMachine {
443445
if (!this.visible && this.settings.stopHidden === true) {
444446
this.stop(onComplete);
445447
} else {
446-
const delay = this.getDelayFromSpins(spins);
448+
const delay = this._getDelayFromSpins(spins);
447449
this.delay = delay;
448450
this._animate(this.direction.to);
449451
raf(() => {
450452
if (!this.stopping && this.running) {
451453
const left = spins - 1;
452454

453-
this.resetPosition(this.direction.first);
455+
this._resetPosition(this.direction.first);
454456
if (left <= 1) {
455457
this.stop(onComplete);
456458
} else {
@@ -483,16 +485,16 @@ module.exports = class SlotMachine {
483485

484486
// Check direction to prevent jumping
485487
if (this._isGoingBackward()) {
486-
this.resetPosition(this.direction.firstToLast);
488+
this._resetPosition(this.direction.firstToLast);
487489
} else if (this._isGoingForward()) {
488-
this.resetPosition(this.direction.lastToFirst);
490+
this._resetPosition(this.direction.lastToFirst);
489491
}
490492

491493
// Update last choosen element index
492494
this.active = this.futureActive;
493495

494496
// Perform animation
495-
const delay = this.getDelayFromSpins(1);
497+
const delay = this._getDelayFromSpins(1);
496498
this.delay = delay;
497499
this._animationFX = FX_STOP;
498500
this._animate(this.getTileOffset(this.active));
@@ -522,8 +524,8 @@ module.exports = class SlotMachine {
522524
}
523525

524526
this._timer = new Timer(() => {
525-
if (typeof this.settings.randomize !== 'function') {
526-
this.settings.randomize = () => this._nextIndex;
527+
if (typeof this._randomize !== 'function') {
528+
this._randomize = () => this._nextIndex;
527529
}
528530
if (!this.visible && this.settings.stopHidden === true) {
529531
raf(() => {

tests/acceptance/constructor-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ describe('Constructor', () => {
7373
});
7474
});
7575

76+
it('sets randomize', () => {
77+
const randomize = () => {};
78+
machine = render({
79+
randomize
80+
});
81+
82+
expect(machine._randomize).to.be.equal(randomize);
83+
});
84+
7685
it('does not auto start', () => {
7786
machine = render();
7887

tests/acceptance/getters-test.js

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -200,156 +200,4 @@ describe('Getters', () => {
200200
expect(machine.visible).to.be.false;
201201
});
202202
});
203-
204-
describe('Next tiles', () => {
205-
it('shows next element: first to second, direction "up"', () => {
206-
machine = render({
207-
direction: 'up'
208-
});
209-
210-
let tile = getVisibleTile(machine);
211-
expect(machine.visibleTile).to.be.equal(0);
212-
expect(machine.active).to.be.equal(0);
213-
expect(tile.innerText).to.be.equal('foo');
214-
215-
machine.next();
216-
217-
tile = getVisibleTile(machine);
218-
expect(machine.visibleTile).to.be.equal(2);
219-
expect(machine.active).to.be.equal(2);
220-
expect(tile.innerText).to.be.equal('wow');
221-
});
222-
223-
it('shows next element: last to first, direction "up"', () => {
224-
machine = render({
225-
active: 2,
226-
direction: 'up'
227-
});
228-
229-
let tile = getVisibleTile(machine);
230-
expect(machine.visibleTile).to.be.equal(2);
231-
expect(machine.active).to.be.equal(2);
232-
expect(tile.innerText).to.be.equal('wow');
233-
234-
machine.next();
235-
236-
tile = getVisibleTile(machine);
237-
expect(machine.visibleTile).to.be.equal(1);
238-
expect(machine.active).to.be.equal(1);
239-
expect(tile.innerText).to.be.equal('bar');
240-
});
241-
242-
it('shows next element: first to second, direction "down"', () => {
243-
machine = render({
244-
direction: 'down'
245-
});
246-
247-
let tile = getVisibleTile(machine);
248-
expect(machine.visibleTile).to.be.equal(0);
249-
expect(machine.active).to.be.equal(0);
250-
expect(tile.innerText).to.be.equal('foo');
251-
252-
machine.next();
253-
254-
tile = getVisibleTile(machine);
255-
expect(machine.visibleTile).to.be.equal(1);
256-
expect(machine.active).to.be.equal(1);
257-
expect(tile.innerText).to.be.equal('bar');
258-
});
259-
260-
it('shows next element: last to first, direction "down"', () => {
261-
machine = render({
262-
direction: 'down',
263-
active: 2
264-
});
265-
266-
let tile = getVisibleTile(machine);
267-
expect(machine.visibleTile).to.be.equal(2);
268-
expect(machine.active).to.be.equal(2);
269-
expect(tile.innerText).to.be.equal('wow');
270-
271-
machine.next();
272-
273-
tile = getVisibleTile(machine);
274-
expect(machine.visibleTile).to.be.equal(0);
275-
expect(machine.active).to.be.equal(0);
276-
expect(tile.innerText).to.be.equal('foo');
277-
});
278-
});
279-
280-
describe('Prev tiles', () => {
281-
it('shows prev element: second to first, direction "up"', () => {
282-
machine = render({
283-
direction: 'up',
284-
active: 1
285-
});
286-
287-
let tile = getVisibleTile(machine);
288-
expect(machine.visibleTile).to.be.equal(1);
289-
expect(machine.active).to.be.equal(1);
290-
expect(tile.innerText).to.be.equal('bar');
291-
292-
machine.prev();
293-
294-
tile = getVisibleTile(machine);
295-
expect(machine.visibleTile).to.be.equal(2);
296-
expect(machine.active).to.be.equal(2);
297-
expect(tile.innerText).to.be.equal('wow');
298-
});
299-
300-
it('shows prev element: first to last, direction "up"', () => {
301-
machine = render({
302-
direction: 'up'
303-
});
304-
305-
let tile = getVisibleTile(machine);
306-
expect(machine.visibleTile).to.be.equal(0);
307-
expect(machine.active).to.be.equal(0);
308-
expect(tile.innerText).to.be.equal('foo');
309-
310-
machine.prev();
311-
312-
tile = getVisibleTile(machine);
313-
expect(machine.visibleTile).to.be.equal(1);
314-
expect(machine.active).to.be.equal(1);
315-
expect(tile.innerText).to.be.equal('bar');
316-
});
317-
318-
it('shows prev element: second to first, direction "down"', () => {
319-
machine = render({
320-
direction: 'down',
321-
active: 1
322-
});
323-
324-
let tile = getVisibleTile(machine);
325-
expect(machine.visibleTile).to.be.equal(1);
326-
expect(machine.active).to.be.equal(1);
327-
expect(tile.innerText).to.be.equal('bar');
328-
329-
machine.prev();
330-
331-
tile = getVisibleTile(machine);
332-
expect(machine.visibleTile).to.be.equal(0);
333-
expect(machine.active).to.be.equal(0);
334-
expect(tile.innerText).to.be.equal('foo');
335-
});
336-
337-
it('shows prev element: first to last, direction "down"', () => {
338-
machine = render({
339-
direction: 'down'
340-
});
341-
342-
let tile = getVisibleTile(machine);
343-
expect(machine.visibleTile).to.be.equal(0);
344-
expect(machine.active).to.be.equal(0);
345-
expect(tile.innerText).to.be.equal('foo');
346-
347-
machine.prev();
348-
349-
tile = getVisibleTile(machine);
350-
expect(machine.visibleTile).to.be.equal(2);
351-
expect(machine.active).to.be.equal(2);
352-
expect(tile.innerText).to.be.equal('wow');
353-
});
354-
});
355203
});

0 commit comments

Comments
 (0)