From fa410dcff628142eefa98d9df8d4baafb6449b3b Mon Sep 17 00:00:00 2001 From: ArturBaybulatov Date: Fri, 8 Jul 2016 15:30:52 +0300 Subject: [PATCH] #ARC-36 #ARC-12 --- assets/index.js | 237 +++++++++--------- .../templates/customer_project_create.html | 2 +- projects/templates/customer_project_edit.html | 21 +- 3 files changed, 133 insertions(+), 127 deletions(-) diff --git a/assets/index.js b/assets/index.js index a3ba716..896f1fa 100644 --- a/assets/index.js +++ b/assets/index.js @@ -106,23 +106,22 @@ var $citySelect = $('.-location-select-city') // Initialize: -getLocationTree(31).then(function(locs) { +getLocationTree(null).then(function(locs) { $countrySelect.select2(_.merge(locationSelectOptions, {data: locs.countries})) $regionSelect.select2(_.merge(locationSelectOptions, {data: locs.regions})) $citySelect.select2(_.merge(locationSelectOptions, {data: locs.cities})) + + var chosenLocId = $('#chosenLocationId').val() + + if (chosenLocId) + updateLocationWidgets(chosenLocId) }) -//var chosenLocId = $('#chosenLocationId').val() -// -//if (chosenLocId) -// updateLocationWidgets(chosenLocId) -// -// -//$locationSelects.on('change', function($evt) { -// var locId = $evt.added.id -// updateLocationWidgets(locId) -//}) +$locationSelects.on('change', function($evt) { + var locId = $evt.added.id + updateLocationWidgets(locId) +}) function updateLocationWidgets(locId) { @@ -131,7 +130,21 @@ function updateLocationWidgets(locId) { $regionSelect.select2({data: locs.regions}) $citySelect.select2({data: locs.cities}) - $('#chosenLocationId').val(locId) + var loc = locs.location + + if (loc && loc.level === 1) { + $countrySelect.select2('val', locs.country.id) + } else if (loc && loc.level === 2) { + $regionSelect.select2('val', locs.region.id) + $countrySelect.select2('val', locs.country.id) + } else if (loc && loc.level === 3) { + $citySelect.select2('val', locs.city.id) + $regionSelect.select2('val', locs.region.id) + $countrySelect.select2('val', locs.country.id) + } + + if (loc) + $('#chosenLocationId').val(loc.id) }) } @@ -145,13 +158,13 @@ $('#realtyId').on('change', function($evt) { $('#realtyName').val(res.name) $('#realtyBuildingClassificationId').val(res.building_classification.id).change() $('#realtyConstructionTypeId').val(res.construction_type.id).change() - //$('#realtyLocationId').val(res.location.id).change() // updateLocationWidgets(res.location.id) + updateLocationWidgets(res.location.id) }) } else { $('#realtyName').val('') $('#realtyBuildingClassificationId').val('').change() $('#realtyConstructionTypeId').val('').change() - //$('#realtyLocationId').val('').change() // updateLocationWidgets(null) + updateLocationWidgets(null) } }) @@ -228,74 +241,65 @@ $fileUploadContainer.on('click', '.existing-file-remove-btn', function($evt) { function getSpecializationTree(specId) { - var specLevel1, specLevel2, specLevel3, specLevel4 - specLevel1 = specLevel2 = specLevel3 = specLevel4 = null + var specs = { + specLevel1: null, specLevel2: null, specLevel3: null, specLevel4: null, + } return $.ajax({url: '/api/specializations/' + specId + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { if (spec.level === 1) { - specLevel1 = spec - - return { - specLevel1: specLevel1, - specLevel2: specLevel2, - specLevel3: specLevel3, - specLevel4: specLevel4, - } + return _.merge(specs, {specLevel1: spec}) } else if (spec.level === 2) { - specLevel2 = spec + var specLevel2 = spec return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel1 = spec + var specLevel1 = spec - return { + return _.merge(specs, { specLevel1: specLevel1, specLevel2: specLevel2, - specLevel3: specLevel3, - specLevel4: specLevel4, - } + }) }) } else if (spec.level === 3) { - specLevel3 = spec + var specLevel3 = spec return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel2 = spec + var specLevel2 = spec return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel1 = spec + var specLevel1 = spec - return { + return _.merge(specs, { specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, - specLevel4: specLevel4, - } + }) }) }) } else if (spec.level === 4) { - specLevel4 = spec + var specLevel4 = spec return $.ajax({url: '/api/specializations/' + specLevel4.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel3 = spec + var specLevel3 = spec return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel2 = spec + var specLevel2 = spec return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'}) .then(function(spec) { - specLevel1 = spec + var specLevel1 = spec - return { + return _.merge(specs, { specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4, - } + }) }) }) }) @@ -313,6 +317,7 @@ function getSpecializationTree(specId) { function getLocationTree(locId) { var locations = { + location: null, country: null, region: null, city: null, countries: [], regions: [], cities: [], } @@ -320,94 +325,92 @@ function getLocationTree(locId) { if (locId === null) { return $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}) .then(function(res) { - countries = _.map(function(item) { - return { - text: item.name, - id: item.id, - origItem: item, - } - }, res.results) - - return _.merge(locations, {countries: countries}) + return _.merge(locations, { + countries: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, res.results), + }) }) } else { return $.ajax({url: '/api/locations/' + locId + '/', method: 'GET', dataType: 'json'}).then(function(loc) { + locations = _.merge(locations, {location: loc}) + if (loc.level === 1) { - country = loc + var country = loc return $.when( - $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}), + $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}), // Countries + $.ajax({url: '/api/locations/?level=2&parent=' + country.id, method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}) // Regions ) - .then(function() { - countries = _.map(function(item) { - return { - text: item.name, - id: item.id, - origItem: item, - } - }, arguments[0][0].results) // WTF???? - - regions = _.map(function(item) { - return { - text: item.name, - id: item.id, - origItem: item, - } - }, arguments[1][0].results) // WTF either??????? - + .then(function(countries, regions) { + return _.merge(locations, { + country: country, + countries: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, countries), + regions: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, regions), + }) + }) + } else if (loc.level === 2) { + var region = loc + + return $.when( + $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}), // Countries + + $.ajax({url: '/api/locations/?level=2&parent=' + region.parent.id, method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}), // Regions + + $.ajax({url: '/api/locations/' + region.parent.id + '/', method: 'GET', dataType: 'json'}) + .then(function(res) {return res}), // Country + + $.ajax({url: '/api/locations/?level=3&parent=' + region.id, method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}) // Cities + ) + .then(function(countries, regions, country, cities) { return _.merge(locations, { country: country, - countries: countries, - regions: regions, + region: region, + countries: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, countries), + regions: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, regions), + cities: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, cities), }) }) + } else if (loc.level === 3) { + var city = loc + + return $.when( + $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}), // Countries + + $.ajax({url: '/api/locations/' + city.parent.id + '/', method: 'GET', dataType: 'json'}) + .then(function(res) {return res}), // Region + + $.ajax({url: '/api/locations/?level=3&parent=' + city.parent.id, method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}) // Cities + ) + .then(function(countries, region, cities) { + locations = _.merge(locations, { + region: region, + city: city, + countries: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, countries), + cities: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, cities), + }) + + return $.when( + $.ajax({url: '/api/locations/?level=2&parent=' + region.parent.id, method: 'GET', dataType: 'json'}) + .then(function(res) {return res.results}), // Regions + + $.ajax({url: '/api/locations/' + region.parent.id + '/', method: 'GET', dataType: 'json'}) + .then(function(res) {return res}) // Country + ) + .then(function(regions, country) { + return _.merge(locations, { + country: country, + regions: _.map(function(item) {return {text: item.name, id: item.id, origItem: item}}, regions), + }) + }) + }) } - //else if (loc.level === 2) { - // region = loc - // - // return $.ajax({url: '/api/locations/?level=2', method: 'GET', dataType: 'json'}) - // .then(function(res) { - // regions = _.map(function(item) { - // return { - // text: item.name, - // id: item.id, - // origItem: item, - // } - // }, res.results) - // - // return { - // country: country, - // countries: countries, - // region: region, - // regions: regions, - // city: city, - // cities: cities, - // } - // }) - //} else if (loc.level === 3) { - // city = loc - // - // return $.ajax({url: '/api/locations/?level=3', method: 'GET', dataType: 'json'}) - // .then(function(res) { - // cities = _.map(function(item) { - // return { - // text: item.name, - // id: item.id, - // origItem: item, - // } - // }, res.results) - // - // return { - // country: country, - // countries: countries, - // region: region, - // regions: regions, - // city: city, - // cities: cities, - // } - // }) - //} }) } } diff --git a/projects/templates/customer_project_create.html b/projects/templates/customer_project_create.html index 83fbdf0..6452d41 100644 --- a/projects/templates/customer_project_create.html +++ b/projects/templates/customer_project_create.html @@ -264,7 +264,7 @@ - +
- +
-
+
+ +
+ +
+ +
+ + + +