var EXPO = EXPO || {}; //isolated namespace //общий для всех страниц модуль Функционал общий для всех страниц // module requires JQuery library // protection against duplication of a module code if (EXPO.company){ console.warn('WARNING: EXPO.company is already defined!'); }else{ EXPO.company = (function() { // variables var that = {}; that.settings = { updateFormClass:'update-profile-form' }; //default module setting that.lang ={}; //dependences //private // factory for on page form objects function Forms(){} // methods to perform ajax responses to send and receive data from the server Forms.prototype = { // ajax request realization getajax: function (dataToSend) { var self = this; if(!dataToSend){ dataToSend = ''; } $.ajax({ type: 'POST', url: self.ajaxPath, data:dataToSend, success: function(data) { self.pullHandler(data); $('#wait-ajax').fadeOut(); } }); }, // check if there exists custom data manipulation handler and evaluate it pushData: function (data, formName) { var handler = this.pushHandler, formDataString, formData = data; if(typeof handler == 'function'){ // if particular data change required $('#wait-ajax').show(); handler(data); } else{ $('#wait-ajax').show(); this.getajax(data); } formDataString = decodeURI(formData).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"').replace(/\//ig, '\\/').replace(/\\n/g, '\\n'); this.formData = JSON.parse('{"' + formDataString + '"}'); this.formId = formName; }, pullData: function (data) { var handler = this.pullHandler; if(typeof handler == 'function'){ handler(data); } else{ throw{ name: 'Error', message: " handler function for processing response after form submit not defined, please define it!" }; } } }; Forms.factory = function (type, ajaxpath) { var constr = type, newForm; if (typeof Forms[constr] !== 'function'){ throw{ name: 'Error', message: constr + "doesen't exist" }; } if(typeof Forms[constr].prototype.pushData !== "function"){ Forms[constr].prototype = new Forms(); } newForm = new Forms[constr](ajaxpath); return newForm; }; Forms.name_form = function (path) { this.pullHandler = function (data) { if (data.success){ $('#static-name-value').text($('#id_name').val()); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.spec_form = function (path) { this.pullHandler = function (data) { if (data.success){ $('#static-spec-value').text($('#id_specialization').val()); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.address_form = function (path) { this.pullHandler = function (data) { if (data.success){ $('#static-address-value').text($('#id_address_inf').val()); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.address_form = function (path) { this.pullHandler = function (data) { if (data.success){ $('#static-address-value').text($('#id_address_inf').val()); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.social_form = function (path) { var self = this, $img, imgSrc, $input, inputValue,item; this.pullHandler = function (data) { if (data.success){ for (item in self.formData) { $input = $('#id_'+item); if($.trim(self.formData[item]) != '' && $input.length){ $img = $('#img-'+item); if($img.length){ inputValue = $.trim($input.val()); if ($img.parent('a').length){ $img.parent('a').attr('href',inputValue); imgSrc = $img.attr('src').replace('_hover',''); $img.attr('src',imgSrc); }else { imgSrc = $img.attr('src').replace('_hover',''); $img.attr('src',imgSrc); $img.wrap(''); } } }else if($input.length){ $img = $('#img-'+item); if ($img.parent('a').length){ imgSrc = $img.attr('src').replace('.png','_hover.png'); $img.attr('src',imgSrc); $img.unwrap(); } } } $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.phone_form = function (path) { this.pullHandler = function (data) { if (data.success){ $('#static-phone-value').text($('#id_phone')[0].value); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.email_form = function (path) { var mailValue; this.pullHandler = function (data) { if (data.success){ mailValue = $('#id_email').val(); $('#static-email-value').attr('href', 'mailto:'+mailValue).text(mailValue); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.web_page_form = function (path) { var userLink; this.pullHandler = function (data) { if (data.success){ userLink = $('#id_web_page').val(); $('#static-web-page-value').attr('href',userLink).text(userLink); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.home_form = function (path) { this.pullHandler = function (data) { if (data.success){ var country = $('#s2id_id_country').find('.select2-chosen').text(), city = $('#s2id_id_city').find('.select2-chosen').text(); $('#static-home-country').text(country); $('#static-home-city').text(city); $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.found_form = function (path) { var currValue, $input, $container, $staticValue, $staticBox, $editButton; this.pullHandler = function (data) { if (data.success){ $input = $('#id_foundation'); currValue = $.trim($input.val()); $container = $input.parents('.p-editable'); $editButton = $input.parents('.edit-wrap').children('e-btn'); $staticValue = $('#static-found-value'); $staticBox = $('#static-foundation'); if(currValue == ''){ $container.removeClass('ic-tel').addClass('add_link_text add_link_text_medium'); $staticValue.hide().children('span').text(''); $staticBox.show(); }else{ $container.removeClass('add_link_text add_link_text_medium').addClass('ic-tel'); $staticValue.show().children('span').text(currValue); $staticBox.hide(); } $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.staff_form = function (path) { var currValue, $input, $container, $staticValue, $staticBox, $editButton; this.pullHandler = function (data) { if (data.success){ $input = $('#id_staff_number'); currValue = $.trim($input.val()); $container = $input.parents('.p-editable'); $editButton = $input.parents('.edit-wrap').children('e-btn'); $staticValue = $('#static-staff_number-value'); $staticBox = $('#static-staff_number'); if(currValue == ''){ $container.removeClass('ic-tel').addClass('add_link_text add_link_text_medium'); $staticValue.hide().children('span').text(''); $staticBox.show(); }else{ $container.removeClass('add_link_text add_link_text_medium').addClass('ic-tel'); $staticValue.show().children('span').text(currValue); $staticBox.hide(); } $('.p-editable').removeClass('pe-current'); } }; this.ajaxPath = path; }; Forms.description_form = function (path) { var currValue, $input = $('#id_description'), $container, $staticValue, $staticBox, $editButton; this.pullHandler = function (data) { if (data.success){ currValue = $.trim($input.val()); $container = $input.parents('.p-editable'); $editButton = $input.parents('.edit-wrap').children('e-btn'); $staticValue = $('#static-description-value'); $staticBox = $('#static-description'); if(currValue == ''){ $container.removeClass('ic-tel').addClass('add_link_text add_link_text_medium'); $staticValue.hide().children('span').text(''); $staticBox.show(); }else{ $container.removeClass('add_link_text add_link_text_medium').addClass('ic-tel'); $staticValue.show().text(currValue); $staticBox.hide(); } $('.p-editable').removeClass('pe-current'); } }; $input.on('keypress', function (e) { var keycode = (e.keyCode ? e.keyCode : e.which); if(keycode == '13'){ return false; } }); this.ajaxPath = path; }; // methods //инициализация общих свойств that.init = function(options) { $.extend(this.lang, options.lang); options.lang = null; $.extend(this.settings, options); var self = this, initMaskedInput = function (inputId) { var maskList = $.masksSort($.masksLoad("/static/client/js/plugins/inputmask/phone-codes.json"), ['#'], /[0-9]|#/, "mask"), maskOpts = { inputmask: { definitions: { '#': { validator: "[0-9]", cardinality: 1 } }, //clearIncomplete: true, showMaskOnHover: false, autoUnmask: true }, match: /[0-9]/, replace: '#', list: maskList, listKey: "mask", onMaskChange: function() { $(this).attr("placeholder", $(this).inputmask("getemptymask")); } }, selector = '#'+inputId; $(selector).attr('placeholder', '+_(___)___-__-__'); $(selector).inputmasks(maskOpts); }; this.forms = {}; $(function () { //start of document.ready $('#'+self.settings.selectBox[0].id).select2({ width: 'element' }); $('#'+self.settings.selectBox[1].id).select2({ placeholder: self.settings.selectBox[1].placeHolder, width: 'element', ajax: { url: self.settings.selectBox[1].path, dataType: "json", quietMillis: 200, data: function(term, page, country){ var country = $('#'+self.settings.selectBox[0].id).val() return {term: term, page: page, country: country}; }, results: function (data) { var results = []; $.each(data, function(index, item){ results.push({ id: item.id, text: item.label }); }); return {results: results}; } }, initSelection : function(element, callback) { var id= $(element).val(); var text = $(element).attr('data-init-text'); callback({id: id, text:text}); } }); $('#'+self.settings.selectBox[2].id).select2({ placeholder: self.settings.selectBox[2].placeholder, width: '368px', multiple: true, maximumSelectionSize: 5, ajax: { url: self.settings.selectBox[2].path, dataType: "json", quietMillis: 200, data: function(term, page, country){ var themes = $('#theme-inf').attr('data-theme'); themes = themes.split(','); themes.pop(); return {term: term, page: page, themes: themes}; }, results: function (data) { var results = []; $.each(data, function(index, item){ results.push({ id: item.id, text: item.label }); }); return {results: results}; } }, initSelection : function(element, callback) { } }); // make mask on phone field input initMaskedInput(self.settings.phoneBox); //forms init $('.'+self.settings.updateFormClass).each(function () { var formName = $(this).attr('id'), path = $(this).attr('action'); //make and initialize form objects self.forms[formName] = Forms.factory(formName,path); // submit events handler $(this).off('submit'); $(this).on('submit', function () { var serialized = $(this).serialize(); // each form post definitin function self.forms[formName].pushData(serialized, formName); return false; }); }); }); }; return that; }()); }