Skip to content

Commit 980cc31

Browse files
authored
Merge pull request #42 from josex2r/refactor
Refactor
2 parents df8975e + 362c8d8 commit 980cc31

19 files changed

+819
-1248
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@ Please don't edit files in the `dist` subdirectory as they are generated via Gru
66
### Code style
77
Regarding code style like indentation and whitespace, **follow the conventions you see used in the source already.**
88

9-
### PhantomJS
10-
While Grunt can run the included unit tests via [PhantomJS](http://phantomjs.org/), this shouldn't be considered a substitute for the real thing. Please be sure to test the `test/*.html` unit test file(s) in _actual_ browsers.
11-
129
## Modifying the code
1310
First, ensure that you have the latest [Node.js](http://nodejs.org/) and [npm](http://npmjs.org/) installed.
1411

15-
Test that Grunt's CLI is installed by running `grunt --version`. If the command isn't found, run `npm install -g grunt-cli`. For more information about installing Grunt, see the [getting started guide](http://gruntjs.com/getting-started).
16-
1712
1. Fork and clone the repo.
1813
1. Run `npm install` to install all dependencies (including Grunt).
19-
1. Run `grunt` to grunt this project.
14+
1. Run `gulp server` to run the server and start debugging the library.
2015

2116
Assuming that you don't see any red, you're ready to go. Just be sure to run `grunt` after making any changes, to ensure that nothing is broken.
2217

2318
## Submitting pull requests
2419

2520
1. Create a new branch, please don't work in your `master` branch directly.
26-
1. Add failing tests for the change you want to make. Run `grunt` to see the tests fail.
21+
1. Add failing tests for the change you want to make. Run `npm test` to see the tests fail.
2722
1. Fix stuff.
28-
1. Run `grunt` to see if the tests pass. Repeat steps 2-4 until done.
29-
1. Open `test/*.html` unit test file(s) in actual browser to ensure tests pass everywhere.
23+
1. Run `npm test` to see if the tests pass. Repeat steps 2-4 until done.
3024
1. Update the documentation to reflect any changes.
3125
1. Push to your fork and submit a pull request.

README.md

Lines changed: 109 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,167 @@
11
# jQuery-SlotMachine [![Build Status](https://travis-ci.org/josex2r/jQuery-SlotMachine.svg?branch=master)](https://travis-ci.org/josex2r/jQuery-SlotMachine) [![Dependency Status](https://david-dm.org/josex2r/jQuery-SlotMachine.svg)](https://david-dm.org/josex2r/jQuery-SlotMachine) [![devDependency Status](https://david-dm.org/josex2r/jQuery-SlotMachine/dev-status.svg)](https://david-dm.org/josex2r/jQuery-SlotMachine#info=devDependencies)
22

3-
A simple, lightweight jQuery plugin to make slot machine animation effect.
3+
> :mega: jQuery is not neccessary now! The name it's just legacy.
44
5-
[Check the example page!](http://josex2r.github.io/jQuery-SlotMachine/)
5+
A simple, and lightweight piece of code to make slot machine animation effect.
6+
It also exports a js wrapper to allow the usage with jQuery.
7+
8+
To preview what you can do [check the example page!](http://josex2r.github.io/jQuery-SlotMachine/)
69

710
## Installation
811

9-
Install the component using [Bower](http://bower.io/):
12+
Install the component using [npm](https://www.npmjs.com/package/jquery-slotmachine):
1013

11-
```sh
12-
$ bower install jquery-slotmachine --save
14+
```bash
15+
npm install jquery-slotmachine --save
1316
```
1417

15-
Include the script located in *dist* folder *after* the jQuery library:
18+
Install the component using [Bower](http://bower.io/):
1619

17-
```html
18-
<script src="/path/to/jquery.slotmachine.min.js"></script>
20+
```bash
21+
bower install jquery-slotmachine --save
1922
```
2023

21-
## Usage
22-
23-
Creating the machine:
24+
## Example
2425

25-
```javascript
26-
var machine = $(foo).slotMachine( params );
26+
```html
27+
<div id="machine">
28+
<div>Madrid</div>
29+
<div>London</div>
30+
<div>New York</div>
31+
</div>
32+
33+
<script>
34+
const el = document.querySelector('#machine');
35+
const machine = new SlotMachine(el, {
36+
active: 1,
37+
delay: 450,
38+
auto: 1500
39+
});
40+
</script>
2741
```
2842

29-
Get machine instance:
43+
> Lookup the sourcecode in the [examples page](http://josex2r.github.io/jQuery-SlotMachine/) to see more examples.
3044
31-
```javascript
32-
var machine = $(foo).slotMachine();
33-
```
45+
## Usage
3446

35-
Shuffle:
47+
Include the script located in *dist* folder:
3648

37-
```javascript
38-
machine.shuffle( repeat, onStopCallback ); //No args to make rotate infinitely, `repeat` is optional
49+
```html
50+
<script src="/path/to/slotmachine.min.js"></script>
3951
```
4052

41-
Change the selected element:
53+
Then you can make it work calling the lib in your app:
4254

4355
```javascript
44-
machine.prev(); //Previous element
45-
46-
machine.next(); //Next element
56+
const element = document.getElementById('my-machine');
57+
const machine = new SlotMachine(element, { /* options */ });
4758
```
4859

49-
Stop the machine:
60+
If you preffer jQuery style then import the wrapper *after* the jQuery library:
5061

51-
```javascript
52-
machine.stop();
62+
```html
63+
<script src="/path/to/jquery.min.js"></script>
64+
<script src="/path/to/jslotmachine.min.js"></script>
65+
<script src="/path/to/jquery.slotmachine.min.js"></script>
5366
```
5467

55-
Get selected element:
56-
5768
```javascript
58-
machine.active; //Current element index
69+
$(document).ready(function(){
70+
$('#my-machine').slotMachine({ /* options */ });
71+
});
5972
```
6073

61-
Get the selected element if shuffling:
74+
### Settings
6275

63-
```javascript
64-
machine.futureActive; //Future active element index
65-
```
66-
67-
Check if the machine is running:
76+
Use the first argument of the function to pass an object with the options:
6877

6978
```javascript
70-
machine.running; //Returns boolean
79+
const machine = new SlotMachine(element, {
80+
active: 2,
81+
auto: true
82+
});
7183
```
7284

73-
Check if the machine is stopping:
85+
| Name | Type | Default | Description |
86+
|----------------|------------|---------------|-------------------------------------------------------------------------------------|
87+
| **active** | `Number` | `0` | The initial visible element (0 means the first one) |
88+
| **delay** | `Number` | `200` | Duration (in ms) of each spin |
89+
| **auto** | `Boolean` | `false` | Runs the carousel mode when creating the machine |
90+
| **spins** | `Number` | `5` | Number of spins after stop in carousel mode |
91+
| **randomize** | `Function` | `null` | Function (returns number) that is going to be called to set the next active element |
92+
| **onComplete** | `Function` | `null` | Callback after each spin in carousel mode |
93+
| **inViewport** | `Boolean` | `true` | Only spin when the machine is inside the viewport |
94+
| **direction** | `String` | `up` | The spin direction (possible values are `up` and `down`) |
95+
| **transition** | `String` | `ease-in-out` | The CSS transition |
96+
97+
### Properties
98+
99+
- `machine.nextActive`: Get the next active element (only while shuffling).
100+
- `machine.nextIndex`: Next element index according to the current direction.
101+
- `machine.prevIndex`: Prev element index according to the current direction.
102+
- `machine.random`: Get rando index between the machine bounds.
103+
- `machine.running`: Check if the machine is running.
104+
- `machine.stopping`: Check if the machine is stopping.
105+
- `machine.visible`: Check if the machine is visible.
106+
- `machine.visibleTile`: Get the current visible element in the machine viewport.
107+
- `machine.active`: Alias to the `active` setting.
108+
- `machine.randomize`: Alias to the `randomize` setting.
109+
- `machine.direction`: Alias to the `direction` setting.
110+
- `machine.transition`: Alias to the `transition` setting.
111+
112+
### Methods
113+
114+
`machine.shuffle(spins, callback)`: Starts spining the machine.
115+
- spins (`Number`): Optionally set the number of spins.
116+
- callback(`Function`): Callback triggered when the machine stops.
74117

75118
```javascript
76-
machine.stopping; //Returns boolean
119+
// Do a single spin
120+
machine.shuffle();
121+
// Do a single spin and then shows an alert
122+
machine.shuffle(() => alert('Stop!'));
123+
// Do 5 spins before stop
124+
machine.shuffle(5);
125+
// Do 7 spins and then showing an alert
126+
machine.shuffle(7, () => alert('Stop!'));
127+
// "Infinite" spins
128+
machine.shuffle(9999999); // O_O
77129
```
78130

79-
Check if the machine is visible:
80-
81-
```javascript
82-
machine.visible; //Returns boolean
83-
```
131+
`machine.stop(callback)`: Manually stops the machine.
132+
- callback(`Function`): Callback triggered when the machine stops.
84133

85-
Change spin result, if the returned value is out of bounds, the element will be randomly choosen:
134+
For example, start spinning the machine and stop it after pressing a button:
86135

87136
```javascript
88-
machine.randomize = foo; //foo must be a function (should return int) or an int
137+
machine.shuffle(99999);
138+
// Add the button listener
139+
myButton.addEventListener('click', () => {
140+
// Stop spinning
141+
machine.stop();
142+
});
89143
```
90144

91-
Change spin direction, machine must not be running:
145+
`machine.next()`/`machine.prev()`: Spin to the next/previous element.
92146

93147
```javascript
94-
machine.direction = direction; //direction must be a String ('up' || 'down')
148+
// Spin to the previous element
149+
machine.prev();
150+
// Spin to the next element
151+
machine.next();
95152
```
96153

97-
Destroy the machine. It will be useful when you want to reuse DOM:
154+
`machine.run()`: Starts the preview mode, it will spin/stop given a delay (more info in options).
98155

99156
```javascript
100-
machine.destroy();
157+
machine.run();
101158
```
102159

103-
## Params
104-
105-
Params must be an object, optionally containing the next parammeters:
106-
107-
#### active
108-
109-
Set the first element
110-
111-
active: 0
112-
113-
#### delay
114-
115-
Set spin animation time
116-
117-
delay: 200
118-
119-
#### auto
120-
121-
Pass an int as miliseconds to make the machine auto rotate
122-
123-
auto: false
124-
125-
#### spins
126-
127-
The number of spins when auto is enabled
128-
129-
spins: false
130-
131-
#### stopHidden
132-
133-
Stop animation if the element is above or below the screen
134-
135-
stopHidden: true
136-
137-
#### randomize
138-
139-
Pass a function to select your own random element. This function must return an integer between 0 (first element) and max number of elements.
140-
141-
randomize: function(activeElementIndex){} //activeElementIndex = current selected index
142-
143-
Example (this machine always shows first element):
160+
`machine.run()`: Destroys the machine. It will be useful when you want to reuse DOM.
144161

145162
```javascript
146-
$('#foo').slotMachine({
147-
randomize : function(activeElementIndex){
148-
return 0;
149-
}
150-
});
163+
machine.destroy();
151164
```
152-
#### direction
153-
154-
Animation direction ('up' || 'down')
155-
156-
direction: 'up'
157165

158166
## Authors
159167

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jQuery-SlotMachine",
33
"description": "A simple jQuery plugin to make slot machine animation effect",
4-
"version": "3.0.1",
4+
"version": "4.0.0",
55
"keywords": [
66
"slots",
77
"gambling",

dist/jquery.slotmachine.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery Slot Machine v3.0.1
2+
* jQuery Slot Machine v4.0.0
33
* https://github.com/josex2r/jQuery-SlotMachineundefined
44
*
55
* Copyright 2014 Jose Luis Represa

dist/jquery.slotmachine.js

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

dist/jquery.slotmachine.min.css

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

dist/jquery.slotmachine.min.js

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

0 commit comments

Comments
 (0)