about summary refs log tree commit diff
path: root/app/javascript/packs
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2017-05-07 22:22:54 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-07 15:22:54 +0200
commitf000673599e484d8fa20a2e39048ad816ef92afe (patch)
tree21bc35aa0aceccb23779b31c01d33a1650e3f396 /app/javascript/packs
parent5b6c2a1e726fbed80ec63265aba861b17c99d40c (diff)
Remove local_time gem, and add similar (#2875)
Diffstat (limited to 'app/javascript/packs')
-rw-r--r--app/javascript/packs/public.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/app/javascript/packs/public.js b/app/javascript/packs/public.js
index 24e7d0189..9a3f6c90b 100644
--- a/app/javascript/packs/public.js
+++ b/app/javascript/packs/public.js
@@ -1,16 +1,70 @@
 import emojify from 'mastodon/emoji';
 import { length } from 'stringz';
+import { default as dateFormat } from 'date-fns/format';
+import distanceInWordsStrict from 'date-fns/distance_in_words_strict';
 
 window.jQuery = window.$ = require('jquery');
 require('jquery-ujs');
 require.context('../images/', true);
 
+const parseFormat = (format) => format.replace(/%(\w)/g, (_, modifier) => {
+  switch (modifier) {
+  case '%':
+    return '%';
+  case 'a':
+    return 'ddd';
+  case 'A':
+    return 'ddd';
+  case 'b':
+    return 'MMM';
+  case 'B':
+    return 'MMMM';
+  case 'd':
+    return 'DD';
+  case 'H':
+    return 'HH';
+  case 'I':
+    return 'hh';
+  case 'l':
+    return 'H';
+  case 'm':
+    return 'M';
+  case 'M':
+    return 'mm';
+  case 'p':
+    return 'A';
+  case 'S':
+    return 'ss';
+  case 'w':
+    return 'd';
+  case 'y':
+    return 'YY';
+  case 'Y':
+    return 'YYYY';
+  default:
+    return `%${modifier}`;
+  }
+});
+
 $(() => {
   $.each($('.emojify'), (_, content) => {
     const $content = $(content);
     $content.html(emojify($content.html()));
   });
 
+  $('time[data-format]').each((_, content) => {
+    const $content = $(content);
+    const format = parseFormat($content.data('format'));
+    const formattedDate = dateFormat($content.attr('datetime'), format);
+    $content.text(formattedDate);
+  });
+
+  $('time.time-ago').each((_, content) => {
+    const $content = $(content);
+    const timeAgo = distanceInWordsStrict(new Date(), $content.attr('datetime'), { addSuffix: true });
+    $content.text(timeAgo);
+  });
+
   $('.video-player video').on('click', e => {
     if (e.target.paused) {
       e.target.play();