You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
446 lines
14 KiB
446 lines
14 KiB
var $specSelects = $('.-spec-select')
|
|
|
|
var specSelectOptions = {
|
|
language: 'ru',
|
|
//minimumInputLength: 1, // Commented out to immediately load remote data
|
|
placeholder: '', // Required by `allowClear`
|
|
allowClear: true,
|
|
|
|
ajax: {
|
|
url: '/api/specializations/',
|
|
dataType: 'json',
|
|
quietMillis: 250,
|
|
cache: true,
|
|
|
|
data: function(term, page) {
|
|
return {
|
|
'name__icontains': term,
|
|
page: page,
|
|
}
|
|
},
|
|
|
|
results: function(data, page) {
|
|
return {
|
|
results: _.map(function(item) {
|
|
return {
|
|
text: item.name,
|
|
id: item.id,
|
|
origItem: item,
|
|
}
|
|
}, data.results),
|
|
|
|
more: (page * 10) < data.count,
|
|
}
|
|
},
|
|
},
|
|
|
|
initSelection: function(element, callback) {
|
|
var id = $(element).val()
|
|
|
|
if (id !== '') {
|
|
$.ajax({url: '/api/specializations/' + id + '/', method: 'GET', dataType: 'json'})
|
|
.then(function(data) {callback(data)})
|
|
}
|
|
}
|
|
}
|
|
|
|
$specSelects.select2(specSelectOptions)
|
|
|
|
var chosenSpecId = $('#chosenSpecId').val()
|
|
|
|
if (chosenSpecId)
|
|
updateSpecializationWidgets(chosenSpecId)
|
|
|
|
$specSelects.on('change', function($evt) {
|
|
var specId = $evt.added.id
|
|
updateSpecializationWidgets(specId)
|
|
})
|
|
|
|
|
|
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
|
|
|
|
$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(specId)
|
|
})
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------
|
|
|
|
|
|
|
|
var $locationSelects = $('.-location-select')
|
|
|
|
var locationSelectOptions = {
|
|
language: 'ru',
|
|
placeholder: '', // Required by `allowClear`
|
|
allowClear: true,
|
|
}
|
|
|
|
|
|
var $countrySelect = $('.-location-select-country')
|
|
var $regionSelect = $('.-location-select-region')
|
|
var $citySelect = $('.-location-select-city')
|
|
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
|
|
|
|
$('#realtyId').on('change', function($evt) {
|
|
var realtyId = Number($(this).val())
|
|
|
|
if (realtyId) {
|
|
loadRealtyDetails(realtyId).then(function(res) {
|
|
$('#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)
|
|
})
|
|
} else {
|
|
$('#realtyName').val('')
|
|
$('#realtyBuildingClassificationId').val('').change()
|
|
$('#realtyConstructionTypeId').val('').change()
|
|
//$('#realtyLocationId').val('').change() // updateLocationWidgets(null)
|
|
}
|
|
})
|
|
|
|
function loadRealtyDetails(realtyId) {
|
|
return $.ajax({
|
|
url: '/api/realties/' + realtyId + '/',
|
|
method: 'GET',
|
|
dataType: 'json',
|
|
})
|
|
.then(function(res) {return res})
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// File uploading ---------------------------------------
|
|
|
|
|
|
var $fileUploadContainer = $('#fileUploadContainer')
|
|
|
|
$('#fileUploadAddBtn').on('click', function($evt) {
|
|
$fileUploadContainer.find('.file-upload-widget').last().children('.file-upload-input').click()
|
|
})
|
|
|
|
$fileUploadContainer.on('change', '.file-upload-input', function($evt) {
|
|
var $fileInput = $(this)
|
|
var $fileUploadWidget = $fileInput.parent('.file-upload-widget')
|
|
var filePath = $fileInput.val().replace(/\\/g, '/')
|
|
var fileName = path.basename(filePath)
|
|
//var fileExt = path.extname(filePath)
|
|
var fileSize = $fileInput.get(0).files && humanFileSize($fileInput.get(0).files[0].size)
|
|
|
|
if (fileName) {
|
|
$fileUploadWidget.children('.file-upload-label').text(fileName + ' ' + fileSize)
|
|
|
|
var $newFileUploadWidget = $fileUploadWidget.clone()
|
|
$newFileUploadWidget.children('.file-upload-label').text('')
|
|
|
|
$fileUploadContainer.children('ul').first().append($newFileUploadWidget)
|
|
|
|
$fileUploadWidget.css('display', 'block')
|
|
}
|
|
})
|
|
|
|
$fileUploadContainer.on('click', '.file-upload-remove-btn', function($evt) {
|
|
var $btn = $(this)
|
|
$btn.parent('.file-upload-widget').remove()
|
|
})
|
|
|
|
$fileUploadContainer.on('click', '.existing-file-remove-btn', function($evt) {
|
|
var $btn = $(this)
|
|
$btn.parent('.existing-file-widget').remove()
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helpers ---------------------------------------------
|
|
|
|
|
|
function getSpecializationTree(specId) {
|
|
var specLevel1, specLevel2, specLevel3, specLevel4
|
|
specLevel1 = specLevel2 = specLevel3 = 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,
|
|
}
|
|
} else if (spec.level === 2) {
|
|
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,
|
|
}
|
|
})
|
|
} else if (spec.level === 3) {
|
|
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,
|
|
}
|
|
})
|
|
})
|
|
} 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,
|
|
}
|
|
})
|
|
})
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
// }
|
|
// })
|
|
//}
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Utils -----------------------------------------------
|
|
|
|
|
|
function humanFileSize(bytes, si) {
|
|
var thresh = si ? 1000 : 1024
|
|
|
|
if (Math.abs(bytes) < thresh)
|
|
return bytes + ' B'
|
|
|
|
var units = si
|
|
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
|
|
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
|
|
|
|
var u = -1
|
|
|
|
do {
|
|
bytes /= thresh
|
|
++u
|
|
} while (Math.abs(bytes) >= thresh && u < units.length-1)
|
|
|
|
return bytes.toFixed(1) + ' ' + units[u]
|
|
}
|
|
|