-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentNavigationCarousel.js
More file actions
247 lines (191 loc) · 7.03 KB
/
contentNavigationCarousel.js
File metadata and controls
247 lines (191 loc) · 7.03 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
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
/*!
* contentNavigationCarousel v1.0.1 (https://github.com/TechTarget/contentNavigationCarousel)
* 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
*/
;(function ( $, window, undefined ) {
'use strict';
// the default settings
var pluginName = 'contentNavigationCarousel';
var defaults = {
autoplay: true,
autoplaySpeed: 10000,
mouseEvent: 'click',
switchSpeed: 500,
equalizeHeights: true
};
// plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
// plugin vars
var o = this.options;
var carousel = $(this.element);
var contentCollection = carousel.find('.contentCollection');
var contentCollectionHeight = 0;
var contentItems = contentCollection.find('.contentItem');
var contentItemHeight = 0;
var contentItemOffset = 0;
var contentLinks = contentItems.find('.contentLink');
var list = $('<div />', {'class': 'contentNavigation'});
var listHeight = 0;
var listItems;
var listItemsCount = 0;
var listItem;
var activeItemIndex = -1;
var nextItemIndex = 0;
var autoplayOverride = carousel.attr('data-autoplay');
// autoplay object
var autoplay = {
// initialize autoplay
start: function() {
this.timer = setTimeout(
$.proxy(this.pause, this),
o.autoplaySpeed
);
},
// don't restart the autoplay until the user moves their mouse pointer off list item
// this pause is in order to give them as much time as they want to read the content
pause: function(){
this.interval = window.setInterval(
$.proxy(this.checkMouseLocation, this),
500
);
},
// check the location of the mouse pointer and continue with autoplay progression
// unless mouse is over active list item
checkMouseLocation: function() {
if (listItems.eq(activeItemIndex).find('a').data('mouse') !== 'on') {
clearInterval(this.interval);
this.getNextItem();
}
},
// determine the next item to select
getNextItem: function(){
// increment the next item index or reset if at end of list
nextItemIndex = (nextItemIndex === listItemsCount - 1) ? 0 : nextItemIndex + 1;
// select next item in list
itemSelect(nextItemIndex);
},
// stop autoplay
stop: function() {
clearTimeout(this.timer);
}
};
// select item to trigger mouseEvent by index
// also pass data as 'triggered' to indicate this was not a user event
var itemSelect = function (index) {
listItems.eq(index).find('a').trigger(o.mouseEvent, ['triggered']);
};
// event handler to show the selected content item
var showContent = function (e, eType) {
// stop autoplay
if (o.autoplay) { autoplay.stop(); }
// if mouse event is a click, prevent the browser following the href
if (o.mouseEvent === 'click') {
e.preventDefault();
}
// cache item selector
listItem = $(this);
// if we got here through a trigger, than change 'mouse' data attr to off
if (eType === 'triggered') {
listItem.data('mouse','off');
}
// get index of current item focus
nextItemIndex = listItem.data('index');
// remove active class
// need to check entire item stack because of animation race conditions
listItems.removeClass('active');
// add active class to current item
listItems.eq(nextItemIndex).addClass('active');
// if we're not on the active item then switch out visible content
if (nextItemIndex !== activeItemIndex) {
contentItems.eq(activeItemIndex).fadeOut(o.switchSpeed/2, function() {
//fade in content on callback
contentItems.eq(nextItemIndex).fadeIn(o.switchSpeed/2);
});
// update activeItemIndex
activeItemIndex = nextItemIndex;
}
// restart autplay
if (o.autoplay) { autoplay.start(); }
};
// this is an inline override for the autoplay; by using the attribute 'data-autoplay' autoplay could be
// turned on (or it's speed changed) on a widget-level; it can also turn off autoplay if it's set to 0
if (autoplayOverride) {
if (autoplayOverride > 0) {
o.autoplay = true;
o.autoplaySpeed = autoplayOverride;
} else {
o.autoplay = false;
o.autoplaySpeed = 0;
}
}
// don't show a list of just one link
if (contentLinks.length <= 1) { return; }
// 'hover' is a helper name, change to 'mouseenter'
if (o.mouseEvent === 'hover') { o.mouseEvent = 'mouseenter'; }
// get list of links from content items; adding index as data attr since it will be faster to add here rather than figure out index later
listItems = $.map(contentLinks, function(link, i) {
listItemsCount++;
return '<li><a href="' + link.href + '" data-index="' + i + '">' + (link.textContent || link.innerText) + '</a></li>';
});
// create jQ collection of of list items since we'll need it later
listItems = $(listItems.join(''));
// insert list into DOM
list.append( $('<ul>').append(listItems) ).insertAfter(contentCollection);
// get the height of content navigation
listHeight = list.height();
// get the height of the tallest content item
contentCollectionHeight = Math.max.apply(null, contentItems.map(function () { return $(this).height(); }).get());
// set the height of the content container to the tallest content item if it's overflow is hidden
// otherwise no content will show
if (contentCollection.css('overflow') === 'hidden') {
contentCollection.height(contentCollectionHeight);
}
// equalize the heights of the collection and nav or
// if set to false, set height of collection to tallest contant item
if (o.equalizeHeights) {
// is there a more clever way to write this?
if (listHeight > contentCollectionHeight) {
contentCollection.height(listHeight);
} else {
list.height(contentCollectionHeight);
}
}
// set the top offsets of all the content items and
// now that their heights are known set display to none
contentItems.each( function() {
contentItemHeight = $(this).height();
contentItemOffset = Math.round((listHeight - contentItemHeight)/2);
$(this).css({'display': 'none','top': contentItemOffset + 'px'});
});
// register event handler to add data attribute of mouse status over element
listItems.on({
mouseenter : function() {
$(this).data('mouse', 'on');
},
mouseleave : function() {
$(this).data('mouse', 'off');
}
}, 'a');
// register event handler for user-defined mouse event
listItems.on(o.mouseEvent, 'a', showContent);
// initialize plugin by selecting the first item in the content navigation list
itemSelect(nextItemIndex);
};
// a lightweight plugin wrapper around the constructor preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
};
}(jQuery, window));