-
Notifications
You must be signed in to change notification settings - Fork 1
/
cm_crew_connect.js
90 lines (79 loc) · 2.61 KB
/
cm_crew_connect.js
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
(function ($) {
Drupal.behaviors.crewConnectPositions = {
attach: function (context, settings) {
////////////////////////////////////////////////////////////////////////
//ON READY FUNCTION
$(document).ready(function() {
var should_filter =
$('input[name=crew_connect_filter_positions]').val();
if (should_filter == 'yes') {
cmCrewConnectConditionalSelects();
}
});
////////////////////////////////////////////////////////////////////////
//CHANGE FUNCTION FOR VOLUNTEER TYPE FIELD
$("select[id$='edit-field-crew-opportunity-type-und']", context).
change(function() {
var should_filter =
$('input[name=crew_connect_filter_positions]').val();
if (should_filter == 'yes') {
cmCrewConnectConditionalSelects();
}
});
////////////////////////////////////////////////////////////////////////
//FUNCTION WILL HANDLE INTERACTIONS BETWEEN POSITION AND OPPORTUNITY TYPE
function cmCrewConnectConditionalSelects() {
var $pos_select = $('#edit-field-crew-position-taxonomy-und');
var opp_type = $('#edit-field-crew-opportunity-type-und').val();
var position = $('#edit-field-crew-position-taxonomy-und').val();
console.log('OPP TYPE: ' + opp_type);
console.log('POSITION: ' + position);
if (opp_type == '_none') {
$('#edit-field-crew-position-taxonomy').hide();
}
else {
var cm_url = '/admin/crew_connect/positions?opp_type=' + opp_type;
console.log(cm_url);
//REMOVE CURRENT OPTIONS, REPLACE WITH OPTIONS FROM AJAX CALL
$pos_select.empty();
if (position == '_none') {
$pos_select.append($("<option></option>")
.attr("value", '_none')
.attr('selected', true)
.text('- Select a value -'));
}
$.getJSON(cm_url, function(data){
if (position && position != '_none') {
var invalid_position = 1;
$.each(data, function(i,item){
if (item.id==position) {
invalid_position = 0;
}
});
console.log('invalid position: ' + invalid_position);
if (invalid_position) {
$pos_select.append($("<option></option>")
.attr("value", '_none')
.attr('selected', true)
.text('- Select a value -'));
}
}
$.each(data, function(i,item){
if (item.id==position) {
$pos_select.append($("<option></option>")
.attr("value", item.id)
.attr('selected', true)
.text(item.label));
}
else {
$pos_select.append($("<option></option>")
.attr("value", item.id)
.text(item.label));
}
});
});
$('#edit-field-crew-position-taxonomy').show();
}
}
}};
}) (jQuery);