Repository: mattbradley/livestampjs Branch: develop Commit: 7572242af2fd Files: 6 Total size: 6.3 KB Directory structure: gitextract_iz7944fx/ ├── LICENSE ├── README.md ├── bower.json ├── component.json ├── livestamp.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ Copyright (c) 2015 Matt Bradley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ [Livestamp.js](http://mattbradley.github.com/livestampjs) ===================================================== A simple, unobtrusive jQuery plugin that provides auto-updating timeago text to your timestamped HTML elements using [Moment.js](http://momentjs.com). Usage ----- See the [livestamp documentation](http://mattbradley.github.com/livestampjs). Dependencies ------------ * [jQuery](http://jquery.com): you already know what this is * [Moment.js](http://momentjs.com): a great JavaScript library for parsing and displaying dates and times License ------- Copyright (c) 2015 Matt Bradley This software is freely distributable under the terms of the [MIT License](http://www.opensource.org/licenses/MIT). ================================================ FILE: bower.json ================================================ { "name": "livestamp", "version": "2.0.0", "keywords": ["livestamp", "timestamp", "timeago"], "ignore": [], "main": "livestamp.js", "dependencies": { "jquery": "*", "moment": "*" } } ================================================ FILE: component.json ================================================ { "name": "livestamp", "repository": "mattbradley/livestampjs", "description": "A simple, unobtrusive jQuery plugin that provides auto-updating timeago text to your timestamped HTML elements using Moment.js.", "version": "2.0.0", "keywords": ["livestamp", "timestamp", "timeago"], "dependencies": {}, "development": {}, "license": "MIT", "main": "livestamp.js", "scripts": ["livestamp.js", "livestamp.min.js"] } ================================================ FILE: livestamp.js ================================================ // Livestamp.js / v2.0.0 / (c) 2015 Matt Bradley / MIT License (function (plugin) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery', 'moment'], plugin); } else { // Browser globals plugin(jQuery, moment); } }(function($, moment) { var updateInterval = 1e3, paused = false, $livestamps = $([]), init = function() { livestampGlobal.resume(); }, prep = function($el, timestamp) { var oldData = $el.data('livestampdata'); if (typeof timestamp == 'number') timestamp *= 1e3; $el.removeAttr('data-livestamp') .removeData('livestamp'); timestamp = moment(timestamp); if (moment.isMoment(timestamp) && !isNaN(+timestamp)) { var newData = $.extend({ }, { 'original': $el.contents() }, oldData); newData.moment = moment(timestamp); $el.data('livestampdata', newData).empty(); $livestamps.push($el[0]); } }, run = function() { if (paused) return; livestampGlobal.update(); setTimeout(run, updateInterval); }, livestampGlobal = { update: function() { $('[data-livestamp]').each(function() { var $this = $(this); prep($this, $this.data('livestamp')); }); var toRemove = []; $livestamps.each(function() { var $this = $(this), data = $this.data('livestampdata'); if (data === undefined) toRemove.push(this); else if (moment.isMoment(data.moment)) { var from = $this.html(), to = data.moment.fromNow(); if (from != to) { var e = $.Event('change.livestamp'); $this.trigger(e, [from, to]); if (!e.isDefaultPrevented()) $this.html(to); } } }); $livestamps = $livestamps.not(toRemove); delete $livestamps.prevObject }, pause: function() { paused = true; }, resume: function() { paused = false; run(); }, interval: function(interval) { if (interval === undefined) return updateInterval; updateInterval = interval; } }, livestampLocal = { add: function($el, timestamp) { if (typeof timestamp == 'number') timestamp *= 1e3; timestamp = moment(timestamp); if (moment.isMoment(timestamp) && !isNaN(+timestamp)) { $el.each(function() { prep($(this), timestamp); }); livestampGlobal.update(); } return $el; }, destroy: function($el) { $livestamps = $livestamps.not($el); $el.each(function() { var $this = $(this), data = $this.data('livestampdata'); if (data === undefined) return $el; $this .html(data.original ? data.original : '') .removeData('livestampdata'); }); return $el; }, isLivestamp: function($el) { return $el.data('livestampdata') !== undefined; } }; $.livestamp = livestampGlobal; $(init); $.fn.livestamp = function(method, options) { if (!livestampLocal[method]) { options = method; method = 'add'; } return livestampLocal[method](this, options); }; })); ================================================ FILE: package.json ================================================ { "name": "livestamp", "version": "2.0.0", "main": "livestamp", "repository": { "type": "git", "url": "git://github.com/mattbradley/livestampjs.git" }, "description": "A simple, unobtrusive jQuery plugin that provides auto-updating timeago text to your timestamped HTML elements using Moment.js.", "keywords": ["livestamp", "timestamp", "timeago"], "homepage": "http://mattbradley.github.com/livestampjs", "bugs": "https://github.com/mattbradley/livestampjs/issues", "license": "MIT", "author": { "name": "Matt Bradley", "url": "https://twitter.com/mattbradley" }, "devDependencies": { "grunt": "~0.4.2", "grunt-gh-pages": "~0.9.0", "grunt-contrib-uglify": "~0.2.7", "grunt-contrib-copy": "~0.5.0", "grunt-bump": "0.0.13" } }