1(function (factory) { 2 if (typeof define === "function" && define.amd) { 3 // AMD. Register as anonymous module. 4 define(["jquery"], factory); 5 } else { 6 // Browser globals. 7 factory(jQuery); 8 } 9})(function ($) { 10 11 "use strict"; 12 13 var PrettyDate = function (element, options) { 14 options = $.isPlainObject(options) ? options : {}; 15 this.$element = $(element); 16 this.defaults = $.extend({}, PrettyDate.defaults, this.$element.data(), options); 17 this.init(); 18 }, 19 20 // Helper variables 21 floor = Math.floor, 22 second = 1000, 23 minute = 60 * second, 24 hour = 60 * minute, 25 day = 24 * hour, 26 week = 7 * day, 27 month = 31 * day, 28 year = 365 * day; 29 30 PrettyDate.prototype = { 31 constructor: PrettyDate, 32 33 init: function () { 34 var $this = this.$element, 35 defaults = this.defaults, 36 isInput = $this.is("input"), 37 originalDate = isInput ? $this.val() : $this.text(); 38 39 this.isInput = isInput; 40 this.originalDate = originalDate; 41 this.format = PrettyDate.fn.parseFormat(defaults.dateFormat); 42 this.setDate(defaults.date || originalDate); 43 this.active = true; 44 45 if (this.date) { 46 this.prettify(); 47 48 if (defaults.autoUpdate) { 49 this.update(); 50 } 51 } 52 }, 53 54 setDate: function (date) { 55 if (date) { 56 this.date = PrettyDate.fn.parseDate(date, this.format); 57 } 58 }, 59 60 prettify: function () { 61 var diff = (new Date()).getTime() - this.date.getTime(), 62 past = diff > 0 ? true : false, 63 messages = this.defaults.messages, 64 $this = this.$element, 65 prettyDate; 66 67 if (!this.active) { 68 return; 69 } 70 71 diff = diff < 0 ? (second - diff) : diff; 72 prettyDate = ( 73 diff < 2 * second ? messages.second : 74 diff < minute ? messages.seconds.replace("%s", floor(diff / second)) : 75 diff < 2 * minute ? messages.minute : 76 diff < hour ? messages.minutes.replace("%s", floor(diff / minute)) : 77 diff < 2 * hour ? messages.hour : 78 diff < day ? messages.hours.replace("%s", floor(diff / hour)) : 79 diff < 2 * day ? (past ? messages.yesterday : messages.tomorrow) : 80 diff < 3 * day ? (past ? messages.beforeYesterday : messages.afterTomorrow) : 81 /* diff < 2 * day ? messages.day : */ 82 diff < week ? messages.days.replace("%s", floor(diff / day)) : 83 diff < 2 * week ? messages.week : 84 diff < 4 * week ? messages.weeks.replace("%s", floor(diff / week)) : 85 diff < 2 * month ? messages.month : 86 diff < year ? messages.months.replace("%s", floor(diff / month)) : 87 diff < 2 * year ? messages.year : messages.years.replace("%s", floor(diff / year)) 88 ); 89 90 prettyDate = prettyDate.replace("%s", past ? this.defaults.beforeSuffix : this.defaults.afterSuffix); 91 92 if (this.isInput) { 93 $this.val(prettyDate); 94 } else { 95 $this.text(prettyDate); 96 } 97 98 this.prettyDate = prettyDate; 99 }, 100 101 destroy: function () { 102 var $this = this.$element, 103 originalDate = this.originalDate; 104 105 if (!this.active) { 106 return; 107 } 108 109 if (this.defaults.autoUpdate && this.autoUpdate) { 110 clearInterval(this.autoUpdate); 111 } 112 113 if (this.isInput) { 114 $this.val(originalDate); 115 } else { 116 $this.text(originalDate); 117 } 118 119 $this.removeData("prettydate"); 120 121 this.active = false; 122 }, 123 124 update: function () { 125 var duration = this.defaults.duration, 126 that = this; 127 128 if (typeof duration === "number" && duration > 0) { 129 this.autoUpdate = setInterval(function () { 130 that.prettify();
131 }, duration); 132 } 133 } 134 }; 135 136 PrettyDate.fn = { 137 parseFormat: function (format) { 138 var parts = typeof format === "string" ? format.match(/(\w+)/g) : [], 139 monthMatched = false; 140 141 if (!parts || parts.length === 0) { 142 throw new Error("Invalid date format."); 143 } 144 145 format = $.map(parts, function (n) { 146 var part = n.substr(0, 1); 147 148 switch (part) { 149 case "S": 150 case "s": 151 part = "s"; 152 break; 153 154 case "m": 155 part = monthMatched ? "m" : "M"; 156 monthMatched = true; 157 break; 158 159 case "H": 160 case "h": 161 part = "h"; 162 break; 163 164 case "D": 165 case "d": 166 part = "D"; 167 break; 168 169 case "M": 170 part = "M"; 171 monthMatched = true; 172 break; 173 174 case "Y": 175 case "y": 176 part = "Y"; 177 break; 178 179 // No default 180 } 181 182 return part; 183 }); 184 185 return format; 186 }, 187 188 parseDate: function (date, format) { 189 var parts = typeof date === "string" ? date.match(/(\d+)/g) : [], 190 data = { 191 Y: 0, 192 M: 0, 193 D: 0, 194 h: 0, 195 m: 0, 196 s: 0 197 }; 198 199 if ($.isArray(parts) && $.isArray(format) && parts.length === format.length) { 200 $.each(format, function (i, n) { 201 data[n] = parseInt(parts[i], 10) || 0; 202 }); 203 204 data.Y += data.Y > 0 && data.Y < 100 ? 2000 : 0; // Year: 14 -> 2014 205 206 date = new Date(data.Y, data.M - 1, data.D, data.h, data.m, data.s); 207 } else { 208 date = new Date(date); 209 } 210 211 return date.getTime() ? date : null; 212 } 213 }; 214 215 PrettyDate.defaults = { 216 afterSuffix: "later", 217 beforeSuffix: "ago", 218 autoUpdate: false, 219 date: null, 220 dateFormat: "YYYY-MM-DD hh:mm:ss", 221 duration: 60000, // milliseconds 222 messages: { 223 second: "Just now", 224 seconds: "%s seconds %s", 225 minute: "1 minute %s", 226 minutes: "%s minutes %s", 227 hour: "1 hour %s", 228 hours: "%s hours %s", 229 day: "1 day %s", 230 days: "%s days %s", 231 week: "1 week %s", 232 weeks: "%s weeks %s", 233 month: "1 month %s", 234 months: "%s months %s", 235 year: "1 year %s", 236 years: "%s years %s", 237 238 // Extra 239 yesterday: "Yesterday", 240 beforeYesterday: "2 days ago",//"The day before yesterday", 241 tomorrow: "Tomorrow", 242 afterTomorrow: "The day after tomorrow" 243 } 244 }; 245 246 PrettyDate.setDefaults = function (options) { 247 $.extend(PrettyDate.defaults, options); 248 }; 249 250 // Register as jQuery plugin 251 $.fn.prettydate = function (options, settings) { 252 return this.each(function () { 253 var $this = $(this), 254 data = $this.data("prettydate"); 255 256 if (!data) { 257 $this.data("prettydate", (data = new PrettyDate(this, options))); 258 } 259 260 if (typeof options === "string" && $.isFunction(data[options])) { 261 data[options](settings); 262 } 263 }); 264 }; 265 266 $.fn.prettydate.constructor = PrettyDate; 267 $.fn.prettydate.setDefaults = PrettyDate.setDefaults; 268 269 $(function () { 270 $("[prettydate]").prettydate(); 271 });
vendor: 3 bytes, line 272
272});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.