3 * glowingpatrol: simple app to report and locate meter maids
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
35 init: function(event_cb) {
36 // width and height auf io and map window
38 ui.height=$(window).height();
39 ui.width=$(window).width();
40 cl("init: width="+ui.width+", height="+ui.height);
41 ui.winwidth=(ui.width-1)/2;
42 ui.winheight=ui.height;
43 if(ui.height>ui.width) {
45 ui.winheight=(ui.height-1)/2;
47 cl("init: winwidth="+ui.winwidth+", winheight="+ui.winheight);
48 $('#io').css('width',ui.winwidth-2*bw);
49 $('#io').css('height',ui.winheight-2*bw);
50 $('#io').css('border',bw+"px solid black");
51 $('#map').css('width',ui.winwidth-2*bw);
52 $('#map').css('height',ui.winheight-2*bw);
53 $('#map').css('border',bw+"px solid black");
54 // io events (navigation)
55 $('.ionavbtn').click(function(event) {
58 // io events (ctrl, info and settings windows)
59 $('.fwbtn').click(function(event) {
60 event_cb(event,'click');
62 $('.hwbtn').click(function(event) {
63 event_cb(event,'click');
65 $('.twbtn').click(function(event) {
66 event_cb(event,'click');
68 // click and long click events
69 $('.longclickbtn').mouseup(function(event) {
70 clearTimeout(ui.touchtimer);
72 event_cb(event,'click');
73 ui.longclick($(this).attr('data-id'),false);
77 $('.longclickbtn').mousedown(function(event) {
79 ui.touchtimer=setTimeout(function() {
81 var did=$(event.target).attr('data-id');
82 ui.longclick(Number(did),true);
83 event_cb(event,'longclick');
88 longclick: function(did,long) {
89 var str="click event: "+did;
100 draw_map: function() {
102 nav_event: function(event) {
103 $('.ionavbtn').each(function() {
105 if(event.target.id==this.id) {
106 $(s).css('background-color',ionav_col_active);
109 $(s).css('background-color',ionav_col_default);
112 var trgwin=event.target.id+"win";
113 ui.main_show(trgwin);
115 main_show: function(win) {
116 $('.iomainwin').each(function() {
119 $(s).css('display','block');
122 $(s).css('display','none');
126 vert_align_text: function(trg) {
127 /* code snippet to vertically align text inside trg divs */
128 $(trg).each(function() {
129 var lh=$(this).parent().height();
130 $(this).css('line-height',lh+'px');
131 $(this).css('vertical-align','middle');
134 draw_content: function(id,content) {
138 set_switch: function(id,active,txt) {
139 var col=ionav_col_default;
142 col=ionav_col_active;
144 if(txt!==undefined) {
147 $(trg).css('background-color',col);
153 loc: geo_default_loc,
158 geo.locupd=setInterval(function() {
162 getpos: function(cb) {
163 if(navigator.geolocation) {
164 navigator.geolocation.getCurrentPosition(function(pos) {
166 cb(geo.loc,'success');
168 cl("geo: error="+err.code);
169 cb(geo.loc,'error',err.code);
173 cb(geo.loc,'unsupported');
176 loc2ll: function(loc) {
178 if('coords' in loc) {
179 ll[0]=loc.coords.latitude;
180 ll[1]=loc.coords.longitue;
182 else if('latitude' in loc) {
199 center: map_default_center,
200 zoom: map_default_zoom,
201 init: function(mapdiv,loc,zoom) {
203 'attribution': map_tp_attr,
204 'maxZoom': map_tp_mzoom
210 map.tile=L.tileLayer(map_tp_url,tpobj);
211 map.mobj=L.map(mapdiv,lobj);
212 map.tile.addTo(map.mobj);
216 if('coords' in loc) {
217 ll=L.latLng(loc.coords.latitude,loc.coords.longitude);
219 else if('latitude' in loc) {
220 ll=L.latLng(loc.latitude,loc.longitude);
223 ll=L.latLng(loc[0],loc[1]);
227 showpos: function(loc) {
229 map.mobj.setView(map.ll(loc),zoom);
231 set_mark: function(coord,type) {
233 var ll=map.ll(coord);
234 if(type=='current') {
236 prop.fillColor='green';
237 prop.fillOpacity=1.0;
238 map.mobj.removeLayer(map.markc);
239 map.markc=L.circle(ll,20,prop).addTo(map.mobj);
244 prop.fillColor='blue';
245 prop.fillOpacity=1.0;
246 map.mobj.removeLayer(map.marks);
247 map.marks=L.circle(ll,20,prop).addTo(map.mobj);
250 remove_mark: function(type) {
253 map.mobj.removeLayer(map.markc);
256 map.mobj.removeLayer(map.marks);
264 document.addEventListener('deviceready',this.startup,false);
265 window.addEventListener('orientationchange',function() {
266 alert("change in orientation detected, rotating ...");
271 startup: function() {
273 ui.init(gp.event_action);
275 ui.main_show("ctrlwin");
278 mapdiv=document.getElementById('map');
279 map.init(mapdiv,geo.loc,map_default_zoom);
285 'geo': db_geo_config,
288 idb.init('gp',gp.dbcallback,1,stores);
291 geo.init(gp.geocallback);
293 dbcallback: function() {
295 idb.get_store_items('geo',function(item) {
296 var str="Update locion";
297 if(!item[1].update) {
298 var ll=geo.loc2ll(geo.loc);
299 str="Stored location<br>"+
300 ll[0].toFixed(3)+","+ll[1].toFixed(3)+")";
302 ui.draw_content('ioctrlstore',str);
303 ui.set_switch('ioctrlstore',item[1].update);
304 gp.status.geo.coords=item[1].coords;
307 idb.get_store_items('map',function(item) {
308 ui.set_switch('ioctrlcurrent',item[1].recenter)
309 ui.set_switch('ioctrlstored',!(item[1].recenter))
310 gp.status.map.recenter=item[1].recenter;
311 if(item[1].current) {
312 idb.get_store_items('geo',function(item) {
313 map.set_mark(item[1].coords,'current');
316 ui.set_switch('ioctrldisc',item[1].current);
317 gp.status.map.current=item[1].current;
318 idb.get_store_items('geo',function(it) {
320 map.set_mark(item[1].coords,'stored');
322 ui.set_switch('ioctrldiss',item[1].stored);
323 gp.status.map.stored=item[1].stored;
324 ui.set_switch('ctrldissobj',item[1].objects);
325 gp.status.map.objects=item[1].objects;
328 geocallback: function(loc,msg,code) {
330 var str='geocallback error: ';
333 str+="unknown error";
336 str+="permission denied";
339 str+="position unavailable";
347 if(gp.status.map.current)
348 map.set_mark(loc,'current');
349 if(gp.status.map.recenter)
353 event_action: function(event,type) {
355 switch(event.target.id) {
357 gp.button_event(event.target.id);
360 gp.switch_event(event.target.id);
364 if(type=="longclick") {
365 cl("going to do some longclick action ...");
368 switch_event: function(id) {
369 cl("event from id "+id);
373 idb.get_store_items('geo',function(item) {
374 var str="Update location";
377 item[1].coords=geo.loc;
378 var ll=geo.loc2ll(geo.loc);
379 str="Stored location<br>("+
380 ll[0].toFixed(3)+","+
381 ll[1].toFixed(3)+")";
383 item[1].update=nstat;
384 idb.update_store_item('geo',1,item[1],
386 gp.status.geo.coords=item[1].coords;
387 ui.draw_content(id,str);
388 ui.set_switch(id,nstat);
390 if(!gp.status.map.recenter)
396 case 'ioctrlcurrent':
397 idb.get_store_items('map',function(item) {
398 item[1].recenter=true;
399 ui.set_switch(id,true)
400 ui.set_switch('ioctrlstored',false)
401 gp.status.map.recenter=true;
402 map.showpos(geo.loc);
403 idb.update_store_item('map',1,item[1]);
407 idb.get_store_items('map',function(item) {
408 item[1].recenter=false;
409 ui.set_switch('ioctrlcurrent',false)
410 ui.set_switch(id,true)
411 gp.status.map.recenter=false;
412 map.showpos(gp.status.geo.coords);
413 idb.update_store_item('map',1,item[1]);
417 idb.get_store_items('map',function(item) {
421 ui.set_switch(id,nstat);
422 gp.status.map.current=nstat;
423 item[1].current=nstat;
424 idb.update_store_item('map',1,item[1]);
426 map.set_mark(geo.loc,'current');
428 map.mobj.removeLayer(map.markc)
433 idb.get_store_items('map',function(item) {
437 ui.set_switch(id,nstat)
438 gp.status.map.stored=nstat;
439 item[1].stored=nstat;
440 idb.update_store_item('map',1,item[1]);
443 gp.status.geo.coords,'stored'
446 map.mobj.removeLayer(map.marks)
450 idb.get_store_items('map',function(item) {
454 ui.set_switch(id,nstat)
455 gp.status.map.objects=nstat;
456 item[1].objects=nstat;
457 idb.update_store_item('map',1,item[1]);
462 button_event: function(id) {
463 var txt='Reporting ';
484 $(document).ready(function() {
485 if('cordova' in window) {