(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.partName = options.partName;
this.subtypes = options.subtypes;
this.selectedSubtype = options.selectedSubtype;
this.startDate = this.form.find('input[name=_startDate]');
this.endDate = this.form.find('input[name=_endDate]');
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));
this.form.find('#ceus-element').on('dfHide dfShow', _.bind(this.ceusOnToggle, this));
return true;
};
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;
}
};
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) {
var subtype = $("#subtype");
var emptyTitle = subtype.find('option:first').text();
var option = $('').attr("value", "").text(emptyTitle);
subtype.empty().append(option);
if ('undefined' != typeof this.subtypes[e.target.value]) {
$('dt#subtype-label, dd#subtype-element').show();
for (var i = 0; i < this.subtypes[e.target.value].length; i++) {
var optionContainer = this.subtypes[e.target.value][i];
option = $('').attr("value", optionContainer.id).text(optionContainer.title);
subtype.append(option);
}
if (this.selectedSubtype) {
subtype.val(this.selectedSubtype);
}
} else {
//hide select
$('dt#subtype-label, dd#subtype-element').hide();
}
};
ns.Form.prototype.ceusOnToggle = function (e) {
var visible = $(e.currentTarget).is(':visible');
this.form.toggleClass('form-no-ceus', !visible);
}
})(qs.defineNS('app.event.admin'));