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
|
<!DOCTYPE html>
<html>
<head>
<title>Light Loop Pi</title>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="/static/css/bootstrap-slider.css"/>
<link rel="stylesheet" href="/static/css/main.css"/>
<script src="/static/js/jquery-2.1.1.min.js"></script>
<script src="/static/js/underscore-min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/bootstrap-slider.min.js"></script>
<script>
function load_data() {
$.get('/data', function(res) {
console.log('Loading: ', res);
$('#speed-slider').slider({
tooltip: 'always',
value: res.tempo
});
_.each(res.trees, function(tree, i) {
var $tree = $('div[data-light-tree=' + i + ']');
_.each(tree, function(channel, j) {
var $channel = $tree.find('div[data-light-channel=' + j + ']');
_.each(channel, function(cell, k) {
var $cell = $channel.find('button[data-light-cell=' + k + ']');
if (cell) {
$cell.addClass('active');
}
});
});
});
});
};
function save_data(data) {
var data = {
tempo: +$('#speed-slider').val(),
trees: []
};
$('.light-tree').each(function(_idx, tree) {
tree = $(tree);
var tree_data = []
tree.find('.light-row').each(function(_idx, row) {
var vals = $(row).find('.light-cell-btn').toArray().map(function(x) {
return x.className.contains('active') ? 1 : 0;
});
tree_data.push(vals);
});
data.trees.push(tree_data);
});
console.log('Saved:', data);
$.ajax({
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
dataType: 'json',
url: '/data',
});
}
$(document).ready(function() {
var TREES = 8;
var CHANNELS = 8;
var CELLS = 24;
_.each(_.range(CHANNELS), function(i) {
var row = $(_.template($('#light-row-template').text(), {i: i, channels: CHANNELS, cells: CELLS}));
$('#light-container').append(row);
$('#tree-btns').append($('<button type="button" class="btn btn-default tree-btn" data-tree="' + i + '">Tree ' + i + '</button>'));
});
$('.tree-btn').click(function() {
var tree = $(this).attr('data-tree');
$('.light-tree').hide();
$('.light-tree[data-light-tree=' + tree +']').show();
});
$('.toggle-row-btn').click(function() {
var row = $(this).parent('.light-row');
$(row).find('.light-cell-btn').toggleClass('active');
})
$('.clear-row-btn').click(function() {
var row = $(this).parent('.light-row');
$(row).find('.light-cell-btn').removeClass('active');
})
$('#save-btn').click(function(){
save_data();
});
load_data();
$('.light-tree[data-light-tree=0]').show();
});
</script>
</head>
<body>
<div class="container">
<h1>Light Loop Pi</h1>
<div class="speed-slider">
<label>Loop speed:</label>
<input id="speed-slider" data-slider-id='ex1Slider' type="text" data-slider-min="500" data-slider-max="5000" data-slider-step="100"/>
</div>
<div class="btn-toolbar" role="toolbar">
<div id="tree-btns" class="btn-group"></div>
<div class="btn-group">
<button id="save-btn" type="button" class="btn btn-primary">Save</button>
</div>
</div>
<div id="light-container"></div>
</div>
<script type="text/template" class="template" id="light-row-template">
<div class="light-tree" data-light-tree="<%= i %>">
<h3>Tree <%= i %></h3>
<% _.each(_.range(channels), function(j) { %>
<div class="light-row" data-light-channel="<%= j %>">
<span>Channel <%= j %></span>
<% _.each(_.range(cells), function(k) { %>
<button type="button" class="btn btn-success light-cell-btn" data-toggle="button" data-light-cell="<%= k %>"> </button>
<% }); %>
<button type="button" class="btn btn-warning toggle-row-btn">Toggle</button>
<button type="button" class="btn btn-warning clear-row-btn">Clear</button>
</div>
<% }); %>
</div>
</script>
</body>
</html>
|