3 * modular: simple app to count whatever you like
4 * author: hackbrd@hackdaworld.org
9 * Licensed to the Apache Software Foundation (ASF) under one
10 * or more contributor license agreements. See the NOTICE file
11 * distributed with this work for additional information
12 * regarding copyright ownership. The ASF licenses this file
13 * to you under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance
15 * with the License. You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing,
20 * software distributed under the License is distributed on an
21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22 * KIND, either express or implied. See the License for the
23 * specific language governing permissions and limitations
29 document.addEventListener('deviceready',this.startup,false);
30 document.addEventListener('backbutton',this.backbtn,false);
37 'conf': db_conf_store,
38 'count': db_count_store
40 idb.init('modular',modular.dbcallback,1,stores);
43 if($('#main').css('display')=='none') {
44 navigator.app.exitApp();
52 dbcallback: function() {
53 $('#main').css('display','none');
54 $('#moddiag').css('display','block');
55 idb.get_store_items('conf',function(item) {
60 html+="<div data-id="+i+" class=mdcbtn>"+
61 item[i].name+"</div>";
63 $('#mdcont').html(html);
64 vert_align_text('.mdhead');
65 vert_align_text('.mdcbtn');
67 $('.mdcbtn').click(function(event) {
68 var conf=$(this).attr('data-id');
69 modular.init_config(conf);
72 $('.mdcbtn').mouseup(function() {
73 clearTimeout(modular.touchtimer);
74 if(!modular.longtouch) {
76 var conf=$(this).attr('data-id');
77 modular.init_config(conf);
81 $('.mdcbtn').mousedown(function(event) {
82 modular.longtouch=false;
83 modular.touchtimer=setTimeout(function() {
85 modular.longtouch=true;
86 var did=$(event.target).attr('data-id');
87 modular.del_conf(Number(did));
92 $('.mdni').change(function(event) {
93 modular.md_input_event(event.target.id);
96 del_conf: function (did) {
97 idb.del_item_by_key('conf',did,function() {
98 setTimeout(function() {
99 cl("deleted configuration "+did+"...");
100 modular.dbcallback();
104 md_input_event: function(id) {
111 var rows=$('#mdirows').val();
112 var cols=$('#mdicols').val();
113 var tot=$('#mditot').val();
115 html="<br><b>Pleas increas rows "+
116 "and/or columns ...</b>";
119 if((rows>0)&&(cols>0))
120 html+="<br>Row-Column: Name Color";
125 for(var r=1;r<=rows;r++) {
126 for(var c=1;c<=cols;c++) {
130 "<input class=mdiin type=text"+
132 "<input class=mdiic type=color"+
138 go="<br><button id=mdngo>Go</button>";
139 $('#mdnc').html(html+s+go);
141 $('#mdngo').click(function() {
145 $('.mdiin').each(function() {
146 na.push($(this).val());
149 $('.mdiic').each(function() {
150 ca.push($(this).val());
153 name: $('#mdiname').val(),
161 idb.add_store_item('conf',item,
163 setTimeout(function() {
177 init_config: function(conf) {
179 cl("starting up last configuration!");
180 idb.get_store_items('conf',function(item) {
182 cl(" last config is "+i);
183 modular.start_counter(item[i]);
184 modular.confignum=Number(i);
185 modular.config=item[i];
189 cl("starting configuration "+conf);
190 idb.get_item_by_key('conf',Number(conf),function(item) {
191 modular.start_counter(item);
192 modular.confignum=Number(conf);
197 start_counter: function(conf) {
198 $('#moddiag').css('display','none');
199 $('#main').css('display','block');
200 var ww=100/conf.cols;
201 var wh=100/conf.rows;
203 for(var i=1;i<=conf.items;i++) {
204 html+="<div id=item"+i+" class=item></div>";
206 $('#main').html(html);
207 for(var i=1;i<=conf.items;i++) {
209 $(id).css('width',ww+'%');
210 $(id).css('height',wh+'%');
211 $(id).css('background-color',conf.colors[i-1]);
212 $(id).html(conf.itemname[i-1]+": "+conf.cnt[i-1]);
216 $('.item').click(function(event) {
217 var id='#'+event.target.id;
218 var istr=event.target.id.replace("item","");
220 modular.inc_counter(id,i)
223 $('.item').mouseup(function() {
224 clearTimeout(modular.touchtimer);
225 if(!modular.longtouch) {
227 var id='#'+event.target.id;
228 var istr=event.target.id.replace("item","");
230 modular.inc_counter(id,i)
234 $('.item').mousedown(function(event) {
235 modular.longtouch=false;
236 modular.touchtimer=setTimeout(function() {
238 modular.longtouch=true;
239 var id='#'+event.target.id;
240 var istr=event.target.id.replace("item","");
242 modular.dec_counter(id,i)
247 inc_counter: function(id,i) {
248 var conf=modular.config;
249 var cn=modular.confignum;
251 var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
253 date: (new Date()).toString(),
256 counter: conf.cnt[i-1]
258 idb.add_store_item('count',cntitem,function() {
259 cl("increased counter for item "+i-1+
260 " to "+conf.cnt[i-1]);
261 idb.update_store_item('conf',cn,conf,function() {
266 dec_counter: function(id,i) {
267 var conf=modular.config;
268 var cn=modular.confignum;
270 var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
272 date: (new Date()).toString(),
275 counter: conf.cnt[i-1]
277 idb.add_store_item('count',cntitem,function() {
278 cl("decreased counter for item "+i-1+
279 " to "+conf.cnt[i-1]);
280 idb.update_store_item('conf',cn,conf,function() {
287 $(document).ready(function() {
288 if('cordova' in window) {