Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
fix(clock): show locale-formatted time (#651)
Browse files Browse the repository at this point in the history
* fix(clock): show locale-formatted time

This makes clock directive use time formatting from the currently loaded
locale. Also refactored it to be a bit cleaner.

Also fallback to locale date for HEADER.DATE/DATETIME. But since the
example config has formatting explicitly defined, this will probably
have no effect on existing configurations.

* Use querySelector
  • Loading branch information
rchl committed Feb 23, 2021
1 parent 10aebe6 commit 86c281e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 50 deletions.
57 changes: 19 additions & 38 deletions scripts/directives/clock.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,33 @@
import { leadZero } from '../globals/utils';

/**
* @ngInject
*
* @type {angular.IDirectiveFactory}
* @param {angular.IFilterService} $filter
*/
export default function ($interval) {
export default function ($filter) {
return {
restrict: 'AE',
replace: true,
link: function ($scope, $el, attrs) {
const $m = document.createElement('div');
const $h = document.createElement('div');
const $colon = document.createElement('div');
const $postfix = document.createElement('div');

$m.classList.add('clock--m');
$h.classList.add('clock--h');

$postfix.classList.add('clock--postfix');

$colon.classList.add('clock--colon');
$colon.textContent = ':';
restrict: 'E',
template: `
<div class="clock--h">{{ hour }}</div
><div class="clock--colon">:</div
><div class="clock--m">{{ minute }}</div
><div class="clock--postfix">{{ postfix }}</div>
`,
link ($scope, $el, attrs) {
const hourEl = $el[0].querySelector('.clock--h');
const minuteEl = $el[0].querySelector('.clock--m');
const postfixEl = $el[0].querySelector('.clock--postfix');

const updateTime = function () {
const d = new Date();
let h = d.getHours();
const m = d.getMinutes();
let postfix = '';
const localeTime = $filter('date')(Date.now(), 'shortTime');
const [hour, remainder] = localeTime.split(':');
const [minute, postfix] = remainder.split(' ');

if (window.CONFIG.timeFormat === 12) {
postfix = h >= 12 ? 'PM' : 'AM';

h = h % 12 || 12;
} else {
h = leadZero(h);
}

$h.textContent = h;
$m.textContent = leadZero(m);
$postfix.textContent = postfix;
hourEl.textContent = hour;
minuteEl.textContent = minute;
postfixEl.textContent = postfix;
};

$el[0].appendChild($h);
$el[0].appendChild($colon);
$el[0].appendChild($m);
$el[0].appendChild($postfix);

updateTime();

const interval = setInterval(updateTime, 1000);
Expand Down
21 changes: 14 additions & 7 deletions scripts/directives/date.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/**
* @ngInject
*
* @type {angular.IDirectiveFactory}
* @typedef {{
* format: string
* date: Date
* }} Scope
*
* @type {angular.IDirectiveFactory<Scope & angular.IScope>}
* @param {angular.IIntervalService} $interval
* @param {angular.ILocaleService} $locale
*/
export default function ($interval) {
export default function ($interval, $locale) {
return {
restrict: 'AE',
restrict: 'E',
replace: true,
scope: {
format: '=',
format: '<',
},
template: '<div class="date" ng-bind="date|date:format"></div>',
link: function ($scope, $el, attrs) {
$scope.format = $scope.format || 'EEEE, LLLL dd';
template: '<div class="date">{{ date | date:format}}</div>',
link ($scope, $el, attrs) {
$scope.format = $scope.format || $locale.DATETIME_FORMATS.longDate;
$scope.date = new Date();

$interval(function () {
Expand Down
2 changes: 1 addition & 1 deletion scripts/directives/headerItem.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<clock></clock>
</div>
<div ng-if="item.type === HEADER_ITEMS.DATE">
<date format="item.dateFormat || item.format"></date>
<date format="item.dateFormat"></date>
</div>
<div ng-if="item.type === HEADER_ITEMS.DATETIME">
<clock></clock>
Expand Down
4 changes: 0 additions & 4 deletions styles/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,6 @@ body {
vertical-align: middle;
}

&--hour, &--minute {

}

&--colon {
animation: blink 2s step-start 0s infinite;
font-size: 0.93em;
Expand Down

0 comments on commit 86c281e

Please sign in to comment.