-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (154 loc) · 7.2 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Projeto criado por Walter Gandarella
*/
(function() {
var _teclado = document.querySelector('div.teclado');
var _botoes = document.getElementsByClassName('botao');
var _lista = document.getElementsByClassName('lista-item');
var _liberado = false;
var _arr = [];
var _config = {itemAtual: 0, campoAtual: 0, campoTemp: ''}
primeiraLista();
carregaLista();
document.querySelector('html').addEventListener('click', function(){
if (!_liberado){
_teclado.classList.remove('teclado-show');
document.querySelector('.lista').classList.remove('lista-teclado');
limpaLista();
}
});
for (var i = 0; i < _lista.length; i++){
_lista[i].addEventListener('click', selLista);
}
for (var i = 0; i < _botoes.length; i++){
_botoes[i].addEventListener('click', botaoTeclado);
}
function selLista(){
limpaLista();
this.classList.add('lista-selected');
_teclado.classList.add('teclado-show');
document.querySelector('.lista').classList.add('lista-teclado');
this.childNodes[1].childNodes[0].classList.add('preco-ativo');
_liberado = true;
_config.itemAtual = parseInt(this.getAttribute('data-id'));
var _rect = this.getBoundingClientRect();
window.setTimeout(function(){
_liberado = false;
var _rbody = document.body.getBoundingClientRect();
var _ancor = document.getElementById('ancora').getBoundingClientRect();
if (_rect.top - _rbody.top > 425){
window.scrollTo(_ancor.top - _rbody.top, 0);
}
}, 200);
}
function proxCampo(){
_lista[_config.itemAtual].childNodes[1].childNodes[_config.campoAtual].classList.remove('preco-ativo');
if (_config.campoAtual == 0){
if (_config.campoTemp != '')
_arr[_config.itemAtual].valor = parseFloat(_config.campoTemp);
_config.campoAtual++;
_config.campoTemp = '';
_lista[_config.itemAtual].childNodes[1].childNodes[_config.campoAtual].classList.add('preco-ativo');
//console.log(_arr[_config.itemAtual]);
} else if (_config.campoAtual == 1){
if (_config.campoTemp != '')
_arr[_config.itemAtual].qtde = parseFloat(_config.campoTemp);
_liberado = false;
concluiEdicao();
//console.log(_arr[_config.itemAtual]);
}
}
function concluiEdicao(){
document.getElementById('total-'+_config.itemAtual).innerHTML = 'R$ '+(parseFloat(_arr[_config.itemAtual].valor) * parseFloat(_arr[_config.itemAtual].qtde)).toFixed(2);
_config.campoAtual = 0;
_config.campoTemp = '';
console.log('limpaLista()');
limpaLista();
}
function limpaLista(){
for (var i = 0; i < _lista.length; i++){
_lista[i].classList.remove('lista-selected');
_lista[i].childNodes[1].childNodes[0].classList.remove('preco-ativo');
_lista[i].childNodes[1].childNodes[1].classList.remove('preco-ativo');
_lista[i].childNodes[1].childNodes[2].classList.remove('preco-ativo');
}
}
function botaoTeclado(){
var _val = this.getAttribute('data-valor');
_liberado = true;
switch(_val){
case 'ok':
proxCampo();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
_config.campoTemp += _val;
if (_config.campoAtual == 0) {
document.getElementById('preco-'+_config.itemAtual).innerHTML = 'R$ '+_config.campoTemp+' / '+_arr[_config.itemAtual].unidadeValor;
} else {
document.getElementById('qtde-'+_config.itemAtual).innerHTML = _config.campoTemp+' '+_arr[_config.itemAtual].unidadeQtde;
}
break;
case 'kg':
case 'g':
case 'l':
case 'ml':
case 'un':
if (_config.campoAtual == 0){
_arr[_config.itemAtual].unidadeValor = _val;
document.getElementById('preco-'+_config.itemAtual).innerHTML = 'R$ '+_config.campoTemp+' / '+_arr[_config.itemAtual].unidadeValor;
} else {
_arr[_config.itemAtual].unidadeQtde = _val;
document.getElementById('qtde-'+_config.itemAtual).innerHTML = _config.campoTemp+' '+_arr[_config.itemAtual].unidadeQtde;
}
break;
case 'c':
limpaLista();
_liberado = false;
break;
}
window.setTimeout(function(){ _liberado = false; }, 200);
}
function primeiraLista(){
_arr.push({item: 'Tomate vermelho', valor: 1.5, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'g'});
_arr.push({item: 'Leite UHT', valor: 2.5, unidadeValor: 'l', qtde: 1, unidadeQtde: 'l'});
_arr.push({item: 'Biscoito', valor: 1.9, unidadeValor: 'un', qtde: 1, unidadeQtde: 'un'});
_arr.push({item: 'Arroz', valor: 2.35, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'kg'});
_arr.push({item: 'Tomate vermelho', valor: 1.5, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'g'});
_arr.push({item: 'Leite UHT', valor: 2.5, unidadeValor: 'l', qtde: 1, unidadeQtde: 'l'});
_arr.push({item: 'Biscoito', valor: 1.9, unidadeValor: 'un', qtde: 1, unidadeQtde: 'un'});
_arr.push({item: 'Arroz', valor: 2.35, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'kg'});
_arr.push({item: 'Tomate vermelho', valor: 1.5, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'g'});
_arr.push({item: 'Leite UHT', valor: 2.5, unidadeValor: 'l', qtde: 1, unidadeQtde: 'l'});
_arr.push({item: 'Biscoito', valor: 1.9, unidadeValor: 'un', qtde: 1, unidadeQtde: 'un'});
_arr.push({item: 'Arroz', valor: 2.35, unidadeValor: 'kg', qtde: 1, unidadeQtde: 'kg'});
}
function carregaLista(){
var _ht = '';
for (var i = 0; i < _arr.length; i++){
_ht += '<li class="lista-item" data-id="'+i+'" id="item-'+i+'">';
_ht += '<div class="linha clearfix">';
_ht += '<input type="checkbox">';
_ht += '<div class="item">'+_arr[i].item+'</div>';
_ht += '</div>';
_ht += '<div class="linha clearfix datalhes">';
_ht += '<span class="preco" id="preco-'+i+'">R$ '+_arr[i].valor+' / '+_arr[i].unidadeValor+'</span>';
_ht += '<span id="qtde-'+i+'">'+_arr[i].qtde+' '+_arr[i].unidadeQtde+'</span>';
_ht += '<span id="total-'+i+'">R$ '+(_arr[i].valor * _arr[i].qtde)+'</span>';
_ht += '</div>';
_ht += '</li>';
}
var _items = document.getElementById('items');
_items.innerHTML = _ht;
}
})();