@websy/websy-designs 1.1.1 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,7 @@
22
22
  APIService
23
23
  ButtonGroup
24
24
  WebsyUtils
25
+ WebsyCarousel
25
26
  Pager
26
27
  */
27
28
 
@@ -197,6 +198,189 @@ class ButtonGroup {
197
198
  }
198
199
  }
199
200
 
201
+ /* global */
202
+
203
+ class WebsyCarousel {
204
+ constructor (elementId, options) {
205
+ const DEFAULTS = {
206
+ currentFrame: 0,
207
+ frameDuration: 4000,
208
+ showFrameSelector: true,
209
+ showPrevNext: true
210
+ }
211
+ this.playTimeoutFn = null
212
+ this.options = Object.assign({}, DEFAULTS, options)
213
+ if (!elementId) {
214
+ console.log('No element Id provided')
215
+ }
216
+ const el = document.getElementById(elementId)
217
+ if (el) {
218
+ this.elementId = elementId
219
+ el.addEventListener('click', this.handleClick.bind(this))
220
+ this.render()
221
+ }
222
+ }
223
+ handleClick (event) {
224
+ if (event.target.classList.contains('websy-next-arrow')) {
225
+ this.next()
226
+ }
227
+ if (event.target.classList.contains('websy-prev-arrow')) {
228
+ this.prev()
229
+ }
230
+ if (event.target.classList.contains('websy-progress-btn' || 'websy-progress-btn-active')) {
231
+ const index = +event.target.getAttribute('data-index')
232
+ let prevFrameIndex = this.options.currentFrame
233
+ this.options.currentFrame = index
234
+ this.showFrame(prevFrameIndex, index)
235
+ }
236
+ }
237
+ next () {
238
+ this.pause()
239
+ let prevFrameIndex = this.options.currentFrame
240
+ if (this.options.currentFrame === this.options.frames.length - 1) {
241
+ this.options.currentFrame = 0
242
+ }
243
+ else {
244
+ this.options.currentFrame++
245
+ }
246
+ this.showFrame(prevFrameIndex, this.options.currentFrame)
247
+ this.play()
248
+ // document.getElementById(`${this.elementId}_frame_${this.options.currentFrame}`)
249
+ // .style.transform = `translateX(-100%)`
250
+ // if (`${this.options.currentFrame === this.options.frames.length - 1}`) {
251
+ // document.getElementById`${this.elementId}_frame_${this.options.currentFrame}`.style.transform = `translateX('-100%')`
252
+ // }
253
+ }
254
+ pause () {
255
+ if (this.playTimeoutFn) {
256
+ clearTimeout(this.playTimeoutFn)
257
+ }
258
+ }
259
+ play () {
260
+ this.playTimeoutFn = setTimeout(() => {
261
+ let prevFrameIndex = this.options.currentFrame
262
+ if (this.options.currentFrame === this.options.frames.length - 1) {
263
+ this.options.currentFrame = 0
264
+ }
265
+ else {
266
+ this.options.currentFrame++
267
+ }
268
+ this.showFrame(prevFrameIndex, this.options.currentFrame)
269
+ this.play()
270
+ }, this.options.frameDuration)
271
+ }
272
+ prev () {
273
+ this.pause()
274
+ let prevFrameIndex = this.options.currentFrame
275
+ if (this.options.currentFrame === 0) {
276
+ this.options.currentFrame = this.options.frames.length - 1
277
+ }
278
+ else {
279
+ this.options.currentFrame--
280
+ }
281
+ this.showFrame(prevFrameIndex, this.options.currentFrame)
282
+ this.play()
283
+ // document.getElementById(`${this.elementId}_frame_${this.options.currentFrame}`)
284
+ // .style.transform = `translateX(100%)`
285
+ }
286
+
287
+ render (options) {
288
+ this.options = Object.assign({}, this.options, options)
289
+ this.resize()
290
+ }
291
+
292
+ resize () {
293
+ const el = document.getElementById(this.elementId)
294
+ if (el) {
295
+ let html = `
296
+ <div class="websy-carousel">
297
+ `
298
+ this.options.frames.forEach((frame, frameIndex) => {
299
+ html += `
300
+ <div id="${this.elementId}_frame_${frameIndex}" class="websy-frame-container animate" style="transform: translateX(${frameIndex === 0 ? '0' : '100%'})">
301
+ `
302
+ frame.images.forEach(image => {
303
+ html += `
304
+ <div style="${image.style || 'position: absolute; width: 100%; height: 100%; top: 0; left: 0;'} background-image: url('${image.url}')" class="${image.classes || ''} websy-carousel-image">
305
+ </div>
306
+ `
307
+ })
308
+ frame.text && frame.text.forEach(text => {
309
+ html += `
310
+ <div style="${text.style || 'position: absolute; width: 100%; height: 100%; top: 0; left: 0;'}" class="${text.classes || ''} websy-carousel-image">
311
+ ${text.html}
312
+ </div>
313
+ `
314
+ })
315
+ html += `</div>`
316
+ })
317
+ if (this.options.showFrameSelector === true) {
318
+ html += `<div class="websy-btn-parent">`
319
+ this.options.frames.forEach((frame, frameIndex) => {
320
+ html += `
321
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-index="${frameIndex}" id="${this.elementId}_selector_${frameIndex}"
322
+ class="websy-progress-btn ${this.options.currentFrame === frameIndex ? 'websy-progress-btn-active' : ''}">
323
+ <title>Ellipse</title><circle cx="256" cy="256" r="192" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"/>
324
+ </svg>
325
+ `
326
+ })
327
+ html += `</div>`
328
+ }
329
+ if (this.options.showPrevNext === true) {
330
+ html += `
331
+ <svg xmlns="http://www.w3.org/2000/svg" class="websy-prev-arrow"
332
+ viewBox="0 0 512 512">
333
+ <title>Caret Back</title>
334
+ <path d="M321.94 98L158.82 237.78a24 24 0 000 36.44L321.94 414c15.57 13.34 39.62 2.28 39.62-18.22v-279.6c0-20.5-24.05-31.56-39.62-18.18z"/>
335
+ </svg>
336
+ </div>
337
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="websy-next-arrow">
338
+ <title>Caret Forward</title>
339
+ <path d="M190.06 414l163.12-139.78a24 24 0 000-36.44L190.06 98c-15.57-13.34-39.62-2.28-39.62 18.22v279.6c0 20.5 24.05 31.56 39.62 18.18z"/>
340
+ </svg>
341
+ `
342
+ }
343
+ html += `
344
+ </div>
345
+ `
346
+ el.innerHTML = html
347
+ }
348
+ this.play()
349
+ // this.showFrameSelector()
350
+ }
351
+
352
+ showFrame (prevFrameIndex, currFrameIndex) {
353
+ let prevTranslateX = prevFrameIndex > currFrameIndex ? '100%' : '-100%'
354
+ let nextTranslateX = prevFrameIndex < currFrameIndex ? '100%' : '-100%'
355
+ if (currFrameIndex === 0 && prevFrameIndex === this.options.frames.length - 1) {
356
+ prevTranslateX = '-100%'
357
+ nextTranslateX = '100%'
358
+ }
359
+ else if (prevFrameIndex === 0 && currFrameIndex === this.options.frames.length - 1) {
360
+ prevTranslateX = '100%'
361
+ nextTranslateX = '-100%'
362
+ }
363
+ const prevF = document.getElementById(
364
+ `${this.elementId}_frame_${prevFrameIndex}`)
365
+ prevF.style.transform = `translateX(${prevTranslateX})`
366
+ const btnInactive = document.getElementById(`${this.elementId}_selector_${prevFrameIndex}`)
367
+ btnInactive.classList.remove('websy-progress-btn-active')
368
+ const newF = document.getElementById(`${this.elementId}_frame_${currFrameIndex}`)
369
+ newF.classList.remove('animate')
370
+ newF.style.transform = `translateX(${nextTranslateX})`
371
+ setTimeout(() => {
372
+ newF.classList.add('animate')
373
+ newF.style.transform = 'translateX(0%)'
374
+ }, 100)
375
+
376
+ const btnActive = document.getElementById(`${this.elementId}_selector_${currFrameIndex}`)
377
+ btnActive.classList.add('websy-progress-btn-active')
378
+ }
379
+
380
+ // showFrameSelector () {
381
+ // }
382
+ }
383
+
200
384
  class WebsyDatePicker {
201
385
  constructor (elementId, options) {
202
386
  this.oneDay = 1000 * 60 * 60 * 24