(function(ns) { "use strict"; /** * @alias app.event.admin.Form * @constructor */ ns.Form = function () { this.construct.apply(this, arguments); }; ns.Form.prototype.construct = function (options) { this.eventNS = 'eventAdmin'; this.form = $('#' + options.id); if (!this.form.length) { return false; } this.idMap = options.idMap; this.partName = options.partName; this.startDateLabels = options.startDateLabels; this.startDate = this.form.find('input[name=_startDate]'); this.endDate = this.form.find('input[name=_endDate]'); this.initRadio('input[name=durationType]', this.durationTypeOnChange); this.initRadio('input[name=type]', this.typeOnChange); this.startDate.datepicker().on('change.' + this.eventNS, _.bind(this.dateRangeOnChange, this)); this.endDate.datepicker().on('change.' + this.eventNS, _.bind(this.dateRangeOnChange, this)); this.form.find('input[name=type]').change(_.bind(this.typeOnChange, this)); return true; }; ns.Form.prototype.getTag = function (name, context) { if (this.idMap.hasOwnProperty(name)) { return $(this.idMap[name], context); } return $([], context); }; ns.Form.prototype.getTime = function (element, defaultTime) { var date = element.datepicker('getDate'); if (date instanceof Date) { return date.getTime(); } return defaultTime || null; }; ns.Form.prototype.dateRangeOnChange = function () { var startTime = this.getTime(this.startDate); if (null == startTime) { return; } var endTime = this.getTime(this.endDate, startTime); var duration = 0; if (startTime > endTime) { return; } if (startTime < endTime) { duration = Math.ceil((endTime - startTime) / (24*60*60*1000) + 1); } else if (startTime == endTime) { duration = 1; } if (duration != this.getTag('time').find('tbody tr').length) { Qs_Form.updatePart(this.form.get(0), this.partName); } }; ns.Form.prototype.initRadio = function (selector, func) { this.form.find(selector).change(_.bind(func, this)); this.form.find(selector + ':checked').change(); }; ns.Form.prototype.typeOnChange = function (e) { if ('committee' == e.target.value && e.target.checked) { this.form.find('input[name=durationType][value=singleDay]').prop('checked', true).change(); } }; ns.Form.prototype.durationTypeOnChange = function (e) { if ('singleDay' == e.target.value && e.target.checked) { this.endDate.datepicker('setDate', null).datepicker('refresh').change(); } else if (null == this.endDate.datepicker('getDate')) { var endDate = this.startDate.datepicker('getDate'); endDate = moment(endDate).add('day', 1).toDate(); this.endDate.datepicker('setDate', endDate).datepicker('refresh').change(); } this.form.find('label[for=startDate]').text(this.startDateLabels[e.target.value]); }; })(qs.defineNS('app.event.admin'));