-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
198 lines (175 loc) · 7.41 KB
/
script.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
~(() => {
const options = { align: 'center', draggable: false, skipSnaps: true, loop: false, autoPlay: false, autoPlayInterval: 5 }
let carousels = []
function setupPrevNextBtns (prevBtn, nextBtn, embla) {
prevBtn.addEventListener('click', embla.scrollPrev, false)
nextBtn.addEventListener('click', embla.scrollNext, false)
}
function disablePrevNextBtns (prevBtn, nextBtn, embla) {
return () => {
if (embla.canScrollPrev()) prevBtn.removeAttribute('disabled');
else prevBtn.setAttribute('disabled', 'disabled')
if (embla.canScrollNext()) nextBtn.removeAttribute('disabled');
else nextBtn.setAttribute('disabled', 'disabled')
}
}
function autoPlay(carouselId, bool, sec = 0) {
const index = carousels.findIndex((obj) => obj.carouselId === carouselId)
if (index === -1) return
if (bool) {
if (!carousels[index].intervalId) {
carousels[index].intervalId = setInterval(() => {
if (!document.hidden) {
const currentIndex = carousels.findIndex((obj) => obj.carouselId === carouselId)
if (currentIndex === -1) return
if(carousels[currentIndex].embla.scrollProgress() !== 1) {
carousels[currentIndex].embla.scrollNext()
return
}
carousels[currentIndex].embla.scrollTo(0)
}
}, sec * 1000)
}
}
}
function initCarouseMultiple(target, options) {
return new Promise((resolve, reject) => {
const wrap = target.querySelector('.embla')
const carouselId = target.getAttribute('id')
const viewPort = wrap.querySelector('.embla__viewport')
const prevBtn = wrap.querySelector('.embla__button--prev')
const nextBtn = wrap.querySelector('.embla__button--next')
let index = carousels.findIndex((obj) => obj.carouselId === carouselId)
if (index !== -1) return
const embla = EmblaCarousel(viewPort, options)
const disablePrevAndNextBtns = disablePrevNextBtns(prevBtn, nextBtn, embla)
setupPrevNextBtns(prevBtn, nextBtn, embla)
embla.on('select', disablePrevAndNextBtns)
embla.on('init', disablePrevAndNextBtns)
carousels.push({carouselId , embla, intervalId: null})
resolve(embla)
})
}
function getDataAttr(el) {
const data = {}
const array = []
array.forEach.call(el.attributes, (attr) => {
if (/^data-/.test(attr.name)) {
var camelCaseName = attr.name.substr(5).replace(/-(.)/g, ($0, $1) => {
return $1.toUpperCase()
})
data[camelCaseName] = parseBool(attr.value)
}
})
return data
}
function carouselDestroy(carouselId) {
const index = carousels.findIndex((obj) => obj.carouselId === carouselId)
if (index === -1) return
if ('embla' in carousels[index]) carousels[index].embla.destroy()
clearInterval(carousels[index].intervalId)
carousels.splice(index, 1)
}
function parseBool(val) { return val === "true" ? true : val }
function contains(whereObj, whatObj){
const data = {}
for(let key in whatObj) {
if(!whereObj[key]) data[key] = false
}
return data
}
let add = false
let $,
isJQuery = typeof jQuery == 'function'
if (isJQuery) $ = jQuery
const isBuilder = document.querySelector('html').classList.contains('is-builder')
if (isBuilder && isJQuery) {
$(document).on('add.cards', (e) => {
const hasClass = $(e.target).hasClass('mbr-embla');
if (!hasClass || add) return Promise.resolve();
const carouselId = e.target.getAttribute('id')
carouselDestroy(carouselId)
const buildOptions = getDataAttr(e.target.querySelector('.embla'))
const falsyOptions = contains(buildOptions, options)
const fnOptions = Object.assign(buildOptions, falsyOptions)
fnOptions.draggable = false
return initCarouseMultiple(e.target, fnOptions).then((embla) => {
embla.reInit(fnOptions);
autoPlay(carouselId, fnOptions.autoPlay, fnOptions.autoPlayInterval);
add = true;
setTimeout(() => {
add = false;
}, 0);
});
}).on('delete.cards', (e) => {
const carouselId = e.target.getAttribute('id')
carouselDestroy(carouselId)
}).on('changeParameter.cards', (e, paramName, value) => {
if ($(e.target).hasClass('mbr-embla')) {
const carouselId = e.target.getAttribute('id')
const prodOptions = getDataAttr(e.target.querySelector('.embla'))
const falsyOptions = contains(prodOptions, options)
const fnOptions = Object.assign(prodOptions, falsyOptions)
switch (paramName) {
case 'loop':
if (value) {
options.loop = true
} else {
options.loop = false
}
break;
case 'autoplay':
options.autoPlay = value
break;
case 'interval':
options.autoPlayInterval = +value
break;
default:
break;
}
carouselDestroy(carouselId)
fnOptions.draggable = false
initCarouseMultiple(e.target, fnOptions)
autoPlay(carouselId, fnOptions.autoPlay, fnOptions.autoPlayInterval)
}
})
} else {
if (typeof window.initCarouseMultiplePlugin === 'undefined') {
window.initCarouseMultiplePlugin = true
document.querySelectorAll('.mbr-embla').forEach((el) => {
const carouselId = el.getAttribute('id')
const prodOptions = getDataAttr(el.querySelector('.embla'))
const falsyOptions = contains(prodOptions, options)
const fnOptions = Object.assign(prodOptions, falsyOptions)
initCarouseMultiple(el, Object.assign(prodOptions, falsyOptions))
autoPlay(carouselId, fnOptions.autoPlay, +fnOptions.autoPlayInterval)
})
}
}
})()
//service section owl carousel
$(".fruits_owl-carousel").owlCarousel({
autoplay: true,
loop: true,
margin: 20,
autoHeight: true,
nav: true,
navText: [
'<i class="fa fa-long-arrow-left" aria-hidden="true"></i>',
'<i class="fa fa-long-arrow-right" aria-hidden="true"></i>',
],
responsive: {
0: {
items: 1,
},
576: {
items: 2,
},
768: {
items: 3,
},
991: {
items: 4,
},
},
});