-
Notifications
You must be signed in to change notification settings - Fork 464
/
select2.js
156 lines (138 loc) · 5.32 KB
/
select2.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
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
145
146
147
148
149
150
151
152
153
154
155
156
;(function ($) {
function get_forwards(element) {
var forwardElem, forwardList, prefix, forwardedData, divSelector, form;
divSelector = "div.dal-forward-conf#dal-forward-conf-for-" +
element.attr("id");
form = element.length > 0 ? $(element[0].form) : $();
forwardElem =
form.find(divSelector).find('script');
if (forwardElem.length === 0) {
return;
}
try {
forwardList = JSON.parse(forwardElem.text());
} catch (e) {
return;
}
if (!Array.isArray(forwardList)) {
return;
}
prefix = $(element).getFormPrefix();
forwardedData = {};
$.each(forwardList, function(ix, f) {
if (f["type"] === "const") {
forwardedData[f["dst"]] = f["val"];
} else if (f["type"] === "field") {
var srcName, dstName;
srcName = f["src"];
if (f.hasOwnProperty("dst")) {
dstName = f["dst"];
} else {
dstName = srcName;
}
// First look for this field in the inline
$field_selector = '[name=' + prefix + srcName + ']';
$field = $($field_selector);
if (!$field.length) {
// As a fallback, look for it outside the inline
$field_selector = '[name=' + srcName + ']';
$field = $($field_selector);
}
if ($field.length) {
if ($field.attr('type') === 'checkbox')
forwardedData[dstName] = $field[0].checked;
else if ($field.attr('type') === 'radio')
forwardedData[dstName] = $($field_selector + ":checked").val();
else
forwardedData[dstName] = $field.val();
}
}
});
return JSON.stringify(forwardedData);
}
$(document).on('autocompleteLightInitialize', '[data-autocomplete-light-function=select2]', function() {
var element = $(this);
// Templating helper
function template(item) {
if (element.attr('data-html') !== undefined) {
var $result = $('<span>');
$result.html(item.text);
return $result;
} else {
return item.text;
}
}
var ajax = null;
if ($(this).attr('data-autocomplete-light-url')) {
ajax = {
url: $(this).attr('data-autocomplete-light-url'),
dataType: 'json',
delay: 250,
data: function (params) {
var data = {
q: params.term, // search term
page: params.page,
create: element.attr('data-autocomplete-light-create') && !element.attr('data-tags'),
forward: get_forwards(element)
};
return data;
},
processResults: function (data, page) {
if (element.attr('data-tags')) {
$.each(data.results, function(index, value) {
value.id = value.text;
});
}
return data;
},
cache: true
};
}
$(this).select2({
tokenSeparators: element.attr('data-tags') ? [','] : null,
debug: true,
placeholder: '',
minimumInputLength: 0,
allowClear: ! $(this).is('[required]'),
templateResult: template,
templateSelection: template,
ajax: ajax,
});
$(this).on('select2:selecting', function (e) {
var data = e.params.args.data;
if (data.create_id !== true)
return;
e.preventDefault();
var select = $(this);
$.ajax({
url: $(this).attr('data-autocomplete-light-url'),
type: 'POST',
dataType: 'json',
data: {
text: data.id,
forward: get_forwards($(this))
},
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", document.csrftoken);
},
success: function(data, textStatus, jqXHR ) {
select.append(
$('<option>', {value: data.id, text: data.text, selected: true})
);
select.trigger('change');
select.select2('close');
}
});
});
});
$('[data-autocomplete-light-function]:not([id*="__prefix__"])').each(function() {
window.__dal__initialize(this);
});
// Remove this block when this is merged upstream:
// https://github.com/select2/select2/pull/4249
$(document).on('DOMSubtreeModified', '[data-autocomplete-light-function=select2] option', function() {
$(this).parents('select').next().find(
'.select2-selection--single .select2-selection__rendered'
).text($(this).text());
});
})(yl.jQuery);