d9d10f45dc9b5309982067fc8eb0dd05fdc87573
[outofuni/modular.git] / app / www / js / index.js
1 /*
2  *
3  * modular: simple app to count whatever you like
4  * author: hackbrd@hackdaworld.org
5  *
6  */
7
8 /*
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
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
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
24  * under the License.
25  */
26
27 var modular = {
28         init: function() {
29                 document.addEventListener('deviceready',this.startup,false);
30                 document.addEventListener('backbutton',this.backbtn,false);
31         },
32         startup: function() {
33                 // indexed db
34                 //idb.name='modular';
35                 //idb.del();
36                 var stores={
37                         'conf': db_conf_store,
38                         'count': db_count_store
39                 };
40                 idb.init('modular',modular.dbcallback,1,stores);
41         },
42         backbtn: function() {
43                 if($('#main').css('display')=='none') {
44                         navigator.app.exitApp();
45                 }
46                 else {
47                         modular.dbcallback();
48                 }
49         },
50         touchtimer: null,
51         longtouch: false,
52         dbcallback: function() {
53                 $('#main').css('display','none');
54                 $('#moddiag').css('display','block');
55                 idb.get_store_items('conf',function(item) {
56                         var html="";
57                         for(var i in item) {
58                                 cl("item "+i);
59                                 //objdbg(item[i]);
60                                 html+="<div data-id="+i+" class=mdcbtn>"+
61                                       item[i].name+"</div>";
62                         }
63                         $('#mdcont').html(html);
64                         vert_align_text('.mdhead');
65                         vert_align_text('.mdcbtn');
66                         /*
67                         $('.mdcbtn').click(function(event) {
68                                 var conf=$(this).attr('data-id');
69                                 modular.init_config(conf);
70                         });
71                         */
72                         $('.mdcbtn').mouseup(function() {
73                                 clearTimeout(modular.touchtimer);
74                                 if(!modular.longtouch) {
75                                         cl("click!");
76                                         var conf=$(this).attr('data-id');
77                                         modular.init_config(conf);
78                                 }
79                                 return false;
80                         });
81                         $('.mdcbtn').mousedown(function(event) {
82                                 modular.longtouch=false;
83                                 modular.touchtimer=setTimeout(function() {
84                                         cl("long touch!");
85                                         modular.longtouch=true;
86                                         var did=$(event.target).attr('data-id');
87                                         modular.del_conf(Number(did));
88                                 },500);
89                                 return false;
90                         });
91                 });
92                 $('.mdni').change(function(event) {
93                         modular.md_input_event(event.target.id);
94                 });
95         },
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();
101                         },200);
102                 });
103         },
104         md_input_event: function(id) {
105                 var html="";
106                 var valid=true;
107                 switch(id) {
108                 case 'mditot':
109                 case 'mdirows':
110                 case 'mdicols':
111                         var rows=$('#mdirows').val();
112                         var cols=$('#mdicols').val();
113                         var tot=$('#mditot').val();
114                         if(tot>rows*cols) {
115                                 html="<br><b>Pleas increas rows "+
116                                      "and/or columns ...</b>";
117                                 valid=false;
118                         }
119                         if((rows>0)&&(cols>0))
120                                 html+="<br>Row-Column: Name Color";
121                         else
122                                 valid=false;
123                         var t=0;
124                         var s="<br>";
125                         for(var r=1;r<=rows;r++) {
126                                 for(var c=1;c<=cols;c++) {
127                                         t+=1;
128                                         if(tot<t) break;
129                                         s+=r+"-"+c+": "+
130                                            "<input class=mdiin type=text"+
131                                            " name=n"+t+">"+
132                                            "<input class=mdiic type=color"+
133                                            " name=c"+t+"><br>";
134                                 }
135                         }
136                         var go="";
137                         if(valid)
138                                 go="<br><button id=mdngo>Go</button>";
139                         $('#mdnc').html(html+s+go);
140                         if(valid)
141                                 $('#mdngo').click(function() {
142                                         var na=[];
143                                         var ca=[];
144                                         var cnta=[];
145                                         $('.mdiin').each(function() {
146                                                 na.push($(this).val());
147                                                 cnta.push(0);
148                                         });
149                                         $('.mdiic').each(function() {
150                                                 ca.push($(this).val());
151                                         });
152                                         var item={
153                                                 name: $('#mdiname').val(),
154                                                 items: tot,
155                                                 rows: rows,
156                                                 cols: cols,
157                                                 itemname: na,
158                                                 cnt: cnta,
159                                                 colors: ca
160                                         };
161                                         idb.add_store_item('conf',item,
162                                                            function() {
163                                                 setTimeout(function() {
164                                                         modular.init_config(
165                                                                 'last'
166                                                         );
167                                                 },200);
168                                         });
169                                 });
170                         break;
171                 default:
172                         break;
173                 }
174         },
175         confignum: 0,
176         config: {},
177         init_config: function(conf) {
178                 if(conf=='last') {
179                         cl("starting up last configuration!");
180                         idb.get_store_items('conf',function(item) {
181                                 for(var i in item);
182                                 cl("  last config is "+i);
183                                 modular.start_counter(item[i]);
184                                 modular.confignum=Number(i);
185                                 modular.config=item[i];
186                         },0,-1);
187                 }
188                 else {
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);
193                                 modular.config=item;
194                         });
195                 }
196         },
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;
202                 var html="";
203                 for(var i=1;i<=conf.items;i++) {
204                         html+="<div id=item"+i+" class=item></div>";
205                 }
206                 $('#main').html(html);
207                 for(var i=1;i<=conf.items;i++) {
208                         var id="#item"+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]);
213                         vert_align_text(id);
214                 }
215                 /*
216                 $('.item').click(function(event) {
217                         var id='#'+event.target.id;
218                         var istr=event.target.id.replace("item","");
219                         var i=Number(istr);
220                         modular.inc_counter(id,i)
221                 });
222                 */
223                 $('.item').mouseup(function() {
224                         clearTimeout(modular.touchtimer);
225                         if(!modular.longtouch) {
226                                 cl("click!");
227                                 var id='#'+event.target.id;
228                                 var istr=event.target.id.replace("item","");
229                                 var i=Number(istr);
230                                 modular.inc_counter(id,i)
231                         }
232                         return false;
233                 });
234                 $('.item').mousedown(function(event) {
235                         modular.longtouch=false;
236                         modular.touchtimer=setTimeout(function() {
237                                 cl("long touch!");
238                                 modular.longtouch=true;
239                                 var id='#'+event.target.id;
240                                 var istr=event.target.id.replace("item","");
241                                 var i=Number(istr);
242                                 modular.dec_counter(id,i)
243                         },500);
244                         return false;
245                 });
246         },
247         inc_counter: function(id,i) {
248                 var conf=modular.config;
249                 var cn=modular.confignum;
250                 conf.cnt[i-1]+=1;
251                 var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
252                 cntitem={
253                         date: (new Date()).toString(),
254                         confignum: cn,
255                         itemnum: i-1,
256                         counter: conf.cnt[i-1]
257                 };
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() {
262                                 $(id).html(html);
263                         });
264                 });
265         },
266         dec_counter: function(id,i) {
267                 var conf=modular.config;
268                 var cn=modular.confignum;
269                 conf.cnt[i-1]-=1;
270                 var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
271                 cntitem={
272                         date: (new Date()).toString(),
273                         confignum: cn,
274                         itemnum: i-1,
275                         counter: conf.cnt[i-1]
276                 };
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() {
281                                 $(id).html(html);
282                         });
283                 });
284         }
285 };
286
287 $(document).ready(function() {
288         if('cordova' in window) {
289                 modular.init();
290         }
291         else {
292                 modular.startup();
293         }
294 });
295