forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistrictDropdown.js
More file actions
144 lines (121 loc) · 3.92 KB
/
districtDropdown.js
File metadata and controls
144 lines (121 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import $ from 'jquery';
window.DistrictDropdownManager = function (existingOptions) {
var districtListFirstLoad = true;
var districtElement = $('#school-district-id');
function setupDistrictDropdown(stateCode) {
var selectize = districtElement[0].selectize;
if (selectize) {
selectize.clear();
selectize.destroy();
}
selectize = districtElement.selectize({
maxItems: 1,
onChange: function () {
var districtId = districtElement[0].selectize.getValue();
$("#school-district-id-form").val(districtId);
}
});
districtElement[0].selectize.load(function (callback) {
$.ajax({
url: "/dashboardapi/v1/school-districts/" + stateCode,
type: 'GET',
error: function () {
callback();
districtListFirstLoad = false;
},
success: function (res) {
var districts = [];
for (var i = 0; i < res.object.length; i++) {
var entry = res.object[i];
districts.push({value: entry.id, text: entry.name});
}
callback(districts);
// Only do this first time we do a load of this dropdown content.
// The assumption is that if we had a valid school_district_id then
// we would hit this codepath immediately after page load.
if (districtListFirstLoad && existingOptions && existingOptions.school_district_id) {
$('#school-district-id')[0].selectize.setValue(existingOptions.school_district_id);
}
districtListFirstLoad = false;
}
});
});
}
function enableDistricts(enable) {
var selectize = districtElement[0].selectize;
if (selectize) {
if (enable) {
selectize.enable();
} else {
selectize.disable();
selectize.clear();
}
}
}
function clearZip() {
var zipElement = $('#school-zipcode');
zipElement.val("");
}
function clearState() {
var stateElement = $("#school-state");
stateElement.val("");
}
function clearDistrict() {
$("#school-district-id-form").val("");
}
$('#school-type').change(function () {
if (['public', 'other'].indexOf($(this).val()) > -1) {
// Show state.
$('#school-state').closest('.form-group').show();
$('#school-zipcode').closest('.form-group').hide();
// And clear ZIP.
clearZip();
} else {
// Show ZIP.
$('#school-state').closest('.form-group').hide();
$('#school-zipcode').closest('.form-group').show();
$('#school-district').closest('.form-group').hide();
// And clear state and district.
clearState();
clearDistrict();
}
});
$('#school-state').change(function () {
if ($(this).val() !== 'other') {
// Show districts. (State is already showing, so ZIP is already clear.)
$('#school-district').closest('.form-group').show();
setupDistrictDropdown($('#school-state').val());
} else {
$('#school-district').closest('.form-group').hide();
// Clear district.
clearDistrict();
}
});
$('#school-district-other').change(function () {
if ($(this).prop('checked')) {
// Disable districts.
enableDistricts(false);
// And clear district.
clearDistrict();
} else {
// Enable districts. (And clear "unknown district".)
enableDistricts(true);
}
});
// Now that all the handlers are set up, initialize the control with existing
// values if they were provided.
if (existingOptions) {
if (existingOptions.school_type) {
$('#school-type').val(existingOptions.school_type).change();
}
if (existingOptions.zip) {
$('#school-zipcode').val(existingOptions.zip).change();
}
if (existingOptions.state) {
$('#school-state').val(existingOptions.state).change();
}
if (existingOptions.school_district_other) {
$('#school-district-other').prop('checked', true).change();
}
}
};