Debian Codegraph @

examples/unit-converter/conversion.js

raw
1 /*
2 * Copyright 2012 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 .pragma library
18
19 // the function converts the value entered in the input to the other units
20 function convert(input, converter, index) {
21 if (input && input.text.length <= 0)
22 return;
23 var baseValue = parseFloat(input.text) / parseFloat(converter.model[index].rate);
24 if (baseValue <= 0.0)
25 return;
26 for (var i = 0; i < converter.count; i++) {
27 if (converter.itemAt(i)) {
28 var inputItem = converter.itemAt(i).children[1]
29 if (inputItem !== input) {
30 inputItem.text = parseFloat(converter.model[i].rate) * baseValue;
31 }
32 }
33 }
34 }
35
36 // the function clears every input field data
37 function clear(converter, except) {
38 for (var i = 0; i < converter.count; i++) {
39 if (!except || (except && except !== converter.itemAt(i).children[1]))
40 converter.itemAt(i).children[1].text = "0.0";
41 }
42 }