UNPKG

vpn.email

Version:
107 lines (89 loc) 3.21 kB
$.widget( "metro.clock" , { version: "1.0.0", options: { showTime: true, showDate: true, timeFormat: '24', dateFormat: 'american', divider: "&nbsp;&nbsp;" }, _create: function () { var that = this, element = this.element, o = this.options; this._setOptionsFromDOM(); this._tick(); this._clockInterval = setInterval(function(){ that._tick(); }, 500); element.data('clock', this); }, _addLeadingZero: function(i){ if (i<10){i="0" + i;} return i; }, _tick: function(){ var that = this, element = this.element, o = this.options; var timestamp = new Date(); var time = timestamp.getTime(); var result = ""; var h = timestamp.getHours(), i = timestamp.getMinutes(), s = timestamp.getSeconds(), d = timestamp.getDate(), m = timestamp.getMonth() + 1, y = timestamp.getFullYear(), a = ''; if (o.timeFormat == '12') { a = " AM"; if (h > 11) { a = " PM"; } if (h > 12) { h = h - 12; } if (h == 0) { h = 12; } } h = this._addLeadingZero(h); i = this._addLeadingZero(i); s = this._addLeadingZero(s); m = this._addLeadingZero(m); d = this._addLeadingZero(d); if (o.showDate) { if (o.dateFormat == 'american') { result += "<span class='date-month'>" + m + "</span>"; result += "<span class='date-divider'>-</span>"; result += "<span class='date-day'>" + d + "</span>"; result += "<span class='date-divider'>-</span>"; result += "<span class='date-year'>" + y + "</span>"; } else { result += "<span class='date-day'>" + d + "</span>"; result += "<span class='date-divider'>-</span>"; result += "<span class='date-month'>" + m + "</span>"; result += "<span class='date-divider'>-</span>"; result += "<span class='date-year'>" + y + "</span>"; } result += o.divider; } if (o.showTime) { result += "<span class='clock-hour'>" + h + "</span>"; result += "<span class='clock-divider'>:</span>"; result += "<span class='clock-minute'>" + i + "</span>"; result += "<span class='clock-divider'>:</span>"; result += "<span class='clock-second'>" + s + "</span>"; } element.html(result); }, _setOptionsFromDOM: function(){ var that = this, element = this.element, o = this.options; $.each(element.data(), function(key, value){ if (key in o) { try { o[key] = $.parseJSON(value); } catch (e) { o[key] = value; } } }); }, _destroy: function () { clearInterval(this._clockInterval); }, _setOption: function ( key, value ) { this._super('_setOption', key, value); } });