forked from baspete/flipstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-flap.js
More file actions
351 lines (316 loc) · 10.9 KB
/
Copy pathsplit-flap.js
File metadata and controls
351 lines (316 loc) · 10.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Home Sweet Global Namespace
var sf = {};
// Namespace for objects defined and used locally in templates
sf.local = {};
// Namespace for plugin-specific javascript,
// to be loaded later in separate files
sf.plugins = {};
/* ********************************************************************* */
/* HOUSEKEEPING */
Array.prototype.rotate = (function() {
var unshift = Array.prototype.unshift,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0,
count = count >> 0;
unshift.apply(this, splice.call(this, count % len, len));
return this;
};
})();
sf.util = {
// Function splits string into array of substrings of len or less.
// Splits happen at the last space.
splitString: function(str,len){
var arr = [];
var words = str.split(" ");
var line = words[0];
for(var i=1;i<words.length;i++){
if(line.length + words[i].length + 1 < len + 1){
line = line + " " + words[i];
} else {
arr.push(line);
line = words[i];
}
}
// push the last line into the array
arr.push(line);
return arr;
},
// Methods for getting query parameters out of the URL
getUrlParams: function(){
var vars = [], param;
var params = window.location.href.slice(window.location.href.indexOf('?') + 1, window.location.href.indexOf('#')).split('&');
for(var i = 0; i < params.length; i++) {
param = params[i].split('=');
vars.push(param[0]);
vars[param[0]] = param[1];
}
return vars;
},
getUrlParam: function(name){
return sf.util.getUrlParams()[name];
}
};
/* ********************************************************************* */
/* BACKBONE COLLECTIONS, MODELS AND VIEWS */
// This View generates the empty markup for the rows
// It is only called once, at document.ready()
// By default it sets 12 rows. Set sf.options.numRows to change this.
sf.Board = Backbone.View.extend({
render: function() {
this.el.find(".row").remove();
for(var i=0;i<(sf.options.numRows);i++){
this.el.append(this.template());
};
this.el.find(".row").each(function(){
sf.display.initRow($(this));
});
return this;
}
});
sf.board = {
// Generate the markup for and initialize a blank board
init: function(options){
var board = new sf.Board;
board.el = options.container;
board.template = _.template(options.template.html());
sf.options.numRows = sf.options.numRows ? sf.options.numRows : 12; // default 12 rows
board.render();
},
// Utility method to clear the board.
clear: function() {
var container = arguments[0],
rows = container.find(".row"),
i=0,
stagger = sf.options.stagger ? sf.options.stagger : 1000;
var loop = function() {
setTimeout(function () {
var groups = $(rows[i]).find(".group");
groups.each(function(){
sf.display.loadGroup(" ",$(this));
})
i++;
if (i < rows.length) {
loop(i);
}
}, stagger);
};
loop();
}
};
// This Collection is used to hold the datset for this board.
sf.Items = Backbone.Collection.extend({
update: function(options){
this.fetch({
success: function(response){
sf.display.loadSequentially(response.toJSON(), options.container);
}
});
},
parse: function(json){
return(sf.plugins[sf.options.plugin].formatData(json)); // normalize this data
}
});
sf.items = {
// Get the data for a board and load it
init: function(options) {
// create the Collection
items = new sf.Items; // NOTE GLOBAL!
items.url = sf.plugins[options.plugin].url(options);
// check if we're using jsonp
if(sf.plugins[options.plugin].dataType ==="jsonp"){
items.sync = function(method, model, options){
options.timeout = 10000;
options.dataType = "jsonp";
return Backbone.sync(method, model, options);
};
}
// pick up any sorting options
if(options.order && options.sort){
items.comparator = function(item){
if(options.order === "desc"){
return -item.get(options.sort);
} else {
return item.get(options.sort);
}
};
}
},
load: function(options){
// get the data and load the chart
items.update(options);
// If user has specified a refresh interval, setInterval()
if(options.refreshInterval){
setInterval(function(){
items.update(options);
}, options.refreshInterval);
}
}
},
/* ********************************************************************* */
/* DISPLAY METHODS */
sf.display = {
// DRUM ARRAYS
// These contain the character sets for each drum. Each position represents
// a character and when prepended by "c" gives the class name which will
// be applied to display that character.
FullDrum: function() {
this.order = [' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','.',',',':',';','?','!','\'','"','+','-','*','/','=','_','@','#','$','%','&'];
},
CharDrum: function() {
this.order = [' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','.',','];
},
NumDrum: function() {
this.order = [' ','0','1','2','3','4','5','6','7','8','9','.',',','$','%'];
},
ImageDrum: function() {
this.order = []; // Intentionally empty here. Override in plugins/<plugin_name>/custom.js
},
initRow: function(row) { // expects the jQuery DOM object
// For each character, construct a drum array and
// attach that array to the element's .data() object
row.find("span").each(function() {
var parent = $(this).closest("div");
if(parent.hasClass("number")){
var drum = new sf.display.NumDrum(); // Numbers only
} else if(parent.hasClass("character")) {
var drum = new sf.display.CharDrum(); // Characters only
} else if(parent.hasClass("image")) {
var drum = new sf.display.ImageDrum(); // Images
} else {
var drum = new sf.display.FullDrum(); // The full set
}
$(this).data("order", drum.order);
// Finally, set each character to a space.
sf.display.change($(this), " ");
});
},
loadSequentially: function() {
var input = arguments[0],
container = arguments[1],
rows = container.find(".row"),
i=0,
stagger = sf.options.stagger ? sf.options.stagger : 1000;
var loop = function() {
setTimeout(function () {
if(input[i]) {
sf.display.loadRow(input[i],$(rows[i]));
}
i++;
if (i < rows.length) {
loop(i);
}
}, stagger);
};
loop();
},
loadRow: function() {
var input = arguments[0],
row = arguments[1], // the row object
key,group;
// load the keys array into the .[key] groups
for(key in input) {
if(input.hasOwnProperty(key)){
group = row.find("."+key);
if(group.length > 0) {
sf.display.loadGroup(input[key], group);
// put that value into that group's data store
group.data("contents",input[key]);
}
}
}
},
loadGroup: function() {
// load a string into a group of display elements
var input = arguments[0], // the input (which may be a string or a int)
input = input+"", // force it into a string
target = arguments[1], // the group object
elements = target.find("span").closest("div"), // may have separators, so check for spans
strLen = elements.size(),
i, characters;
// ###################################
// STATUS INDICATORS
// Add the class "on" to the correct element
if(target.hasClass("status")) {
target.find("div").removeClass("on");
target.find(".s"+input).addClass("on");
// ###################################
// IMAGES
// Only one display element--no need to iterate
} else if(target.find(".image").length > 0){
sf.display.change(target.find("span"), input, false);
// ###################################
// NORMAL CHARACTERS
// otherwise, this group is composed of split-flap character or number elements
} else {
input = input.toUpperCase();
// get individual characters and pad the array
// with spaces (to clear any existing characters)
characters = input.split("");
for(i=0;i<strLen;i++) {
if(typeof(characters[i]) === 'undefined') {
characters[i] = " ";
}
}
// trim the array to the number of display elements
characters = characters.slice(0,strLen);
// assign them to the display elements
for(var i=0;i<characters.length;i++) {
// TODO: is there a more efficient way to do this?
sf.display.change($(elements[i]).find("span"), characters[i], true);
}
}
},
change: function() {
var container = arguments[0],
c = arguments[1], // the new character
isChar = arguments[2], // true if this is an character (not an image)
index, i, j;
// get the curent order of the display element's drum
var values = container.data("order");
// how many times do we need to increment the drum?
index = values.indexOf(c);
// increment the drum
for(i=0;i<index;i++) {
sf.display.show(container, (values[i + 1]), isChar);
}
// rotate the dom element's stored array to the new order for next time
container.data("order", values.rotate(index));
},
show: function() {
var container = arguments[0],
i = arguments[1],
isChar = arguments[2],
c;
c = isChar ? "c"+(i) : (i); // character class names are preceded by "c"
// punctuation has special class names
// TODO: can we be more efficient here? This method gets called a lot!
switch(i) {
case " ": c = "csp"; break;
case ".": c = "cper"; break;
case ",": c = "ccom"; break;
case ":": c = "ccol"; break;
case ";": c = "csem"; break;
case "?": c = "cque"; break;
case "!": c = "cexc"; break;
case "'": c = "capo"; break;
case '"': c = "cquo"; break;
case "+": c = "cplu"; break;
case "-": c = "cmin"; break;
case "*": c = "cstr"; break;
case "/": c = "csla"; break;
case "=": c = "ceq"; break;
case "_": c = "cund"; break;
case "@": c = "cat"; break;
case "#": c = "chsh"; break;
case "$": c = "cdol"; break;
case "%": c = "cpct"; break;
case "&": c = "camp"; break;
}
container.fadeOut(50, function(){
container.removeClass().addClass(c);
}).fadeIn(50);
}
};
/* END DISPLAY METHODS */
/* ********************************************************************* */