From 3446d56f4d9f030ec96b6db98a1be1fecb66be88 Mon Sep 17 00:00:00 2001 From: Nate Geslin Date: Tue, 4 Oct 2016 13:04:59 -0500 Subject: [PATCH] bugfix/ATC-4 - updates .ready() method to use a localStorage icao on load if one exists --- .../scripts/airport/AirportController.js | 30 ++++++++++++------- src/assets/scripts/airport/AirportModel.js | 15 +++++----- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/assets/scripts/airport/AirportController.js b/src/assets/scripts/airport/AirportController.js index 0ddf4db7..1351c63a 100644 --- a/src/assets/scripts/airport/AirportController.js +++ b/src/assets/scripts/airport/AirportController.js @@ -7,6 +7,13 @@ import { STORAGE_KEY } from '../constants/storageKeys'; // Temporary const declaration here to attach to the window AND use as internal property const airport = {}; +/** + * @property DEFAULT_AIRPORT_ICAO + * @type {string} + * @final + */ +const DEFAULT_AIRPORT_ICAO = 'ksfo'; + /** * @class AirportController */ @@ -22,6 +29,8 @@ export default class AirportController { } /** + * Lifecycle method. Should run only once on App initialiazation + * * @for AirportController * @method init_pre */ @@ -32,6 +41,8 @@ export default class AirportController { } /** + * Lifecycle method. Should run only once on App initialiazation + * * Load each airport in the `airportLoadList` * * @for AirportController @@ -44,16 +55,18 @@ export default class AirportController { } /** + * Lifecycle method. Should run only once on App initialiazation + * * @for AirportController * @method ready */ ready() { - let airportName = 'ksfo'; + let airportName = DEFAULT_AIRPORT_ICAO; - if (!_has(localStorage, STORAGE_KEY.ATC_LAST_AIRPORT) || - !_has(prop.airport.airports, STORAGE_KEY.ATC_LAST_AIRPORT) + if (_has(localStorage, STORAGE_KEY.ATC_LAST_AIRPORT) || + _has(prop.airport.airports, localStorage[STORAGE_KEY.ATC_LAST_AIRPORT].toLowerCase()) ) { - airportName = 'ksfo'; + airportName = localStorage[STORAGE_KEY.ATC_LAST_AIRPORT].toLowerCase(); } this.airport_set(airportName); @@ -64,16 +77,13 @@ export default class AirportController { * @method airport_set */ airport_set(icao) { - // TODO: simplify these ifs by combining them - if (!icao) { - if (_has(localStorage, STORAGE_KEY.ATC_LAST_AIRPORT)) { - icao = localStorage[STORAGE_KEY.ATC_LAST_AIRPORT]; - } + if (!icao && _has(localStorage, STORAGE_KEY.ATC_LAST_AIRPORT)) { + icao = localStorage[STORAGE_KEY.ATC_LAST_AIRPORT]; } icao = icao.toLowerCase(); - if (!icao in prop.airport.airports) { + if (!prop.airport.airports[icao]) { console.log(`${icao}: no such airport`); return; diff --git a/src/assets/scripts/airport/AirportModel.js b/src/assets/scripts/airport/AirportModel.js index 1e1c2c3a..384bfac1 100644 --- a/src/assets/scripts/airport/AirportModel.js +++ b/src/assets/scripts/airport/AirportModel.js @@ -15,6 +15,7 @@ import { angle_offset } from '../math/circle'; import { getOffset } from '../math/flightMath'; import { vlen, vsub, vadd, vscale, raysIntersect } from '../math/vector'; import { LOG } from '../constants/logLevel'; +import { SELECTORS } from '../constants/selectors'; import { STORAGE_KEY } from '../constants/storageKeys'; // TODO: This function should really live in a different file and have tests. @@ -369,17 +370,17 @@ export default class AirportModel { localStorage[STORAGE_KEY.ATC_LAST_AIRPORT] = this.icao; prop.airport.current = this; - $('#airport') + $(SELECTORS.DOM_SELECTORS.AIRPORT) .text(this.icao.toUpperCase()) .attr('title', this.name); prop.canvas.draw_labels = true; - $('.toggle-labels').toggle(!_isEmpty(this.maps)); - $('.toggle-restricted-areas').toggle((this.restricted_areas || []).length > 0); - $('.toggle-sids').toggle(!_isEmpty(this.sids)); + $(SELECTORS.DOM_SELECTORS.TOGGLE_LABELS).toggle(!_isEmpty(this.maps)); + $(SELECTORS.DOM_SELECTORS.TOGGLE_RESTRICTED_AREAS).toggle((this.restricted_areas || []).length > 0); + $(SELECTORS.DOM_SELECTORS.TOGGLE_SIDS).toggle(!_isEmpty(this.sids)); prop.canvas.dirty = true; - $('.toggle-terrain').toggle(!_isEmpty(this.terrain)); + $(SELECTORS.DOM_SELECTORS.TOGGLE_TERRAIN).toggle(!_isEmpty(this.terrain)); window.gameController.game_reset_score(); this.start = window.gameController.game_time(); @@ -446,8 +447,8 @@ export default class AirportModel { apt.terrain = {}; for (const i in data.features) { - const f = data.features[i], - ele = round(f.properties.elevation / 0.3048, 1000); // m => ft, rounded to 1K (but not divided) + const f = data.features[i]; + const ele = round(f.properties.elevation / 0.3048, 1000); // m => ft, rounded to 1K (but not divided) if (!apt.terrain[ele]) { apt.terrain[ele] = [];