// Fancy widget initializations ------------------------ $(function() { var $specSelects = $('.-spec-select') var select2Options = { 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, //createSearchChoice: function(term, data) { // if ($(data).filter(function() {return this.text.localeCompare(term) === 0}).length === 0) { // return {id: term, text: term} // } //}, 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, orig_item: item, } }, data.results), more: (page * 10) < data.count, } }, }, initSelection: function(element, callback) { var id = $(element).val() if (id !== '') { $.ajax('/api/specializations/' + id, {dataType: 'json'}) .then(function(data) {callback(data)}) } } } $specSelects.select2(select2Options) var $specSelect1 = $('.-spec-select-level-1') var $specSelect2 = $('.-spec-select-level-2') var $specSelect3 = $('.-spec-select-level-3') var $specSelect4 = $('.-spec-select-level-4') //$specSelect1.on('change', function($evt) { // var spec = $evt.added.orig_item // console.log('Select 1 changed:', spec) //}) $specSelect2.on('change', function($evt) { var spec = $evt.added.orig_item console.log('Select 2 changed:', spec) $specSelect1.select2('data', {id: spec.parent.id, text: spec.parent.name}, true) //$.ajax({url: '/api/specializations/99123/', method: 'GET'}) // .then(function(res) {console.log(res)}) // .fail(function() {console.log('Error occurred')}) }) //$specSelect3.on('change', function($evt) { // var spec = $evt.added.orig_item // console.log('Select 3 changed:', spec) //}) // //$specSelect4.on('change', function($evt) { // var spec = $evt.added.orig_item // console.log('Select 4 changed:', spec) //}) window.$specSelect1 = $specSelect1 window.$specSelect2 = $specSelect2 window.$specSelect3 = $specSelect3 window.$specSelect4 = $specSelect4 }) //function select2_search ($el, term) { // $el.select2('open'); // // // Get the search box within the dropdown or the selection // // Dropdown = single, Selection = multiple // var $search = $specSelect1.data('select2').dropdown.$search.val('b') // // This is undocumented and may change in the future // // $search.val(term); // $search.trigger('keyup'); //} //$('button').on('click', function () { // var $select = $($(this).data('target')); // select2_search($select, 'Arizona'); //}); // 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] } //--------------------------------------------------- $('#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() }) } else { $('#realtyName').val('') $('#realtyBuildingClassificationId').val('').change() $('#realtyConstructionTypeId').val('').change() $('#realtyLocationId').val('').change() } }) function loadRealtyDetails(realtyId) { return $.ajax({ url: '/api/realties/' + realtyId + '/', method: 'GET', dataType: 'json', }) .then(function(res) {return res}) .fail(function() {console.error('Failed', arguments)}) } //--------------------------------------- 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() })