From 884423f86cd58cb9ba83c3ebbaa0fd19ef799ff0 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Mon, 1 Jun 2015 16:23:32 -0400 Subject: [PATCH 1/4] Make usage of moment consistent throughout app This always includes the user's language preference when loading moment. It also deletes the moment global, so if you don't require it somehow you will get an error trying to call `moment`'. This is setup to provide moment to both angular and plain JavaScript modules. In angular modules, just add `moment` as a dependency. In plain js use `import moment from 'tower/shared/moment/moment';`. --- awx/ui/static/js/app.js | 2 ++ .../lists/jobs/jobs-list.directive.js | 3 ++- awx/ui/static/js/shared/long-date.filter.js | 23 +++++++++---------- awx/ui/static/js/shared/moment/main.js | 9 ++++++++ awx/ui/static/js/shared/moment/moment.js | 18 +++++++++++++++ .../static/js/shared/moment/moment.value.js | 18 +++++++++++++++ .../date-picker/date-picker.directive.js | 4 ++-- awx/ui/static/js/system-tracking/main.js | 3 +-- .../js/system-tracking/search-date-range.js | 2 ++ .../system-tracking/string-or-date.filter.js | 3 ++- .../system-tracking.controller.js | 3 ++- .../system-tracking/system-tracking.route.js | 1 + 12 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 awx/ui/static/js/shared/moment/main.js create mode 100644 awx/ui/static/js/shared/moment/moment.js create mode 100644 awx/ui/static/js/shared/moment/moment.value.js diff --git a/awx/ui/static/js/app.js b/awx/ui/static/js/app.js index 65650ac259..c2040eb3b6 100644 --- a/awx/ui/static/js/app.js +++ b/awx/ui/static/js/app.js @@ -37,6 +37,7 @@ import setupMenu from 'tower/setup-menu/main'; import mainMenu from 'tower/main-menu/main'; import browserData from 'tower/browser-data/main'; import dashboard from 'tower/dashboard/main'; +import moment from 'tower/shared/moment/main'; import {JobDetailController} from 'tower/controllers/JobDetail'; import {JobStdoutController} from 'tower/controllers/JobStdout'; @@ -82,6 +83,7 @@ var tower = angular.module('Tower', [ setupMenu.name, mainMenu.name, dashboard.name, + moment.name, 'AuthService', 'Utilities', 'LicenseHelper', diff --git a/awx/ui/static/js/dashboard/lists/jobs/jobs-list.directive.js b/awx/ui/static/js/dashboard/lists/jobs/jobs-list.directive.js index 0ebdc16c7a..31a433db1a 100644 --- a/awx/ui/static/js/dashboard/lists/jobs/jobs-list.directive.js +++ b/awx/ui/static/js/dashboard/lists/jobs/jobs-list.directive.js @@ -1,6 +1,7 @@ /* jshint unused: vars */ export default - [function JobsList() { + ['moment', + function JobsList(moment) { return { restrict: 'E', link: link, diff --git a/awx/ui/static/js/shared/long-date.filter.js b/awx/ui/static/js/shared/long-date.filter.js index 845ed86491..254c4cd09c 100644 --- a/awx/ui/static/js/shared/long-date.filter.js +++ b/awx/ui/static/js/shared/long-date.filter.js @@ -4,16 +4,15 @@ * All Rights Reserved *************************************************/ -angular.module('longDateFilter', []).filter('longDate', function() { - return function(input) { - - // navigator.language is available in all modern browsers. - // however navigator.languages is a new technology that - // lists the user's preferred languages, the first in the array - // being the user's top choice. navigator.languages is currently - // comptabile with chrome>v32, ffox>32, but not IE/Safari - var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage), - date = moment(input).locale(lang); +function longDateFilter(moment, input) { + var date = moment(input); return date.format('l LTS'); - }; -}); +} + +angular.module('longDateFilter', []) + .filter('longDate', + [ 'moment', + function(moment) { + return _.partial(longDateFilter, moment); + } + ]); diff --git a/awx/ui/static/js/shared/moment/main.js b/awx/ui/static/js/shared/moment/main.js new file mode 100644 index 0000000000..f1c70d88c0 --- /dev/null +++ b/awx/ui/static/js/shared/moment/main.js @@ -0,0 +1,9 @@ +import newMoment from './moment'; + +export default + angular.module('moment', ['angularMoment']) + .config(function() { + // Remove the global variable for moment + delete window.moment; + }) + .constant('moment', newMoment); diff --git a/awx/ui/static/js/shared/moment/moment.js b/awx/ui/static/js/shared/moment/moment.js new file mode 100644 index 0000000000..f8c9b3599b --- /dev/null +++ b/awx/ui/static/js/shared/moment/moment.js @@ -0,0 +1,18 @@ +var originalMoment = window.moment; + +export default function moment() { + + // navigator.language is available in all modern browsers. + // however navigator.languages is a new technology that + // lists the user's preferred languages, the first in the array + // being the user's top choice. navigator.languages is currently + // comptabile with chrome>v32, ffox>32, but not IE/Safari + var lang = navigator.languages ? + navigator.languages[0] : + (navigator.language || navigator.userLanguage); + + originalMoment.locale(lang); + return originalMoment.apply(this, arguments); +} + + diff --git a/awx/ui/static/js/shared/moment/moment.value.js b/awx/ui/static/js/shared/moment/moment.value.js new file mode 100644 index 0000000000..f8c9b3599b --- /dev/null +++ b/awx/ui/static/js/shared/moment/moment.value.js @@ -0,0 +1,18 @@ +var originalMoment = window.moment; + +export default function moment() { + + // navigator.language is available in all modern browsers. + // however navigator.languages is a new technology that + // lists the user's preferred languages, the first in the array + // being the user's top choice. navigator.languages is currently + // comptabile with chrome>v32, ffox>32, but not IE/Safari + var lang = navigator.languages ? + navigator.languages[0] : + (navigator.language || navigator.userLanguage); + + originalMoment.locale(lang); + return originalMoment.apply(this, arguments); +} + + diff --git a/awx/ui/static/js/system-tracking/date-picker/date-picker.directive.js b/awx/ui/static/js/system-tracking/date-picker/date-picker.directive.js index 550a7bebd4..f4b4529a4a 100644 --- a/awx/ui/static/js/system-tracking/date-picker/date-picker.directive.js +++ b/awx/ui/static/js/system-tracking/date-picker/date-picker.directive.js @@ -7,8 +7,8 @@ /* jshint unused: vars */ export default - [ '$rootScope', - function() { + [ 'moment', + function(moment) { return { restrict: 'E', scope: { diff --git a/awx/ui/static/js/system-tracking/main.js b/awx/ui/static/js/system-tracking/main.js index be08358455..3a42fd821e 100644 --- a/awx/ui/static/js/system-tracking/main.js +++ b/awx/ui/static/js/system-tracking/main.js @@ -16,8 +16,7 @@ import datePicker from './date-picker/main'; export default angular.module('systemTracking', - [ 'angularMoment', - utilities.name, + [ utilities.name, shared.name, datePicker.name ]) diff --git a/awx/ui/static/js/system-tracking/search-date-range.js b/awx/ui/static/js/system-tracking/search-date-range.js index c2b5141a2a..fa2aa72855 100644 --- a/awx/ui/static/js/system-tracking/search-date-range.js +++ b/awx/ui/static/js/system-tracking/search-date-range.js @@ -4,6 +4,8 @@ * All Rights Reserved *************************************************/ +import moment from 'tower/shared/moment/moment'; + export function searchDateRange(dateString) { var date; diff --git a/awx/ui/static/js/system-tracking/string-or-date.filter.js b/awx/ui/static/js/system-tracking/string-or-date.filter.js index 4c0cc65160..4e3f7d387e 100644 --- a/awx/ui/static/js/system-tracking/string-or-date.filter.js +++ b/awx/ui/static/js/system-tracking/string-or-date.filter.js @@ -6,7 +6,8 @@ export default [ 'amDateFormatFilter', - function(dateFormat) { + 'moment', + function(dateFormat, moment) { return function(string, format) { if (moment.isMoment(string)) { return dateFormat(string, format); diff --git a/awx/ui/static/js/system-tracking/system-tracking.controller.js b/awx/ui/static/js/system-tracking/system-tracking.controller.js index da1a249664..6e1c15950e 100644 --- a/awx/ui/static/js/system-tracking/system-tracking.controller.js +++ b/awx/ui/static/js/system-tracking/system-tracking.controller.js @@ -15,7 +15,7 @@ function controller($rootScope, initialFactData, getDataForComparison, waitIndicator, - + moment, _) { // var inventoryId = $routeParams.id; var hostIds = $routeParams.hosts.split(','); @@ -201,6 +201,7 @@ export default 'factScanData', 'getDataForComparison', 'Wait', + 'moment', 'lodashAsPromised', controller ]; diff --git a/awx/ui/static/js/system-tracking/system-tracking.route.js b/awx/ui/static/js/system-tracking/system-tracking.route.js index 26cc66472b..8c30e1b88f 100644 --- a/awx/ui/static/js/system-tracking/system-tracking.route.js +++ b/awx/ui/static/js/system-tracking/system-tracking.route.js @@ -5,6 +5,7 @@ *************************************************/ import {searchDateRange} from './search-date-range'; +import moment from 'tower/shared/moment/moment'; export default { name: 'systemTracking', From 6d6d8c66c2cf08c984380ea05f904e60a17fedcd Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Mon, 1 Jun 2015 16:30:11 -0400 Subject: [PATCH 2/4] Fix tests --- .../tests/unit/system-tracking/single-host-data.service-test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js b/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js index fd054c2b38..3a8ab37660 100644 --- a/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js +++ b/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js @@ -1,5 +1,6 @@ import systemTracking from 'tower/system-tracking/main'; import {describeModule} from '../describe-module'; +import moment from 'tower/shared/moment/moment'; describeModule(systemTracking.name) .testService('factScanDataService', function(test, restStub) { From bcca803323882d853be93353cfb22e6134ffb087 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Mon, 1 Jun 2015 16:45:13 -0400 Subject: [PATCH 3/4] Copy all properties of original moment to wrapped object --- awx/ui/static/js/shared/moment/moment.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/awx/ui/static/js/shared/moment/moment.js b/awx/ui/static/js/shared/moment/moment.js index f8c9b3599b..9d7ba7cc91 100644 --- a/awx/ui/static/js/shared/moment/moment.js +++ b/awx/ui/static/js/shared/moment/moment.js @@ -1,6 +1,6 @@ var originalMoment = window.moment; -export default function moment() { +function moment() { // navigator.language is available in all modern browsers. // however navigator.languages is a new technology that @@ -15,4 +15,6 @@ export default function moment() { return originalMoment.apply(this, arguments); } +_.merge(moment, originalMoment); +export default moment; From d8e1887036287c10859714cb5119527ad6bcc4e1 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Mon, 1 Jun 2015 17:04:14 -0400 Subject: [PATCH 4/4] Remove accidentally committed value --- awx/ui/static/js/shared/moment/moment.value.js | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 awx/ui/static/js/shared/moment/moment.value.js diff --git a/awx/ui/static/js/shared/moment/moment.value.js b/awx/ui/static/js/shared/moment/moment.value.js deleted file mode 100644 index f8c9b3599b..0000000000 --- a/awx/ui/static/js/shared/moment/moment.value.js +++ /dev/null @@ -1,18 +0,0 @@ -var originalMoment = window.moment; - -export default function moment() { - - // navigator.language is available in all modern browsers. - // however navigator.languages is a new technology that - // lists the user's preferred languages, the first in the array - // being the user's top choice. navigator.languages is currently - // comptabile with chrome>v32, ffox>32, but not IE/Safari - var lang = navigator.languages ? - navigator.languages[0] : - (navigator.language || navigator.userLanguage); - - originalMoment.locale(lang); - return originalMoment.apply(this, arguments); -} - -