-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.js
310 lines (273 loc) · 9.27 KB
/
prometheus.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
;(function (window, $, undefined) {
"use strict";
/**
* This function provides the tile creation and the transition handling as the name says
* @param {string} way The way of the transition (= in/out)
* @param {Object} settings The animation perferences given by this.settings.animation
* @param {string} activeClass The activeClass from this.settings
* @param {string} image The url of the image get by jQuery like "url(http://domain.com/assets/slide1.jpg)"
*/
$.fn.transition = function (way, settings, activeClass, image) {
var $container = $(this),
width = $container.width(),
height = $container.height(),
$img = $container.find('.tiles'),
initialized = ($img.find('.tile').length > 0),
imageBackground = (initialized ? image : $img.css('background-image')),
n_tiles = settings.cols * settings.rows,
tiles = [],
$tiles,
range = function (min, max, rand) {
var arr = ( new Array(++max - min) )
.join('.').split('.')
.map(function (v, i) {
return min + i
});
return rand
? arr.map(function (v) {
return [ Math.random(), v ]
})
.sort().map(function (v) {
return v[ 1 ]
})
: arr;
},
createTilesByImage = function () {
for (var i = 0; i < n_tiles; i++) {
tiles.push('<div class="tile"></div>');
}
$tiles = $(tiles.join(''));
$tiles.css({
width: width / settings.cols,
height: height / settings.rows,
backgroundImage: imageBackground
}).css3({
transition: 'none'
});
$img.append($tiles);
$tiles.each(function () {
var pos = $(this).position();
$(this).css('backgroundPosition', -pos.left + 'px ' + -pos.top + 'px');
});
$tiles.css3({
'transition-property': 'all',
'transition-duration': '.3s',
'transition-timing-function': 'cubic-bezier(0.445, 0.050, 0.550, 0.950)'
});
},
animateTilesWithDelay = function () {
var tilesArr = range(0, n_tiles, settings.random),
tileSpeed = settings.speed / n_tiles;
tilesArr.forEach(function (tile, i) {
$tiles.eq(tile).css3({'transition-delay': ((i * tileSpeed) / 1000) + 's'}).addClass('animate');
});
};
createTilesByImage();
$img.css('background-image', '');
animateTilesWithDelay();
};
/**
* Creator of magic, constructor of Prometheus
* @param {Object} $slider Requires a jQuery node
* @param {Object=} options An optional object of options to override the defaults
* @constructor
*/
var Prometheus = function ($slider, options) {
var _this = this;
this.$slider = $slider;
this.settings = (options != undefined) ? $.extend({}, this.defaults, options) : this.defaults;
this.settings.animation = ((options != undefined) && (options.animation != undefined)) ? $.extend({}, this.defaults.animation, options.animation) : this.defaults.animation;
this.$slides = this.$slider.find(this.settings.slidesSelector);
this.sliderLocked = false;
this.timer = undefined;
this.transitionEndTimer = undefined;
this.totalImages = (this.$slides.length);
this.currentPos = 0;
if (this.$slider.data('initialized')) return;
this.$slider.data('initialized', true);
this.$slides.eq(0).addClass(this.settings.activeClass);
this.$slides.eq(1).addClass('preparator');
this.turnImagesIntoContainers();
this.initScriptsFromSettings();
if (this.settings.autoSlide) {
this.timer = setTimeout(function () {
_this.slide(1);
}, this.settings.startDuration);
}
};
/**
* It initializes the scripts which are defined in the setting
*/
Prometheus.prototype.initScriptsFromSettings = function () {
var _this = this;
$.each(['beforeSlide', 'beforeTransition', 'afterTransition', 'afterSlide'], function () {
var __this = this.toString();
$.subscribe(__this, function () {
try {
_this.settings[__this]().call();
} catch (e) {
$.each(_this.settings[__this], function (i) {
_this.settings[__this][i]();
});
}
});
});
};
/**
* Replaces every <img/> tag to a <div/> with a background image
*/
Prometheus.prototype.turnImagesIntoContainers = function () {
var _this = this,
i = 0;
this.images = [];
this.$slides.each(function () {
var $slide = $(this),
$img = $slide.find('img[data-role=background]'),
$tiles = $('<div class="tiles"></div>');
if (!$img.length) {
_this.images[i] = '';
i++;
return;
}
$img.hide();
$slide.append($tiles);
$tiles.css('background-image', 'url(' + $img.attr('src') + ')');
if ($img.data()) $tiles.data($img.data());
if (!!$img.attr('class')) $tiles.addClass($img.attr('class'));
_this.images[i] = $tiles.css('background-image');
$img.remove();
i++;
});
};
/**
* Heart of Prometheus
* @param {number} direction Represents an index of where we want to go
* @param {boolean=} forceSlide Forces to slide, even if the slider is locked
*/
Prometheus.prototype.slide = function (direction, forceSlide) {
var _this = this,
currentPos = this.currentPos,
nextPos = this.getNextIndex(direction);
$.publish('beforeSlide', nextPos);
if (!this.sliderLocked || forceSlide) {
var $currentSlide = this.$slides.eq(currentPos),
$nextSlide = this.$slides.eq(nextPos);
if ($nextSlide.data('animation')) this.$slider.find('ul').removeClass().addClass($nextSlide.data('animation'));
else this.$slider.find('ul').removeClass().addClass(this.settings.animation.type);
$currentSlide.removeClass('preparator');
$nextSlide.addClass('preparator');
$.publish('beforeTransition', nextPos);
$currentSlide.transition('out', this.settings.animation, this.settings.activeClass, this.images[this.currentPos]);
if (this.transitionEndTimer) clearTimeout(this.transitionEndTimer);
this.transitionEndTimer = setTimeout(function () {
$currentSlide.removeClass(_this.settings.activeClass);
$currentSlide.find('.tiles').css('background-image', _this.images[currentPos]);
$currentSlide.find('.tile').remove();
$nextSlide.addClass(_this.settings.activeClass);
$nextSlide.removeClass('preparator');
$.publish('afterTransition', nextPos);
}, (this.settings.animation.speed + 180));
this.currentPos = nextPos;
}
$.publish('afterSlide', nextPos);
if (this.settings.autoSlide) {
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(function () {
_this.slide(1);
}, this.settings.duration);
}
};
/**
* Counts the next slides number
* @param {number} to Represents an index of where we want to go
* @returns {number}
*/
Prometheus.prototype.getNextIndex = function (to) {
if ((this.currentPos + to) >= this.totalImages) return 0;
else if ((this.currentPos + to) < 0) return this.totalImages - 1;
else return this.currentPos + to;
};
/**
* The default options of Prometheus
*/
Prometheus.prototype.defaults = {
activeClass: 'active',
slidesSelector: "li",
autoSlide: true,
startDuration: 5000,
duration: 3000,
animation: {
type: 'fade',
cols: 7,
rows: 2,
random: true,
speed: 2000
},
stopOnHover: false,
directionNavigation: false,
directionNavigationPrev: '',
directionNavigationNext: '',
controlNavigation: false,
timerBar: false,
keyboardNavigation: false,
keyboardNavigationPrev: 37,
keyboardNavigationNext: 39,
mouseScrollNavigation: false,
touchNavigation: false,
parallaxEffecting: false,
beforeSlide: [],
beforeTransition: [],
afterTransition: [],
afterSlide: []
};
/**
* Array of transitions used by random transition
* @type {Array}
*/
Prometheus.prototype.transitions = [
'fade',
'slide',
'blocks',
'blocksZoom',
'dots',
'googleNowCards'
];
/**
* Initializer of Prometheus Slider
* @param {Object=} options
* @returns {*|jQuery}
* @constructor
*/
$.fn.Prometheus = function (options) {
return $(this).each(
function () {
var modifiers = [
'stopOnHover',
'directionNavigation',
'controlNavigation',
'timerBar',
'keyboardNavigation',
'mouseScrollNavigation',
'touchNavigation',
'parallaxEffecting'
],
loadNecessaryPlugins = function () {
var i = 0;
while (i < modifiers.length) {
var modifier = modifiers[i];
if (modifier.indexOf(p.settings) && (p.settings[modifier] === true)) {
p[modifier]();
}
i++;
}
},
p = new Prometheus($(this), options);
if (typeof PrometheusDecorators === 'undefined')
window.PrometheusDecorators = {};
$.extend(Prometheus.prototype, window.PrometheusDecorators);
//p.initializeRandomTransitions();
loadNecessaryPlugins();
}
);
};
})(window, jQuery);