Skip to content

Commit 8cfe6e5

Browse files
committed
Use ks-util for most utility functions. Fix up a few things left over from the previous refactoring.
1 parent f2ce35a commit 8cfe6e5

9 files changed

Lines changed: 44 additions & 171 deletions

File tree

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"js-cookie": "^2.2.0",
2020
"ks-astronomy": "^1.0.1",
2121
"ks-date-time-zone": "^1.4.0",
22+
"ks-util": "^1.2.0",
2223
"rxjs": "^6.2.1"
2324
},
2425
"devDependencies": {

src/app.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface AppService {
66
updateSettings(newSettings: Settings);
77
forecastHasBeenUpdated(): void;
88
updateSunriseAndSunset(rise: string, set: string);
9+
updateMarqueeState(isScrolling: boolean);
910
}

src/clock.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
// Started by using https://codepen.io/dudleystorey/pen/HLBki, but this has grown and changed quite a bit from there.
2121

2222
import { getDayOfWeek, KsDateTime, KsTimeZone } from 'ks-date-time-zone';
23-
import { isRaspbian } from './util';
23+
import { isIE, isRaspbian, padLeft } from 'ks-util';
2424
import { AppService } from './app.service';
25+
import * as $ from 'jquery';
2526

2627
const SVC_NAMESPACE = 'http://www.w3.org/2000/svg';
2728

28-
function pad(n) {
29-
return (n < 10 ? '0' : '') + n;
30-
}
31-
3229
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
3330
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
3431

@@ -62,7 +59,7 @@ export class Clock {
6259
private _hideSeconds = false;
6360

6461
public timezone = KsTimeZone.OS_ZONE;
65-
public hasCompletingAnimation;
62+
public hasCompletingAnimation = false;
6663

6764
constructor(private appService: AppService) {
6865
this.secHand = document.getElementById('sec-hand');
@@ -83,6 +80,9 @@ export class Clock {
8380
this.hasBeginElement = !!this.sweep.beginElement;
8481

8582
this.decorateClockFace();
83+
84+
if (isIE())
85+
$('#clock-container').addClass('clock-container-ie-fix');
8686
}
8787

8888
public start(): void {
@@ -212,11 +212,11 @@ export class Clock {
212212
const animationTime = (doMechanicalSecondHandEffect ? 200 : 0);
213213
const now = this.appService.getCurrentTime() + animationTime;
214214
const date = new KsDateTime(now, this.timezone);
215-
const walltime = date.wallTime;
216-
const secs = walltime.sec;
215+
const wallTime = date.wallTime;
216+
const secs = wallTime.sec;
217217
const secRotation = 6 * secs;
218-
const mins = walltime.min;
219-
const hour = walltime.hrs;
218+
const mins = wallTime.min;
219+
const hour = wallTime.hrs;
220220

221221
if (doMechanicalSecondHandEffect)
222222
sweepSecondHand(this.lastSecRotation, secRotation);
@@ -225,15 +225,15 @@ export class Clock {
225225
this.lastSecRotation = secRotation;
226226
rotate(this.minHand, 6 * mins + 0.1 * secs);
227227
rotate(this.hourHand, 30 * (hour % 12) + mins / 2 + secs / 120);
228-
setTimeout(() => this.tick(), 1000 - walltime.millis);
228+
setTimeout(() => this.tick(), 1000 - wallTime.millis);
229229

230230
setTimeout(() => {
231-
const dayOfTheWeek = getDayOfWeek(walltime.n);
231+
const dayOfTheWeek = getDayOfWeek(wallTime.n);
232232

233233
this.dayOfWeekCaption.textContent = daysOfWeek[dayOfTheWeek].toUpperCase();
234-
this.dateCaption.textContent = pad(walltime.d);
235-
this.monthCaption.textContent = months[walltime.m - 1].toUpperCase();
236-
this.yearCaption.textContent = walltime.y.toString();
234+
this.dateCaption.textContent = padLeft(wallTime.d, 2, '0');
235+
this.monthCaption.textContent = months[wallTime.m - 1].toUpperCase();
236+
this.yearCaption.textContent = wallTime.y.toString();
237237
this.day2Caption.textContent = daysOfWeek[(dayOfTheWeek + 2) % 7];
238238
this.day3Caption.textContent = daysOfWeek[(dayOfTheWeek + 3) % 7];
239239
this.zoneCaption.textContent = this.timezone.zoneName + ' UTC' + KsTimeZone.formatUtcOffset(date.utcOffsetSeconds);
@@ -251,8 +251,8 @@ export class Clock {
251251
}
252252

253253
this.timeCaption.textContent =
254-
pad(displayHour) + ':' +
255-
pad(mins) + (this._hideSeconds ? '' : ':' + pad(secs)) + suffix;
254+
padLeft(displayHour, 2, '0') + ':' +
255+
padLeft(mins, 2, '0') + (this._hideSeconds ? '' : ':' + padLeft(secs, 2, '0')) + suffix;
256256

257257
if (mins !== this.lastMinute || this.lastTick + 60000 <= now) {
258258
this.appService.updateTime(hour, mins, this.lastMinute < 0);

src/ephemeris.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ import { getDateFromDayNumber_SGC, KsDateTime, KsTimeZone } from 'ks-date-time-z
2525
import * as $ from 'jquery';
2626
import { setSvgHref } from './util';
2727
import { AppService } from './app.service';
28+
import { padLeft } from 'ks-util';
2829

2930
const solarSystem = new SolarSystem();
3031
const eventFinder = new EventFinder();
3132
const planets = [SUN, MOON, MERCURY, VENUS, MARS, JUPITER, SATURN];
3233
const planetIds = ['sun', 'moon', 'mercury', 'venus', 'mars', 'jupiter', 'saturn'];
33-
const planetElems: JQuery[] = [];
34-
35-
function pad(n) {
36-
return (n < 10 ? '0' : '') + n;
37-
}
3834

3935
function getMoonPhaseIcon(phase: number) {
40-
return `assets/moon/phase-${pad(Math.round(phase / 360 * 28) % 28)}.svg`;
36+
return `assets/moon/phase-${padLeft(Math.round(phase / 360 * 28) % 28, 2, '0')}.svg`;
4137
}
4238

4339
function formatTime(date: KsDateTime, amPm: boolean) {
@@ -53,7 +49,7 @@ function formatTime(date: KsDateTime, amPm: boolean) {
5349
suffix = (date.wallTime.hrs < 12 ? 'a' : 'p');
5450
}
5551

56-
return pad(hours) + ':' + pad(date.wallTime.min) + suffix;
52+
return padLeft(hours, 2, '0') + ':' + padLeft(date.wallTime.min, 2, '0') + suffix;
5753
}
5854

5955
export class Ephemeris {
@@ -63,12 +59,13 @@ export class Ephemeris {
6359
private sunsets: JQuery[] = [];
6460
private moons: JQuery[] = [];
6561
private phaseTimes: JQuery[] = [];
62+
private planetElems: JQuery[] = [];
6663

6764
private _hidePlanets = false;
6865

6966
constructor(private appService: AppService) {
7067
planetIds.forEach((planet, index) => {
71-
planetElems[index] = $('#' + planetIds[index]);
68+
this.planetElems[index] = $('#' + planetIds[index]);
7269
});
7370

7471
this.planetTracks = $('#planet-tracks');
@@ -114,7 +111,7 @@ export class Ephemeris {
114111
planets.forEach((planet, index) => {
115112
const eclipticLongitude = solarSystem.getEclipticPosition(planet, time_JDE).longitude.degrees;
116113
const altitude = solarSystem.getHorizontalPosition(planet, time_JDU, observer).altitude.degrees;
117-
const elem = planetElems[index];
114+
const elem = this.planetElems[index];
118115
let targetAltitude = -REFRACTION_AT_HORIZON;
119116

120117
if (planet === SUN || planet === MOON)

src/forecast.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import * as $ from 'jquery';
2121
import { KsDateTime, KsTimeZone } from 'ks-date-time-zone';
22-
import { getTextWidth, isEdge, isIE, isRaspbian, setSvgHref } from './util';
22+
import { getTextWidth, isEdge, isIE, isRaspbian } from 'ks-util';
23+
import { setSvgHref } from './util';
2324
import { AppService } from './app.service';
2425

2526
interface CommonConditions {
@@ -143,7 +144,7 @@ export class Forecast {
143144
});
144145
}
145146

146-
public refreshForecastFromCache(): void {
147+
public refreshFromCache(): void {
147148
if (this.lastForecastData)
148149
this.displayForecast(this.lastForecastData);
149150
}
@@ -421,11 +422,11 @@ export class Forecast {
421422
const offsetWidth = element.offsetWidth;
422423

423424
if (textWidth + padding <= offsetWidth) {
424-
// setMarqueeIsAnimated(false);
425+
this.appService.updateMarqueeState(false);
425426
return;
426427
}
427428

428-
// setMarqueeIsAnimated(true);
429+
this.appService.updateMarqueeState(true);
429430

430431
if (this.animationStyleSheet.cssRules.length > 0)
431432
this.animationStyleSheet.deleteRule(0);

src/main.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Clock } from './clock';
2323
import { AppService } from './app.service';
2424
import { Forecast } from './forecast';
2525
import { KsDateTime, KsTimeZone } from 'ks-date-time-zone';
26-
import { isIE, setFullScreen } from './util';
26+
import { setFullScreen } from 'ks-util';
2727
import { Settings } from './settings';
2828
import { SettingsDialog } from './settings-dialog';
2929
import { Ephemeris } from './ephemeris';
@@ -62,7 +62,7 @@ export class AwClockApp implements AppService {
6262

6363
private lastCursorMove = 0;
6464
private lastForecast = 0;
65-
private lastTimezone = KsTimeZone.OS_ZONE;
65+
private lastTimezone: KsTimeZone;
6666
private lastHour = -1;
6767
private frequent = false;
6868

@@ -74,6 +74,7 @@ export class AwClockApp implements AppService {
7474
this.clock = new Clock(this);
7575
this.clock.amPm = this.settings.amPm;
7676
this.clock.hideSeconds = this.settings.hideSeconds;
77+
this.lastTimezone = this.clock.timezone;
7778

7879
this.forecast = new Forecast(this);
7980

@@ -88,9 +89,6 @@ export class AwClockApp implements AppService {
8889
this.cityLabel = $('#city');
8990
this.dimmer = $('#dimmer');
9091

91-
if (isIE())
92-
$('#clock-container').addClass('clock-container-ie-fix');
93-
9492
this.cityLabel.text(this.settings.city);
9593

9694
document.addEventListener('keypress', event => {
@@ -134,7 +132,7 @@ export class AwClockApp implements AppService {
134132
// If it's a new day, make sure we update the weather display to show the change of day,
135133
// even if we aren't polling for new weather data right now.
136134
if (hour < this.lastHour || hour === 0 && minute === 0)
137-
this.forecast.refreshForecastFromCache();
135+
this.forecast.refreshFromCache();
138136

139137
if (this.indoor.available)
140138
this.indoor.update(this.settings.celsius);
@@ -179,14 +177,18 @@ export class AwClockApp implements AppService {
179177
this.cityLabel.text(newSettings.city);
180178
this.clock.amPm = newSettings.amPm;
181179
this.clock.hideSeconds = newSettings.hideSeconds;
182-
// this.ephemeris.hidePlanets = newSettings.hidePlanets;
180+
this.ephemeris.hidePlanets = newSettings.hidePlanets;
183181
this.clock.triggerRefresh();
184182
}
185183

186184
public updateSunriseAndSunset(rise: string, set: string): void {
187185
this.updateDimming(this.getCurrentTime(), rise, set);
188186
}
189187

188+
public updateMarqueeState(isScrolling: boolean) {
189+
this.clock.hasCompletingAnimation = isScrolling;
190+
}
191+
190192
private updateDimming(now: number, todayRise: string, todaySet: string): void {
191193
if (this.settings.dimming) {
192194
let start = this.settings.dimmingStart;

src/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import * as Cookies from 'js-cookie';
21-
import { toBoolean } from './util';
21+
import { toBoolean } from 'ks-util';
2222

2323
export class Settings {
2424
latitude = 42.75;

0 commit comments

Comments
 (0)