Skip to content

Commit 31bd4c9

Browse files
authored
Merge pull request #20 from vartiainen/frontend_work_0001
Sorting asks by shortest distance
2 parents 17b2e83 + 23d7ea0 commit 31bd4c9

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

  • resources/assets/scripts

resources/assets/scripts/app.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,27 @@ Jelpi.asks = class {
150150
}
151151
init() {
152152
this.filters.init();
153+
}
154+
update() {
155+
this.parent.locate( this.render.bind(this) );
156+
}
157+
render(locationResult) {
153158
let randomType = () => {
154159
let needs = [ 'Food', 'Medicine', 'Supplies' ];
155160
return needs[ Math.floor( Math.random() * needs.length ) ];
156-
}
161+
},
162+
randomLocationDelta = () => {
163+
let random = Math.pow( Math.random(), 10 );
164+
return random * ( Math.random() < .5 ? -1 : 1 );
165+
};
157166
for(let i = 0; i < 100; i++){
158167
this.data.push({
159168
type: randomType(),
160169
timeStamp: Date.now() - Math.random() * 1000 * 60 * 60 * 48 - 1000 * 60 * 60 * 24 * 10 * 0,
161-
latitude: 61.92410999999999 + ( ( Math.random() - .5 ) * 2 ),
162-
longitude: 25.748151 + ( ( Math.random() - .5 ) * 2 ),
170+
latitude: locationResult.coords.latitude + randomLocationDelta(),
171+
longitude: locationResult.coords.longitude + randomLocationDelta(),
163172
});
164173
}
165-
}
166-
update() {
167-
this.parent.locate( this.render.bind(this) );
168-
}
169-
render(locationResult) {
170174
this.array.map(ask => { ask.destroy() });
171175
this.array = [];
172176
this.data.map( this.initAsk.bind(this, locationResult) );
@@ -178,7 +182,15 @@ Jelpi.asks = class {
178182
ask.match() && this.array.push( ask );
179183
}
180184
sort() {
181-
// l('SORT');
185+
let calculateDistance = (coordinates) => {
186+
return Jelpi.helper.crowFlightBetweenCoordinates( this.parent.location.coords, coordinates );
187+
},
188+
compareDistance = (a, b) => {
189+
let aDistance = calculateDistance( a.properties ),
190+
bDistance = calculateDistance( b.properties );
191+
return aDistance - bDistance;
192+
};
193+
this.array.sort( compareDistance );
182194
}
183195
}
184196
Jelpi.asksFilters = class {

0 commit comments

Comments
 (0)