remotes/origin/setup
ArturBaybulatov 10 years ago
parent 29b9550dff
commit b603fa6c52
  1. 286
      assets/index.js

@ -46,40 +46,35 @@ var specSelectOptions = {
$specSelects.select2(specSelectOptions)
var $specSelect1 = $('.-spec-select-level-1')
var $specSelect2 = $('.-spec-select-level-2')
var $specSelect3 = $('.-spec-select-level-3')
var $specSelect4 = $('.-spec-select-level-4')
var chosenSpecId = $('#chosenSpecId').val()
if (chosenSpecId) {
$.ajax({url: '/api/specializations/' + chosenSpecId + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
return updateSpecializationWidgets(spec)
})
}
if (chosenSpecId)
updateSpecializationWidgets(chosenSpecId)
$specSelects.on('change', function($evt) {
updateSpecializationWidgets($evt.added.origItem)
var specId = $evt.added.id
updateSpecializationWidgets(specId)
})
function updateSpecializationWidgets(spec) {
return getSpecializationTree(spec).then(function(specs) {
var $specSelect1 = $('.-spec-select-level-1')
var $specSelect2 = $('.-spec-select-level-2')
var $specSelect3 = $('.-spec-select-level-3')
var $specSelect4 = $('.-spec-select-level-4')
function updateSpecializationWidgets(specId) {
return getSpecializationTree(specId).then(function(specs) {
var specLevel1 = specs.specLevel1
var specLevel2 = specs.specLevel2
var specLevel3 = specs.specLevel3
var specLevel4 = specs.specLevel4
var chosenSpecId = specs.chosenSpecId
$specSelect1.select2('data', specLevel1 ? {id: specLevel1.id, text: specLevel1.name, origItem: specLevel1} : null)
$specSelect2.select2('data', specLevel2 ? {id: specLevel2.id, text: specLevel2.name, origItem: specLevel2} : null)
$specSelect3.select2('data', specLevel3 ? {id: specLevel3.id, text: specLevel3.name, origItem: specLevel3} : null)
$specSelect4.select2('data', specLevel4 ? {id: specLevel4.id, text: specLevel4.name, origItem: specLevel4} : null)
$('#chosenSpecId').val(chosenSpecId)
$('#chosenSpecId').val(specId)
})
}
@ -99,45 +94,46 @@ var $locationSelects = $('.-location-select')
var locationSelectOptions = {
language: 'ru',
//minimumInputLength: 1, // Commented out to immediately load remote data
placeholder: '', // Required by `allowClear`
allowClear: true,
}
//$locationSelects.select2(locationSelectOptions)
var $countrySelect = $('.-location-select-country')
var $regionSelect = $('.-location-select-region')
var $citySelect = $('.-location-select-city')
var chosenLocationId = $('#chosenLocationId').val()
$countrySelect.select2(_.merge(locationSelectOptions, {
query: function(query) {
$.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'})
.then(function(res) {
query.callback({
results: _.map(function(item) {
return {
text: item.name,
id: item.id,
origItem: item,
}
}, res.results)
})
})
},
}))
// Initialize:
getLocationTree(31).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)
//
//
//$locationSelects.on('change', function($evt) {
// var locId = $evt.added.id
// updateLocationWidgets(locId)
//})
function updateLocationWidgets(locId) {
return getLocationTree(locId).then(function(locs) {
$countrySelect.select2({data: locs.countries})
$regionSelect.select2({data: locs.regions})
$citySelect.select2({data: locs.cities})
$('#chosenLocationId').val(locId)
})
}
@ -149,13 +145,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()
//$('#realtyLocationId').val(res.location.id).change() // updateLocationWidgets(res.location.id)
})
} else {
$('#realtyName').val('')
$('#realtyBuildingClassificationId').val('').change()
$('#realtyConstructionTypeId').val('').change()
//$('#realtyLocationId').val('').change()
//$('#realtyLocationId').val('').change() // updateLocationWidgets(null)
}
})
@ -231,29 +227,13 @@ $fileUploadContainer.on('click', '.existing-file-remove-btn', function($evt) {
// Helpers ---------------------------------------------
function getSpecializationTree(spec) {
function getSpecializationTree(specId) {
var specLevel1, specLevel2, specLevel3, specLevel4
specLevel1 = specLevel2 = specLevel3 = specLevel4 = null
var chosenSpecId = ''
if (spec.level === 1) {
specLevel1 = spec
chosenSpecId = spec.id
return $.when({
specLevel1: specLevel1,
specLevel2: specLevel2,
specLevel3: specLevel3,
specLevel4: specLevel4,
chosenSpecId: chosenSpecId,
})
} else if (spec.level === 2) {
specLevel2 = spec
chosenSpecId = spec.id
return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
return $.ajax({url: '/api/specializations/' + specId + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
if (spec.level === 1) {
specLevel1 = spec
return {
@ -261,15 +241,8 @@ function getSpecializationTree(spec) {
specLevel2: specLevel2,
specLevel3: specLevel3,
specLevel4: specLevel4,
chosenSpecId: chosenSpecId,
}
})
} else if (spec.level === 3) {
specLevel3 = spec
chosenSpecId = spec.id
return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
} else if (spec.level === 2) {
specLevel2 = spec
return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'})
@ -281,16 +254,9 @@ function getSpecializationTree(spec) {
specLevel2: specLevel2,
specLevel3: specLevel3,
specLevel4: specLevel4,
chosenSpecId: chosenSpecId,
}
})
})
} else if (spec.level === 4) {
specLevel4 = spec
chosenSpecId = spec.id
return $.ajax({url: '/api/specializations/' + specLevel4.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
} else if (spec.level === 3) {
specLevel3 = spec
return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET', dataType: 'json'})
@ -306,12 +272,35 @@ function getSpecializationTree(spec) {
specLevel2: specLevel2,
specLevel3: specLevel3,
specLevel4: specLevel4,
chosenSpecId: chosenSpecId,
}
})
})
})
}
} else if (spec.level === 4) {
specLevel4 = spec
return $.ajax({url: '/api/specializations/' + specLevel4.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
specLevel3 = spec
return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
specLevel2 = spec
return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET', dataType: 'json'})
.then(function(spec) {
specLevel1 = spec
return {
specLevel1: specLevel1,
specLevel2: specLevel2,
specLevel3: specLevel3,
specLevel4: specLevel4,
}
})
})
})
}
})
}
@ -322,39 +311,106 @@ function getSpecializationTree(spec) {
//function getLocationTree(loc) {
// var country, region, city, countries, regions, cities
// country = region = city = countries = regions = cities = null
//
// var chosenLocId = ''
//
// if (loc.level === 1) {
// country = loc
// chosenLocId = loc.id
//
// return $.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'})
// .then(function(res) {
// specLevel1 = spec
//
// return {
// specLevel1: specLevel1,
// specLevel2: specLevel2,
// specLevel3: specLevel3,
// specLevel4: specLevel4,
// chosenSpecId: chosenSpecId,
// }
// })
//
// return $.when({
// country: country,
// region: region,
// city: city,
// chosenLocId: chosenLocId,
// })
// }
//
// ........
//}
function getLocationTree(locId) {
var locations = {
country: null, region: null, city: null,
countries: [], regions: [], cities: [],
}
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})
})
} else {
return $.ajax({url: '/api/locations/' + locId + '/', method: 'GET', dataType: 'json'}).then(function(loc) {
if (loc.level === 1) {
country = loc
return $.when(
$.ajax({url: '/api/locations/?level=1', method: 'GET', dataType: 'json'}),
$.ajax({url: '/api/locations/?level=2&parent=' + country.id, method: 'GET', dataType: 'json'})
)
.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???????
return _.merge(locations, {
country: country,
countries: countries,
regions: 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,
// }
// })
//}
})
}
}

Loading…
Cancel
Save