Skip to content

Commit

Permalink
Ограничение по городу и подсказки по ФИО
Browse files Browse the repository at this point in the history
- При смене «населённого пункта» на поле «адрес» накладываются соответствующие ограничения
- Для физлиц работают подсказки по ФИО, для юрлиц — по организациям
  • Loading branch information
nalgeon authored Nov 8, 2017
1 parent 75ac5b9 commit aab7538
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,61 @@
showField("shipping_address_zip", address.postal_code);
}

function listenCityChange() {
var $cityKladr = $("#shipping_address_full_locality_name");
$cityKladr.on("change", onCityChange);
// initial check on page load
onCityChange();
}

function onCityChange() {
var kladrSelector = "[name='shipping_address[kladr_json]']";
var kladr_id = null;
try {
var kladr = JSON.parse($(kladrSelector).val());
kladr_id = kladr.kladr_code || kladr.code;
} catch (e) {
// do nothing
}
enforceCity(kladr_id);
}

function enforceCity(kladr_id) {
clearField("shipping_address_address");
clearField("shipping_address_zip");
var sgt = $("#shipping_address_address").suggestions();
if (kladr_id) {
setLocations(sgt, kladr_id);
} else {
clearLocations(sgt);
}
}

function setLocations(sgt, kladr_id) {
sgt.setOptions({
constraints: {
locations: { kladr_id: kladr_id }
},
restrict_value: true
});
}

function clearLocations(sgt) {
sgt.setOptions({
constraints: {
locations: null
},
restrict_value: false
});
}

$(function () {
init("client_name", "PARTY", showParty, clearParty);
init("tabs-organization #client_name", "PARTY", showParty, clearParty);
init("tabs-person #client_name", "NAME", pass, pass);
init("client_bank_name", "BANK", showBank, clearBank);
init("shipping_address_address", "ADDRESS", showAddress, clearAddress);
listenCityChange();
init("client_email", "EMAIL", pass, pass, { suggest_local: false });
});

}(window.jQuery, window.DADATA_TOKEN))

0 comments on commit aab7538

Please sign in to comment.