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
33 init: function(event_cb) {
34 // width and height auf io and map window
35 ui.height=$(document).height();
36 ui.width=$(document).width();
37 cl("init: width="+ui.width+", height="+ui.height);
38 ui.winwidth=(ui.width-1)/2;
39 ui.winheight=ui.height;
40 if(ui.height>ui.width) {
42 ui.winheight=(ui.height-1)/2;
44 cl("init: winwidth="+ui.winwidth+", winheight="+ui.winheight);
45 $('#io').css('width',ui.winwidth);
46 $('#io').css('height',ui.winheight);
47 $('#map').css('width',ui.winwidth);
48 $('#map').css('height',ui.winheight);
49 // io events (navigation)
50 $('.ionavbtn').click(function(event) {
53 // io events (ctrl, info and settings windows)
54 $('.fwbtn').click(function(event) {
55 event_cb(event,'switch');
57 $('.hwbtn').click(function(event) {
58 event_cb(event,'switch');
60 $('.twbtn').click(function(event) {
61 event_cb(event,'switch');
70 draw_map: function() {
72 nav_event: function(event) {
73 $('.ionavbtn').each(function() {
75 if(event.target.id==this.id) {
76 $(s).css('background-color',ionav_col_active);
79 $(s).css('background-color',ionav_col_default);
82 var trgwin=event.target.id+"win";
85 main_show: function(win) {
86 $('.iomainwin').each(function() {
89 $(s).css('display','block');
92 $(s).css('display','none');
96 vert_align_text: function(trg) {
97 /* code snippet to vertically align text inside trg divs */
98 $(trg).each(function() {
99 var lh=$(this).parent().height();
100 $(this).css('line-height',lh+'px');
101 $(this).css('vertical-align','middle');
104 draw_content: function(id,content) {
108 set_switch: function(id,active,txt) {
109 var col=ionav_col_default;
112 col=ionav_col_active;
114 if(txt!==undefined) {
117 $(trg).css('background-color',col);
122 loc: geo_default_loc,
127 geo.locupd=setInterval(function() {
131 getpos: function(cb) {
132 if(navigator.geolocation) {
133 navigator.geolocation.getCurrentPosition(function(pos) {
135 cb(geo.loc,'success');
137 cl("geo: error="+err.code);
138 cb(geo.loc,'error',err.code);
142 cb(geo.loc,'unsupported');
153 center: map_default_center,
154 zoom: map_default_zoom,
155 init: function(mapdiv,loc,zoom) {
157 'attribution': map_tp_attr,
158 'maxZoom': map_tp_mzoom
164 map.tile=L.tileLayer(map_tp_url,tpobj);
165 map.mobj=L.map(mapdiv,lobj);
166 map.tile.addTo(map.mobj);
170 if('coords' in loc) {
171 ll=L.latLng(loc.coords.latitude,loc.coords.longitude);
173 else if('latitude' in loc) {
174 ll=L.latLng(loc.latitude,loc.longitude);
177 ll=L.latLng(loc[0],loc[1]);
181 showpos: function(loc) {
183 map.mobj.setView(map.ll(loc),zoom);
185 set_mark: function(coord,type) {
187 var ll=map.ll(coord);
188 if(type=='current') {
190 prop.fillColor='green';
191 prop.fillOpacity=1.0;
192 map.mobj.removeLayer(map.markc);
193 map.markc=L.circle(ll,20,prop).addTo(map.mobj);
198 prop.fillColor='blue';
199 prop.fillOpacity=1.0;
200 map.mobj.removeLayer(map.marks);
201 map.marks=L.circle(ll,20,prop).addTo(map.mobj);
204 remove_mark: function(type) {
207 map.mobj.removeLayer(map.markc);
210 map.mobj.removeLayer(map.marks);
218 document.addEventListener('deviceready',this.startup,false);
219 window.addEventListener('orientationchange',function() {
220 alert("gp: orientational change detected");
225 startup: function() {
227 ui.init(gp.event_action);
229 ui.main_show("ctrlwin");
232 mapdiv=document.getElementById('map');
233 map.init(mapdiv,geo.loc,map_default_zoom);
239 'geo': db_geo_config,
242 idb.init('gp',gp.dbcallback,1,stores);
245 geo.init(gp.geocallback);
247 dbcallback: function() {
249 idb.get_store_items('geo',function(item) {
250 var str='('+item[1].coords[0].toFixed(3)+','+
251 item[1].coords[1].toFixed(3)+')';
252 var str='Store location '+str;
253 ui.draw_content('ioctrlstore',str);
254 gp.status.geo.coords=item[1].coords;
257 idb.get_store_items('map',function(item) {
258 ui.set_switch('ioctrlcurrent',item[1].recenter)
259 ui.set_switch('ioctrlstored',!(item[1].recenter))
260 gp.status.map.recenter=item[1].recenter;
261 if(item[1].current) {
262 idb.get_store_items('geo',function(item) {
263 map.set_mark(item[1].coords,'current');
266 ui.set_switch('ioctrldisc',item[1].current);
267 gp.status.map.current=item[1].current;
268 idb.get_store_items('geo',function(it) {
270 map.set_mark(item[1].coords,'stored');
271 var str='Display stored location'+
272 '<br>('+it[1].coords[0].toFixed(3)+','+
273 it[1].coords[1].toFixed(3)+')';
274 ui.draw_content('ioctrldiss',str);
276 ui.set_switch('ioctrldiss',item[1].stored);
277 gp.status.map.stored=item[1].stored;
278 ui.set_switch('ctrldissobj',item[1].objects);
279 gp.status.map.objects=item[1].objects;
282 geocallback: function(loc,msg,code) {
283 var str="Display current location<br>(";
288 str+="unknown error";
291 str+="permission denied";
294 str+="position unavailable";
303 var latlng=L.latLng(loc.coords.latitude,
304 loc.coords.longitude);
305 str+=latlng[0].toFixed(3)+','+latlng[1].toFixed(3);
306 if(gp.status.map.current) {
307 map.set_mark(latlng,'current');
309 if(gp.status.map.recenter)
312 map.showpos(gp.status.geo.coords);
315 ui.draw_content('ioctrldisc',str);
317 event_action: function(event,type) {
319 gp.switch_event(event.target.id);
321 gp.button_event(event.target.id);
323 switch_event: function(id) {
324 cl("event from id "+id);
328 idb.get_store_items('geo',function(item) {
329 item[1].coords=geo.loc;
330 idb.update_store_item('geo',1,item[1],
332 gp.status.geo.coords=item[1].coords;
333 var str='Stored location ('+
334 item[1].coords[0].toFixed(3)+
336 item[1].coords[1].toFixed(3)+
338 ui.draw_content('ioctrlstore',str);
340 if(!gp.status.map.recenter)
346 case 'ioctrlcurrent':
347 idb.get_store_items('map',function(item) {
348 item[1].recenter=true;
349 ui.set_switch(id,true)
350 ui.set_switch('ioctrlstored',false)
351 gp.status.map.recenter=true;
352 map.showpos(geo.loc);
353 idb.update_store_item('map',1,item[1]);
357 idb.get_store_items('map',function(item) {
358 item[1].recenter=false;
359 ui.set_switch('ioctrlcurrent',false)
360 ui.set_switch(id,true)
361 gp.status.map.recenter=false;
362 map.showpos(gp.status.geo.coords);
363 idb.update_store_item('map',1,item[1]);
367 idb.get_store_items('map',function(item) {
371 ui.set_switch(id,nstat);
372 gp.status.map.current=nstat;
373 item[1].current=nstat;
374 idb.update_store_item('map',1,item[1]);
376 map.set_mark(geo.loc,'current');
378 map.mobj.removeLayer(map.markc)
383 idb.get_store_items('map',function(item) {
387 ui.set_switch(id,nstat)
388 gp.status.map.stored=nstat;
389 item[1].stored=nstat;
390 idb.update_store_item('map',1,item[1]);
393 gp.status.geo.coords,'stored'
396 map.mobj.removeLayer(map.marks)
400 idb.get_store_items('map',function(item) {
404 ui.set_switch(id,nstat)
405 gp.status.map.objects=nstat;
406 item[1].objects=nstat;
407 idb.update_store_item('map',1,item[1]);
412 button_event: function(id) {
413 var txt='Reporting ';
434 $(document).ready(function() {
435 //if(Modernizr.hasEvent('deviceready')) {
436 if('cordova' in window) {
441 alert("via startup");