UNPKG

metro4

Version:

The front-end framework for Build responsive, mobile-first projects on the web with the first front-end component library in Metro Style

117 lines (99 loc) 3.49 kB
var Clock = { init: function( options, elem ) { this.options = $.extend( {}, this.options, options ); this.elem = elem; this.element = $(elem); this._clockInterval = null; this._setOptionsFromDOM(); this._create(); Utils.exec(this.options.onClockCreate, [this.element]); return this; }, options: { showTime: true, showDate: true, timeFormat: '24', dateFormat: 'american', divider: "&nbsp;&nbsp;", onClockCreate: Metro.noop }, _setOptionsFromDOM: function(){ var that = this, element = this.element, o = this.options; $.each(element.data(), function(key, value){ if (key in o) { try { o[key] = JSON.parse(value); } catch (e) { o[key] = value; } } }); }, _create: function(){ var that = this; this._tick(); this._clockInterval = setInterval(function(){ that._tick(); }, 500); }, _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); }, changeAttribute: function(attributeName){ }, destroy: function(){ clearInterval(this._clockInterval); this._clockInterval = null; this.element.html(""); } }; Metro.plugin('clock', Clock);