/** * @requires $ jQuery * @requires _ Underscore JS */ (function (ns) { "use strict"; ns.Form = qs.createObject(); ns.Form.prototype = { initialize: function (options) { this.eventNS = 'eventForm'; this.options = $.extend({ requestUrl: undefined, formId: undefined, attendeeNodeId: undefined, maxAttendeeCount: undefined, /** @type {String} Optional, used at user end */ loginBlockId: undefined }, options); this.formNode = $('#' + this.options.formId).required(); this.attendeeNode = this.formNode.find('#' + this.options.attendeeNodeId).required(); this.initEvents(); this.initWaiver(); $('#hideSignUpForm').show(); return this; }, initEvents: function () { if (this.options.loginBlockId) { $('a[data-register-nonmember]').on('click.' + this.eventNS, _.bind(this.onRegisterNonMemberClick, this)); } this.formNode.find('[data-action="add"]').on('click.' + this.eventNS, _.bind(this.onAddAttendeeClick, this)); this.formNode.find('[data-attendee]').each(_.bind(function (idx, node) { this.initAttendeeForm($(node)); }, this)); return this; }, initWaiver: function () { $('body').on('click', '[data-waiver-btn]', $.proxy(this.onWaiverBtnClick, this)); }, onWaiverBtnClick: function (e) { var element = $('[data-waiver-paragraph]'); if (element.length) { $.fancybox({ titleShow: false, transitionIn: 'none', transitionOut: 'none', content: element.html(), autoScale: true, autoDimensions: false, width: 600, height: 'auto', padding: 30, scrolling: 'auto' }); } }, onRegisterNonMemberClick: function (e) { e.preventDefault(); $('#' + this.options.loginBlockId).hide(); $('#' + this.options.formId).show(); return null; }, onAddAttendeeClick: function (e) { e.preventDefault(); var attendeeForms = this.attendeeNode.find('[data-attendee]'), formsCount = +attendeeForms.size(), lastIndex = +attendeeForms.last().data('attendee'); if (formsCount >= this.options.maxAttendeeCount) { alert('Only ' + this.options.maxAttendeeCount + ' additional attendees allowed!'); return null; } qs.ajax(this.requestUrl, { action: 'renderAttendee', lastIndex: lastIndex }).done(_.bind(this.onRenderAttendeeResponse, this)); return null; }, /** * @param {Object} response * @param {String} response.formHtml * @param {Number} response.formIndex * @param {String} response.evalData */ onRenderAttendeeResponse: function (response) { var form, formHtml = _.str.trim(response.formHtml); if (formHtml) { form = $(formHtml); this.attendeeNode.append(form); this.initAttendeeForm(form); this.updateLegendsNumbers(); if (response.evalData) { $.globalEval(response.evalData); } } return this; }, initAttendeeForm: function (form) { form.find('[data-action="delete"]').bind('click', _.bind(this.onDeleteAttendeeClick, this)); return this; }, updateLegendsNumbers: function () { var idx = 1; this.attendeeNode.find('[data-attendee]').each(function() { $(this).find('legend:first').html(idx++); }); return this; }, onDeleteAttendeeClick: function (e) { e.preventDefault(); var block = $(e.currentTarget).closest('[data-attendee]'), index = +block.data('attendee'); if (1 < this.attendeeNode.find('[data-attendee]').size()) { block.remove(); this.updateLegendsNumbers(); } else { block.find('input[type="text"][name^="attendee[' + index + ']"]').val(''); } return null; } }; })(qs.defineNS('app.event.form'));