Commit a07d0f77 authored by Robert Knight's avatar Robert Knight

Remove Moment.js

Moment.js is only used in a single place, for converting ISO8601 date-time strings
from the annotation's 'updated' field to user-friendly
date strings (eg. 'Wednesday 17 Dec, 18:59') for the tooltips
for the last-updated link in the annotation card.

Replacing this with functions from the built-in Date object saves
167KB from the generated bundle size (see
moment/min/moment-with-locales.min.js) and also speeds up the digest
cycle. For 20 annotation cards in the /stream view, generating the
date string used to take 6-7ms with Moment. It takes <2ms with
the built in functions in a current build of Chrome.

This still shows up as the most expensive watch expression, so
we might want to add caching as well.
parent 86256af9
...@@ -126,7 +126,6 @@ module.exports = angular.module('h', [ ...@@ -126,7 +126,6 @@ module.exports = angular.module('h', [
.directive('topBar', require('./directive/top-bar')) .directive('topBar', require('./directive/top-bar'))
.filter('converter', require('./filter/converter')) .filter('converter', require('./filter/converter'))
.filter('moment', require('./filter/moment'))
.filter('persona', require('./filter/persona').filter) .filter('persona', require('./filter/persona').filter)
.filter('urlencode', require('./filter/urlencode')) .filter('urlencode', require('./filter/urlencode'))
.filter('documentTitle', require('./filter/document-title')) .filter('documentTitle', require('./filter/document-title'))
......
...@@ -739,6 +739,14 @@ function AnnotationController( ...@@ -739,6 +739,14 @@ function AnnotationController(
return domainModel.updated; return domainModel.updated;
}; };
vm.updatedString = function () {
if (!domainModel.updated) {
return '';
}
var date = new Date(domainModel.updated);
return date.toDateString() + ', ' + date.toLocaleTimeString();
}
vm.user = function() { vm.user = function() {
return domainModel.user; return domainModel.user;
}; };
......
...@@ -404,7 +404,8 @@ describe('annotation.js', function() { ...@@ -404,7 +404,8 @@ describe('annotation.js', function() {
}, },
target: [{}], target: [{}],
uri: 'http://example.com', uri: 'http://example.com',
user: 'acct:bill@localhost' user: 'acct:bill@localhost',
updated: '2015-05-10T20:18:56.613388+00:00',
}; };
} }
...@@ -1104,6 +1105,18 @@ describe('annotation.js', function() { ...@@ -1104,6 +1105,18 @@ describe('annotation.js', function() {
}); });
}); });
describe('#updatedString()', function () {
it('returns the current time', function () {
var annotation = defaultAnnotation();
var controller = createDirective(annotation).controller;
var expectedDate = new Date(annotation.updated);
// the exact format of the result will depend on the current locale,
// but check that at least the current year and time are present
assert.match(controller.updatedString(), new RegExp('.*2015.*' +
expectedDate.toLocaleTimeString()));
});
});
describe('share', function() { describe('share', function() {
it('sets and unsets the open class on the share wrapper', function() { it('sets and unsets the open class on the share wrapper', function() {
var parts = createDirective(); var parts = createDirective();
......
var moment = require('moment');
module.exports = ['$window', function ($window) {
return function (value, format) {
return moment(value).format(format);
};
}];
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
<!-- Timestamp --> <!-- Timestamp -->
<a class="annotation-timestamp" <a class="annotation-timestamp"
target="_blank" target="_blank"
title="{{vm.updated() | moment:'LLLL'}}" title="{{vm.updatedString()}}"
ng-if="!vm.editing() && vm.updated()" ng-if="!vm.editing() && vm.updated()"
ng-href="{{vm.baseURI}}a/{{vm.id()}}" ng-href="{{vm.baseURI}}a/{{vm.id()}}"
>{{vm.timestamp}}</a> >{{vm.timestamp}}</a>
......
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
"is-equal-shallow": "^0.1.3", "is-equal-shallow": "^0.1.3",
"jquery": "1.11.1", "jquery": "1.11.1",
"js-polyfills": "^0.1.11", "js-polyfills": "^0.1.11",
"moment": "^2.10.6",
"ng-tags-input": "2.2.0", "ng-tags-input": "2.2.0",
"node-uuid": "^1.4.3", "node-uuid": "^1.4.3",
"postcss": "^5.0.6", "postcss": "^5.0.6",
...@@ -95,8 +94,7 @@ ...@@ -95,8 +94,7 @@
"annotator": "./h/static/scripts/vendor/annotator.js", "annotator": "./h/static/scripts/vendor/annotator.js",
"angular": "./node_modules/angular/angular.js", "angular": "./node_modules/angular/angular.js",
"hammerjs": "./node_modules/hammerjs/hammer.js", "hammerjs": "./node_modules/hammerjs/hammer.js",
"jquery": "./node_modules/jquery/dist/jquery.js", "jquery": "./node_modules/jquery/dist/jquery.js"
"moment": "./node_modules/moment/min/moment-with-locales.js"
}, },
"browserify-shim": { "browserify-shim": {
"annotator": { "annotator": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment