/** * @version $Id$ * @uses $ jQuery * @uses _ Underscore */ "use strict"; qs.defineNS('app.event.admin'); app.event.admin.Form = qs.createObject(); app.event.admin.Form.prototype = { initialize: function (options) { this.eventNS = 'eventAdmin'; this.options = $.extend({ formId: undefined, /** @type {Array} List of relation compatible with jQuery.dynamicForm format */ relations: undefined, timeDefaults: undefined }, options); this.form = $('#' + this.options.formId); this.startDate = $('#_startDate'); this.registrationStartDate = $('#_registrationStartDate'); this.registrationEndDate = $('#_registrationEndDate'); this.endDate = $('#_endDate'); $(this.form).dynamicForm({ relations: this.options.relations }); this.startDate .datepicker('option', 'onClose', _.bind(this.onStartDateChange, this)) .on('change.' + this.eventNS, _.bind(this.onStartDateChange, this)); this.endDate .datepicker('option', 'onClose', _.bind(this.onEndDateChange, this)) .on('change.' + this.eventNS, _.bind(this.onEndDateChange, this)); this.registrationStartDate.on('change.' + this.eventNS, _.bind(this.onRegistrationStartDateChange, this)) this.registrationEndDate.on('change.' + this.eventNS, _.bind(this.onRegistrationEndDateChange, this)) return this; }, /** * Used by relations config in "AbstractForm->_initRelations()" * /site/App/Event/Admin/Form/AbstractForm.php * @static */ switchToSingle: function () { var radioMulti = $('#durationType-multiple'), radioSingle = $('#durationType-single'), date1, startDate = $('#_startDate'), endDate = $('#_endDate'); if (radioMulti.is(':checked')) { radioMulti.removeAttr('checked'); radioSingle.attr('checked', 'checked').change(); } if ((date1 = startDate.datepicker('getDate')) && +date1 !== +endDate.datepicker('getDate')) { endDate.datepicker('setDate', date1).datepicker('refresh').trigger('change'); } $('label[for="startDate"]').text('Date'); return true; }, /** * Used by relations config in "AbstractForm->_initRelations()" * /site/App/Event/Admin/Form/AbstractForm.php * @static */ switchToMulti: function () { $('label[for="startDate"]').text('Start Date'); return true; }, elementTimeSetValue: function(selectIdPrefix, strValue) { if (!strValue) { return false; } var timeParts = strValue.split(':'); var timeVals = { g: '', i: '', a: '' }; if (timeParts.length > 1) { timeVals.g = parseInt(timeParts[0]); if (timeVals.g > 24) timeVals.g = 0; var minutes= parseInt(timeParts[1]); timeVals.i = (minutes < 15) ? '00' : (minutes < 30) ? '15' : (minutes < 45) ? '30' : '45'; if (timeVals.g <= 12) { timeVals.a = 'am'; } else { timeVals.g -= 12; timeVals.a = 'pm'; } } $('#' + selectIdPrefix + '-g').val(timeVals.g); $('#' + selectIdPrefix + '-i').val(timeVals.i); $('#' + selectIdPrefix + '-a').val(timeVals.a); }, onRegistrationStartDateChange: function (event) { var emptyFlag = (!$('#registrationStartTime_g').val()); if (this.registrationStartDate.val() && emptyFlag) { this.elementTimeSetValue('registrationStartTime', this.options.timeDefaults[0]); } else { this.elementTimeSetValue('registrationStartTime', ''); } }, onRegistrationEndDateChange: function() { var emptyFlag = (!$('#registrationEndTime_g').val()); if (this.registrationEndDate.val() && emptyFlag) { this.elementTimeSetValue('registrationEndTime', this.options.timeDefaults[1]); } else { this.elementTimeSetValue('registrationEndTime', ''); } }, /** * Service method, set required status visibility for fields labels * @param labelIdsArray ids of parent of label element */ setLabelsDynamicRequiredStatus: function(labelIdsArray, required) { var requiredFlag = (required) | false; for(var i = 0, l = labelIdsArray.length; i < l; i++) { if (requiredFlag) $('#' + labelIdsArray[i] + ' > label').addClass('required'); else $('#' + labelIdsArray[i] + ' > label').removeClass('required'); } }, /** * Service method, make required status not visible for all fields with dynamic required status */ resetLabelsDRequiredStatus: function() { app.event.admin.Form.prototype.setLabelsDynamicRequiredStatus([ 'timeRanges-label', 'registrationStartDate-label', 'registrationStartTime-label', 'registrationEndDate-label', 'registrationEndTime-label', 'location-label', 'regularPrice-label', 'memberPrice-label' ]); }, resetFieldsErrors: function() { $('#event-form_elements .errors').html(''); }, onRegularTypeChanged: function() { app.event.admin.Form.prototype.resetFieldsErrors(); app.event.admin.Form.prototype.resetLabelsDRequiredStatus(); }, onGroupTypeChanged: function() { app.event.admin.Form.prototype.resetFieldsErrors(); app.event.admin.Form.prototype.resetLabelsDRequiredStatus(); }, onCommitteeTypeChanged: function() { app.event.admin.Form.prototype.resetFieldsErrors(); app.event.admin.Form.prototype.setLabelsDynamicRequiredStatus([ 'timeRanges-label', 'registrationStartDate-label', 'registrationStartTime-label', 'registrationEndDate-label', 'registrationEndTime-label', 'location-label', 'regularPrice-label', 'memberPrice-label' ], true); app.event.admin.Form.prototype.switchToSingle.call(this); }, onOtherTypeChanged: function() { app.event.admin.Form.prototype.resetFieldsErrors(); app.event.admin.Form.prototype.resetLabelsDRequiredStatus(); app.event.admin.Form.prototype.setLabelsDynamicRequiredStatus([ 'registrationStartDate-label', 'registrationStartTime-label', 'registrationEndDate-label', 'registrationEndTime-label', 'regularPrice-label', 'memberPrice-label' ], true); }, onStartDateChange: function () { var date1, radioSingle = $('#durationType-single'); if (radioSingle.is(':checked')) { date1 = this.startDate.datepicker('getDate'); this.endDate.datepicker('setDate', date1).datepicker('refresh'); } this.updateDuration(); return true; }, onEndDateChange: function () { this.updateDuration(); return true; }, updateDuration: function () { var duration, date1 = this.startDate.datepicker('getDate'), date2 = this.endDate.datepicker('getDate'); if (date1 && date2) { // check date range if (date1 > date2) { Qs_Form.displayErrorsHtml(this.form.get(0), {timeRanges: []}); duration = 0; } else { duration = (date2 - date1) / (24 * 60 * 60 * 1000) + 1; } // update duration duration = Math.abs(Math.floor(duration)); $('#timeRanges').data('instance').setCount(duration); } return this; } };