jquery.countdown.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*!
  2. * The Final Countdown for jQuery v2.2.0 (http://hilios.github.io/jQuery.countdown/)
  3. * Copyright (c) 2016 Edson Hilios
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. (function(factory) {
  23. "use strict";
  24. if (typeof define === "function" && define.amd) {
  25. define([ "jquery" ], factory);
  26. } else {
  27. factory(jQuery);
  28. }
  29. })(function($) {
  30. "use strict";
  31. var instances = [], matchers = [], defaultOptions = {
  32. precision: 100,
  33. elapse: false,
  34. defer: false
  35. };
  36. matchers.push(/^[0-9]*$/.source);
  37. matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
  38. matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
  39. matchers = new RegExp(matchers.join("|"));
  40. function parseDateString(dateString) {
  41. if (dateString instanceof Date) {
  42. return dateString;
  43. }
  44. if (String(dateString).match(matchers)) {
  45. if (String(dateString).match(/^[0-9]*$/)) {
  46. dateString = Number(dateString);
  47. }
  48. if (String(dateString).match(/\-/)) {
  49. dateString = String(dateString).replace(/\-/g, "/");
  50. }
  51. return new Date(dateString);
  52. } else {
  53. throw new Error("Couldn't cast `" + dateString + "` to a date object.");
  54. }
  55. }
  56. var DIRECTIVE_KEY_MAP = {
  57. Y: "years",
  58. m: "months",
  59. n: "daysToMonth",
  60. d: "daysToWeek",
  61. w: "weeks",
  62. W: "weeksToMonth",
  63. H: "hours",
  64. M: "minutes",
  65. S: "seconds",
  66. D: "totalDays",
  67. I: "totalHours",
  68. N: "totalMinutes",
  69. T: "totalSeconds"
  70. };
  71. function escapedRegExp(str) {
  72. var sanitize = str.toString().replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
  73. return new RegExp(sanitize);
  74. }
  75. function strftime(offsetObject) {
  76. return function(format) {
  77. var directives = format.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);
  78. if (directives) {
  79. for (var i = 0, len = directives.length; i < len; ++i) {
  80. var directive = directives[i].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/), regexp = escapedRegExp(directive[0]), modifier = directive[1] || "", plural = directive[3] || "", value = null;
  81. directive = directive[2];
  82. if (DIRECTIVE_KEY_MAP.hasOwnProperty(directive)) {
  83. value = DIRECTIVE_KEY_MAP[directive];
  84. value = Number(offsetObject[value]);
  85. }
  86. if (value !== null) {
  87. if (modifier === "!") {
  88. value = pluralize(plural, value);
  89. }
  90. if (modifier === "") {
  91. if (value < 10) {
  92. value = "0" + value.toString();
  93. }
  94. }
  95. format = format.replace(regexp, value.toString());
  96. }
  97. }
  98. }
  99. format = format.replace(/%%/, "%");
  100. return format;
  101. };
  102. }
  103. function pluralize(format, count) {
  104. var plural = "s", singular = "";
  105. if (format) {
  106. format = format.replace(/(:|;|\s)/gi, "").split(/\,/);
  107. if (format.length === 1) {
  108. plural = format[0];
  109. } else {
  110. singular = format[0];
  111. plural = format[1];
  112. }
  113. }
  114. if (Math.abs(count) > 1) {
  115. return plural;
  116. } else {
  117. return singular;
  118. }
  119. }
  120. var Countdown = function(el, finalDate, options) {
  121. this.el = el;
  122. this.$el = $(el);
  123. this.interval = null;
  124. this.offset = {};
  125. this.options = $.extend({}, defaultOptions);
  126. this.firstTick = true;
  127. this.instanceNumber = instances.length;
  128. instances.push(this);
  129. this.$el.data("countdown-instance", this.instanceNumber);
  130. if (options) {
  131. if (typeof options === "function") {
  132. this.$el.on("update.countdown", options);
  133. this.$el.on("stoped.countdown", options);
  134. this.$el.on("finish.countdown", options);
  135. } else {
  136. this.options = $.extend({}, defaultOptions, options);
  137. }
  138. }
  139. this.setFinalDate(finalDate);
  140. if (this.options.defer === false) {
  141. this.start();
  142. }
  143. };
  144. $.extend(Countdown.prototype, {
  145. start: function() {
  146. if (this.interval !== null) {
  147. clearInterval(this.interval);
  148. }
  149. var self = this;
  150. this.update();
  151. this.interval = setInterval(function() {
  152. self.update.call(self);
  153. }, this.options.precision);
  154. },
  155. stop: function() {
  156. clearInterval(this.interval);
  157. this.interval = null;
  158. this.dispatchEvent("stoped");
  159. },
  160. toggle: function() {
  161. if (this.interval) {
  162. this.stop();
  163. } else {
  164. this.start();
  165. }
  166. },
  167. pause: function() {
  168. this.stop();
  169. },
  170. resume: function() {
  171. this.start();
  172. },
  173. remove: function() {
  174. this.stop.call(this);
  175. instances[this.instanceNumber] = null;
  176. delete this.$el.data().countdownInstance;
  177. },
  178. setFinalDate: function(value) {
  179. this.finalDate = parseDateString(value);
  180. },
  181. update: function() {
  182. if (this.$el.closest("html").length === 0) {
  183. this.remove();
  184. return;
  185. }
  186. var now = new Date(), newTotalSecsLeft;
  187. newTotalSecsLeft = this.finalDate.getTime() - now.getTime();
  188. newTotalSecsLeft = Math.ceil(newTotalSecsLeft / 1e3);
  189. newTotalSecsLeft = !this.options.elapse && newTotalSecsLeft < 0 ? 0 : Math.abs(newTotalSecsLeft);
  190. if (this.totalSecsLeft === newTotalSecsLeft || this.firstTick) {
  191. this.firstTick = false;
  192. return;
  193. } else {
  194. this.totalSecsLeft = newTotalSecsLeft;
  195. }
  196. this.elapsed = now >= this.finalDate;
  197. this.offset = {
  198. seconds: this.totalSecsLeft % 60,
  199. minutes: Math.floor(this.totalSecsLeft / 60) % 60,
  200. hours: Math.floor(this.totalSecsLeft / 60 / 60) % 24,
  201. days: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  202. daysToWeek: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  203. daysToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 % 30.4368),
  204. weeks: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7),
  205. weeksToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7) % 4,
  206. months: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 30.4368),
  207. years: Math.abs(this.finalDate.getFullYear() - now.getFullYear()),
  208. totalDays: Math.floor(this.totalSecsLeft / 60 / 60 / 24),
  209. totalHours: Math.floor(this.totalSecsLeft / 60 / 60),
  210. totalMinutes: Math.floor(this.totalSecsLeft / 60),
  211. totalSeconds: this.totalSecsLeft
  212. };
  213. if (!this.options.elapse && this.totalSecsLeft === 0) {
  214. this.stop();
  215. this.dispatchEvent("finish");
  216. } else {
  217. this.dispatchEvent("update");
  218. }
  219. },
  220. dispatchEvent: function(eventName) {
  221. var event = $.Event(eventName + ".countdown");
  222. event.finalDate = this.finalDate;
  223. event.elapsed = this.elapsed;
  224. event.offset = $.extend({}, this.offset);
  225. event.strftime = strftime(this.offset);
  226. this.$el.trigger(event);
  227. }
  228. });
  229. $.fn.countdown = function() {
  230. var argumentsArray = Array.prototype.slice.call(arguments, 0);
  231. return this.each(function() {
  232. var instanceNumber = $(this).data("countdown-instance");
  233. if (instanceNumber !== undefined) {
  234. var instance = instances[instanceNumber], method = argumentsArray[0];
  235. if (Countdown.prototype.hasOwnProperty(method)) {
  236. instance[method].apply(instance, argumentsArray.slice(1));
  237. } else if (String(method).match(/^[$A-Z_][0-9A-Z_$]*$/i) === null) {
  238. instance.setFinalDate.call(instance, method);
  239. instance.start();
  240. } else {
  241. $.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi, method));
  242. }
  243. } else {
  244. new Countdown(this, argumentsArray[0], argumentsArray[1]);
  245. }
  246. });
  247. };
  248. });