|
1 |
| -/*! SlotMachine - v2.0.11 - 2015-07-21 |
| 1 | +/*! SlotMachine - v2.1.0 - 2015-07-21 |
2 | 2 | * https://github.com/josex2r/jQuery-SlotMachine
|
3 | 3 | * Copyright (c) 2015 Jose Luis Represa; Licensed MIT */
|
4 | 4 | (function($, window, document, undefined) {
|
|
11 | 11 | spins: 5, //Number of spins when auto [int]
|
12 | 12 | randomize: null, //Randomize function, must return an integer with the selected position
|
13 | 13 | complete: null, //Callback function(result)
|
14 |
| - stopHidden: true //Stops animations if the element isn´t visible on the screen |
| 14 | + stopHidden: true, //Stops animations if the element isn´t visible on the screen |
| 15 | + direction: 'up' //Animation direction ['up'||'down'] |
15 | 16 | };
|
16 | 17 |
|
17 | 18 | var FX_FAST = 'slotMachineBlurFast',
|
|
208 | 209 | //Set min top offset
|
209 | 210 | this._minTop = -this._$fakeFirstTile.outerHeight();
|
210 | 211 |
|
| 212 | + //Direction parammeters |
| 213 | + this._direction = { |
| 214 | + selected: this.settings.direction === 'down' ? 'down' : 'up', |
| 215 | + up: { |
| 216 | + initial: this.getTileOffset(this.active), |
| 217 | + first: 0, |
| 218 | + last: this.getTileOffset(this.$tiles.length), |
| 219 | + to: this._maxTop, |
| 220 | + firstToLast: this.getTileOffset(this.$tiles.length), |
| 221 | + lastToFirst: 0 |
| 222 | + }, |
| 223 | + down: { |
| 224 | + initial: this.getTileOffset(this.active), |
| 225 | + first: this.getTileOffset(this.$tiles.length), |
| 226 | + last: 0, |
| 227 | + to: this._minTop, |
| 228 | + firstToLast: this.getTileOffset(this.$tiles.length), |
| 229 | + lastToFirst: 0 |
| 230 | + }, |
| 231 | + get: function(param){return this[this.selected][param];} |
| 232 | + }; |
| 233 | + |
211 | 234 | //Show active element
|
212 |
| - this.$container.css('margin-top', this.getTileOffset(this.active)); |
| 235 | + this.$container.css('margin-top', this._direction.get('initial')); |
213 | 236 |
|
214 | 237 | //Start auto animation
|
215 | 238 | if (this.settings.auto !== false) {
|
|
222 | 245 | }
|
223 | 246 |
|
224 | 247 | /**
|
225 |
| - * @desc PRIVATE - Get element offset top |
| 248 | + * @desc PUBLIC - Get element offset top |
226 | 249 | * @param int index - Element position
|
227 | 250 | * @return int - Negative offset in px
|
228 | 251 | */
|
|
235 | 258 | };
|
236 | 259 |
|
237 | 260 | /**
|
238 |
| - * @desc PRIVATE - Get current showing element index |
| 261 | + * @desc PUBLIC - Get current showing element index |
239 | 262 | * @return int - Element index
|
240 | 263 | */
|
241 | 264 | SlotMachine.prototype.getVisibleTile = function() {
|
|
260 | 283 | };
|
261 | 284 |
|
262 | 285 | /**
|
263 |
| - * @desc PRIVATE - Get random element different than last shown |
| 286 | + * @desc PUBLIC - Get random element different than last shown |
264 | 287 | * @param boolean cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat
|
265 | 288 | * @return int - Element index
|
266 | 289 | */
|
|
275 | 298 | };
|
276 | 299 |
|
277 | 300 | /**
|
278 |
| - * @desc PRIVATE - Get random element based on the custom randomize function |
| 301 | + * @desc PUBLIC - Get random element based on the custom randomize function |
279 | 302 | * @return int - Element index
|
280 | 303 | */
|
281 | 304 | SlotMachine.prototype.getCustom = function() {
|
|
293 | 316 | };
|
294 | 317 |
|
295 | 318 | /**
|
296 |
| - * @desc PRIVATE - Get the previous element |
| 319 | + * @desc PRIVATE - Get the previous element (no direction related) |
297 | 320 | * @return int - Element index
|
298 | 321 | */
|
299 |
| - SlotMachine.prototype.getPrev = function() { |
| 322 | + SlotMachine.prototype._getPrev = function() { |
300 | 323 | var prevIndex = (this.active - 1 < 0) ? (this.$tiles.length - 1) : (this.active - 1);
|
301 | 324 | return prevIndex;
|
302 | 325 | };
|
303 | 326 |
|
304 | 327 | /**
|
305 |
| - * @desc PRIVATE - Get the next element |
| 328 | + * @desc PRIVATE - Get the next element (no direction related) |
306 | 329 | * @return int - Element index
|
307 | 330 | */
|
308 |
| - SlotMachine.prototype.getNext = function() { |
| 331 | + SlotMachine.prototype._getNext = function() { |
309 | 332 | var nextIndex = (this.active + 1 < this.$tiles.length) ? (this.active + 1) : 0;
|
310 | 333 | return nextIndex;
|
311 | 334 | };
|
312 | 335 |
|
| 336 | + /** |
| 337 | + * @desc PUBLIC - Get the previous element dor selected direction |
| 338 | + * @return int - Element index |
| 339 | + */ |
| 340 | + SlotMachine.prototype.getPrev = function() { |
| 341 | + return this._direction.selected === 'up' ? this._getPrev() : this._getNext(); |
| 342 | + }; |
| 343 | + |
| 344 | + /** |
| 345 | + * @desc PUBLIC - Get the next element |
| 346 | + * @return int - Element index |
| 347 | + */ |
| 348 | + SlotMachine.prototype.getNext = function() { |
| 349 | + return this._direction.selected === 'up' ? this._getNext() : this._getPrev(); |
| 350 | + }; |
| 351 | + |
| 352 | + /** |
| 353 | + * @desc PUBLIC - Set the spin direction |
| 354 | + * @return Object - Direction data |
| 355 | + */ |
| 356 | + SlotMachine.prototype.setDirection = function(direction) { |
| 357 | + if (this.isRunning) { |
| 358 | + return; |
| 359 | + } |
| 360 | + this._direction.selected = direction === 'down' ? 'down' : 'up'; |
| 361 | + return this._direction[this._direction.selected]; |
| 362 | + }; |
| 363 | + |
313 | 364 | /**
|
314 | 365 | * @desc PRIVATE - Set CSS classes to make speed effect
|
315 | 366 | * @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP]
|
|
333 | 384 | * @desc PRIVATE - Reset active element position
|
334 | 385 | */
|
335 | 386 | SlotMachine.prototype._resetPosition = function() {
|
336 |
| - this.$container.css('margin-top', this.getTileOffset(this.active)); |
| 387 | + this.$container.css('margin-top', this._direction.get('initial')); |
337 | 388 | };
|
338 | 389 |
|
339 | 390 | /**
|
|
371 | 422 | };
|
372 | 423 |
|
373 | 424 | /**
|
374 |
| - * @desc PRIVATE - Starts shuffling the elements |
| 425 | + * @desc PUBLIC - Starts shuffling the elements |
375 | 426 | * @param int repeations - Number of shuffles (undefined to make infinite animation
|
376 | 427 | * @return int - Returns result index
|
377 | 428 | */
|
|
421 | 472 | self.stop();
|
422 | 473 | } else {
|
423 | 474 | this.$container.animate({
|
424 |
| - marginTop: this._maxTop |
| 475 | + marginTop: this._direction.get('to') |
425 | 476 | }, delay, 'linear', function() {
|
426 | 477 | //Reset top position
|
427 |
| - self.$container.css('margin-top', 0); |
| 478 | + self.$container.css('margin-top', self._direction.get('first')); |
428 | 479 |
|
429 | 480 | if (spins - 1 <= 0) {
|
430 | 481 | self.stop();
|
|
439 | 490 | };
|
440 | 491 |
|
441 | 492 | /**
|
442 |
| - * @desc PRIVATE - Stop shuffling the elements |
| 493 | + * @desc PUBLIC - Stop shuffling the elements |
443 | 494 | * @return int - Returns result index
|
444 | 495 | */
|
445 | 496 | SlotMachine.prototype.stop = function(showGradient) {
|
|
465 | 516 | if (this.futureActive > this.active) {
|
466 | 517 | //We are moving to the prev (first to last)
|
467 | 518 | if (this.active === 0 && this.futureActive === this.$tiles.length - 1) {
|
468 |
| - this.$container.css('margin-top', this.getTileOffset(this.$tiles.length)); |
| 519 | + this.$container.css('margin-top', this._direction.get('firstToLast')); |
469 | 520 | }
|
470 | 521 | } else {
|
471 | 522 | //We are moving to the next (last to first)
|
472 | 523 | if (this.active === this.$tiles.length - 1 && this.futureActive === 0) {
|
473 |
| - this.$container.css('margin-top', 0); |
| 524 | + this.$container.css('margin-top', this._direction.get('lastToFirst')); |
474 | 525 | }
|
475 | 526 | }
|
476 | 527 |
|
|
506 | 557 | };
|
507 | 558 |
|
508 | 559 | /**
|
509 |
| - * @desc PRIVATE - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively |
| 560 | + * @desc PUBLIC - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively |
510 | 561 | */
|
511 | 562 | SlotMachine.prototype.auto = function() {
|
512 | 563 | var self = this;
|
|
529 | 580 | }, this.settings.auto);
|
530 | 581 | };
|
531 | 582 |
|
532 |
| - |
533 |
| - |
534 | 583 | /*
|
535 | 584 | * Create new plugin instance if needed and return it
|
536 | 585 | */
|
|
0 commit comments