-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslideProjectorCarousel.coffee
More file actions
164 lines (137 loc) · 4.75 KB
/
slideProjectorCarousel.coffee
File metadata and controls
164 lines (137 loc) · 4.75 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
###!
slideProjectorCarousel v1.0.3 (https://github.com/TechTarget/slideProjectorCarousel)
Author: Morgan Wigmanich <okize123@gmail.com> (http://github.com/okize)
Copyright (c) 2013 | Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php
###
# use AMD or browser globals to create a jQuery plugin.
((factory) ->
if typeof define is "function" and define.amd
define ["jquery"], factory
else
factory jQuery
) ($) ->
# plugin constructor
Plugin = (element, options) ->
@element = element
@options = $.extend({}, defaults, options)
@_defaults = defaults
@_name = pluginName
@init()
pluginName = "slideProjectorCarousel"
# the default settings
defaults =
autoplay: true
autoplaySpeed: 5000
slidesToShow: 3
slidesToMove: 3
Plugin::init = ->
# plugin vars
o = @options
carousel = $(@element)
projectionContainer = carousel.find(".projection")
projections = projectionContainer.find("li")
slidesContainer = carousel.find(".slides")
slidesList = slidesContainer.find("ul")
slides = slidesList.find("li")
# can't get width here since width will depend on
# whether there are navigation controls or not
slideWidth = 0
slideCount = slides.length
currentSlide = -1
nextSlide = 0
autoplayOverride = carousel.data("autoplay")
navigation = {} # the navigation object
# creates the html that compromises the navigation elements
# inserts itself into dom and binds event handlers to arrows
addNavigation = ->
# @todo
navigation.btnPrevious = $("<a>",
class: "slideControls previous disabled"
href: "#"
title: "Previous"
text: "Previous"
).on("click", (e) ->
e.preventDefault()
unless $(this).hasClass("disabled")
slidesContainer.trigger "slides.move", "previous"
)
# @todo
navigation.btnNext = $("<a>",
class: "slideControls next"
href: "#"
title: "Next"
text: "Next"
).on("click", (e) ->
e.preventDefault()
slidesContainer.trigger "slides.move", "next" unless $(this).hasClass("disabled")
)
# @todo
slidesContainer.addClass("hasControls").append navigation.btnPrevious, navigation.btnNext
# @todo
slideWidth = slides.outerWidth()
# @todo
moveStrip = (e, direction) ->
if direction is "previous"
navigation.btnPrevious.addClass "disabled"
navigation.btnNext.removeClass "disabled"
slidesList.css "left", 0
if direction is "next"
navigation.btnPrevious.removeClass "disabled"
navigation.btnNext.addClass "disabled"
slidesList.css "left", -(slideWidth * o.slidesToMove)
# inline override for autoplay; use the attribute 'data-autoplay' to control autoplay speed in the view layer
# setting 'data-autoplay' to 0 will disable autoplay
if typeof autoplayOverride isnt "undefined"
if autoplayOverride > 0
o.autoplay = true
o.autoplaySpeed = autoplayOverride
else
o.autoplay = false
o.autoplaySpeed = 0
# autoplay object
autoplay =
# initialize autoplay
start: ->
@timer = setTimeout($.proxy(@getNextSlide, this), o.autoplaySpeed)
# determine the next item to select
getNextSlide: ->
# increment the next item index or reset if at end of list
nextSlide = (if (nextSlide is slideCount - 1) then 0 else nextSlide + 1)
# select next item in list
slides.eq(nextSlide).trigger "click"
# stop autoplay
stop: ->
clearTimeout @timer
# @todo
slides.on "click", (e) ->
e.preventDefault()
autoplay.stop() if o.autoplay
slides.removeClass "active"
$this = $(this)
$this.addClass "active"
index = $this.index()
currentSlide = index
nextSlide = index
projections.css "z-index", 0
projections.eq(index).css
"z-index": 1
top: 0
# restart autoplay
autoplay.start() if o.autoplay
# @todo
projections.on "click", (e) ->
e.preventDefault()
link = $(this).find("a")
openIn = (if !!link.attr("target") then "_blank" else "_self")
window.open link.attr("href"), openIn
# if there are more slides than the amount set in slidesToShow, add navigation to slides
addNavigation() if slideCount > o.slidesToShow
# restart autplay
autoplay.start() if o.autoplay
# bind handlers
slidesContainer.on "slides.move", moveStrip
# a lightweight plugin wrapper around the constructor preventing against multiple instantiations
$.fn[pluginName] = (options) ->
@each ->
$.data this, "plugin_" + pluginName, new Plugin(this, options) unless $.data(this, "plugin_" + pluginName)