From 9fdcbe205339404d41b27ce8d0a3feb806debb2c Mon Sep 17 00:00:00 2001 From: mitri4 Date: Mon, 20 Mar 2017 17:28:00 +0300 Subject: [PATCH] refactor js, move scripts in eternal files --- .../static/js/client/search-external-api.js | 129 +++++++++ project/static/js/dialogs.js | 16 ++ .../js/profile/search-bank-external-api.js | 36 +++ .../js/profile/search-main-external-api.js | 158 +++++++++++ project/templates/customer/clients/list.html | 166 +----------- project/templates/customer/profile/edit.html | 246 +----------------- project/templates/hbs/bank-tpl.html | 28 ++ 7 files changed, 372 insertions(+), 407 deletions(-) create mode 100644 project/static/js/client/search-external-api.js create mode 100644 project/static/js/profile/search-bank-external-api.js create mode 100644 project/static/js/profile/search-main-external-api.js create mode 100644 project/templates/hbs/bank-tpl.html diff --git a/project/static/js/client/search-external-api.js b/project/static/js/client/search-external-api.js new file mode 100644 index 0000000..0bbf45b --- /dev/null +++ b/project/static/js/client/search-external-api.js @@ -0,0 +1,129 @@ +$(document).ready(function() { + var clientForm = $("#client-edit-form"), + clientSearch = $("#searchClientInput", clientForm), + clientName = $("#id_name", clientForm), + clientInn = $("#id_inn", clientForm), + clientKpp = $("#id_kpp", clientForm), + clientOqrn = $("#id_ogrn", clientForm), + clientAddress = $("#id_address", clientForm), + bankSearch = $("#searchClientBankInput", clientForm), + bankBic = $("#id_bank_bik", clientForm), + bankFullName = $("#id_bank_name", clientForm), + bankCorrespondentAccount = $("#id_bank_korr_account", clientForm), + clientInputs = [clientName, clientInn, clientOqrn, clientAddress]; + + + function confirmChangeDataPromise(inputArray) { + var dlg_msg = $('#dialog-message'); + var emptyInput = true; + var defer = $.Deferred(); + + for (var i = inputArray.length - 1; i >= 0; --i) { + + if (inputArray[i].val()) { + emptyInput = false; + break; + } + } + + if (!emptyInput) { + dlg_msg.dialog({ + title: 'Изменить данные', + buttons: + {'Да': + function(){ + defer.resolve(true); + $(this).dialog('close'); + }, 'Нет': + function(){ + defer.resolve(false); + $(this).dialog('close'); + } + } + }).html('Имеются уже заполненные данные, они будут изменены, продолжить?'); + dlg_msg.dialog('open'); + + } else { + defer.resolve(true); + } + + return defer.promise(); + } + + function cleanSearchInput() { + clientSearch.val(''); + } + + function changeDataInInputs(data) { + if (data.type === "LEGAL"|| data.type === "INDIVIDUAL" ) { + if (data.name) + clientName.val(join([data.opf && data.opf.short || "", data.name.short || data.name.full], " ")); + if (data.name && data.name.full) + clientName.val(join([data.opf && data.opf.full || "", data.name.full], " ")); + clientInn.val(data.inn); + clientKpp.val(data.kpp); + clientOqrn.val(data.ogrn); + if (data.address) + clientAddress.val(data.address.value); + } else { + if (data.name && data.name.full || "") { + bankFullName.val(data.name && data.name.full || ""); + } + if (data.name && data.name.payment || "") { + bankFullName.val(data.name && data.name.payment || ""); + } + bankBic.val(data.bic); + bankCorrespondentAccount.val(data.correspondent_account); + + var advancedObj = expandObject(data, 118, "right"); + fillResultSearchBankTemplate(advancedObj); + } + } + + + function showSuggestion(suggestion) { + + var data = suggestion.data; + if (!data) return; + if (data.type === "LEGAL"|| data.type === "INDIVIDUAL" ) { + confirmChangeDataPromise(clientInputs).then(function (answer) { + if (answer) { + changeDataInInputs(data) + } + cleanSearchInput(); + }) ; + } else { + changeDataInInputs(data) + } + + } + + function fillInput(input, type, count) { + + input.suggestions({ + token: daDataExternalApiKey, + type: type, + count: count, + onSelect: showSuggestion + }); + } + + bankSearch.on('keyup',(function () { + if (bankSearch.val().length === 0) { + clearResultSearchDiv($("#searchBankResult")); + clearSearchInputs([bankFullName, bankBic, bankCorrespondentAccount]); + } + })); + + clientSearch.on('keyup',(function () { + if (clientSearch.val().length === 0) { + clearSearchInputs([clientName, clientInn, clientKpp, clientOqrn, clientAddress]); + } else { + + } + })); + + fillInput(clientSearch, "PARTY", 5); + fillInput(bankSearch, "BANK", 1); + +}); \ No newline at end of file diff --git a/project/static/js/dialogs.js b/project/static/js/dialogs.js index 316214f..19ba8d0 100644 --- a/project/static/js/dialogs.js +++ b/project/static/js/dialogs.js @@ -156,6 +156,18 @@ function update_form_fields(form, obj_values) { //------------------------------------------------------- + +function getType() { + return isOrg ? "LEGAL" : "INDIVIDUAL"; +} + + +function getPartOfPersonName(fullName, part) { + if (!fullName) return; + return fullName.split(' ')[part]; +} + + function expandObject(obj, width, align) { obj["templateColWidth"] = width; obj["templateColAlign"] = align; @@ -163,6 +175,7 @@ function expandObject(obj, width, align) { return obj } + function clearResultSearchDiv(div) { if ($(div).html().trim() !== '') { @@ -170,6 +183,7 @@ function clearResultSearchDiv(div) { } } + function clearSearchInputs(input) { if (input instanceof Array) { @@ -184,11 +198,13 @@ function clearSearchInputs(input) { } + function join(arr) { var separator = arguments.length > 1 ? arguments[1] : ", "; return arr.filter(function(n){return n}).join(separator); } + function fillResultSearchBankTemplate(obj) { var bankResultDiv = $("#searchBankResult"); diff --git a/project/static/js/profile/search-bank-external-api.js b/project/static/js/profile/search-bank-external-api.js new file mode 100644 index 0000000..8b8774d --- /dev/null +++ b/project/static/js/profile/search-bank-external-api.js @@ -0,0 +1,36 @@ +$(document).ready(function() { + var bankForm = $("#bank-account-edit-form"), + bankSearch = $("#searchBankAPI"), + bankBic = $("#id_bik", bankForm), + bankShortName = $("#id_short_name", bankForm), + bankFullName = $("#id_name", bankForm), + bankCorrespondentAccount = $("#id_korr_account", bankForm); + + function showSuggestion(suggestion) { + var data = suggestion.data; + if (!data) return; + + bankShortName.val(data.name && data.name.payment || ""); + bankFullName.val(data.name && data.name.full && data.name.payment || ""); + bankBic.val(data.bic); + bankCorrespondentAccount.val(data.correspondent_account); + + var advancedObj = expandObject(data, 110, "left"); + fillResultSearchBankTemplate(advancedObj); + } + + bankSearch.suggestions({ + token: daDataExternalApiKey, + type: "BANK", + count: 2, + onSelect: showSuggestion + }); + + bankSearch.on('keyup',(function () { + if (bankSearch.val().length === 0) { + clearResultSearchDiv($("#searchBankResult")); + clearSearchInputs([bankFullName, bankShortName, bankBic, bankCorrespondentAccount]); + } + })); + +}); \ No newline at end of file diff --git a/project/static/js/profile/search-main-external-api.js b/project/static/js/profile/search-main-external-api.js new file mode 100644 index 0000000..cb4797f --- /dev/null +++ b/project/static/js/profile/search-main-external-api.js @@ -0,0 +1,158 @@ +$(document).ready(function() { + + var paramsType, + profileForm = $("#profile-form"), + profileSearch = $("#searchAPI"), + profileBossName = $("#id_boss_name", profileForm), + profileBossSurname =$("#id_boss_surname", profileForm), + profileBossMiddleName =$("#id_boss_midname", profileForm), + profileInn = $("#id_inn", profileForm), + profileKpp = $("#id_kpp", profileForm), + profileOqrn = $("#id_ogrn", profileForm), + profileAddress = $("#id_address", profileForm), + profileLegalAddress = $("#id_jur_address", profileForm), + profilePostalAddress = $("#id_real_address", profileForm), + profileInputs = []; + + + function confirmChangeDataPromise(inputArray) { + var dlg_msg = $('#dialog-message'); + var emptyInput = true; + var defer = $.Deferred(); + + for (var i = inputArray.length - 1; i >= 0; --i) { + + if (inputArray[i].val()) { + emptyInput = false; + break; + } + } + + if (!emptyInput) { + dlg_msg.dialog({ + title: 'Изменить данные', + buttons: + {'Да': + function(){ + defer.resolve(true); + $(this).dialog('close'); + }, 'Нет': + function(){ + defer.resolve(false); + $(this).dialog('close'); + } + } + }).html('Имеются уже заполненные данные, они будут изменены, продолжить?'); + dlg_msg.dialog('open'); + + } else { + defer.resolve(true); + } + + return defer.promise(); + } + + function cleanSearchInput() { + profileSearch.val(''); + } + + function changeDataInInputs(data) { + + if (paramsType ==="LEGAL") { + + if (data.name) + profileOrgName.val(join([data.opf && data.opf.short || "", data.name.short || data.name.full], " ")); + if (data.name && data.name.full) + profileOrgFullName.val(join([data.opf && data.opf.full || "", data.name.full], " ")); + if (data.address) { + profileAddress.val(data.address.value); + profileLegalAddress.val(data.address.value); + } + + if (data.management) { + + var management = data.management; + if (management.post) {profileBossTitle.val(data.management.post)} + if (management.name) { + var fullName = management.name; + profileBossSurname.val(getPartOfPersonName(fullName, 0)); + profileBossName.val(getPartOfPersonName(fullName, 1)); + profileBossMiddleName.val(getPartOfPersonName(fullName, 2)); + } + } + + } else { + + if (data.name && data.name.full) { + var profileFullName = data.name.full; + profileBossSurname.val(getPartOfPersonName(profileFullName, 0)); + profileBossName.val(getPartOfPersonName(profileFullName, 1)); + profileBossMiddleName.val(getPartOfPersonName(profileFullName, 2)); + } + + if (data.address) { + var address = data.address.value; + profileAddress.val(address); + profileLegalAddress.val(address); + profilePostalAddress.val(address); + } + + if (data.state && data.state.registration_date) { + profileRegDate.val((new Date(data.state.registration_date)).toLocaleDateString()) + } + } + + profileInn.val(data.inn); + profileKpp.val(data.kpp); + profileOqrn.val(data.ogrn); + } + + function showSuggestion(suggestion) { + + var data = suggestion.data; + if (!data) return; + + confirmChangeDataPromise(profileInputs).then(function (answer) { + if (answer) { + changeDataInInputs(data) + } + cleanSearchInput(); + }) ; + } + + paramsType = getType(); + + if (paramsType === "LEGAL") { + + var profileOrgName = $('#id_name', profileForm), + profileOrgFullName = $('#id_full_name', profileForm), + profileBossTitle = $('#id_boss_title', profileForm); + + profileInputs = [ + profileOrgName, profileOrgFullName, profileInn, profileKpp, + profileOqrn, profileBossName, profileBossSurname, profileBossMiddleName, + profileAddress, profileLegalAddress + ]; + + } else { + + var profileRegDate = $('#id_ip_reg_date', profileForm); + + profileInputs = [ + profileBossName, profileBossSurname, profileBossMiddleName, + profileInn, profileKpp, profileOqrn, profileRegDate, + profileAddress, profileLegalAddress, profilePostalAddress + ]; + } + + profileSearch.suggestions({ + token: daDataExternalApiKey, + type: "PARTY", + params: { + type: paramsType + }, + count: 5, + onSelect: showSuggestion + }); + +}); \ No newline at end of file diff --git a/project/templates/customer/clients/list.html b/project/templates/customer/clients/list.html index 0332f24..252cf77 100644 --- a/project/templates/customer/clients/list.html +++ b/project/templates/customer/clients/list.html @@ -47,173 +47,11 @@ {% block js %} - + - - - -{# TODO: to carry out #} - -{% verbatim %} - -{% endverbatim %} + {% include 'hbs/bank-tpl.html' %} {% endblock %} diff --git a/project/templates/customer/profile/edit.html b/project/templates/customer/profile/edit.html index 653161a..a081fdb 100644 --- a/project/templates/customer/profile/edit.html +++ b/project/templates/customer/profile/edit.html @@ -548,7 +548,6 @@ - + + - - - - - {# TODO: to carry out #} - -{% verbatim %} - -{% endverbatim %} + {% include 'hbs/bank-tpl.html' %} {% endblock js %} \ No newline at end of file diff --git a/project/templates/hbs/bank-tpl.html b/project/templates/hbs/bank-tpl.html new file mode 100644 index 0000000..92463ea --- /dev/null +++ b/project/templates/hbs/bank-tpl.html @@ -0,0 +1,28 @@ +{% verbatim %} + +{% endverbatim %} \ No newline at end of file