-
Notifications
You must be signed in to change notification settings - Fork 0
/
pres.js
253 lines (229 loc) · 6.96 KB
/
pres.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
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
248
249
250
251
252
253
/*!
* Simple presentation engine
* @author Konstantin Likhter <konstantin@likhter.ru>
*/
;
!function(window, undefined) {
// add .exists routine to jQuery
if (typeof $.fn.exists !== 'function') {
$.fn.exists = function() {
return this.length > 0;
};
}
/**
* Use Object.keys if available or use jQuery based solution
* for obtaining all of object's keys.
*
* @type {Function}
*/
var getKeys = (function() {
return typeof Object.keys === 'function'
? Object.keys
: function(obj) {
return obj.map(function(key,val) {
return val;
});
};
})();
var
/**
* List of settings for presentations grabbed from `data-` attributes
* @type {Object}
*/
presentationConfig,
/**
* User-specified config for plug-in.
*
* @type {Object}
*/
config = {
presentationSelector: '.presentation',
headerSelector: '.header',
footerSelector: '.footer',
slideSelector: '.slide',
presentationSelector: '.presentation',
headerSelector: '.header',
footerSelector: '.footer',
slideSelector: '.slide',
contentsSelector: '.contents'
};
var
/** @type {jQuery|null} */
$currentSlide = null,
/** @type {jQuery|null} */
$nextSlide = null,
/** @type {jQuery|null} */
$prevSlide = null;
/**
* List of effects applicable for slides.
*
* Every effect is described by function where first parameter is pointer to currently
* displayed slide (jQuery object) and second parameter is pointer to jquery object of
* slide to be displayed.
*
* @type {Object}
*/
var effects = {
toggle: function($current, $next) {
$current.hide();
$next.show();
},
fade: function($current, $next) {
$current.fadeOut();
$next.fadeIn();
},
slideDown: function($current, $next) {
$current.slideUp();
$next.slideDown();
}
};
/**
* Apply key bindings. See keyMap variable for details.
* @function
*/
var applyKeyBindings = function() {
$.each(keyMap, function(code, method) {
$(document).bind('keydown.key' + code, function(e) {
var pressed = e.keyCode || e.which;
if (pressed == code) {
method();
return false;
}
});
});
};
/**
* Reset key bindings. See keyMap variable for details
* @function
*/
var resetKeyBindings = function() {
$.each(keyMap, function(code, method) {
$(document).unbind('keydown.key' + code);
});
};
/**
* Show previous slide if exists.
* @function
*/
var showPrevSlide = function() {
if ($prevSlide.exists()) {
showSlide($prevSlide);
}
};
/**
* Show next slide if exists or exit presentation
* @function
*/
var showNextSlide = function() {
if ($nextSlide.exists()) {
showSlide($nextSlide);
} else {
exitPresentation();
}
};
/**
* Show new slide
*
* @function
* @param {jQuery} $newSlide slide to be displayed
*/
var showSlide = function($newSlide) {
// select appropriate effect for the next slide and apply it
var effect = $newSlide.data('intro-effect') // effect specified for slide
|| presentationConfig.defaultEffect // effect specified for the whole presentation
|| 'toggle'; // default effect
effects[effect]($currentSlide, $newSlide);
// update pointers to next, previous and current slides
$nextSlide = $newSlide.next(config.slideSelector);
$prevSlide = $newSlide.prev(config.slideSelector);
$currentSlide = $newSlide;
};
/**
* Start presentation specified by jQuery object.
* @function
* @param {jQuery} $el jQuery element of presentation
*/
var startPresentation = function($el) {
// check for header and footer and display them
var hnf = $el.children(config.headerSelector + ',' + config.footerSelector);
if (hnf.exists()) {
hnf.show();
}
$el.show();
// load config for this presentation
presentationConfig = $el.data();
// load first slide
$currentSlide = $el.children(config.slideSelector).first();
applyKeyBindings();
showSlide($currentSlide);
};
/**
* Exit currently displayed presentation.
* @function
*/
var exitPresentation = function() {
resetKeyBindings();
$(config.presentationSelector).hide();
$(config.contentsSelector).show();
};
/**
* Keycode -> Handler
* See applyKeyBindings and resetKeyBindings methods.
*
* @type {Object}
*/
var keyMap = {
// Prev: 33 -- pgup, 37 -- left, 38 -- up, 8 -- backspace
33: showPrevSlide,
37: showPrevSlide,
38: showPrevSlide,
8: showPrevSlide,
// Next: 34 -- pgdown, 39 -- right, 40 -- down, 13 -- enter, 32 -- space
34: showNextSlide,
39: showNextSlide,
40: showNextSlide,
13: showNextSlide,
32: showNextSlide,
// Exit: 27 -- escape
27: exitPresentation
};
/**
* Entrance point for plugin.
* See readme for usage.
*
* @function
* @memberOf jQuery.fn
*
* @param {Object} cfg Configuration parameters (selectors)
* @param {Object} effs List of advanced effects
*/
$.fn.presentify = function(cfg, effs) {
// extend configs
config = $.extend(config, cfg);
effects = $.extend(effects, effs);
var presList = {};
// collect list of presentations for the container
this.children(config.presentationSelector).each(function(i, pres) {
var $pres = $(pres);
presList[$pres.data('title')] = $pres;
});
var titles = getKeys(presList);
if (titles.length == 1) {
// start if only one slideshow is presented
startPresentation(presList[titles[0]]);
} else {
// fill in contents container
var $container = $(config.contentsSelector);
$.each(presList, function(presName, $presEl) {
$('<div></div>', {
text: presName
})
.bind('click', function() {
$container.hide();
startPresentation($presEl);
})
.appendTo($container);
});
}
};
}(window);