-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknockout.js.html
295 lines (272 loc) · 11.2 KB
/
knockout.js.html
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
<!-- David Upton -->
<!-- [email protected] -->
<!--
This script showcases using knockout to import data from a json file (e.g. could be returned from
a REST request) and format in a UI.
The UI allows the user to add, edit and delete the data in the browser.
Chnages dynamically update a row-total.
A button is provided to preview what would be exported (knockouts dynamic referencing is showcased as
the export data changes when the user changes it in the UI).
Finally an upload button is provided which emulates an ajax call to a REST service to upadte the databse.
** NOTES:
- REST scripts assumed but not provided - this will need to be aware that the data has been detached
- Minimal validation is provided
- localisation is framed but not fully implemented
Languages and frameworks used:
- javascript
- HTML
- CSS3 (animated spinner)
- knockout JS
- jQuery
- bootstrap
-->
<!DOCTYPE html>
<head>
<base href="http://learn.knockoutjs.com/" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js'></script>
<script type='text/javascript' src='http://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
.ins-1{margin-left:20px;}
#data-header{font-weight:bold;}
.month{display:inline-block; min-width:35px; max-width:35px;}
.year,
.total{display:inline-block; min-width:60px; max-width:60px;}
.total{text-align: right}
li{list-style-type: none}
a.clickable{font-size:smaller}
export-message{max-height:50px; max-width: 200px}
/* css3 spinner */
.loader,
.loader:before,
.loader:after {border-radius: 50%;width: 12px;height: 12px;-webkit-animation-fill-mode: both;animation-fill-mode: both;-webkit-animation: load7 1.8s infinite ease-in-out;animation: load7 1.8s infinite ease-in-out; }
.loader {color: #337ab7;border-color: #2e6da4;font-size: 15px;position: relative;margin-top: -30px;margin-left: 40px;text-indent: -9999em;-webkit-transform: translateZ(0);-ms-transform: translateZ(0);transform: translateZ(0);-webkit-animation-delay: -0.16s;animation-delay: -0.16s;}
.loader:before,
.loader:after {content: '';position: absolute;top: 0;}
.loader:before {left: -15px;-webkit-animation-delay: -0.32s;animation-delay: -0.32s;}
.loader:after {left: 15px;}
@-webkit-keyframes load7 {
0%,80%,100% {box-shadow: 0 2.5em 0 -1.3em;}
40% {box-shadow: 0 2.5em 0 0;}
}
@keyframes load7 {
0%,80%,100% {box-shadow: 0 2.5em 0 -1.3em;}
40% {box-shadow: 0 2.5em 0 0;}
}
</style>
</head>
<html>
<body>
<h2 class="ins-1">Knockout/jQuery/Bootstrap Database Example</h2>
<p class="ins-1">Basic manipulation of data sourced from a remote database.</p>
<ul>
<li id="data-header">
<span class="year">Year</span>
<span class="month-wrapper" data-bind="foreach: months()">
<span class="month" data-bind="text: $data"></span>
</span>
<span class="total">Total</span>
</li>
</ul>
<ul data-bind="foreach: pivot">
<li id="data-body">
<input class="year" data-bind="value: year" />
<span class="month-wrapper" data-bind="foreach: values">
<input class="month" data-bind="value: value" />
</span>
<span class="total" data-bind="text: total"></span>
<span><a href="#" class="clickable edit" data-bind="click: $root.editRow, rowEnabled: false">Edit</a></span>
<span><a href="#" class="clickable remove" data-bind="click: $root.removeYear">Remove</a></span>
</li>
</ul>
<ul>
<li>
<button class="btn btn-primary" data-bind="click: addYear">Add New Row</button>
<button class="btn btn-warning" data-bind="click: viewExportAll">Preview Export Data</button>
</li>
</ul>
<div id="exported-data" data-bind="fadeVisible: 0">
<h3 class="ins-1">Data to be Exported</h3>
<p class="ins-1">Data transposed and prepared for upload back to database.</p>
<ul data-bind="foreach: exportData()">
<li data-bind="text: ko.toJSON($data)"></li>
</ul>
<ul>
<li><button id="export" class="btn btn-success" data-bind="click: uploadAll">Export Data</button></li>
<li><p id="export-message"></p></li>
</ul>
</div>
</body>
<script type='text/javascript'>
// data defined as fake database json returns
var serverData = [
{Year: 2012, Month: 1, Value: .5},
{Year: 2012, Month: 2, Value: .5},
{Year: 2012, Month: 3, Value: .75},
{Year: 2012, Month: 4, Value: .75},
{Year: 2012, Month: 5, Value: .5},
{Year: 2012, Month: 6, Value: .5},
{Year: 2012, Month: 7, Value: .75},
{Year: 2012, Month: 8, Value: .75},
{Year: 2012, Month: 9, Value: .5},
{Year: 2012, Month: 10, Value: .5},
{Year: 2012, Month: 11, Value: .75},
{Year: 2012, Month: 12, Value: .75},
{Year: 2013, Month: 1, Value: .5},
{Year: 2013, Month: 2, Value: .5},
{Year: 2013, Month: 3, Value: .75},
{Year: 2013, Month: 4, Value: .75},
{Year: 2013, Month: 5, Value: .5},
{Year: 2013, Month: 6, Value: .5},
{Year: 2013, Month: 7, Value: .75},
{Year: 2013, Month: 8, Value: .75},
{Year: 2013, Month: 9, Value: .5},
{Year: 2013, Month: 10, Value: .5},
{Year: 2013, Month: 11, Value: .75},
{Year: 2013, Month: 12, Value: .75},
{Year: 2014, Month: 1, Value: .85}
];
var monthMap = {
"en-US": ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
};
/**
* Bindings to initialize the UI
*/
ko.bindingHandlers.fadeVisible = {
init: function(element, valueAccessor) {
var shouldDisplay = valueAccessor();
$(element).toggle(shouldDisplay);
}
};
ko.bindingHandlers.rowEnabled = {
init: function(element, valueAccessor) {
var shouldDisplay = valueAccessor();
$(element).parents('li').find('input').attr('disabled', (!shouldDisplay));
}
};
/**
* Data struct for use in UI
*/
function YearData(year, values){
var self = this;
self.year = ko.observable(year);
self.values = ko.observableArray([]);
$(values).each(function(key, val) {
self.values.push(new YearDataValues(val))
});
self.total = ko.computed(function(){
var total = 0;
$(self.values()).each(function(a, b){
total = parseFloat(total) + parseFloat(b.value());
});
return parseFloat(total).toFixed(2);
});
}
function YearDataValues(value){
var self = this;
self.value = ko.observable(value);
}
/**
* Data struct to map the original dataset
*/
function RawDataModel(year, month, value){
var self = this;
self.Year = parseInt(year);
self.Month = parseInt(month);
self.Value = parseFloat(value,1).toFixed(2);
}
function VM() {
var self = this;
/**
* Get the browsers language, so we can give translated months
*/
var language = navigator.languages && navigator.languages[0] || // Chrome / Firefox
navigator.language || // All browsers
navigator.userLanguage; // IE <= 10
/**
* Creates an object wit a modified structure for the UI
*/
self.pivot = ko.observableArray([]);
self.makePivot = function(){
var yr = serverData[0].Year;
self.pivot = ko.observableArray([]);
var num = self.pivot.push(new YearData(yr, []));
for (var i = 0; i < serverData.length; i++) {
if (yr != serverData[i].Year) {
yr = serverData[i].Year;
num = self.pivot.push(new YearData(yr, []));
}
self.pivot()[num-1].values.push(new YearDataValues(serverData[i].Value));
}
for (var j=i%12; j<12; j++) {
self.pivot()[num-1].values.push(new YearDataValues(0));
}
};
self.makePivot();
/**
* Creates an object in the same style as the original data from JSON
*/
self.exports = ko.observableArray([]);
self.exportData = ko.computed(function(){
var obj = ko.toJS(self);
self.exports=[];
for (var i = 0; i < obj.pivot.length; i++) {
if (typeof obj.pivot[i] === "undefined") continue;
for (var j = 0; j < obj.pivot[i].values.length; j++){
var val = obj.pivot[i].values[j].value;
if (!(!isNaN(parseFloat(val)) && isFinite(val))) continue;
self.exports.push(new RawDataModel(obj.pivot[i].year, (j+1), val));
}
}
return self.exports;
});
/**
* Provides a basic mapping for month names
*/
self.months = function() {
if (typeof language === "undefined") language ="en-US";
return monthMap[language];
};
/**
* Removes an entire year from the database
*/
self.removeYear = function(year){
self.pivot.remove(year);
};
/**
* Displays the data-export area
*/
self.viewExportAll = function(data, element){
$("#exported-data").fadeIn();
$("#export").addClass("btn-primary").removeClass("btn-success").text('Upload to Database');
};
/**
* Mimicks data upload
*/
self.uploadAll = function(data, element){
$("#export-message").text('').addClass('loader');
$(element.target).hide().removeClass("btn-primary");
setTimeout(function(){
$("#export-message").removeClass('loader');
$(element.target).addClass("btn-success").html('Data Uploaded').fadeIn();
}, 4000);
};
/**
* Adds a new record - with default values
*/
self.addYear = function(){
self.pivot.push(new YearData(new Date().getFullYear(), new Array(12).fill(0.08)));
};
/**
* Makes a data row editable.
*/
self.editRow = function(data, event){
$(event.target).parents('li').find('input').attr('disabled', false);
};
}
// bind the view model
ko.applyBindings(new VM());
</script>
</body>
</html>