balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

jquery.countdown.js (8308B)


      1 /*!
      2  * The Final Countdown for jQuery v2.0.4 (http://hilios.github.io/jQuery.countdown/)
      3  * Copyright (c) 2014 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 PRECISION = 100;
     32     var instances = [], matchers = [];
     33     matchers.push(/^[0-9]*$/.source);
     34     matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
     35     matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
     36     matchers = new RegExp(matchers.join("|"));
     37     function parseDateString(dateString) {
     38         if (dateString instanceof Date) {
     39             return dateString;
     40         }
     41         if (String(dateString).match(matchers)) {
     42             if (String(dateString).match(/^[0-9]*$/)) {
     43                 dateString = Number(dateString);
     44             }
     45             if (String(dateString).match(/\-/)) {
     46                 dateString = String(dateString).replace(/\-/g, "/");
     47             }
     48             return new Date(dateString);
     49         } else {
     50             throw new Error("Couldn't cast `" + dateString + "` to a date object.");
     51         }
     52     }
     53     var DIRECTIVE_KEY_MAP = {
     54         Y: "years",
     55         m: "months",
     56         w: "weeks",
     57         d: "days",
     58         D: "totalDays",
     59         H: "hours",
     60         M: "minutes",
     61         S: "seconds"
     62     };
     63     function strftime(offsetObject) {
     64         return function(format) {
     65             var directives = format.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);
     66             if (directives) {
     67                 for (var i = 0, len = directives.length; i < len; ++i) {
     68                     var directive = directives[i].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/), regexp = new RegExp(directive[0]), modifier = directive[1] || "", plural = directive[3] || "", value = null;
     69                     directive = directive[2];
     70                     if (DIRECTIVE_KEY_MAP.hasOwnProperty(directive)) {
     71                         value = DIRECTIVE_KEY_MAP[directive];
     72                         value = Number(offsetObject[value]);
     73                     }
     74                     if (value !== null) {
     75                         if (modifier === "!") {
     76                             value = pluralize(plural, value);
     77                         }
     78                         if (modifier === "") {
     79                             if (value < 10) {
     80                                 value = "0" + value.toString();
     81                             }
     82                         }
     83                         format = format.replace(regexp, value.toString());
     84                     }
     85                 }
     86             }
     87             format = format.replace(/%%/, "%");
     88             return format;
     89         };
     90     }
     91     function pluralize(format, count) {
     92         var plural = "s", singular = "";
     93         if (format) {
     94             format = format.replace(/(:|;|\s)/gi, "").split(/\,/);
     95             if (format.length === 1) {
     96                 plural = format[0];
     97             } else {
     98                 singular = format[0];
     99                 plural = format[1];
    100             }
    101         }
    102         if (Math.abs(count) === 1) {
    103             return singular;
    104         } else {
    105             return plural;
    106         }
    107     }
    108     var Countdown = function(el, finalDate, callback) {
    109         this.el = el;
    110         this.$el = $(el);
    111         this.interval = null;
    112         this.offset = {};
    113         this.instanceNumber = instances.length;
    114         instances.push(this);
    115         this.$el.data("countdown-instance", this.instanceNumber);
    116         if (callback) {
    117             this.$el.on("update.countdown", callback);
    118             this.$el.on("stoped.countdown", callback);
    119             this.$el.on("finish.countdown", callback);
    120         }
    121         this.setFinalDate(finalDate);
    122         this.start();
    123     };
    124     $.extend(Countdown.prototype, {
    125         start: function() {
    126             if (this.interval !== null) {
    127                 clearInterval(this.interval);
    128             }
    129             var self = this;
    130             this.update();
    131             this.interval = setInterval(function() {
    132                 self.update.call(self);
    133             }, PRECISION);
    134         },
    135         stop: function() {
    136             clearInterval(this.interval);
    137             this.interval = null;
    138             this.dispatchEvent("stoped");
    139         },
    140         pause: function() {
    141             this.stop.call(this);
    142         },
    143         resume: function() {
    144             this.start.call(this);
    145         },
    146         remove: function() {
    147             this.stop();
    148             instances[this.instanceNumber] = null;
    149             delete this.$el.data().countdownInstance;
    150         },
    151         setFinalDate: function(value) {
    152             this.finalDate = parseDateString(value);
    153         },
    154         update: function() {
    155             if (this.$el.closest("html").length === 0) {
    156                 this.remove();
    157                 return;
    158             }
    159             this.totalSecsLeft = this.finalDate.getTime() - new Date().getTime();
    160             this.totalSecsLeft = Math.ceil(this.totalSecsLeft / 1e3);
    161             this.totalSecsLeft = this.totalSecsLeft < 0 ? 0 : this.totalSecsLeft;
    162             this.offset = {
    163                 seconds: this.totalSecsLeft % 60,
    164                 minutes: Math.floor(this.totalSecsLeft / 60) % 60,
    165                 hours: Math.floor(this.totalSecsLeft / 60 / 60) % 24,
    166                 days: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
    167                 totalDays: Math.floor(this.totalSecsLeft / 60 / 60 / 24),
    168                 weeks: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7),
    169                 months: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 30),
    170                 years: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 365)
    171             };
    172             if (this.totalSecsLeft === 0) {
    173                 this.stop();
    174                 this.dispatchEvent("finish");
    175             } else {
    176                 this.dispatchEvent("update");
    177             }
    178         },
    179         dispatchEvent: function(eventName) {
    180             var event = $.Event(eventName + ".countdown");
    181             event.finalDate = this.finalDate;
    182             event.offset = $.extend({}, this.offset);
    183             event.strftime = strftime(this.offset);
    184             this.$el.trigger(event);
    185         }
    186     });
    187     $.fn.countdown = function() {
    188         var argumentsArray = Array.prototype.slice.call(arguments, 0);
    189         return this.each(function() {
    190             var instanceNumber = $(this).data("countdown-instance");
    191             if (instanceNumber !== undefined) {
    192                 var instance = instances[instanceNumber], method = argumentsArray[0];
    193                 if (Countdown.prototype.hasOwnProperty(method)) {
    194                     instance[method].apply(instance, argumentsArray.slice(1));
    195                 } else if (String(method).match(/^[$A-Z_][0-9A-Z_$]*$/i) === null) {
    196                     instance.setFinalDate.call(instance, method);
    197                     instance.start();
    198                 } else {
    199                     $.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi, method));
    200                 }
    201             } else {
    202                 new Countdown(this, argumentsArray[0], argumentsArray[1]);
    203             }
    204         });
    205     };
    206 });