You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.9 KiB

9 years ago
  1. // Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
  2. (function($, moment) {
  3. var updateInterval = 1e3,
  4. paused = false,
  5. $livestamps = $([]),
  6. init = function() {
  7. livestampGlobal.resume();
  8. },
  9. prep = function($el, timestamp) {
  10. var oldData = $el.data('livestampdata');
  11. if (typeof timestamp == 'number')
  12. timestamp *= 1e3;
  13. $el.removeAttr('data-livestamp')
  14. .removeData('livestamp');
  15. timestamp = moment(timestamp);
  16. if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
  17. var newData = $.extend({ }, { 'original': $el.contents() }, oldData);
  18. newData.moment = moment(timestamp);
  19. $el.data('livestampdata', newData).empty();
  20. $livestamps.push($el[0]);
  21. }
  22. },
  23. run = function() {
  24. if (paused) return;
  25. livestampGlobal.update();
  26. setTimeout(run, updateInterval);
  27. },
  28. livestampGlobal = {
  29. update: function() {
  30. $('[data-livestamp]').each(function() {
  31. var $this = $(this);
  32. prep($this, $this.data('livestamp'));
  33. });
  34. var toRemove = [];
  35. $livestamps.each(function() {
  36. var $this = $(this),
  37. data = $this.data('livestampdata');
  38. if (data === undefined)
  39. toRemove.push(this);
  40. else if (moment.isMoment(data.moment)) {
  41. var from = $this.html(),
  42. to = data.moment.fromNow();
  43. if (from != to) {
  44. var e = $.Event('change.livestamp');
  45. $this.trigger(e, [from, to]);
  46. if (!e.isDefaultPrevented())
  47. $this.html(to);
  48. }
  49. }
  50. });
  51. $livestamps = $livestamps.not(toRemove);
  52. },
  53. pause: function() {
  54. paused = true;
  55. },
  56. resume: function() {
  57. paused = false;
  58. run();
  59. },
  60. interval: function(interval) {
  61. if (interval === undefined)
  62. return updateInterval;
  63. updateInterval = interval;
  64. }
  65. },
  66. livestampLocal = {
  67. add: function($el, timestamp) {
  68. if (typeof timestamp == 'number')
  69. timestamp *= 1e3;
  70. timestamp = moment(timestamp);
  71. if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
  72. $el.each(function() {
  73. prep($(this), timestamp);
  74. });
  75. livestampGlobal.update();
  76. }
  77. return $el;
  78. },
  79. destroy: function($el) {
  80. $livestamps = $livestamps.not($el);
  81. $el.each(function() {
  82. var $this = $(this),
  83. data = $this.data('livestampdata');
  84. if (data === undefined)
  85. return $el;
  86. $this
  87. .html(data.original ? data.original : '')
  88. .removeData('livestampdata');
  89. });
  90. return $el;
  91. },
  92. isLivestamp: function($el) {
  93. return $el.data('livestampdata') !== undefined;
  94. }
  95. };
  96. $.livestamp = livestampGlobal;
  97. $(init);
  98. $.fn.livestamp = function(method, options) {
  99. if (!livestampLocal[method]) {
  100. options = method;
  101. method = 'add';
  102. }
  103. return livestampLocal[method](this, options);
  104. };
  105. })(jQuery, moment);