-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
406 lines (353 loc) · 13 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href='{% static "css/ui-lightness/jquery-ui-1.10.0.custom.min.css" %}' type="text/css" media="screen, projection">
<link rel="stylesheet" href='{% static "bootstrap/css/bootstrap.min.css" %}' media="screen">
<!-- <link rel="stylesheet" href='{% static "bootstrap/css/bootstrap-responsive.min.css" %}' media="screen"> -->
</head>
<style>
#transaction-popover {
left: 50%;
width: 400px;
max-width: 400px;
margin-left: -200px;
}
.modal_like {
padding: 15px;
background-color: #ffffff;
border: 1px solid #999;
border: 1px solid rgba(0, 0, 0, 0.3);
*border: 1px solid #999;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
outline: none;
-webkit-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-webkit-background-clip: padding-box;
-moz-background-clip: padding-box;
background-clip: padding-box;
}
#expense_date a {
height: 50px;
}
#expenses-list .edit {
display: none;
}
#expenses-list .view {
display: block;
}
#expenses-list .editing .edit {
display: block;
}
#expenses-list .editing .view {
display: none;
}
#tabs { height: 98%; }
.transaction_items .ui-selecting { background: #FECA40; }
.transaction_items .ui-selected { background: #F9F9F9; }
.transaction_items { margin: 0; padding: 0;}
#expense_date li { text-align: center; }
</style>
<body>
<div class="container">
<div id="transaction-popover" class="popover fade top in"
id="transaction-popover">
<div class="arrow"></div>
<h3 class="popover-title">
Choose expense
<button type="button" class="close" data-dismiss="popover">×</button>
</h3>
<div class="popover-content input-append">
<select data-bind="foreach: moneyList">
<option data-bind="text: $data"></option>
</select>
<button class="btn" type="button">Apply</button>
</div>
</div>
<div class="row">
<div class="navbar span8 offset2">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul id="global_nav" class="nav">
<li><a href="#expenses">Expenses</a></li>
<li><a href="#tabs-2">Aims</a></li>
<li><a href="#transactions">Transactions</a></li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="span8 offset2" id="global_nav_content">
<div id="expenses">
<div id="expense_date" class="pagination pagination-centered">
<ul>
</ul>
</div>
<table id="expenses-list" class="table table-condensed table-striped">
<thead>
<tr>
<th>Name</th>
<th>Planned</th>
<th>Actual</th>
</tr>
</thead>
<tbody data-bind="foreach: expenses">
<tr>
<td data-bind="css: { editing: editing_name }">
<div class="view">
<label data-bind="text: name, event: { dblclick: $root.editItemName }"></label>
</div>
<input type="text" class="edit" data-bind="value: name, selectAndFocus: editing_name, valueUpdate: 'afterkeydown', enterKey: $root.stopEditing, event: { blur: $root.stopEditing }">
</td>
<td data-bind="css: { editing: editing_plan }">
<div class="view">
<label data-bind="text: plan, event: { dblclick: $root.editItemPlan }"></label>
</div>
<input type="text" class="edit" data-bind="value: plan, valueUpdate: 'afterkeydown', enterKey: $root.stopEditing, selectAndFocus: editing_plan, event: { blur: $root.stopEditing }">
</td>
<td>
<div class="view">
<label data-bind="text: real"></label>
</div>
</td>
</tr>
</tbody>
</table>
<form><fieldset>
<div class="input-append">
<input type="text" placeholder="expense name ##.#" data-bind="value: newExpenseValue, enterKey: newExpense">
<button class="btn" type="button">Add New</button>
</div>
<span class="help-block">You must enter price with cents like: 300.00</span>
</fieldset></form>
</div>
<div id="transactions">
<div>
Load file
<form action="{% url easyfin.views.upload_file %}"
enctype="multipart/form-data" method="post">
{% csrf_token %} Please specify a file, or a set of files: <input
id="fileupload" type="file" name="file"
data-url="{% url easyfin.views.upload_file %}">
</form>
</div>
<div id="transaction_items_table"
data-bind="foreach: newTransactions">
<div>
<span data-bind="text: category"></span>
<table class="table table-hover">
<tbody class="transaction_items" data-bind="foreach: data">
<tr>
<td data-bind="text: date"></td>
<td data-bind="text: comment"></td>
<td data-bind="text: income"></td>
<td data-bind="text: outcome"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery via Google + local fallback, see h5bp.com -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="{% static "jquery-1.9.0.min.js" %}"><\/script>')</script>
<script type='text/javascript' src='{% static "js/jquery-ui-1.10.0.custom.min.js" %}'></script>
<script type='text/javascript' src='{% static "js/upload/jquery.ui.widget.js" %}'></script>
<script type='text/javascript' src='{% static "js/upload/jquery.iframe-transport.js" %}'></script>
<script type='text/javascript' src='{% static "js/upload/jquery.fileupload.js" %}'></script>
<script type='text/javascript' src='{% static "jquery.cookie.js" %}'></script>
<script type='text/javascript' src='{% static "bootstrap/js/bootstrap.min.js" %}'></script>
<script type='text/javascript' src='{% static "knockout-2.2.1.js" %}'></script>
<script type='text/javascript' src='{% static "js/calendar.js" %}'></script>
<script type='text/javascript' src='{% static "js/bootstrap_date_pagination_controller.js" %}'></script>
<script type='text/javascript' src='{% static "js/expenses_view_model.js" %}'></script>
<script>
$("#global_nav a").click(function(e) {
e.preventDefault();
$("#global_nav li").removeClass('active');
$(this).parent().addClass('active');
$("#global_nav_content>div").hide();
$($(this).attr('href')).show();
});
$("#transaction-popover").hide();
// Django csrf token settings
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain : false, // obviates need for sameOrigin test
beforeSend : function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
// Knockout code
var ENTER_KEY = 13;
// a custom binding to handle the enter key (could go in a separate library)
ko.bindingHandlers.enterKey = {
init : function(element, valueAccessor, allBindingsAccessor, data) {
var wrappedHandler, newValueAccessor;
// wrap the handler with a check for the enter key
wrappedHandler = function(data, event) {
if (event.keyCode === ENTER_KEY) {
valueAccessor().call(this, data, event);
}
};
// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor = function() {
return {
keyup : wrappedHandler
};
};
// call the real event binding's init function
ko.bindingHandlers.event.init(element, newValueAccessor,
allBindingsAccessor, data);
}
};
// wrapper to hasfocus that also selects text and applies focus async
ko.bindingHandlers.selectAndFocus = {
init : function(element, valueAccessor, allBindingsAccessor) {
ko.bindingHandlers.hasfocus.init(element, valueAccessor,
allBindingsAccessor);
ko.utils.registerEventHandler(element, 'focus', function() {
element.focus();
});
},
update : function(element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus
setTimeout(function() {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
}
};
function TransactionsViewModel() {
var self = this;
var categories = [];
self.newTransactions = ko.observableArray([]);
self.updateTransactions = function(repository)
{
for (var category in repository.Data)
{
for (var comment in repository.Data[category])
{
var item = {}
item.date = repository.Data[comment].length;
item.comment = comment;
item.income = 0;
item.outcome = 0;
for (var transactionIndex in repository.Data[category][comment])
{
var transaction = repository.Data[category][comment][transactionIndex];
item.income = item.income + transaction.income;
item.outcome = item.outcome + transaction.outcome;
}
self.newTransactions.push(item);
}
}
};
}
var urlExpensesViewModle = {};
urlExpensesViewModle.get = '{% url easyfin.views.getExpenses %}';
urlExpensesViewModle.update = '{% url easyfin.views.updateExpense %}';
urlExpensesViewModle.add = '{% url easyfin.views.newExpense %}';
var expensesVM = new ExpensesViewModel(urlExpensesViewModle);
var transactionsVM = new TransactionsViewModel();
var expenseCalendar = new BootstrapDatePaginationController("expense_date", expensesVM);
ko.applyBindings(expensesVM, document.getElementById("expenses"));
ko.applyBindings(transactionsVM, document.getElementById("transactions"));
function TransactionPopoverWidget() {
var self = this;
var targetEvent;
self.moneyList = ko.observableArray([]);
function getData() {
self.moneyList.removeAll();
$.post('{% url easyfin.views.getMoneyList %}', {}, function(allData) {
$.each(allData, function(index, value) {
self.moneyList.push(value);
});
});
};
self.show = function(event)
{
targetEvent = event;
$("#transaction-popover").css({top: $($(targetEvent.target).children(".ui-selected").get(0)).offset().top - $("#transaction-popover").height()});
$("#transaction-popover").show();
};
function findIndexByCategory(category)
{
for ( var i = 0; i < transactionsVM.newTransactions().length; i = i + 1) {
if (transactionsVM.newTransactions()[i].category === category) {
return i;
}
}
}
self.apply = function() {
var category = $("#transaction-popover select").val();
var caregoryIndex = findIndexByCategory(category);
//TODO: is it possible to remove by index? Make this code faster?
var result = [];
$(targetEvent.target).children(".ui-selected")
.each(
function(index) {
var index = $(targetEvent.target)
.children("tr").index(this);
var d = transactionsVM.newTransactions()[0]
.data()[index];
transactionsVM.newTransactions()[categoryIndex].data
.push(d);
result.push(d);
});
$.each(result, function(index, el) {
transactionsVM.newTransactions()[0].data.remove(el)
});
$("#transaction-popover").hide();
};
$("#transaction-popover .popover-title .close").click(function() {
$("#transaction-popover").hide();
});
$("#transaction-popover .popover-content .btn").click(function() {
self.apply();
});
getData();
}
var transactionPopover = new TransactionPopoverWidget();
ko.applyBindings(transactionPopover, document
.getElementById("transaction-popover"));
function OnTransactionSelection(event, ui) {
transactionPopover.show(event);
}
$(function() {
$('#fileupload').fileupload({
dataType : 'json',
done : function(e, data) {
$.each(data.result.files, function(i, file) {
$.each(file.data, function(j, row) {
var a_row = {};
a_row.category = row.category;
a_row.data = ko.observableArray([]);
$.each(row.data, function(k, data_row) {
a_row.data.push(data_row);
});
transactionsVM.newTransactions.push(a_row);
});
});
$(".transaction_items").selectable({
filter : "tr",
stop : OnTransactionSelection
});
}
});
});
</script>
</body>
</html>