-
Notifications
You must be signed in to change notification settings - Fork 460
/
view.js
151 lines (132 loc) · 5.1 KB
/
view.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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function View(window) {
this.display = window.document.querySelector('#calculator-display');
this.buttons = window.document.querySelectorAll('#calculator-buttons button');
window.addEventListener('keydown', this.handleKey_.bind(this));
Array.prototype.forEach.call(this.buttons, function(button) {
button.addEventListener('click', this.handleClick_.bind(this));
button.addEventListener('mousedown', this.handleMouse_.bind(this));
button.addEventListener('touchstart', this.handleTouch_.bind(this));
button.addEventListener('touchmove', this.handleTouch_.bind(this));
button.addEventListener('touchend', this.handleTouchEnd_.bind(this));
button.addEventListener('touchcancel', this.handleTouchEnd_.bind(this));
}, this);
}
View.prototype.clearDisplay = function(values) {
this.display.innerHTML = '';
this.addValues(values);
};
View.prototype.addResults = function(values) {
this.appendChild_(this.display, null, 'div', 'hr');
this.addValues(values);
};
View.prototype.addValues = function(values) {
var equation = this.makeElement_('div', 'equation');
this.appendChild_(equation, null, 'span', 'accumulator', values.accumulator);
this.appendChild_(equation, null, 'span', 'operation');
this.appendChild_(equation, '.operation', 'span', 'operator');
this.appendChild_(equation, '.operation', 'span', 'operand', values.operand);
this.appendChild_(equation, '.operator', 'div', 'spacer');
this.appendChild_(equation, '.operator', 'div', 'value', values.operator);
this.setAttribute_(equation, '.accumulator', 'aria-hidden', 'true');
this.display.appendChild(equation).scrollIntoView();
};
View.prototype.setValues = function(values) {
var equation = this.display.lastElementChild;
this.setContent_(equation, '.accumulator', values.accumulator || '');
this.setContent_(equation, '.operator .value', values.operator || '');
this.setContent_(equation, '.operand', values.operand || '');
};
View.prototype.getValues = function() {
var equation = this.display.lastElementChild;
return {
accumulator: this.getContent_(equation, '.accumulator') || null,
operator: this.getContent_(equation, '.operator .value') || null,
operand: this.getContent_(equation, '.operand') || null,
};
};
/** @private */
View.prototype.handleKey_ = function(event) {
this.onKey.call(this, event.shiftKey ? ('^' + event.which) : event.which);
}
/** @private */
View.prototype.handleClick_ = function(event) {
this.onButton.call(this, event.target.dataset.button)
}
/** @private */
View.prototype.handleMouse_ = function(event) {
event.target.setAttribute('data-active', 'mouse');
}
/** @private */
View.prototype.handleTouch_ = function(event) {
event.preventDefault();
this.handleTouchChange_(event.touches[0]);
}
/** @private */
View.prototype.handleTouchEnd_ = function(event) {
this.handleTouchChange_(null);
}
/** @private */
View.prototype.handleTouchChange_ = function(location) {
var previous = this.touched;
if (!this.isInButton_(previous, location)) {
this.touched = this.findButtonContaining_(location);
if (previous)
previous.removeAttribute('data-active');
if (this.touched) {
this.touched.setAttribute('data-active', 'touch');
this.onButton.call(this, this.touched.dataset.button);
}
}
}
/** @private */
View.prototype.findButtonContaining_ = function(location) {
var found;
for (var i = 0; location && i < this.buttons.length && !found; ++i) {
if (this.isInButton_(this.buttons[i], location))
found = this.buttons[i];
}
return found;
}
/** @private */
View.prototype.isInButton_ = function(button, location) {
var bounds = location && button && button.getClientRects()[0];
var x = bounds && location.clientX;
var y = bounds && location.clientY;
var x1 = bounds && bounds.left;
var x2 = bounds && bounds.right;
var y1 = bounds && bounds.top;
var y2 = bounds && bounds.bottom;
return (bounds && x >= x1 && x < x2 && y >= y1 && y < y2);
}
/** @private */
View.prototype.makeElement_ = function(tag, classes, content) {
var element = this.display.ownerDocument.createElement(tag);
element.setAttribute('class', classes);
element.textContent = content || '';
return element;
};
/** @private */
View.prototype.appendChild_ = function(root, selector, tag, classes, content) {
var parent = (root && selector) ? root.querySelector(selector) : root;
parent.appendChild(this.makeElement_(tag, classes, content));
};
/** @private */
View.prototype.setAttribute_ = function(root, selector, name, value) {
var element = root && root.querySelector(selector);
if (element)
element.setAttribute(name, value);
};
/** @private */
View.prototype.setContent_ = function(root, selector, content) {
var element = root && root.querySelector(selector);
if (element)
element.textContent = content || '';
};
/** @private */
View.prototype.getContent_ = function(root, selector) {
var element = root && root.querySelector(selector);
return element ? element.textContent : null;
};