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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import { $gettext } from './gettext';
const MultiPersonSearch = {
init: function() {
$('.multi_person_search_link').each(function() {
// init js form
$(this).attr('href', $(this).data('js-form'));
// init form if it is loaded via ajax
$(this).on('dialog-open', function(event, parameters) {
MultiPersonSearch.dialog(
$(parameters.dialog)
.find('.mpscontainer')
.data('dialogname')
);
});
});
},
dialog: function(name) {
var count_template = _.template(
$gettext('Sie haben %{ count } Personen ausgewählt'),
{interpolate: /%\{\s*(.+?)\s*}/g}
);
this.name = name;
$('#' + name + '_selectbox').multiSelect({
selectableHeader: '<div>' + $gettext('Suchergebnisse') + '</div>',
selectionHeader:
'<div>' + count_template({ count: "<span id='" + this.name + "_count'>0</span>" }) + '.</div>',
selectableFooter:
'<a href="javascript:STUDIP.MultiPersonSearch.selectAll();">' +
$gettext('Alle hinzufügen') +
'</a>',
selectionFooter:
'<a href="javascript:STUDIP.MultiPersonSearch.unselectAll();">' +
$gettext('Alle entfernen') +
'</a>'
});
$('#' + this.name).on('keyup keypress', function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
MultiPersonSearch.search();
return false;
}
});
$('#' + this.name + '_selectbox').change(function() {
MultiPersonSearch.count();
});
$('#' + this.name + ' .quickfilter').click(function() {
MultiPersonSearch.loadQuickfilter($(this).data('quickfilter'));
return false;
});
},
loadQuickfilter(title) {
MultiPersonSearch.removeAllNotSelected();
var count = 0;
$('#' + this.name + '_quickfilter_' + title + ' option').each(function () {
count += MultiPersonSearch.append(
$(this).val(),
$(this).text(),
$(this).data('avatar'),
MultiPersonSearch.isAlreadyMember($(this).val())
);
});
if (count === 0) {
MultiPersonSearch.append('--', $gettext(' Dieser Filter enthält keine (neuen) Personen.'), null, true);
}
MultiPersonSearch.refresh();
},
isAlreadyMember(user_id) {
if ($('#' + this.name + '_selectbox_default option[value="' + user_id + '"]').length > 0) {
return true;
} else {
return false;
}
},
search: function() {
$('#' + this.name + '_resetsearch').addClass('visible');
const searchterm = $('#' + this.name + '_searchinput').val();
const not_found_template = _.template(
$gettext('Es wurden keine neuen Ergebnisse für "%{ needle }" gefunden.'),
{interpolate: /%\{\s*(.+?)\s*}/g}
);
$.getJSON(
STUDIP.URLHelper.getURL('dispatch.php/multipersonsearch/ajax_search/' + this.name, { s: searchterm }),
function(data) {
MultiPersonSearch.removeAllNotSelected();
var searchcount = 0;
$.each(data, function(i, item) {
searchcount += MultiPersonSearch.append(
item.user_id,
item.text,
item.avatar,
item.member
);
});
MultiPersonSearch.refresh();
if (searchcount == 0) {
MultiPersonSearch.append('--', not_found_template({ needle: searchterm }), null, true);
MultiPersonSearch.refresh();
}
}
);
return false;
},
selectAll: function() {
$('#' + this.name + '_selectbox').multiSelect('select_all');
this.count();
},
unselectAll: function() {
$('#' + this.name + '_selectbox').multiSelect('deselect_all');
this.count();
},
removeAll: function() {
$('#' + this.name + '_selectbox option').remove();
this.refresh();
},
removeAllNotSelected: function() {
$('#' + this.name + '_selectbox option:not(:selected)').remove();
this.refresh();
},
resetSearch: function() {
$('#' + this.name + '_resetsearch').removeClass('visible');
$('#' + this.name + '_searchinput').val('');
MultiPersonSearch.removeAllNotSelected();
},
append: function(value, text, avatar, disabled) {
if ($('#' + this.name + '_selectbox option[value=' + value + ']').length == 0) {
$('#' + this.name + '_selectbox').multiSelect('addOption', {
value: value,
text: text,
disabled: disabled
});
if (avatar) {
$('#' + this.name + '_selectbox option[value=' + value + ']').attr('style', 'background-image: url(' + avatar + ')');
}
return 1;
}
return 0;
},
refresh: function() {
$('#' + this.name + '_selectbox').multiSelect('refresh');
MultiPersonSearch.count();
},
count: function() {
$('#' + this.name + '_count').text($('#' + this.name + '_selectbox option:enabled:selected').length);
}
};
export default MultiPersonSearch;
|