-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
82 lines (74 loc) · 2.02 KB
/
main.js
File metadata and controls
82 lines (74 loc) · 2.02 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
function getSeed() {
return new Date().toJSON().slice(0,10);
}
// check every minute if seed has changed
setInterval(function() {
viewModel.seed(getSeed());
}, 1000 * 60);
var viewModel = {
newValue: ko.observable(''),
items: ko.observable([]),
seed: ko.observable(getSeed()),
addItem: function() {
var current = this.items().slice();
current.push(this.newValue());
this.newValue('');
this.items(current);
},
removeItem: function(item) {
var current = viewModel.items().slice();
current = current.filter(function(i) {return i != item;});
viewModel.items(current);
},
shuffledItems: ko.pureComputed(function() {
var seed = viewModel.seed();
var items = viewModel.items().map(function(i) {
return {
label: i,
hash: sha1(seed + i)
};
});
items.sort(function(a, b) {
return a.hash.localeCompare(b.hash);
});
return items.map(function(i) {
return i.label;
});
})
};
//http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
var getParameterByName = function(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var firebaseId = getParameterByName('firebaseId');
if (!firebaseId) {
alert('firebaseId is needed');
}
var firebase = new Firebase('https://' + firebaseId + '.firebaseio.com');
var firstValue = true;
firebase.on('value', function(snapshot) {
if (firstValue) {
firstValue = false;
$('*').show();
$('.loading').hide();
}
viewModel.items(snapshot.val()||[]);
});
viewModel.items.subscribe(function(items) {
firebase.set(items);
});
//init
$(document).ready(function() {
ko.applyBindings(viewModel);
if (firstValue) {
$('body').children().hide();
$('.loading').show();
}
var title = getParameterByName('title');
if (title) {
$('h1').text(title);
}
});