/** * @version $Id$ * @uses $ jQuery * @uses _ Underscore */ "use strict"; (function (ns) { ns.TimeRanges = qs.createObject(); ns.TimeRanges.prototype = { initialize: function (options) { this.options = $.extend({ id: undefined, /** @type {String} Row template compatible with _.str.tpl() (placeholder {index}) */ template: undefined }, options); /** @type {jQuery} */ this.element = $('#' + this.options.id); if (1 != this.element.size()) { throw new Error('form.element.TimeRanges: Element "#' + this.options.id + '" not found'); } this.element.data('instance', this); this.refreshDayPrefixes(); return this; }, setCount: function (count) { count = +count; var rows = this.element.find('tr'), rowCount = rows.size(); if (count < 0 || count > 365) { throw new Error('Wrong parameter count'); } if (count >= rowCount) { for (var i = rowCount; i < count; ++i) { var row = _.str.tpl(this.options.template, {index: i + 1}); this.element.append(row); } } else { rows.slice(count).remove(); } this.refreshDayPrefixes(); return this; }, refreshDayPrefixes: function () { var rows = this.element.find('tr'), count = rows.size(); if (1 == count) { rows.find('[data-day-prefix]').hide(); } else { rows.find('[data-day-prefix]').show(); } return this; } }; })(qs.defineNS('form.element'));