initial checkin of modular app
[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         },
31         startup: function() {
32                 // indexed db
33                 //idb.name='modular';
34                 //idb.del();
35                 var stores={
36                         'conf': db_conf_store,
37                         'count': db_count_store
38                 };
39                 idb.init('modular',modular.dbcallback,1,stores);
40         },
41         dbcallback: function() {
42                 idb.get_store_items('conf',function(item) {
43                         var html="";
44                         for(var i in item) {
45                                 cl("item "+i);
46                                 objdbg(item[i]);
47                                 html+="<div data-id="+i+" class=mdcbtn>"+
48                                       item[i].name+"</div>";
49                         }
50                         $('#mdcont').html(html);
51                         vert_align_text('.mdhead');
52                         vert_align_text('.mdcbtn');
53                         $('.mdcbtn').click(function(event) {
54                                 var conf=$(this).attr('data-id');
55                                 modular.init_config(conf);
56                         });
57                 });
58                 $('.mdni').change(function(event) {
59                         modular.md_input_event(event.target.id);
60                 });
61         },
62         md_input_event: function(id) {
63                 var html="";
64                 var valid=true;
65                 switch(id) {
66                 case 'mditot':
67                 case 'mdirows':
68                 case 'mdicols':
69                         var rows=$('#mdirows').val();
70                         var cols=$('#mdicols').val();
71                         var tot=$('#mditot').val();
72                         if(tot>rows*cols) {
73                                 html="<br><b>Pleas increas rows "+
74                                      "and/or columns ...</b>";
75                                 valid=false;
76                         }
77                         if((rows>0)&&(cols>0))
78                                 html+="<br>Row-Column: Name Color";
79                         else
80                                 valid=false;
81                         var t=0;
82                         var s="<br>";
83                         for(var r=1;r<=rows;r++) {
84                                 for(var c=1;c<=cols;c++) {
85                                         t+=1;
86                                         if(tot<t) break;
87                                         s+=r+"-"+c+": "+
88                                            "<input class=mdiin type=text"+
89                                            " name=n"+t+">"+
90                                            "<input class=mdiic type=color"+
91                                            " name=c"+t+"><br>";
92                                 }
93                         }
94                         var go="";
95                         if(valid)
96                                 go="<br><button id=mdngo>Go</button>";
97                         $('#mdnc').html(html+s+go);
98                         if(valid)
99                                 $('#mdngo').click(function() {
100                                         var na=[];
101                                         var ca=[];
102                                         var cnta=[];
103                                         $('.mdiin').each(function() {
104                                                 na.push($(this).val());
105                                                 cnta.push(0);
106                                         });
107                                         $('.mdiic').each(function() {
108                                                 ca.push($(this).val());
109                                         });
110                                         var item={
111                                                 name: $('#mdiname').val(),
112                                                 items: tot,
113                                                 rows: rows,
114                                                 cols: cols,
115                                                 itemname: na,
116                                                 cnt: cnta,
117                                                 colors: ca
118                                         };
119                                         idb.add_store_item('conf',item,
120                                                            function() {
121                                                 modular.init_config('last');
122                                         });
123                                 });
124                         break;
125                 default:
126                         break;
127                 }
128         },
129         confignum: 0,
130         config: {},
131         init_config: function(conf) {
132                 if(conf=='last') {
133                         cl("starting up last configuration!");
134                         idb.get_store_items('conf',function(item) {
135                                 for(var i in item);
136                                 modular.start_counter(item[i]);
137                                 modular.confignum=Number(i);
138                                 modular.config=item[i];
139                         },0,-1);
140                 }
141                 else {
142                         cl("starting configuration "+conf);
143                         idb.get_item_by_key('conf',Number(conf),function(item) {
144                                 modular.start_counter(item);
145                                 modular.confignum=Number(conf);
146                                 modular.config=item;
147                         });
148                 }
149         },
150         start_counter: function(conf) {
151                 $('#moddiag').css('display','none');
152                 $('#main').css('display','block');
153                 var ww=100/conf.cols;
154                 var wh=100/conf.rows;
155                 var html="";
156                 for(var i=1;i<=conf.items;i++) {
157                         html+="<div id=item"+i+" class=item></div>";
158                 }
159                 $('#main').html(html);
160                 for(var i=1;i<=conf.items;i++) {
161                         var id="#item"+i;
162                         $(id).css('width',ww+'%');
163                         $(id).css('height',wh+'%');
164                         $(id).css('background-color',conf.colors[i-1]);
165                         $(id).html(conf.itemname[i-1]+": "+conf.cnt[i-1]);
166                         vert_align_text(id);
167                 }
168                 $('.item').click(function(event) {
169                         var id='#'+event.target.id;
170                         var istr=event.target.id.replace("item","");
171                         var i=Number(istr);
172                         modular.inc_counter(id,i)
173                 });
174         },
175         inc_counter: function(id,i) {
176                 var conf=modular.config;
177                 var cn=modular.confignum;
178                 conf.cnt[i-1]+=1;
179                 var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
180                 cntitem={
181                         date: (new Date()).toString(),
182                         confignum: cn,
183                         itemnum: i-1,
184                         counter: conf.cnt[i-1]
185                 };
186                 idb.add_store_item('count',cntitem,function() {
187                         cl("increased counter for item "+i-1+
188                            " to "+conf.cnt[i-1]);
189                         idb.update_store_item('conf',cn,conf,function() {
190                                 $(id).html(html);
191                         });
192                 });
193         }
194 };
195
196 $(document).ready(function() {
197         if('cordova' in window) {
198                 modular.init();
199         }
200         else {
201                 modular.startup();
202         }
203 });
204