+/*
+ *
+ * modular: simple app to count whatever you like
+ * author: hackbrd@hackdaworld.org
+ *
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var modular = {
+ init: function() {
+ document.addEventListener('deviceready',this.startup,false);
+ },
+ startup: function() {
+ // indexed db
+ //idb.name='modular';
+ //idb.del();
+ var stores={
+ 'conf': db_conf_store,
+ 'count': db_count_store
+ };
+ idb.init('modular',modular.dbcallback,1,stores);
+ },
+ dbcallback: function() {
+ idb.get_store_items('conf',function(item) {
+ var html="";
+ for(var i in item) {
+ cl("item "+i);
+ objdbg(item[i]);
+ html+="<div data-id="+i+" class=mdcbtn>"+
+ item[i].name+"</div>";
+ }
+ $('#mdcont').html(html);
+ vert_align_text('.mdhead');
+ vert_align_text('.mdcbtn');
+ $('.mdcbtn').click(function(event) {
+ var conf=$(this).attr('data-id');
+ modular.init_config(conf);
+ });
+ });
+ $('.mdni').change(function(event) {
+ modular.md_input_event(event.target.id);
+ });
+ },
+ md_input_event: function(id) {
+ var html="";
+ var valid=true;
+ switch(id) {
+ case 'mditot':
+ case 'mdirows':
+ case 'mdicols':
+ var rows=$('#mdirows').val();
+ var cols=$('#mdicols').val();
+ var tot=$('#mditot').val();
+ if(tot>rows*cols) {
+ html="<br><b>Pleas increas rows "+
+ "and/or columns ...</b>";
+ valid=false;
+ }
+ if((rows>0)&&(cols>0))
+ html+="<br>Row-Column: Name Color";
+ else
+ valid=false;
+ var t=0;
+ var s="<br>";
+ for(var r=1;r<=rows;r++) {
+ for(var c=1;c<=cols;c++) {
+ t+=1;
+ if(tot<t) break;
+ s+=r+"-"+c+": "+
+ "<input class=mdiin type=text"+
+ " name=n"+t+">"+
+ "<input class=mdiic type=color"+
+ " name=c"+t+"><br>";
+ }
+ }
+ var go="";
+ if(valid)
+ go="<br><button id=mdngo>Go</button>";
+ $('#mdnc').html(html+s+go);
+ if(valid)
+ $('#mdngo').click(function() {
+ var na=[];
+ var ca=[];
+ var cnta=[];
+ $('.mdiin').each(function() {
+ na.push($(this).val());
+ cnta.push(0);
+ });
+ $('.mdiic').each(function() {
+ ca.push($(this).val());
+ });
+ var item={
+ name: $('#mdiname').val(),
+ items: tot,
+ rows: rows,
+ cols: cols,
+ itemname: na,
+ cnt: cnta,
+ colors: ca
+ };
+ idb.add_store_item('conf',item,
+ function() {
+ modular.init_config('last');
+ });
+ });
+ break;
+ default:
+ break;
+ }
+ },
+ confignum: 0,
+ config: {},
+ init_config: function(conf) {
+ if(conf=='last') {
+ cl("starting up last configuration!");
+ idb.get_store_items('conf',function(item) {
+ for(var i in item);
+ modular.start_counter(item[i]);
+ modular.confignum=Number(i);
+ modular.config=item[i];
+ },0,-1);
+ }
+ else {
+ cl("starting configuration "+conf);
+ idb.get_item_by_key('conf',Number(conf),function(item) {
+ modular.start_counter(item);
+ modular.confignum=Number(conf);
+ modular.config=item;
+ });
+ }
+ },
+ start_counter: function(conf) {
+ $('#moddiag').css('display','none');
+ $('#main').css('display','block');
+ var ww=100/conf.cols;
+ var wh=100/conf.rows;
+ var html="";
+ for(var i=1;i<=conf.items;i++) {
+ html+="<div id=item"+i+" class=item></div>";
+ }
+ $('#main').html(html);
+ for(var i=1;i<=conf.items;i++) {
+ var id="#item"+i;
+ $(id).css('width',ww+'%');
+ $(id).css('height',wh+'%');
+ $(id).css('background-color',conf.colors[i-1]);
+ $(id).html(conf.itemname[i-1]+": "+conf.cnt[i-1]);
+ vert_align_text(id);
+ }
+ $('.item').click(function(event) {
+ var id='#'+event.target.id;
+ var istr=event.target.id.replace("item","");
+ var i=Number(istr);
+ modular.inc_counter(id,i)
+ });
+ },
+ inc_counter: function(id,i) {
+ var conf=modular.config;
+ var cn=modular.confignum;
+ conf.cnt[i-1]+=1;
+ var html=conf.itemname[i-1]+": "+conf.cnt[i-1];
+ cntitem={
+ date: (new Date()).toString(),
+ confignum: cn,
+ itemnum: i-1,
+ counter: conf.cnt[i-1]
+ };
+ idb.add_store_item('count',cntitem,function() {
+ cl("increased counter for item "+i-1+
+ " to "+conf.cnt[i-1]);
+ idb.update_store_item('conf',cn,conf,function() {
+ $(id).html(html);
+ });
+ });
+ }
+};
+
+$(document).ready(function() {
+ if('cordova' in window) {
+ modular.init();
+ }
+ else {
+ modular.startup();
+ }
+});
+