-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntryView.js
164 lines (118 loc) · 3.39 KB
/
EntryView.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
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
var EntryView = function(entry) {
var that = this;
if (!(entry instanceof d83entry)) {
throw new TypeError('expecting a d83entry');
}
var box = null;
var externalUserChangePriceCallback = null;
(function() {
//
// setup for the containing div element
//
box = document.createElement('div');
box.classList.add('row');
box.classList.add('list-group-item');
box.style.cursor = 'pointer';
//
// setup for the position/path column
//
var posField = document.createElement('div');
posField.classList.add('col-md-1');
box.appendChild(posField);
//
// create a label for this entry's path/position
//
var label = document.createElement('span');
label.classList.add('label');
label.classList.add('label-default');
label.appendChild(document.createTextNode(entry.getPrettyPath()));
posField.appendChild(label);
//
// setup for the summary column
//
var summaryField = document.createElement('div');
box.appendChild(summaryField);
summaryField.classList.add('col-md-6');
summaryField.innerHTML = entry.summary;
//
// column for the quantity
//
var quantityField = document.createElement('div');
quantityField.classList.add('col-md-2');
quantityField.innerHTML = entry.quantity;
box.appendChild(quantityField);
//
// column for the price
//
var priceField = document.createElement('div');
priceField.classList.add('col-md-2');
priceField.classList.add('form-group');
box.appendChild(priceField);
var priceInput = document.createElement('input');
priceInput.classList.add('form-control');
priceField.appendChild(priceInput);
//
// column for the total price
//
var totalPriceField = document.createElement('div');
totalPriceField.classList.add('col-md-1');
box.appendChild(totalPriceField);
//
// callback to update the total price
//
priceInput.addEventListener('change', function(evt) {
var priceRegex = /^\d+(,(\d\d))?$/;
var matches = priceRegex.exec(priceInput.value);
if (matches === null) {
priceField.classList.add('has-error');
}
else {
var centsPerEuro = 100;
var priceCents = parseInt(matches[0]) * centsPerEuro;
if (matches[2] !== undefined) {
priceCents += parseInt(matches[2]);
}
// clear the contents of the node
if (totalPriceField.hasChildNodes()) {
totalPriceField.removeChild(
totalPriceField.firstChild);
}
// in case the field had an error previously
// reset it
priceField.classList.remove('has-error');
entry.netPricePerUnit = priceCents;
var prettyTotal = (entry.getNetTotal() / 100).toFixed(2);
totalPriceField.appendChild(
document.createTextNode(prettyTotal));
// notify the outside world that a price has been changed
externalUserChangePriceCallback();
}
});
})();
this.getElement = function() {
return box;
};
this.setClickCallback = function(func) {
if (typeof(func) !== 'function') {
throw new TypeError('expecting a function as a callback');
}
box.onclick = function(evt) {
func(that);
};
};
this.getEntry = function() {
return entry;
};
this.setUserChangePriceCallback = function(func) {
if (typeof(func) !== 'function') {
throw new TypeError('expecting a function');
}
externalUserChangePriceCallback = func;
};
this.setActive = function() {
box.classList.add('active');
};
this.setInactive = function() {
box.classList.remove('active');
};
};