The version of vichan running on lainchan.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

116 lignes
4.0KB

  1. /*
  2. * local-time.js
  3. * https://github.com/savetheinternet/Tinyboard/blob/master/js/local-time.js
  4. *
  5. * Released under the MIT license
  6. * Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
  7. * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
  8. *
  9. * Usage:
  10. * // $config['additional_javascript'][] = 'js/jquery.min.js';
  11. * // $config['additional_javascript'][] = 'js/strftime.min.js';
  12. * $config['additional_javascript'][] = 'js/local-time.js';
  13. *
  14. */
  15. $(document).ready(function(){
  16. 'use strict';
  17. var iso8601 = function(s) {
  18. s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
  19. s = s.replace(/-/,"/").replace(/-/,"/");
  20. s = s.replace(/T/," ").replace(/Z/," UTC");
  21. s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
  22. return new Date(s);
  23. };
  24. var zeropad = function(num, count) {
  25. return [Math.pow(10, count - num.toString().length), num].join('').substr(1);
  26. };
  27. var dateformat = (typeof strftime === 'undefined') ? function(t) {
  28. return zeropad(t.getMonth() + 1, 2) + "/" + zeropad(t.getDate(), 2) + "/" + t.getFullYear().toString().substring(2) +
  29. " (" + [_("Sun"), _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")][t.getDay()] + ") " +
  30. // time
  31. zeropad(t.getHours(), 2) + ":" + zeropad(t.getMinutes(), 2) + ":" + zeropad(t.getSeconds(), 2);
  32. } : function(t) {
  33. // post_date is defined in templates/main.js
  34. return strftime(window.post_date, t, datelocale);
  35. };
  36. function timeDifference(current, previous) {
  37. var msPerMinute = 60 * 1000;
  38. var msPerHour = msPerMinute * 60;
  39. var msPerDay = msPerHour * 24;
  40. var msPerMonth = msPerDay * 30;
  41. var msPerYear = msPerDay * 365;
  42. var elapsed = current - previous;
  43. if (elapsed < msPerMinute) {
  44. return 'Just now';
  45. } else if (elapsed < msPerHour) {
  46. return Math.round(elapsed/msPerMinute) + (Math.round(elapsed/msPerMinute)<=1 ? ' minute ago':' minutes ago');
  47. } else if (elapsed < msPerDay ) {
  48. return Math.round(elapsed/msPerHour ) + (Math.round(elapsed/msPerHour)<=1 ? ' hour ago':' hours ago');
  49. } else if (elapsed < msPerMonth) {
  50. return Math.round(elapsed/msPerDay) + (Math.round(elapsed/msPerDay)<=1 ? ' day ago':' days ago');
  51. } else if (elapsed < msPerYear) {
  52. return Math.round(elapsed/msPerMonth) + (Math.round(elapsed/msPerMonth)<=1 ? ' month ago':' months ago');
  53. } else {
  54. return Math.round(elapsed/msPerYear ) + (Math.round(elapsed/msPerYear)<=1 ? ' year ago':' years ago');
  55. }
  56. }
  57. var do_localtime = function(elem) {
  58. var times = elem.getElementsByTagName('time');
  59. var currentTime = Date.now();
  60. for(var i = 0; i < times.length; i++) {
  61. var t = times[i].getAttribute('datetime');
  62. var postTime = new Date(t);
  63. times[i].setAttribute('data-local', 'true');
  64. if (!localStorage.show_relative_time || localStorage.show_relative_time === 'false') {
  65. times[i].innerHTML = dateformat(iso8601(t));
  66. times[i].setAttribute('title', timeDifference(currentTime, postTime.getTime()));
  67. } else {
  68. times[i].innerHTML = timeDifference(currentTime, postTime.getTime());
  69. times[i].setAttribute('title', dateformat(iso8601(t)));
  70. }
  71. }
  72. };
  73. if (window.Options && Options.get_tab('general') && window.jQuery) {
  74. var interval_id;
  75. Options.extend_tab('general', '<label id="show-relative-time"><input type="checkbox">' + _('Show relative time') + '</label>');
  76. $('#show-relative-time>input').on('change', function() {
  77. if (localStorage.show_relative_time === 'true') {
  78. localStorage.show_relative_time = 'false';
  79. clearInterval(interval_id);
  80. } else {
  81. localStorage.show_relative_time = 'true';
  82. interval_id = setInterval(do_localtime, 30000, document);
  83. }
  84. // no need to refresh page
  85. do_localtime(document);
  86. });
  87. if (localStorage.show_relative_time === 'true') {
  88. $('#show-relative-time>input').attr('checked','checked');
  89. interval_id = setInterval(do_localtime, 30000, document);
  90. }
  91. // allow to work with auto-reload.js, etc.
  92. $(document).on('new_post', function(e, post) {
  93. do_localtime(post);
  94. });
  95. }
  96. do_localtime(document);
  97. });