/** * @requires moment */ (function(ns) { "use strict"; /** * @alias lib.form.element.TimeSelect * @constructor */ ns.TimeSelect = function () { this.construct.apply(this, arguments); }; ns.TimeSelect.prototype.construct = function (options) { this.id = options.id; this.format = options.format; this.element = $('#' + this.id); if (!this.element.length) { throw new Error('TimeSelect element [id="' + this.id + '"] initialization failed'); } this.dropdowns = this.element.parent().find('select[data-target="' + this.id +'"]'); this.dropdowns.change(_.bind(this.onChange, this)); }; ns.TimeSelect.prototype.onChange = function () { var value = this.format; this.dropdowns.each(function () { if (!this.value.length) { value = ''; return false; } value = value.replace($(this).data('part'), this.value); return true; }); var time = moment(value, this.getJsFormat()); if (time.isValid()) { value = time.format('HH:mm:ss'); } else { value = ''; } this.element.val(value); }; ns.TimeSelect.prototype.getJsFormat = function () { return this.format.replace('g', 'h').replace('i', 'mm'); }; })(qs.defineNS('lib.form.element'));