-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapdataobjects.js
More file actions
551 lines (474 loc) · 15.2 KB
/
mapdataobjects.js
File metadata and controls
551 lines (474 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*!
* Datalous Javascript Mapping Library v2.0
* https://github.com/slangberg/Datalous-Core
* Copyright 2013 Sam Langberg (samlangberg.com)
* Released under the Creative Commons Attribution-NonCommercial 4.0 International License
*/
var applicationCore = function(core) {//Application shell this code is the over all controls and data storgage controls all the other
this.astarcore=core;//this holds all sastar object
this.loadGrid=function(data)
{
if(!data)//this checks if the data has been sent as a ver or if it should use its stored map data
{
if(typeof this.mapdata["map"] !== "undefined")//checks to see if the data is avialble
{
this.closedata = eval(this.mapdata["map"]);//unpacks the jason
this.astarcore.loadGrid(this.closedata);//send data to astar core
console.log("closed data set from store");
}
else
{
console.error("no data for map");
}
}
else
{
this.startdata=data;// uses passed in data as start data
console.log("use input data for maps closed");
}
}
this.getFromLocalData=function()//this checks local storage for mao data then sets map data based on what it finds
{
if(typeof(Storage)!=="undefined")
{
if(localStorage.getItem("mapdata"))//check to see if map data is in local storage
{
var mapdata = JSON.parse(localStorage.getItem("mapdata"));//parses the jason local storage var
console.log("map data in local local");
return mapdata;
}//end if has mapdata
else
{
return false;
console.error("no local data for map");
}
}//edn if local
else
{
return false;
console.error("no local local storage data for map");
}//end dle no lcoal
}
this.loadData=function(data)//loads map data
{
this.mapdata = data;
if(typeof(Storage)!=="undefined")
{
localStorage.setItem("mapdata", JSON.stringify(data));
console.log("map data from input - now saved in local");
}//end if starge
}//loadData
this.construct=function(mapdata)
{
var d = new Date();
var n = d.toLocaleString();
console.log("<----- Construct Start: " + n +" ------------->");//for devloper displays load dats
if(typeof mapdata !== "undefined")
{
this.loadData(mapdata);//asgins map data to object peroepty
this.astarcore.genGrid();//gnerates astar cell object grid
this.loadGrid();//this loads the closed squares
if(this.hasOwnProperty("setStartData")){this.setStartData();} else { console.error("Application has not been extended with place data");}//checks to see if method has been extend into app then runs it
if(this.hasOwnProperty("setPlaceData")){this.setPlaceData();} else { console.error("Application has not been extended with place data");}//checks to see if method has been extend into app then runs it
if(this.hasOwnProperty("setPersonData")){this.setPersonData();} else {console.error("Application has not been extended with person data");}//checks to see if method has been extend into app then runs it
console.log("<----- Construct End ------------->");
}
else
{
console.error("construct fail no app data");
}
}//construct
this.setStart=function(start,type)//this processs the start data and start type data and runs the right method accrdingly
{
if(!type){ console.error("app set start fail: no start type"); return false;}//check to see no type throws error and restuns flase
switch(type)
{
case "code":
this.setStartAs(start,"start");//this sets astar start from Token location code, check stokenData
console.log("- start set using startFromData");
break;
case "person"://this sets astar start from person name, check personData
this.setPersonAs(start,"start");
console.log("- start set using setPersonAs");
break;
case "place":
this.setPlaceAs(start,"start");//this sets astar start from place id, check placeData
console.log("- start set using setPlaceAs");
break;
default:
console.error("app set start fail: no vaild start type");//throw error if unsupported place type
return false;
}
}//end set start
this.setTarget=function(target,type)//this processs the target data and start type data and runs the right method accrdingly
{
switch(type)
{
case "code":
this.setStartAs(target,"target");//this sets astar start from start location code, check startData
console.log("- start set using startFromData");
break;
case "type"://this is for place type like mens bath, womens bath, exit
if(this.astarcore.foundstart)//can only process target of type if start has been set
{
this.findClosest(target);//this sets astar target from place type then runs astar to find closest, check placeData
console.log("- target set using findClosest");
}
else
{
console.error("set target error: can not find closest with no start");
}
break;
case "person":
console.log("t"+ target);
this.setPersonAs(target,"target");//sets person as target checl personData
console.log("- target set using setPersonAs");
break;
case "place":
this.setPlaceAs(target,"target");//sets palce as target checl personData
break;
default:
console.error("set target error: not vaild target type");
}
}//end set target
this.dataParser=function(data,input,flag)
{
if(typeof data !== "undefined")
{
switch(type)
{
case "start":
this.astarcore.setStart(data[input].y,data[input].x);
break;
case "target":
this.astarcore.setTarget(data[input].y,data[input].x);
break;
case "find":
this.astarcore.markGrid(data[input].y,data[input].x,flag);
break;
default:
console.error("dataParser Failed: Not vaild type found"); return;
}
return data[input];//returns the found data for extracting other data in the application
}
}
this.clearMap=function()//this processs the target data and start type data and runs the right method accrdingly
{
}
};//end app
//START DATA////////////////////////////////
function startData()
{
this.setStartData=function(data)
{
if(!data)
{
if(typeof this.mapdata["start"] !== "undefined")//checks to see if start map data exists then sets startdata
{
this.startdata=this.mapdata["start"];
}
else
{
console.error("no data for start");
}
}
else
{
this.startdata=data;
console.log("use data for start");
}
}
this.setStartAs=function(code,type)//sets start for uquie start location id code
{
if(typeof this.mapdata["start"] !== "undefined")
{
switch(type)
{
case "start":
this.astarcore.setStart(this.startdata[code].y,this.startdata[code].x);
break;
case "target":
this.astarcore.setTarget(this.startdata[code].y,this.startdata[code].x);
break;
case "find":
this.astarcore.markGrid(this.startdata[code].y,this.startdata[code].x,'person');
break;
default:
console.error("setStarAs Failed: Not vaild type found"); return;
}
return this.startdata[code];//returns the found data for extracting other data in the application
}
}//end setStatt
this.startFromXY=function(x,y)//sets start from x or y cordinets
{
this.astarcore.setStart(x,y);
}//end setStatt
this.showAllStart=function()//this mark the grid for each start location
{
for (start in this.startdata)
{
this.astarcore.markGrid(this.startdata[start].x,this.startdata[start].y,'place');
}
}
this.hideAllStart=function()//this will clear the grid for each start location
{
for (start in this.startdata)
{
this.astarcore.clearTile(this.startdata[start].x,this.startdata[start].y);
}
}
}
//END OF START DATA///////////////////////
//PLACE DATA////////////////////////////////
function placeData(core)
{
//this.astarcore=core;
this.data;
this.setPlaceData=function(data)
{
if(!data)//checks to see if data is passed in then sets startdata
{
if(typeof this.mapdata["place"] !== "undefined")//checks to see if palce map data exists then sets startdata
{
this.placedata=this.mapdata["place"];
console.log("place data set from store");
}
else
{
console.error("no data for place");
}
}
else
{
this.placedata=data;
console.log("use data for placedata");
}
}
this.setPlaceAs=function (id, setas)//find a place by a unquie id code
{
for (x in this.placedata)//loops through all place data
{
var type = this.placedata[x];//extracts type
for (locationtype in type)//loops through all of the places in that place type
{
if(type[locationtype]["id"] == id)//matches the proved id
{
if(setas)
{
switch(setas)//check to see if start ot target
{
case "start":
this.astarcore.setStart(type[locationtype].y,type[locationtype].x);
break;
case "target":
this.astarcore.setTarget(type[locationtype].y,type[locationtype].x);
break;
case "find":
this.astarcore.markGrid(type[locationtype].y,type[locationtype].x,'place');
return type[locationtype];
break;
default:
console.error("setPlaceAs setas error: not vaild setas");
}//swtich
}//if set as
return type[locationtype];
}//if key = id
}//for locationtype in type
}//for (x in this.placedata
}//end findby id
this.findClosest=function (type)//find the closest location of a type
{
var results=new Array();
if(typeof this.mapdata["place"] === "undefined"){console.error("findMulti Failed: Not vaild place type"); return false;}//checks if vlaid palce type
else{
for (var i = 0; i < this.placedata[type].length; i++)//complie array of palce typew
{
x = this.placedata[type][i].x
y = this.placedata[type][i].y
var result=[y,x];
results.push(result);
}//end loop
var shorestindex = this.astarcore.findClosest(results);//runs if start valiadation, an runs astar to all place then return the index of the loaction that is closet to start
return this.placedata[type][shorestindex];
}//end else
}//end findClosest
this.markAll=function (type,flagtype)//puts a flag in cells for all loactions of a type
{
if(typeof this.mapdata["place"] === "undefined"){console.error("markAll Failed: Not vaild place type"); return false;}//end if
else{
for (var i = 0; i < this.placedata[type].length; i++)
{
x = this.placedata[type][i].x
y = this.placedata[type][i].y
this.astarcore.markGrid(x,y,flagtype);
}//end loop
}//end else
}//end mark all
this.clearAll=function (type)//puts a flag in cells for all loactions of a type
{
if(!this.placedata[type]){console.error("clearAll Failed: Not vaild place type"); return;}//end if
else{
for (var i = 0; i < this.placedata[type].length; i++)
{
x = this.placedata[type][i].x
y = this.placedata[type][i].y
this.astarcore.clearTile(x,y);
}//end loop
}//end else
}//end mark all
}
//END PLACE DATA///////////////////////
//PERSON DATA////////////////////////////////
function personData()
{
//this.astarcore=core;
this.setPersonData=function(data)
{
if(!data)//checks to see if data is passed in then sets persondata
{
if(typeof this.mapdata["person"] !== "undefined") //checks to see if person map data exists then sets startdata
{
this.persondata=this.mapdata["person"];
console.log("person data set from store");
}
else
{
console.error("no data for persondata");
}
}
else
{
this.persondata=data; //checks to see if person map data has been pased in then sets persondata
console.log("use data for persondata");
}
}
this.createNameArray=function()
{
this.namearray=new Array();
for (property in this.persondata)//creates an plain array of the person data indexed vaule to ne used in third party aout complete search functions can use arry index to find data in objedt
{
this.namearray.push(property);
}//end loop
return this.namearray;
}//createNameArray
this.setPersonAs=function (name,action)
{
name = name.replace("+"," ");//cleans inputs of url encoded charcters
name = name.replace("-"," ");//cleans inputs of url encoded charcters
name = name.toLowerCase();//strips caps to make comparsions easyer
if(!this.persondata[name]){console.error("locatePerson Failed: No Matching result found"); return false;}//if passed in name does not exits throws error treturns false
else{
this.curResult = this.persondata[name];//saves result as a object property for easy acesss
switch(action)
{
case "start":
this.astarcore.setStart(this.persondata[name].y,this.persondata[name].x);
break;
case "target":
this.astarcore.setTarget(this.persondata[name].y,this.persondata[name].x);
break;
case "find":
this.astarcore.markGrid(this.persondata[name].y,this.persondata[name].x,'person');
break;
default:
console.error("setPersonAs Failed: Not vaild type found"); return;
}
}//end else
}//end setPersonAs
this.createGroup=function(attribute,term)//this will create and return an object made of of pepole whos proived attribute matchs a provied term
{
var resultgroup = function() {};
for (person in this.persondata)
{
if(this.persondata[person][attribute] == term)
{
resultgroup[person] = this.persondata[person];
}
}//end loop
this.curResult = resultgroup;
return resultgroup
}
this.showAllPepole=function()//this mark the grid for each person
{
for (name in this.persondata)
{
this.astarcore.markGrid(this.persondata[name].x,this.persondata[name].y,'person');
}
}
this.hideAllPepole=function()//this will clear the grid for each person
{
for (name in this.persondata)
{
this.astarcore.clearTile(this.persondata[name].x,this.persondata[name].y);
}
}
}
//END PLACE DATA///////////////////////
//url DATA////////////////////////////////
function urlData()
{
this.pathgen = false;
this.urlread = false;
this.cleanurl = true;
this.getUrlVar= function(variable)
{
var urlsobj ={};
var full = window.location.href;
var qstring = full.split("?");
if(qstring[1])
{
var vars = qstring[1].split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
urlsobj[pair[0]]=pair[1];
if(pair[0] == variable){return pair[1];}
}
return(false);
this.urlread = true;
}//end if qssting
//this.urlsobj = urlsobj;
// return urlsobj;
}
this.runUrl=function(calllback)
{
console.log("<--------- start url read --------->");
var start = this.getUrlVar("start");
var starttype = this.getUrlVar("stype");
var target = this.getUrlVar("target");
var targettype = this.getUrlVar("ttype");
if(start)
{
console.log("- url start found:"+ start);
if(starttype)
{
this.cleanurl = false;
this.setStart(start,starttype);
console.log("- url start type found:"+ starttype);
}
else
{
console.error("url error: no start type found");
}
}
if(target)
{
console.log("- target found:"+ target);
if(targettype)
{
this.cleanurl = false;
console.log("- target type found:"+ targettype);
this.setTarget(target,targettype);
}
else
{
console.error("url error: not vaild target type");
}
}
if(this.astarcore.foundstart && this.astarcore.foundtarget)
{
this.astarcore.runAstar();
this.astarcore.genPath();
this.pathgen = true}
console.log("<--------- end url read --------->");
}//runUrl
}//END OF Url DATA///////////////////////