Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
timestamp: Extract and improve timerender.get_timestamp_for_flatpickr.
Rohitt Vashishthac87446a8de91
diff --git a/frontend_tests/node_tests/timerender.js b/frontend_tests/node_tests/timerender.jsindex 0f33ad07a65..b1d80fba07c 100644--- a/frontend_tests/node_tests/timerender.js+++ b/frontend_tests/node_tests/timerender.js@@ -2,6 +2,7 @@ set_global('$', global.make_zjquery());set_global('page_params', {twenty_four_hour_time: true,});+set_global('moment', require('moment-timezone'));set_global('XDate', zrequire('XDate', 'xdate'));zrequire('timerender');@@ -130,6 +131,16 @@ run_test('get_full_time', () => {assert.equal(expected, actual);});+run_test('get_timestamp_for_flatpickr', () => {+ const unix_timestamp = 1495091573000; // 5/18/2017 7:12:53 AM (UTC+0)+ const iso_timestamp = '2017-05-18T07:12:53Z'; // ISO 8601 date format+ const func = timerender.get_timestamp_for_flatpickr;+ // Invalid timestamps should show current time.+ assert.equal(func("random str").valueOf(), moment().valueOf());+ // Valid ISO timestamps should return Date objects.+ assert.equal(func(iso_timestamp).valueOf(), moment(unix_timestamp).valueOf());+});+run_test('absolute_time_12_hour', () => {set_global('page_params', {twenty_four_hour_time: false,diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.jsindex d49eadb23e5..4057db6e12e 100644--- a/static/js/composebox_typeahead.js+++ b/static/js/composebox_typeahead.js@@ -774,7 +774,7 @@ const show_flatpickr = (element, callback, default_timestamp) => {mode: 'single',enableTime: true,clickOpens: false,- defaultDate: default_timestamp || moment().format(),+ defaultDate: default_timestamp,plugins: [new confirmDatePlugin({})], // eslint-disable-line new-cap, no-undefpositionElement: element,dateFormat: 'Z',@@ -885,18 +885,10 @@ exports.content_typeahead_selected = function (item, event) {beginning = beginning.substring(0, start) + item + '** ';} else if (this.completing === 'time_jump') {let timestring = beginning.substring(beginning.lastIndexOf('<time:'));- let default_timestamp;if (timestring.startsWith('<time:') && timestring.endsWith('>')) {timestring = timestring.substring(6, timestring.length - 1);- moment.suppressDeprecationWarnings = true;- try {- // If there's already a time in the compose box here,- // we use it to initialize the flatpickr instance.- default_timestamp = moment(timestring).toDate();- } catch {- // Otherwise, default to showing the current time.- }}+ const timestamp = timerender.get_timestamp_for_flatpickr(timestring);const on_timestamp_selection = (val) => {const datestr = val;@@ -908,7 +900,7 @@ exports.content_typeahead_selected = function (item, event) {textbox.caret(beginning.length, beginning.length);compose_ui.autosize_textarea();};- show_flatpickr(this.$element[0], on_timestamp_selection, default_timestamp);+ show_flatpickr(this.$element[0], on_timestamp_selection, timestamp);return beginning + rest;}diff --git a/static/js/timerender.js b/static/js/timerender.jsindex b4c3149101a..e3e2574333f 100644--- a/static/js/timerender.js+++ b/static/js/timerender.js@@ -226,6 +226,23 @@ exports.get_full_time = function (timestamp) {return new XDate(timestamp * 1000).toISOString();};+exports.get_timestamp_for_flatpickr = (timestring) => {+ let timestamp;+ moment.suppressDeprecationWarnings = true;+ try {+ // If there's already a valid time in the compose box,+ // we use it to initialize the flatpickr instance.+ timestamp = moment(timestring);+ } finally {+ // Otherwise, default to showing the current time.+ if (!timestamp || !timestamp.isValid()) {+ timestamp = moment();+ }+ }+ moment.suppressDeprecationWarnings = false;+ return timestamp.toDate();+};+exports.stringify_time = function (time) {if (page_params.twenty_four_hour_time) {return time.toString('HH:mm');