Commit 129f2632 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'leipert-update-timeago-to-use-no-jquery' into 'master'

Update localTimeAgo to accept Arrays and NodeList

See merge request gitlab-org/gitlab!65851
parents 7037d42a a12a9ee4
...@@ -42,7 +42,7 @@ export default class Activities { ...@@ -42,7 +42,7 @@ export default class Activities {
} }
updateTooltips() { updateTooltips() {
localTimeAgo($('.js-timeago', '.content_list')); localTimeAgo(document.querySelectorAll('.content_list .js-timeago'));
} }
reloadActivities() { reloadActivities() {
......
...@@ -93,7 +93,7 @@ export default class CommitsList { ...@@ -93,7 +93,7 @@ export default class CommitsList {
.text(n__('%d commit', '%d commits', commitsCount)); .text(n__('%d commit', '%d commits', commitsCount));
} }
localTimeAgo($processedData.find('.js-timeago')); localTimeAgo($processedData.find('.js-timeago').get());
return processedData; return processedData;
} }
......
import $ from 'jquery';
import * as timeago from 'timeago.js'; import * as timeago from 'timeago.js';
import { languageCode, s__, createDateTimeFormat } from '../../../locale'; import { languageCode, s__, createDateTimeFormat } from '../../../locale';
import { formatDate } from './date_format_utility'; import { formatDate } from './date_format_utility';
...@@ -97,21 +96,21 @@ export const getTimeago = () => ...@@ -97,21 +96,21 @@ export const getTimeago = () =>
/** /**
* For the given elements, sets a tooltip with a formatted date. * For the given elements, sets a tooltip with a formatted date.
* @param {JQuery} $timeagoEls * @param {Array<Node>|NodeList} elements
* @param {Boolean} setTimeago * @param {Boolean} updateTooltip
*/ */
export const localTimeAgo = ($timeagoEls, setTimeago = true) => { export const localTimeAgo = (elements, updateTooltip = true) => {
const { format } = getTimeago(); const { format } = getTimeago();
$timeagoEls.each((i, el) => { elements.forEach((el) => {
$(el).text(format($(el).attr('datetime'), timeagoLanguageCode)); el.innerText = format(el.dateTime, timeagoLanguageCode);
}); });
if (!setTimeago) { if (!updateTooltip) {
return; return;
} }
function addTimeAgoTooltip() { function addTimeAgoTooltip() {
$timeagoEls.each((i, el) => { elements.forEach((el) => {
// Recreate with custom template // Recreate with custom template
el.setAttribute('title', formatDate(el.dateTime)); el.setAttribute('title', formatDate(el.dateTime));
}); });
......
...@@ -183,7 +183,7 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -183,7 +183,7 @@ document.addEventListener('DOMContentLoaded', () => {
return true; return true;
}); });
localTimeAgo($('abbr.timeago, .js-timeago'), true); localTimeAgo(document.querySelectorAll('abbr.timeago, .js-timeago'), true);
/** /**
* This disables form buttons while a form is submitting * This disables form buttons while a form is submitting
......
...@@ -333,8 +333,9 @@ export default class MergeRequestTabs { ...@@ -333,8 +333,9 @@ export default class MergeRequestTabs {
axios axios
.get(`${source}.json`) .get(`${source}.json`)
.then(({ data }) => { .then(({ data }) => {
document.querySelector('div#commits').innerHTML = data.html; const commitsDiv = document.querySelector('div#commits');
localTimeAgo($('.js-timeago', 'div#commits')); commitsDiv.innerHTML = data.html;
localTimeAgo(commitsDiv.querySelectorAll('.js-timeago'));
this.commitsLoaded = true; this.commitsLoaded = true;
this.scrollToContainerElement('#commits'); this.scrollToContainerElement('#commits');
...@@ -407,7 +408,7 @@ export default class MergeRequestTabs { ...@@ -407,7 +408,7 @@ export default class MergeRequestTabs {
initChangesDropdown(this.stickyTop); initChangesDropdown(this.stickyTop);
localTimeAgo($('.js-timeago', 'div#diffs')); localTimeAgo(document.querySelectorAll('#diffs .js-timeago'));
syntaxHighlight($('#diffs .js-syntax-highlight')); syntaxHighlight($('#diffs .js-syntax-highlight'));
if (this.isDiffAction(this.currentAction)) { if (this.isDiffAction(this.currentAction)) {
......
...@@ -358,7 +358,7 @@ export default class Notes { ...@@ -358,7 +358,7 @@ export default class Notes {
setupNewNote($note) { setupNewNote($note) {
// Update datetime format on the recent note // Update datetime format on the recent note
localTimeAgo($note.find('.js-timeago'), false); localTimeAgo($note.find('.js-timeago').get(), false);
this.collapseLongCommitList(); this.collapseLongCommitList();
this.taskList.init(); this.taskList.init();
...@@ -511,7 +511,7 @@ export default class Notes { ...@@ -511,7 +511,7 @@ export default class Notes {
Notes.animateAppendNote(noteEntity.html, discussionContainer); Notes.animateAppendNote(noteEntity.html, discussionContainer);
} }
localTimeAgo($('.js-timeago'), false); localTimeAgo(document.querySelectorAll('.js-timeago'), false);
Notes.checkMergeRequestStatus(); Notes.checkMergeRequestStatus();
return this.updateNotesCount(1); return this.updateNotesCount(1);
} }
......
...@@ -15,7 +15,7 @@ const updateCommitList = (url, $loadingIndicator, $commitList, params) => { ...@@ -15,7 +15,7 @@ const updateCommitList = (url, $loadingIndicator, $commitList, params) => {
.then(({ data }) => { .then(({ data }) => {
$loadingIndicator.hide(); $loadingIndicator.hide();
$commitList.html(data); $commitList.html(data);
localTimeAgo($('.js-timeago', $commitList)); localTimeAgo($commitList.get(0).querySelectorAll('.js-timeago'));
}); });
}; };
......
...@@ -166,7 +166,7 @@ export default class UserTabs { ...@@ -166,7 +166,7 @@ export default class UserTabs {
const tabSelector = `div#${action}`; const tabSelector = `div#${action}`;
this.$parentEl.find(tabSelector).html(data.html); this.$parentEl.find(tabSelector).html(data.html);
this.loaded[action] = true; this.loaded[action] = true;
localTimeAgo($('.js-timeago', tabSelector)); localTimeAgo(document.querySelectorAll(`${tabSelector} .js-timeago`));
this.toggleLoading(false); this.toggleLoading(false);
}) })
...@@ -209,7 +209,7 @@ export default class UserTabs { ...@@ -209,7 +209,7 @@ export default class UserTabs {
container, container,
url: $(`${container} .overview-content-list`).data('href'), url: $(`${container} .overview-content-list`).data('href'),
...options, ...options,
postRenderCallback: () => localTimeAgo($('.js-timeago', container)), postRenderCallback: () => localTimeAgo(document.querySelectorAll(`${container} .js-timeago`)),
}); });
} }
......
import $ from 'jquery';
import { getTimeago, localTimeAgo, timeFor } from '~/lib/utils/datetime/timeago_utility'; import { getTimeago, localTimeAgo, timeFor } from '~/lib/utils/datetime/timeago_utility';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import '~/commons/bootstrap'; import '~/commons/bootstrap';
...@@ -81,16 +80,16 @@ describe('TimeAgo utils', () => { ...@@ -81,16 +80,16 @@ describe('TimeAgo utils', () => {
`With User Setting timeDisplayRelative: $timeDisplayRelative`, `With User Setting timeDisplayRelative: $timeDisplayRelative`,
({ timeDisplayRelative, text }) => { ({ timeDisplayRelative, text }) => {
it.each` it.each`
timeagoArg | title updateTooltip | title
${false} | ${'some time'} ${false} | ${'some time'}
${true} | ${'Feb 18, 2020 10:22pm UTC'} ${true} | ${'Feb 18, 2020 10:22pm UTC'}
`( `(
`has content: '${text}' and tooltip: '$title' with timeagoArg = $timeagoArg`, `has content: '${text}' and tooltip: '$title' with updateTooltip = $updateTooltip`,
({ timeagoArg, title }) => { ({ updateTooltip, title }) => {
window.gon = { time_display_relative: timeDisplayRelative }; window.gon = { time_display_relative: timeDisplayRelative };
const element = document.querySelector('time'); const element = document.querySelector('time');
localTimeAgo($(element), timeagoArg); localTimeAgo([element], updateTooltip);
jest.runAllTimers(); jest.runAllTimers();
......
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