-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
255 lines (197 loc) · 6.1 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Recipt Input</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
<script src="libs/jQuery-tagEditor/jquery.caret.min.js"></script>
<script src="libs/jQuery-tagEditor/jquery.tag-editor.min.js"></script>
<script type="text/javascript">
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function addItem(){
$('recipt .items').append($('template.item').html());
$('.tagsinput').tagEditor();
}
function updateStatistics(){
recipts = JSON.parse(localStorage.getItem('recipts'));
if(recipts){
$('.num-of-recipts').html(recipts.length);
$('.preview').html(makeReciptPreview(recipts[recipts.length-1]));
}
}
function makeReciptPreview(obj){
function makeList(obj){
var result = '<ul>';
$.each(obj, function(k, v) {
result += '<li>';
if ($.isArray(v)){
result += 'Items: ';
v.forEach(function(item){
result += makeList(item);
});
}else{
result += k + ": " + v + "</li>";
}
result += '</li>';
});
result += '</ul>';
return result;
}
return makeList(obj);
}
function addReciptToStorage(recipt_obj){
recipts = JSON.parse(localStorage.getItem('recipts'));
if(!recipts){
recipts = [];
}
recipts.push(recipt_obj);
localStorage.setItem('recipts', JSON.stringify(recipts));
updateStatistics();
}
function makeReciptObject(){
var recipt_obj={
'items': []
};
$recipt = $('recipt');
// Save recipt main data
$recipt.children('input').each(function(){
var $this = $(this);
recipt_obj[$this.attr('name')] = $this.val();
});
// Save items
$recipt.find('item').each(function(){
var $item = $(this),
item_obj = {};
isValid = true;
$item.find('input').each(function(){
var $this = $(this);
if($this.attr('required') && $.trim($this.val()).length === 0){
isValid = false;
return false;
}
item_obj[$this.attr('name')] = $this.val();
});
// So that we don't add empty/nonvalid items
if(isValid){
recipt_obj['items'].push(item_obj);
}
});
return recipt_obj;
}
function start(){
$('.app').append($('template.recipt').html());
addItem();
updateStatistics();
}
$(document).ready(function(){
start();
/**********************************/
/** Saves recipt and reset forms **/
/**********************************/
$('button.save').click(function(ev){
console.log(makeReciptObject());
addReciptToStorage(makeReciptObject());
$('recipt .items item').remove();
addItem();
});
/***************************************************************/
/** Creates a temporary json file and opens a download prompt **/
/***************************************************************/
$('button.export.json').click(function(ev){
recipts = localStorage.getItem('recipts');
var date = new Date(),
filename = 'recipts_' + date.getFullYear() + '_' + date.getMonth() + '_' + date.getDate() + '.json';
download(filename, recipts);
});
/****************************************************************/
/** Erases all recipts from localstorage. Gives a prompt first **/
/****************************************************************/
$('button.erase').click(function(ev){
var r = confirm("This will clear all locally stored recipts. Only do this if you have exported the existing data.");
if (r === true) {
localStorage.removeItem('recipts');
updateStatistics();
}
});
/**************************************************************/
/** Generates new items forms when the last one is filled in **/
/**************************************************************/
$("recipt").on('focusout', '.items input', function(ev){
var $this = $(this),
$required_inputs = $(this).parents('item').find('input[required]'),
isValid = true;
$required_inputs.each(function (){
if($.trim($(this).val()).length === 0){
isValid = false;
}
});
if( isValid && $this.parents('item').is('item:nth-last-child(1)') ){
addItem();
$('html, body').animate({
scrollTop: $("item:last").offset().top
}, 1000);
}
if( !isValid && $this.parents('item').is('item:nth-last-child(2)') ){
$('recipt .items item:last').remove();
}
});
});
</script>
<link rel="stylesheet" href="libs/jQuery-tagEditor/jquery.tag-editor.css">
<style>
template{
display: none;
}
.statistics{
float: right;
}
.statistics .preview ul{
padding-bottom: 10px;
}
.tag-editor{
width: 400px;
}
</style>
</head>
<body>
<template class="recipt">
<recipt>
Date: <input type="date" name="date"> <br>
Store <input type="text" name="store"> <br>
Person <input type="text" name="person"> <br>
Items
<div class="items"></div>
</recipt>
</template>
<template class="item">
<item>
<ul>
<li>Name <input type="text" name="name" required></li>
<li>Price <input type="number" name="price" required></li>
<li>No. of items <input type="number" name="amount" required value="1"></li>
<li>Price modifier <input type="number" name="modifier"></li>
<li>Tags <input type="text" class="tagsinput" data-role="tagsinput" name="tags"></li>
</ul>
</item>
</template>
<div class="statistics">
Number of recipts: <span class="num-of-recipts"></span> <br>
Latest recipt: <div class="preview"></div>
</div>
<div class="app">
</div>
<div class="buttons">
<button class="save">Save recipt</button>
<button class="export json">Export as JSON</button>
<button class="erase">ERASE ALL RECIPTS</button>
</div>
</body>
</html>