recipe-simulator.js
6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
jQuery(function($){
$(document).ready(function(){
RecipeSimulator.init();
});
var RecipeSimulator = {
init: function(){
// Check if we have to invoke the methods
if ( this.hasRecipeSimulator() ){
// Get all simulators
let $simulators = $( '.uo-recipe-simulator' );
// Init each simulator
let _this = this,
Simulator;
$simulators.each( function( index ){
Simulator = $.extend( true, {}, _this.Simulator );
Simulator.init( $( this ) );
});
}
},
hasRecipeSimulator: function(){
let output = false;
if ( typeof window.hasRecipeSimulator !== 'undefined' || window.hasRecipeSimulator !== null && window.hasRecipeSimulator == true ){
output = true;
}
return output;
},
Simulator: {
config: {
visibleItems: 8, // How many items do you want to show?
maxSelected: 3, // How many tasks do you want to select? this number is the maximum, 1 is the minimum
carousel: {
interval: 5000, // Time between each change in ms
duration: 600, // The animation duration
}
},
$elements: {},
init: function( $simulator, $list, $items ){
// Save elements
this.$elements.simulator = $simulator;
this.$elements.list = $simulator.find( '.uo-recipe-simulator__items ul' ),
this.$elements.items = $simulator.find( '.uo-recipe-simulator__items li' );
// Check visible items
if ( this.$elements.items.length < this.config.visibleItems ){
this.config.visibleItems = this.$elements.items.length;
}
// Shuffle Items
this.shuffleItems();
// Set new height based on the visible items
this.setCarouselHeight();
// Move one time to init
this.moveItemCarousel();
// Create Carousel
this.playCarousel();
},
shuffleItems: function(){
while ( this.$elements.items.length ){
this.$elements.list.append( this.$elements.items.splice( Math.floor( Math.random() * this.$elements.items.length ), 1 )[ 0 ] );
}
// Get new order
this.$elements.items = this.$elements.list.find( 'li' );
// Get first item
this.$elements.firstItem = this.$elements.items.eq( 0 );
},
playCarousel(){
// Start carousel animation
setTimeout( () => {
// Move one time
this.moveItemCarousel();
// Play carousel
this.playCarousel();
}, this.config.carousel.interval );
},
moveItemCarousel(){
// Remove selected elements before moving
this.$elements.items.removeClass( 'uo-recipe-simulator__item--selected' );
// Move element
this.$elements.firstItem.animate( { marginTop: - this.$elements.firstItem.outerHeight(), opacity: 0 }, this.config.carousel.duration, () => {
// Move element to the end
this.$elements.firstItem.appendTo( this.$elements.list );
// Show element
this.$elements.firstItem.css( { 'margin': 0 } );
this.$elements.firstItem.animate( { opacity: 1 }, this.config.carousel.duration );
// Find new first element
this.$elements.firstItem = this.$elements.list.find( 'li:first' );
// Update items
this.$elements.items = this.$elements.list.find( 'li' );
// Set new height based on the new visible items
this.setCarouselHeight();
// Select new items
this.getRandomItems().addClass( 'uo-recipe-simulator__item--selected' );
});
},
setCarouselHeight(){
// Set new height based on the new visible items
this.$elements.list.css({ height: Tools.sumHeight( this.getVisibleItems() ) });
},
getVisibleItems(){
// Get all the silibings elements and get the following ( this.config.visibleItems - 1 ). -1 because nextAll doesn't return itself
return $([ this.$elements.firstItem, this.$elements.firstItem.nextAll().slice( 0, this.config.visibleItems - 1 ) ]).map( function(){ return this.get() });
},
getRandomItems(){
// Get visible items
let items = this.getVisibleItems(),
randomItems = [];
// Get the number of items to select
let toSelect = Tools.randomNumber( 1, this.config.maxSelected );
// Get random items
for ( let i = 1; i <= toSelect; i++ ){
// Select a random item
let randomSelect = Tools.randomNumber( 0, items.length - 1 ),
randomItem = items.eq( randomSelect );
// Save random item
randomItems.push( randomItem );
// Delete element from items
items.filter( function(){
let $this = $( this ),
output = true;
if ( $this.is( randomItem ) ){
output = false;
}
});
}
// Return items ( in jQuery wrapped set of elements )
return $( randomItems ).map( function(){ return this.get() });
}
},
}
var Tools = {
contains_element: function (haystack, arr){
/*
* Check if an array has at least on element from another array
* Example:
* A = [1, 2, 3]; B = [5, 3, 7]; C = [8, 9, 10]
* contains_element( A, B ) => true
* contains_element( A, C ) => false
* contains_element( B, C ) => false
*/
return arr.some(function (v){
return haystack.indexOf(v) >= 0;
});
},
sumHeight( nodes ){
let sum = 0;
nodes.each( function(){
sum += $(this).outerHeight();
});
return sum;
},
randomNumber: function( min, max ){
return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
}
}
});