var fasoon = fasoon || {}; (function (ns, $) { "use strict"; ns.QuotePriceStep4 = function () { this.construct.apply(this, arguments); }; var proto = ns.QuotePriceStep4.prototype; proto.construct = function (options) { this.options = $.extend({ form_id: undefined, plugins_url: undefined, save_action: undefined, ajax_url: undefined, persons_class: undefined, add_person_class: undefined, new_person_form_class: undefined, persons_data: {}, load_contact_data_id: undefined, contact_data: {}, contact_data_confirmation: undefined, errorsContainerClass: 'form-error' }, options); this.form = $('#' + this.options.form_id); if (!this.form.length) { alert('Form #' + this.options.form_id + ' is not found.'); return; } this.persons = $('.' + this.options.persons_class, this.form); if (!this.persons.length) { alert('Container .' + this.options.persons_class + ' is not found.'); return; } this.new_person_form = $('.' + this.options.new_person_form_class + ' form'); if (this.new_person_form.length) { this.new_person_form.on('submit', $.proxy(this.new_person_form_submit, this)); $('[type=submit]', this.new_person_form).on('click', $.proxy(this.required_show, this)); // ff fix this.load_contact_data_btn = $('#' + this.options.load_contact_data_id, this.new_person_form); if (this.load_contact_data_btn.length) { this.load_contact_data_btn.on('click', $.proxy(this.load_contact_data, this)); } } this.add_person_btn = $('.' + this.options.add_person_class, this.form); this.add_person_btn .on('click', $.proxy(this.add_person_form, this)); $('.' + this.options.new_person_form_class + ' .person-cancel').on('click', $.proxy(this.hide_person_form, this)); this.init_persons_actions(); }; proto.load_contact_data = function () { var is_empty = true; for (var i in this.options.contact_data) { if ('prefix' == i) { continue; } if ($('[name=' + i + ']', this.new_person_form).val()) { is_empty = false; break; } } if (!is_empty && !confirm(this.options.contact_data_confirmation)) { return; } for (var i in this.options.contact_data) { if ('prefix' == i) { $('[name=' + i + '][value=' + this.options.contact_data[i] + ']').prop('checked', true); } else { $('[name=' + i + ']', this.new_person_form).val(this.options.contact_data[i]).trigger('blur');; } } }; proto.hide_submit_group = function() { this.form.find('.submit').hide(); }; proto.show_submit_group = function() { this.form.find('.submit').show(); }; proto.hide_add_person_btn = function() { this.add_person_btn.closest('div').hide(); }; proto.show_add_person_btn = function() { this.add_person_btn.closest('div').show(); }; proto.hide_person_form = function () { $('.' + this.options.new_person_form_class).hide(); this.clear_person_forms(); if (this.persons.html()) { this.show_submit_group(); this.show_add_person_btn(); } else { this.add_person_form(); } }; proto.add_person_form = function() { this.hide_submit_group(); this.hide_add_person_btn(); if (this.persons.html()) { this.load_contact_data_btn.hide(); } else { this.load_contact_data_btn.show(); } $('.' + this.options.new_person_form_class).show(); $(document).scrollTop(0); }; proto.new_person_form_submit = function() { var data = this.new_person_form.serializeObject(); data.action = this.options.save_action; $.post(this.options.ajax_url, data, $.proxy(this.new_form_submit_success, this), 'json'); return false; }; proto.new_form_submit_success = function (response) { if (response.is_success) { this.options.persons_data = response.persons_data; this.persons.html(response.persons_html); this.init_persons_actions(); this.removeErrors(); this.hide_person_form(); } else { if (response.hasOwnProperty('wrong_nonce') && !response.wrong_nonce) { document.location.reload(); } else { alert(response.error); } } }; proto.removeErrors = function () { $('.' + this.options.errorsContainerClass).remove(); }; proto.clear_person_forms = function () { if (this.new_person_form.length) { this.new_person_form[0].reset(); $('.filled', this.new_person_form).removeClass('filled'); $('[name=id]', this.new_person_form).val(''); } this.required_hide(); // ff fix }; proto.required_hide = function () { // ff fix if (this.new_person_form.length) { this.new_person_form.addClass('required-hide'); } }; proto.required_show = function () { // ff fix if (this.new_person_form.length) { this.new_person_form.removeClass('required-hide'); } }; proto.init_persons_actions = function () { $('.person_action', this.persons).on('click', $.proxy(this.person_action, this)); }; proto.person_action = function (ev) { var a = $(ev.target); var id = a.data('id'); var type = a.data('type'); var data = this.options.persons_data[type][id]; data.action = this.options.save_action; data.id = id; if ('person' === type) { data._wpnonce = $('[name=_wpnonce]', this.new_person_form).val(); } if ('delete' === a.data('action')) { data.sub_action = 'delete'; $.post(this.options.ajax_url, data, $.proxy(this.new_form_submit_success, this), 'json'); } else if ('edit' === a.data('action')) { var form; if ('person' === type) { form = this.new_person_form; } $.each(data, function(name, val) { var $el = $('[name="'+name+'"]', form), type = $el.attr('type'); switch(type) { case 'checkbox': $el.attr('checked', 'checked'); break; case 'radio': $el.filter('[value="'+val+'"]').attr('checked', 'checked'); break; default: $el.val(val); } $el.trigger('blur'); }); if ('person' === type) { this.add_person_form(); } } return false; }; })(fasoon, jQuery);