@ -9,6 +9,7 @@ var STUB_IMAGE_URL = '/static/img/profile.jpg'
// Plugins init --------------------------------------------
$ ( '.datepicker' ) . datepicker ( )
$ ( '[data-tooltip]' ) . tooltip ( {
@ -25,102 +26,101 @@ $('[data-tooltip]').tooltip({
// Specialization 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 ,
}
} ,
; ( function ( ) {
var specSelectOptions = {
language : 'ru' ,
//minimumInputLength: 1, // Commented out to immediately load remote data
placeholder : 'Выберите специализацию' , // Required by `allowClear`
allowClear : true ,
results : function ( data , page ) {
return {
results : _ . map ( function ( item ) {
return {
id : item . id ,
text : item . name ,
origItem : item ,
}
} , data . results ) ,
more : ( page * API _PAGE _SIZE ) < data . count ,
}
ajax : {
url : null ,
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 {
id : item . id ,
text : item . name ,
origItem : item ,
}
} , data . results ) ,
more : ( page * API _PAGE _SIZE ) < 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 ) } )
}
}
}
$ ( '.-spec-select-container' ) . each ( function ( i , container ) {
var $container = $ ( container )
var $specSelects = $ ( container ) . find ( '.-spec-select' )
var $chosenSpecId = $container . find ( '.-chosen-spec-id' ) . first ( )
var $specSelect1 = $container . find ( '.-spec-select-level-1' ) . first ( )
var $specSelect2 = $container . find ( '.-spec-select-level-2' ) . first ( )
var $specSelect3 = $container . find ( '.-spec-select-level-3' ) . first ( )
var $specSelect4 = $container . find ( '.-spec-select-level-4' ) . first ( )
var specSelects = [ $specSelect1 , $specSelect2 , $specSelect3 , $specSelect4 ]
$ ( '.-spec-select-container' ) . each ( function ( i , container ) {
var $container = $ ( container )
var $specSelects = $container . find ( '.-spec-select' )
var $chosenSpecId = $container . find ( '.-chosen-spec-id' ) . first ( )
var $specSelect1 = $container . find ( '.-spec-select-level-1' ) . first ( )
var $specSelect2 = $container . find ( '.-spec-select-level-2' ) . first ( )
var $specSelect3 = $container . find ( '.-spec-select-level-3' ) . first ( )
var $specSelect4 = $container . find ( '.-spec-select-level-4' ) . first ( )
var specSelects = [ $specSelect1 , $specSelect2 , $specSelect3 , $specSelect4 ]
reinitSpecializationsByWorkType ( $specSelects , null )
var chosenSpecId = $chosenSpecId . val ( )
if ( chosenSpecId )
updateSpecializationWidgets ( chosenSpecId , $container , $chosenSpecId , specSelects )
$specSelects . on ( 'change' , function ( $evt ) {
var specId = $evt . added ? $evt . added . id : null
updateSpecializationWidgets ( specId , $container , $chosenSpecId , specSelects )
} )
} )
$specSelects . select2 ( specSelectOptions )
function reinitSpecializationsByWorkType ( $specSelects , workTypeId ) {
$ . get ( '/api/specializations/?parent__name=_root' )
. then ( function ( res ) {
var rootSpecs = res . results
var rootSpec = workTypeId == null ? rootSpecs [ 0 ] : ( { 1 : rootSpecs [ 0 ] , 2 : rootSpecs [ 1 ] , 3 : rootSpecs [ 2 ] } ) [ workTypeId ]
$specSelects . select2 ( _ . merge ( specSelectOptions , {
ajax : { url : format ( '/api/specializations/?lft__gte=%s&rght__lte=%s' , rootSpec . lft , rootSpec . rght ) } ,
} ) )
} )
}
var chosenSpecId = $chosenSpecId . val ( )
if ( chosenSpecId )
updateSpecializationWidgets ( chosenSpecId , $container , $chosenSpecId , specSelects )
function updateSpecializationWidgets ( specId , $container , $chosenSpecId , specSelects ) {
return getSpecializationTree ( specId ) . then ( function ( specs ) {
var specLevel1 = specs . specLevel1
var specLevel2 = specs . specLevel2
var specLevel3 = specs . specLevel3
var specLevel4 = specs . specLevel4
specSelects [ 0 ] . select2 ( 'data' , specLevel1 ? { id : specLevel1 . id , text : specLevel1 . name , origItem : specLevel1 } : null )
specSelects [ 1 ] . select2 ( 'data' , specLevel2 ? { id : specLevel2 . id , text : specLevel2 . name , origItem : specLevel2 } : null )
specSelects [ 2 ] . select2 ( 'data' , specLevel3 ? { id : specLevel3 . id , text : specLevel3 . name , origItem : specLevel3 } : null )
specSelects [ 3 ] . select2 ( 'data' , specLevel4 ? { id : specLevel4 . id , text : specLevel4 . name , origItem : specLevel4 } : null )
$chosenSpecId . val ( specId )
} )
}
$specSelects . on ( 'change' , function ( $evt ) {
var specId = $evt . added ? $evt . added . id : null
updateSpecializationWidgets ( specId , $container , $chosenSpecId , specSelects )
} )
} )
function updateSpecializationWidgets ( specId , $container , $chosenSpecId , specSelects ) {
return getSpecializationTree ( specId ) . then ( function ( specs ) {
var specLevel1 = specs . specLevel1
var specLevel2 = specs . specLevel2
var specLevel3 = specs . specLevel3
var specLevel4 = specs . specLevel4
specSelects [ 0 ] . select2 ( 'data' , specLevel1 ? { id : specLevel1 . id , text : specLevel1 . name , origItem : specLevel1 } : null )
specSelects [ 1 ] . select2 ( 'data' , specLevel2 ? { id : specLevel2 . id , text : specLevel2 . name , origItem : specLevel2 } : null )
specSelects [ 2 ] . select2 ( 'data' , specLevel3 ? { id : specLevel3 . id , text : specLevel3 . name , origItem : specLevel3 } : null )
specSelects [ 3 ] . select2 ( 'data' , specLevel4 ? { id : specLevel4 . id , text : specLevel4 . name , origItem : specLevel4 } : null )
$chosenSpecId . val ( specId )
} )
}
window . reinitSpecializationsByWorkType = reinitSpecializationsByWorkType
} ( ) )
@ -134,67 +134,77 @@ function updateSpecializationWidgets(specId, $container, $chosenSpecId, specSele
// Specialization select (simple) -------------------------------
var simpleSpecSelectsOptions = {
language : 'ru' ,
placeholder : 'Выберите специализацию' , // Required by `allowClear`
allowClear : true ,
}
var $simpleSpecContainer = $ ( '#simpleSpecContainer' )
var $emptySimpleSpecWidget = $simpleSpecContainer . find ( '.-simple-spec-widget' ) . first ( )
$simpleSpecContainer . find ( '.-chosen-simple-spec-id' ) . each ( function ( i , el ) {
var $el = $ ( el )
var specId = Number ( $el . val ( ) )
; ( function ( ) {
var simpleSpecSelectsOptions = {
language : 'ru' ,
placeholder : 'Выберите специализацию' , // Required by `allowClear`
allowClear : true ,
//initSelection: function(element, callback) {
// var id = $(element).val()
//
// if (id !== '') {
// $.ajax({url: '/api/specializations/' + id + '/', method: 'GET', dataType: 'json'})
// .then(function(data) {callback(data)})
// }
//}
}
if ( specId )
initSimpleSpecSelect2 ( $el . closest ( '.-simple-spec-widget' ) . find ( '.-simple-spec-select' ) . first ( ) , specId )
} )
$ ( '#addSpec' ) . on ( 'click' , function ( $evt ) {
var $newSimpleSpecWidget = $emptySimpleSpecWidget . clone ( )
$simpleSpecContainer . append ( $newSimpleSpecWidget )
var $newSimpleSpecSelect = $newSimpleSpecWidget . find ( '.-simple-spec-select' ) . first ( )
var $simpleSpecContainer = $ ( '#simpleSpecContainer' )
var $emptySimpleSpecWidget = $simpleSpecContainer . find ( '.-simple-spec-widget' ) . first ( )
initSimpleSpecSelect2 ( $newSimpleSpecSelect ) . then ( function ( ) {
$newSimpleSpecWidget . css ( 'display' , 'block' )
$simpleSpecContainer . find ( '.-chosen-simple-spec-id' ) . each ( function ( i , el ) {
var $el = $ ( el )
var specId = Number ( $el . val ( ) )
if ( specId )
initSimpleSpecSelect2 ( $el . closest ( '.-simple-spec-widget' ) . find ( '.-simple-spec-select' ) . first ( ) , specId )
} )
} )
$simpleSpecContainer . on ( 'change' , '.-simple-spec-select' , function ( $evt ) {
if ( $evt . added )
$ ( this ) . parent ( ) . children ( '.-chosen-simple-spec-id' ) . first ( ) . val ( $evt . added . id )
} )
function initSimpleSpecSelect2 ( $select , specId ) {
return $ . ajax ( { url : '/api/specializations/' , method : 'GET' , dataType : 'json' } )
. then ( function ( res ) {
var specs = res . results
$select . select2 ( _ . merge ( simpleSpecSelectsOptions , {
data : _ . map ( function ( spec ) {
return {
$ ( '#addSpec' ) . on ( 'click' , function ( $evt ) {
var $newSimpleSpecWidget = $emptySimpleSpecWidget . clone ( )
$simpleSpecContainer . append ( $newSimpleSpecWidget )
var $newSimpleSpecSelect = $newSimpleSpecWidget . find ( '.-simple-spec-select' ) . first ( )
initSimpleSpecSelect2 ( $newSimpleSpecSelect ) . then ( function ( ) {
$newSimpleSpecWidget . css ( 'display' , 'block' )
} )
} )
$simpleSpecContainer . on ( 'change' , '.-simple-spec-select' , function ( $evt ) {
if ( $evt . added )
$ ( this ) . parent ( ) . children ( '.-chosen-simple-spec-id' ) . first ( ) . val ( $evt . added . id )
} )
function initSimpleSpecSelect2 ( $select , specId ) {
return $ . ajax ( { url : '/api/specializations/' , method : 'GET' , dataType : 'json' } )
. then ( function ( res ) {
var specs = res . results
$select . select2 ( _ . merge ( simpleSpecSelectsOptions , {
data : _ . map ( function ( spec ) {
return {
id : spec . id ,
text : _ . repeat ( spec . level - 1 , '---' ) + spec . name ,
origItem : spec ,
}
} , specs ) ,
} ) )
if ( specId ) {
var spec = _ . find ( { id : specId } , specs )
$select . select2 ( 'data' , {
id : spec . id ,
text : _ . repeat ( spec . level - 1 , '---' ) + spec . name ,
origItem : spec ,
}
} , specs ) ,
} ) )
if ( specId ) {
var spec = _ . find ( { id : specId } , specs )
$select . select2 ( 'data' , {
id : spec . id ,
text : _ . repeat ( spec . level - 1 , '---' ) + spec . name ,
origItem : spec ,
} )
}
} )
}
} )
}
} )
}
} ( ) )
@ -208,46 +218,49 @@ function initSimpleSpecSelect2($select, specId) {
// Team invitation contractor select -------------------------------
var contractorSelectOptions = {
language : 'ru' ,
placeholder : 'Выберите пользователя' , // Required by `allowClear`
allowClear : true ,
ajax : {
url : '/api/users/' ,
dataType : 'json' ,
quietMillis : 250 ,
cache : true ,
data : function ( term , page ) {
return {
username _ _icontains : term ,
page : page ,
is _contractor : 'true' ,
}
} ,
; ( function ( ) {
var contractorSelectOptions = {
language : 'ru' ,
placeholder : 'Выберите пользователя' , // Required by `allowClear`
allowClear : true ,
results : function ( data , page ) {
return {
results : _ . map ( function ( item ) {
return {
id : item . id ,
text : format ( '%s (%s)' , item . username , item . get _full _name ) ,
origItem : item ,
}
} , data . results ) ,
more : ( page * API _PAGE _SIZE ) < data . count ,
}
ajax : {
url : '/api/users/' ,
dataType : 'json' ,
quietMillis : 250 ,
cache : true ,
data : function ( term , page ) {
return {
username _ _icontains : term ,
page : page ,
is _contractor : 'true' ,
}
} ,
results : function ( data , page ) {
return {
results : _ . map ( function ( item ) {
return {
id : item . id ,
text : format ( '%s (%s)' , item . username , item . get _full _name ) ,
origItem : item ,
}
} , data . results ) ,
more : ( page * API _PAGE _SIZE ) < data . count ,
}
} ,
} ,
} ,
}
function initContractorSelect ( $select , excludeIds ) {
contractorSelectOptions . ajax . url = format ( '%s?id__in!=%s' , contractorSelectOptions . ajax . url , excludeIds . join ( ',' ) )
return $select . select2 ( contractorSelectOptions )
}
}
function initContractorSelect ( $select , excludeIds ) {
contractorSelectOptions . ajax . url = format ( '%s?id__in!=%s' , contractorSelectOptions . ajax . url , excludeIds . join ( ',' ) )
return $select . select2 ( contractorSelectOptions )
}
window . initContractorSelect = initContractorSelect
} ( ) )
@ -303,92 +316,93 @@ function initContractorSelect($select, excludeIds) {
// Location select ----------------------------------------------
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 ( 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 } ) )
; ( function ( ) {
var $locationSelects = $ ( '.-location-select' )
var chosenLocId = $ ( '#chosenLocationId' ) . val ( )
var locationSelectOptions = {
language : 'ru' ,
placeholder : 'Выберите местоположение' , // Required by `allowClear`
allowClear : true ,
}
if ( chosenLocId )
updateLocationWidgets ( chosenLocId )
} )
$locationSelects . on ( 'change' , function ( $evt ) {
updateLocationWidgets ( $evt . added ? $evt . added . id : null )
} )
function updateLocationWidgets ( locId ) {
return getLocationTree ( locId ) . then ( function ( locs ) {
$countrySelect . select2 ( { data : locs . countries } )
$regionSelect . select2 ( { data : locs . regions } )
$citySelect . select2 ( { data : locs . cities } )
var loc = locs . location
var $countrySelect = $ ( '.-location-select-country' )
var $regionSelect = $ ( '.-location-select-region' )
var $citySelect = $ ( '.-location-select-city' )
// Initialize:
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 } ) )
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 )
}
var chosenLocId = $ ( '#chosenLocationId' ) . val ( )
if ( loc )
$ ( '#chosenLocationId' ) . val ( loc . i d)
if ( chosenLocId )
updateLocationWidgets ( chosenLocId )
} )
}
$ ( '#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 ( )
updateLocationWidgets ( res . location . id )
$locationSelects . on ( 'change' , function ( $evt ) {
updateLocationWidgets ( $evt . added ? $evt . added . id : null )
} )
function updateLocationWidgets ( locId ) {
return getLocationTree ( locId ) . then ( function ( locs ) {
$countrySelect . select2 ( { data : locs . countries } )
$regionSelect . select2 ( { data : locs . regions } )
$citySelect . select2 ( { data : locs . cities } )
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 )
} )
} else {
$ ( '#realtyName' ) . val ( '' )
$ ( '#realtyBuildingClassificationId' ) . val ( '' ) . change ( )
$ ( '#realtyConstructionTypeId' ) . val ( '' ) . change ( )
updateLocationWidgets ( null )
}
} )
function loadRealtyDetails ( realtyId ) {
return $ . ajax ( {
url : '/api/realties/' + realtyId + '/' ,
method : 'GET' ,
dataType : 'json' ,
$ ( '#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 ( )
updateLocationWidgets ( res . location . id )
} )
} else {
$ ( '#realtyName' ) . val ( '' )
$ ( '#realtyBuildingClassificationId' ) . val ( '' ) . change ( )
$ ( '#realtyConstructionTypeId' ) . val ( '' ) . change ( )
updateLocationWidgets ( null )
}
} )
. then ( function ( res ) { return res } )
}
function loadRealtyDetails ( realtyId ) {
return $ . ajax ( {
url : '/api/realties/' + realtyId + '/' ,
method : 'GET' ,
dataType : 'json' ,
} )
. then ( function ( res ) { return res } )
}
} ( ) )
@ -404,43 +418,43 @@ function loadRealtyDetails(realtyId) {
// TODO: Add file number and overall size limit support
var $fileUploadContainer = $ ( '#fileUploadContainer' )
$ ( '#fileUploadAddBtn' ) . on ( 'click' , function ( $evt ) {
$fileUploadContainer . find ( '.file-upload-widget' ) . last ( ) . find ( '.file-upload-input' ) . click ( )
} )
$fileUploadContainer . on ( 'change' , '.file-upload-input' , function ( $evt ) {
var $fileInput = $ ( this )
var $fileUploadWidget = $fileInput . closest ( '.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 )
; ( function ( ) {
var $fileUploadContainer = $ ( '#fileUploadContainer' )
if ( fileName ) {
$fileUploadWidget . find ( '.file-upload-label' ) . text ( fileName + ' ' + fileSize )
var $newFileUploadWidget = $fileUploadWidget . clone ( )
$newFileUploadWidget . find ( '.file-upload-label' ) . text ( '' )
$fileUploadContainer . find ( 'ul' ) . first ( ) . append ( $newFileUploadWidget )
$ ( '#fileUploadAddBtn' ) . on ( 'click' , function ( $evt ) {
$fileUploadContainer . find ( '.file-upload-widget' ) . last ( ) . find ( '.file-upload-input' ) . click ( )
} )
$fileUploadContainer . on ( 'change' , '.file-upload-input' , function ( $evt ) {
var $fileInput = $ ( this )
var $fileUploadWidget = $fileInput . closest ( '.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 )
$fileUploadWidget . css ( 'display' , 'block' )
}
} )
$fileUploadContainer . on ( 'click' , '.file-upload-remove-btn' , function ( $evt ) {
var $btn = $ ( this )
$btn . closest ( '.file-upload-widget' ) . remove ( )
} )
$fileUploadContainer . on ( 'click' , '.existing-file-remove-btn' , function ( $evt ) {
var $btn = $ ( this )
$btn . closest ( '.existing-file-widget' ) . remove ( )
} )
if ( fileName ) {
$fileUploadWidget . find ( '.file-upload-label' ) . text ( fileName + ' ' + fileSize )
var $newFileUploadWidget = $fileUploadWidget . clone ( )
$newFileUploadWidget . find ( '.file-upload-label' ) . text ( '' )
$fileUploadContainer . find ( 'ul' ) . first ( ) . append ( $newFileUploadWidget )
$fileUploadWidget . css ( 'display' , 'block' )
}
} )
$fileUploadContainer . on ( 'click' , '.file-upload-remove-btn' , function ( $evt ) {
var $btn = $ ( this )
$btn . closest ( '.file-upload-widget' ) . remove ( )
} )
$fileUploadContainer . on ( 'click' , '.existing-file-remove-btn' , function ( $evt ) {
var $btn = $ ( this )
$btn . closest ( '.existing-file-widget' ) . remove ( )
} )
} ( ) )