/
siteslidr.js
203 lines (176 loc) · 6.02 KB
/
siteslidr.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
/**
* @fileOverview SiteSlidr: Live sites presentation tool
* @author Dmitry Agafonov <Dmitry@Agafonov.pp.ru>
* @version 0.0.1
*/
"use strict";
(function ($) {
// Private data and functions
var VERSION = [0, 0, 1],
version = function () {
return VERSION.join('.');
},
// Default settings for an app
defaults = {
'background': '#333'
},
// Default site settings
// (site is a synonym for slide here and below)
site_defaults = {
'id': null,
'url': 'siteslidr_0.html',
'duration': 5000,
'transition': {'type': 'fade', 'duration': 500},
'elem': null
},
// Runtime data
stage = {
'root': null,
'title': null,
'overlay': null,
'sites': [],
'current': 0,
'next': 0,
'total': 0,
'background': null,
'timer': null,
'busy': false
},
// XXX remove???
fatal_error = function (e) {
throw ('Error. ' + e);
},
// Set all things up
init = function (config) {
// Is stage ready?
if (stage.root) {
return true;
}
// Prepare the stage, if not defined by user
stage.root = $('#SiteSlidr_Stage');
if (stage.root.length === 0) {
stage.root = $('<div id="SiteSlidr_Stage"></div>').hide();
$('body').append(stage.root);
}
stage.root.append('<div id="SiteSlidr_Overlay"></div>');
stage.root.append('<div id="SiteSlidr_Control"><p></p></div>');
var ctlPlay = $('<span id="SiteSlidr_Play">Play</span>').on('click', function () {
play(false);
});
var ctlStop = $('<span id="SiteSlidr_Stop">Stop</span>').on('click', stop);
var ctlPause = $('<span id="SiteSlidr_Pause">Pause</span>').on('click', pause);
var ctlPrevious = $('<span id="SiteSlidr_Previous">Previous</span>').on('click', previous);
var ctlNext = $('<span id="SiteSlidr_Next">Next</span>').on('click', next);
$('#SiteSlidr_Control p')
.append(ctlStop)
.append(ctlPlay)
.append(ctlPause)
.append(ctlPrevious)
.append(ctlNext)
.append('<span id="SiteSlidr_Counter"></span>');
// Apply user's defaults
$.extend(site_defaults, config.defaults);
// Add all the slides to stage
for (var i = config.sites.length - 1; i >= 0; i--) {
add(config.sites[i]);
}
stage.current = 0;
stage.root.fadeIn();
return true;
},
// Create site frame and add to stage.
// Initial state is hidden and url XXX TODO not loaded XXX.
add = function (siteconfig) {
var site = $.extend({}, site_defaults, siteconfig);
site.elem = $("<iframe id='" +
site.id + "' class='SiteSlidr_Frame' src='" +
site.url + "'></iframe>").hide();
// Don't mess presentation by long transitions
if (site.transition.duration > site.duration/2) {
site.transition.duration = site.duration/2;
}
stage.root.append(site.elem);
stage.sites.unshift(site);
stage.total = stage.sites.length;
return site.id;
},
pause = function () {
if (stage.timeout) {
window.clearTimeout(stage.timeout);
stage.timeout = null;
$('#SiteSlidr_Control')
.addClass('paused')
.removeClass('playing');
}
},
rewind = function () {
stage.next = 0;
switcher();
},
stop = function () {
pause();
rewind();
},
// Hide current and show new slide
switcher = function () {
var cur = stage.sites[stage.current],
nxt = stage.sites[stage.next];
$('#SiteSlidr_Counter').html((stage.next + 1) + '/' + stage.total);
if (stage.busy) {
return;
}
stage.busy = true;
nxt.elem.addClass('front').fadeIn(nxt.transition.duration, function () {
nxt.elem.removeClass('front');
if (cur.id !== nxt.id) {
cur.elem.hide();
}
stage.current = stage.next;
stage.busy = false;
});
},
play = function (forward) {
window.clearTimeout(stage.timeout);
if (forward !== false) {
next();
}
stage.timeout = window.setTimeout(play, stage.sites[stage.next].duration);
$('#SiteSlidr_Control')
.addClass('playing')
.removeClass('paused');
},
next = function () {
stage.next = stage.current + 1;
if (stage.next === stage.total) {
stage.next = 0;
}
switcher();
},
previous = function () {
stage.next = stage.current - 1;
if (stage.next === -1) {
stage.next = stage.total - 1;
}
switcher();
},
run = function (config) {
try {
if (config.sites[0].url === '') {
throw ('NotConfigured');
} else {
init(config);
}
} catch (e) {
fatal_error('SiteSlidr was not properly configured:' + e + ' ' +
'Read the docs at http://github.com/agafonovdmitry/siteslidr');
}
rewind();
play(false);
}
//
// Public interface
//
window.SiteSlidr = {
'run': run
};
}(jQuery));