Last active
October 12, 2015 22:23
-
-
Save culttm/43820de790bcc0ad8db4 to your computer and use it in GitHub Desktop.
Init owlCarousel with custom DOM options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Init owlCarousel with custom DOM options | |
http://www.owlcarousel.owlgraphic.com/docs/api-options.html | |
@plugin-options | |
@responsive-options | |
@nav-text | |
https://github.com/smashingboxes/OwlCarousel2 | |
### | |
+ do ($ = jQuery, window) -> | |
'use strict'; | |
class CreateCarousel | |
constructor: (element) -> | |
@.el = $(element) | |
@.options = { | |
loop: false | |
nav: false | |
responsive: {} | |
navText: [] | |
} | |
build: -> | |
$this = @.el | |
opt = @.extendOptions() | |
if typeof $.fn.owlCarousel == 'function' | |
$this.owlCarousel opt | |
else | |
console.log 'You must install owlCarousel https://github.com/smashingboxes/OwlCarousel2' | |
getOptions: -> | |
$this = @.el | |
optionsObj = { | |
domOptions: $this.data('plugin-options') | |
responsiveDomOptions: $this.data('responsive-options') | |
navText: $this.data('nav-text') | |
} | |
extendOptions: -> | |
$this = @.el | |
$options = @.options | |
$optionsObj = @.getOptions() | |
if $optionsObj? and Object.keys($optionsObj).length != 0 | |
$.extend $options, $optionsObj.domOptions | |
if $optionsObj.responsiveDomOptions? and Object.keys($optionsObj.responsiveDomOptions).length != 0 | |
opts = prepareResponseOption $optionsObj.responsiveDomOptions | |
$.extend true, $options.responsive, opts | |
if $optionsObj.navText? | |
$.extend true, $options.navText, $optionsObj.navText.split(',') | |
return $options | |
prepareResponseOption = (opts) -> | |
for key, value of opts | |
opts[key] = { | |
items: value | |
} | |
return opts | |
Plugin = (option) -> | |
@.each -> | |
$this = $(@) | |
data = $this.data('cl.createcarousel') | |
if !data | |
$this.data('cl.createcarousel', (data = new CreateCarousel @)) | |
if (typeof option == 'string') | |
data[option]() | |
return | |
$ -> | |
$('.carousel').each -> | |
Plugin.call($(@), 'build') | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment