diff --git a/assets/index.js b/assets/index.js index cef74c4..4e1eb4d 100644 --- a/assets/index.js +++ b/assets/index.js @@ -15,12 +15,6 @@ $(function() { 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, @@ -34,7 +28,7 @@ $(function() { return { text: item.name, id: item.id, - orig_item: item, + origItem: item, } }, data.results), @@ -60,57 +54,69 @@ $(function() { 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')}) + $specSelects.on('change', function($evt) { + getSpecializationTree($evt.added.origItem).then(function(specs) { + $specSelect1.select2('data', specs.specLevel1 ? {id: specs.specLevel1.id, text: specs.specLevel1.name, origItem: specs.specLevel1} : null) + $specSelect2.select2('data', specs.specLevel2 ? {id: specs.specLevel2.id, text: specs.specLevel2.name, origItem: specs.specLevel2} : null) + $specSelect3.select2('data', specs.specLevel3 ? {id: specs.specLevel3.id, text: specs.specLevel3.name, origItem: specs.specLevel3} : null) + $specSelect4.select2('data', specs.specLevel4 ? {id: specs.specLevel4.id, text: specs.specLevel4.name, origItem: specs.specLevel4} : null) + }) }) - - //$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'); -//} +function getSpecializationTree(spec) { + var specLevel1, specLevel2, specLevel3, specLevel4 + specLevel1 = specLevel2 = specLevel3 = specLevel4 = null + + if (spec.level === 1) { + specLevel1 = spec + return $.when({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'}) + .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'}) + .then(function(spec) { + specLevel2 = spec + + return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET'}) + .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'}) + .then(function(spec) { + specLevel3 = spec + + return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET'}) + .then(function(spec) { + specLevel2 = spec + + return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET'}) + .then(function(spec) { + specLevel1 = spec + return {specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4} + }) + }) + }) + } +} + -//$('button').on('click', function () { -// var $select = $($(this).data('target')); -// select2_search($select, 'Arizona'); -//});