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.opt = { updateFormClass:'update-profile-form' }; //default module setting that.lang ={}; //dependences var $waiter; //private /** * Rating functional. Object stores data, HTML Instance of rating field, and can refresh its value * @param {Object} opt - options for rating instance, like HTML Id and other * @constructor */ function Rating(opt){ this.opt = opt; this.$inst = $('#'+opt.id); this.value = this.$inst.text; } Rating.prototype = { /** * refreshes value of HTML instance with new. * @param {number} newRating - new profile rating value */ refresh: function (newRating) { this.value = newRating; this.$inst.text(this.value); } }; // 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, processData: false, contentType: false, success: function(data) { self.pullHandler(data); if(data.success){ EXPO.company.rating.refresh(data.rating); } $('#wait-ajax').fadeOut(); } }); }, // check if there exists custom data manipulation handler and evaluate it pushData: function (data, formName) { var handler = this.pushHandler, formData = data; if(typeof formData != "string"){ this.formData =formData; }else{ this.formData = JSON.parse('{"' + decodeURI(formData).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}'); } this.formId = formName; if(typeof handler == 'function'){ // if particular data change required this.formData = handler(data, formName); this.getajax(data); $('#wait-ajax').show(); } else{ this.getajax(data); $('#wait-ajax').show(); } }, 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!" }; } }, /** * refreshes form state: if value was entered by user, then there will be label with rhis value * or there will be prompt to enter value * @function * @public */ refreshState: function () { if($.trim( this.$staticValue.text()) == '' && this.$wrapper.length && this.refrashable){ this.$editable.addClass('hidden'); this.$editable.removeClass(EXPO.company.opt.formCurrentClass); this.$description.removeClass('hidden'); }else if(this.$wrapper.length && this.refrashable){ this.$editable.removeClass('hidden'); this.$description.addClass('hidden'); } } }; 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.refreshState(); }else{ this.$editable.addClass('err'); } }; 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.refreshState(); }else{ this.$editable.addClass('err'); } }; 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.refreshState(); }else{ this.$editable.addClass('err'); } }; 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); $input.parent('.required').removeClass('err'); 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'); }else { for (item in data.errors) { $input = $('#id_' + item); $input.parent('.required').addClass('err'); } } }; this.pushHandler = function (data, formName){ var formData = $('#'+formName).serialize(); return JSON.parse('{"' + decodeURI(formData).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}'); }; 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.refreshState(); } }; 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.refreshState(); }else{ this.$editable.addClass('err'); } }; 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.refreshState(); } }; 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.refreshState(); } }; this.ajaxPath = path; }; Forms.found_form = function (path) { var currValue, $input; this.pullHandler = function (data) { if (data.success){ $input = $('#id_foundation'); currValue = $.trim($input.val()); this.$staticValue.text(currValue); $('.p-editable').removeClass('pe-current'); this.refreshState(); } }; this.ajaxPath = path; }; Forms.staff_form = function (path) { var currValue, $input; this.pullHandler = function (data) { if (data.success){ $input = $('#id_staff_number'); currValue = $.trim($input.val()); this.$staticValue.text(currValue); $('.p-editable').removeClass('pe-current'); this.refreshState(); } }; this.ajaxPath = path; }; Forms.description_form = function (path) { var currValue, $input = $('#id_description'); this.pullHandler = function (data) { if (data.success){ currValue = $.trim($input.val()); this.$staticValue.text(currValue); $('.p-editable').removeClass('pe-current'); this.refreshState(); } }; $input.on('keypress', function (e) { var keycode = (e.keyCode ? e.keyCode : e.which); if(keycode == '13'){ return false; } }); this.ajaxPath = path; }; Forms.avatar_form = function (path) { var self = this, $input = $('#id_avatar'), $editPhoto = $('#pic-edit-photo'), $pickBlock = $('#pick-block'), $form, $avatar = $('img',$pickBlock); /** * callback after successful request to the server * @param data -data object recieved from server */ this.pullHandler = function (data) { if (data.success){ if($avatar.length){ $avatar.attr('src', data.url); } $('.p-editable').removeClass('pe-current'); } }; this.pushHandler = function(data){ if(typeof data != 'string'){ self.getajax(data); $('#wait-ajax').show(); }else{ $form = $('#'+self.formId); $form.off('submit'); $form.trigger('submit'); } }; $input.on('change', function () { $(this).parents('form').submit(); }); this.ajaxPath = path; this.refreshState = function () { if($.trim(this.$image.attr('src')) == this.noImageSrc){ this.$editable.addClass('hidden'); this.$description.removeClass('hidden'); }else{ this.$editable.removeClass('hidden'); this.$description.addClass('hidden'); } } }; // methods //инициализация общих свойств that.init = function(options) { // todo: убрать одновременное открытие форм $.extend(this.lang, options.lang); options.lang = null; $.extend(this.opt, 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 } }, showMaskOnHover: false, autoUnmask: true }, match: /[0-9]/, replace: '#', list: maskList, listKey: "mask", onMaskChange: function() { $(this).attr("placeholder", '+_(___)___-__-__'); } }, selector = '#'+inputId; $(selector).inputmasks(maskOpts); }; this.rating = new Rating(this.opt.rating); this.forms = {}; $(function () { var $editables = $('.'+self.opt.editableClass); //start of document.ready $waiter = $('#wait-ajax').css({'z-index': '8012'}); /** * styling with 3-rd party libs */ $('#'+self.opt.selectBox[0].id).select2({ width: 'element' }); $('#'+self.opt.selectBox[1].id).select2({ placeholder: self.opt.selectBox[1].placeHolder, width: 'element', ajax: { url: self.opt.selectBox[1].path, dataType: "json", quietMillis: 200, data: function(term, page, country){ var country = $('#'+self.opt.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.opt.selectBox[2].id).val('0'); $('#'+self.opt.selectBox[2].id).select2({ placeholder: self.opt.selectBox[2].placeholder, width: '368px', multiple: true, maximumSelectionSize: 5, ajax: { url: self.opt.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) { console.log(element); var data = [], dataObjArr; element.val(''); if($.trim(element.attr('data-predifined')) != '' && $.trim(element.attr('data-predifined')) != '[]'){ dataObjArr = JSON.parse(element.attr('data-predifined')); callback(dataObjArr); }else{ $('#tag_form .select2-input').width(336).addClass('select2-default').val(self.opt.selectBox[2].placeholder); } } }); $('#'+self.opt.selectBox[2].id).on('change', function (e) { var $form = $('#tag_form'), afterAJAX = function (data) { console.log('response complete'); console.log(data); }, doAJAX = function (dataToSend) { var self = this; if(!dataToSend){ dataToSend = ''; } $.ajax({ type: 'POST', url: $form.attr('action'), data:dataToSend, success: function(data) { afterAJAX(data); $('#wait-ajax').fadeOut(); } }); }; doAJAX($form.serialize()); $('#wait-ajax').show(); console.log('change complete'); console.log(e.val); }); // make mask on phone field input initMaskedInput(self.opt.phoneBox); //forms init $('.'+self.opt.updateFormClass).each(function () { var formName = $(this).attr('id'), path = $(this).attr('action'), inputData, $wrapper = $(this).closest('.'+self.opt.formWrapperClass), $staticValue = $('.'+self.opt.staticValueClass, $wrapper), $description = $('.'+self.opt.descriptionClass, $wrapper), $editable = $('.'+self.opt.editableClass, $wrapper), $closeButton = $('.'+self.opt.closeButtonClass, $wrapper), $image = $('.'+self.opt.avatarImageClass, $wrapper); //make and initialize form objects self.forms[formName] = Forms.factory(formName,path); if($('.'+self.opt.editButtonClass, $wrapper).hasClass('no-refresh')){ self.forms[formName].refrashable = false; }else{ self.forms[formName].refrashable = true; } self.forms[formName].$form = $(this); self.forms[formName].$wrapper = $wrapper; self.forms[formName].$staticValue = $staticValue; self.forms[formName].$description = $description; self.forms[formName].$editable = $editable; /** * Send proper information if there is image. mainly for avatar editing */ if($image.length){ self.forms[formName].$image = $image; self.forms[formName].noImageSrc = self.opt.noImageSrc; } self.forms[formName].refreshState(); // submit events handler $(this).off('submit'); $(this).on('submit', function () { //check if formData is supported $waiter.show(); if ( window.FormData && ("upload" in ($.ajaxSettings.xhr())) ){ inputData = new FormData(this); self.forms[formName].pushData(inputData, formName); } else { inputData = $(this).serialize(); self.forms[formName].pushData(inputData, formName); } return false; }); $closeButton.on('click', function() { $editable.removeClass(self.opt.formCurrentClass); self.forms[formName].refreshState(); return false; }); /** * edit button */ $('.'+self.opt.editButtonClass+', .'+self.opt.avatarButtonClass, $wrapper).on('click', function () { //$editables.removeClass(self.opt.formCurrentClass); //close other active editable forms var $wrapper = $(this).closest('.'+self.opt.formWrapperClass), formId = $wrapper.find('.'+self.opt.updateFormClass).attr('id'), $description = $wrapper.find('.'+self.opt.descriptionClass), $editable = $wrapper.find('.'+self.opt.editableClass); $editables.each(function () { var $wrapper = $(this).closest('.'+self.opt.formWrapperClass), formId = $wrapper.find('.'+self.opt.updateFormClass).attr('id'), $description = $wrapper.find('.'+self.opt.descriptionClass), $editable = $wrapper.find('.'+self.opt.editableClass); if($(this).hasClass(self.opt.formCurrentClass)){ if(self.forms[formId].refrashable){ $description.removeClass('hidden'); $editable.removeClass(self.opt.formCurrentClass).addClass('hidden'); }else{ $editable.removeClass(self.opt.formCurrentClass); } } }); if(self.forms[formId].refrashable){ $editable.removeClass('hidden'); $description.addClass('hidden'); }else{ } $editable.addClass(self.opt.formCurrentClass); //$editable.each(function() { // $(this).removeClass(self.opt.formCurrentClass); //}); //$editable.toggleClass(self.opt.formCurrentClass); // close editable form when user clicks outside $(document).on('click.form', function(event) { var targetObj = $(event.target); if (targetObj.parents().filter('.e-form').length < 1) { $editable.removeClass(self.opt.formCurrentClass); self.forms[formName].refreshState(); $(document).off('click.form'); } }); return false; }); }); }); }; return that; }()); }