-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (112 loc) · 3.4 KB
/
app.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
/* global $ */
/* global localStorage */
var template = function(text) {
return `\
<p>
<input type="button" class="timer back" value="0:00">
<span class="back">${text}</span>
<i class="glyphicon glyphicon-remove"></i>
</p>`;
};
var saveList = function() {
var items = $('div.list').children('p').map( function() {
return {
'name' : $(this).children('span').first().text(),
'back' : $(this).children('input').hasClass('back'),
'front' : $(this).children('input').hasClass('front'),
'old' : $(this).children('input').hasClass('old'),
'time': $(this).children('input').val(),
};
}).get();
localStorage.setItem('SpeakerList', JSON.stringify(items));
return items;
// console.log(JSON.stringify(items));
// alert(JSON.stringify(items));
};
var dumpList = function() {
console.log( saveList() );
var $mesg = $('<span/>').text('Saved').addClass('h3 red text-center');
$('.header h1').append($mesg);
$mesg.fadeOut(1000);
}
var loadList = function() {
var previousList = localStorage.getItem('SpeakerList');
if (previousList === null) {
return;
}
$('div.list').empty();
var items = JSON.parse(previousList);
// console.log(items);
items.forEach( function(item) {
var $new_item = $(template(item.name));
$('div.list').append( $new_item );
var $input = $new_item.children('input').first();
$input.removeClass('back');
$input.val(item.time);
if (item.back) { $input.addClass('back'); }
if (item.front) {
var timearray = item.time.split(':');
$input.addClass('front').timer({
seconds: timearray[0] * 60 + timearray[1],
format: '%m:%S',
});
$new_item.children('span').removeClass('back').addClass('front');
}
if (item.old) {
$input.addClass('old');
$new_item.children('span')
.removeClass('back').removeClass('front').addClass('old');
}
});
$('')
};
var updateTally = function() {
var tally = ($('.back').length - 1 )/2;
$('#tally').text(`(${tally} waiting)`);
saveList();
};
var main = function() {
loadList();
$('form').submit(function() {
var new_item = $('input#todo').val();
if (new_item === "") {
saveList();
} else {
if (new_item == "save") { dumpList(); }
else if (new_item == "load") { loadList(); }
else if (new_item == "clear") { $('div.list').empty(); }
else { $('div.list').append(template(new_item)); }
$('input#todo').val("");
$('input#todo').focus();
}
updateTally();
return false;
});
$(document).on('click', '.glyphicon-remove', function() {
$(this).parent().remove();
updateTally();
});
$(document).on('click', '.timer', function() {
if ( $(this).hasClass('front') ) {
$(this)
.removeClass('front')
.removeClass('back')
.addClass('old')
.timer('remove');
$(this).parents('p').children('span')
.removeClass('back').removeClass('front').addClass('old');
} else {
$('.front').removeClass('front').addClass('old').timer('remove');
var timearray = $(this).val().split(':');
$(this).addClass('front').removeClass('back').timer({
seconds: timearray[0] * 60 + timearray[1],
format: '%m:%S',
});
$(this).parents('p').children('span')
.removeClass('back').addClass('front');
}
updateTally();
});
updateTally();
};
$(document).ready(main);