1 var ComponentsDropdowns = function() {
3 var handleSelect2 = function() {
5 $('#select2_sample1').select2({
6 placeholder: 'Select an option',
10 $('#select2_sample2').select2({
11 placeholder: 'Select a State',
15 $('#select2_sample3').select2({
16 placeholder: 'Select...',
18 minimumInputLength: 1,
19 query: function(query) {
24 for (i = 1; i < 5; i++) {
26 for (j = 0; j < i; j++) {
38 function format(state) {
42 return "<img class='flag' src='" + Metronic.getGlobalImgPath() + "flags/" + state.id.toLowerCase() + ".png'/> " + state.text;
44 $("#select2_sample4").select2({
45 placeholder: "Select a Country",
48 formatSelection: format,
49 escapeMarkup: function(m) {
54 $("#select2_sample5").select2({
55 tags: ["red", "green", "blue", "yellow", "pink"]
59 function movieFormatResult(movie) {
60 var markup = "<table class='movie-result'><tr>";
61 if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
62 markup += "<td valign='top'><img src='" + movie.posters.thumbnail + "'/></td>";
64 markup += "<td valign='top'><h5>" + movie.title + "</h5>";
65 if (movie.critics_consensus !== undefined) {
66 markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
67 } else if (movie.synopsis !== undefined) {
68 markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
70 markup += "</td></tr></table>"
74 function movieFormatSelection(movie) {
78 $("#select2_sample6").select2({
79 placeholder: "Search for a movie",
80 minimumInputLength: 1,
81 ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
82 url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
84 data: function(term, page) {
86 q: term, // search term
88 apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
91 results: function(data, page) { // parse the results into the format expected by Select2.
92 // since we are using custom formatting functions we do not need to alter remote JSON data
98 initSelection: function(element, callback) {
99 // the input tag has a value attribute preloaded that points to a preselected movie's id
100 // this function resolves that id attribute to an object that select2 can render
101 // using its formatResult renderer - that way the movie name is shown preselected
102 var id = $(element).val();
105 "http://api.rottentomatoes.com/api/public/v1.0/movies/" +
108 apikey: "ju6z9mjyajq2djue3gbvv26t"
111 }).done(function(data) {
116 formatResult: movieFormatResult, // omitted for brevity, see the source of this page
117 formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
118 dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
119 escapeMarkup: function(m) {
121 } // we do not want to escape markup since we are displaying html in results
125 var handleSelect2Modal = function() {
127 $('#select2_sample_modal_1').select2({
128 placeholder: "Select an option",
132 $('#select2_sample_modal_2').select2({
133 placeholder: "Select a State",
137 $("#select2_sample_modal_3").select2({
139 minimumInputLength: 1,
140 query: function(query) {
145 for (i = 1; i < 5; i++) {
147 for (j = 0; j < i; j++) {
155 query.callback(data);
159 function format(state) {
160 if (!state.id) return state.text; // optgroup
161 return "<img class='flag' src='" + Metronic.getGlobalImgPath() +
162 "flags/" + state.id.toLowerCase() + ".png'/> " + state.text;
164 $("#select2_sample_modal_4").select2({
166 formatResult: format,
167 formatSelection: format,
168 escapeMarkup: function(m) {
173 $("#select2_sample_modal_5").select2({
174 tags: ["red", "green", "blue", "yellow", "pink"]
178 function movieFormatResult(movie) {
179 var markup = "<table class='movie-result'><tr>";
180 if (movie.posters !== undefined && movie.posters.thumbnail !==
182 markup += "<td valign='top'><img src='" + movie.posters.thumbnail +
185 markup += "<td valign='top'><h5>" + movie.title + "</h5>";
186 if (movie.critics_consensus !== undefined) {
187 markup += "<div class='movie-synopsis'>" + movie.critics_consensus +
189 } else if (movie.synopsis !== undefined) {
190 markup += "<div class='movie-synopsis'>" + movie.synopsis +
193 markup += "</td></tr></table>"
197 function movieFormatSelection(movie) {
201 $("#select2_sample_modal_6").select2({
202 placeholder: "Search for a movie",
203 minimumInputLength: 1,
204 ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
205 url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
207 data: function(term, page) {
209 q: term, // search term
211 apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
214 results: function(data, page) { // parse the results into the format expected by Select2.
215 // since we are using custom formatting functions we do not need to alter remote JSON data
221 initSelection: function(element, callback) {
222 // the input tag has a value attribute preloaded that points to a preselected movie's id
223 // this function resolves that id attribute to an object that select2 can render
224 // using its formatResult renderer - that way the movie name is shown preselected
225 var id = $(element).val();
228 "http://api.rottentomatoes.com/api/public/v1.0/movies/" +
231 apikey: "ju6z9mjyajq2djue3gbvv26t"
234 }).done(function(data) {
239 formatResult: movieFormatResult, // omitted for brevity, see the source of this page
240 formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
241 dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
242 escapeMarkup: function(m) {
244 } // we do not want to escape markup since we are displaying html in results
248 var handleBootstrapSelect = function() {
249 $('.bs-select').selectpicker({
255 var handleMultiSelect = function() {
256 $('#my_multi_select1').multiSelect();
257 $('#my_multi_select2').multiSelect({
258 selectableOptgroup: true,
259 afterSelect: function(values) {
262 afterDeselect: function(values) {
268 var bindUserByRole = function(users, agents, telephones, add, remove, selected) {
269 var select = document.createElement('select');
270 select.className = 'multi-select';
271 select.setAttribute('multiple', 'multiple');
274 selected = selected[0].split('&');
275 selected = _.map(selected, function(elm) {
276 return elm.split('/')[1];
281 var optgroup = document.createElement('optgroup');
282 optgroup.label = 'AGENTS';
283 for (var i = 0; i < agents.rows.length; i++) {
284 var option = document.createElement('option');
285 option.value = agents.rows[i].name;
286 option.text = agents.rows[i].fullname;
288 if (selected && _.includes(selected, agents.rows[i].name)) {
289 option.setAttribute('selected', '');
291 optgroup.appendChild(option);
293 select.appendChild(optgroup);
297 var optgroup = document.createElement('optgroup');
298 optgroup.label = 'USERS';
299 for (var i = 0; i < users.rows.length; i++) {
300 var option = document.createElement('option');
301 option.value = users.rows[i].name;
302 option.text = users.rows[i].fullname;
304 if (selected && _.includes(selected, users.rows[i].name)) {
305 option.setAttribute('selected', '');
307 optgroup.appendChild(option);
309 select.appendChild(optgroup);
313 var optgroup = document.createElement('optgroup');
314 optgroup.label = 'TELEPHONES';
315 for (var i = 0; i < telephones.rows.length; i++) {
316 var option = document.createElement('option');
317 option.value = telephones.rows[i].name;
318 option.text = telephones.rows[i].fullname;
320 if (selected && _.includes(selected, telephones.rows[i].name)) {
321 option.setAttribute('selected', '');
323 optgroup.appendChild(option);
325 select.appendChild(optgroup);
328 var div = document.getElementById('multi-select-user-by-role');
330 div.appendChild(select);
333 $(select).multiSelect({
334 selectableFooter: 'Agents available',
335 selectionFooter: 'Agents associated',
336 selectableOptgroup: true,
337 selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
338 selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
339 afterInit: function(ms) {
341 $selectableSearch = that.$selectableUl.prev(),
342 $selectionSearch = that.$selectionUl.prev(),
343 selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
344 selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
346 that.qs1 = $selectableSearch
347 .quicksearch(selectableSearchString)
348 .on('keydown', function(e) {
349 if (e.which === 40) {
350 that.$selectableUl.focus();
355 that.qs2 = $selectionSearch
356 .quicksearch(selectionSearchString)
357 .on('keydown', function(e) {
359 that.$selectionUl.focus();
364 afterSelect: function(values) {
370 // teams().select(values);
372 afterDeselect: function(values) {
378 // teams().deselect(values);
383 var bindTeam = function(teamAgents, agents, teams) {
384 var select = document.createElement('select');
385 select.className = 'multi-select';
386 select.setAttribute('multiple', 'multiple');
388 for (var agent in agents) {
389 var option = document.createElement('option');
390 option.value = agents[agent].id;
391 option.text = agents[agent].fullname + ' <' + agents[agent].name +
392 ',' + agents[agent].internal + '>';
394 if (_.includes(teamAgents, agents[agent].id)) {
395 option.setAttribute('selected', '');
398 select.appendChild(option);
401 var div = document.getElementById('multi-select-team');
403 div.appendChild(select);
405 $(select).multiSelect({
406 selectableFooter: 'Agents available',
407 selectionFooter: 'Agents associated',
408 selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
409 selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
410 afterInit: function(ms) {
412 $selectableSearch = that.$selectableUl.prev(),
413 $selectionSearch = that.$selectionUl.prev(),
414 selectableSearchString = '#' + that.$container.attr('id') +
415 ' .ms-elem-selectable:not(.ms-selected)',
416 selectionSearchString = '#' + that.$container.attr('id') +
417 ' .ms-elem-selection.ms-selected';
419 that.qs1 = $selectableSearch.quicksearch(
420 selectableSearchString)
421 .on('keydown', function(e) {
422 if (e.which === 40) {
423 that.$selectableUl.focus();
428 that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
429 .on('keydown', function(e) {
431 that.$selectionUl.focus();
436 afterSelect: function(values) {
439 teams().select(values);
441 afterDeselect: function(values) {
444 teams().deselect(values);
449 var bindQueue = function(teams, queues, agents) {
450 var select = document.createElement('select');
451 select.className = 'multi-select';
452 select.setAttribute('multiple', 'multiple');
454 for (var team in teams) {
455 var optgroup = document.createElement('optgroup');
456 optgroup.label = teams[team].name;
458 for (var user in teams[team].Users) {
459 var option = document.createElement('option');
460 option.value = teams[team].Users[user].id;
461 option.text = teams[team].Users[user].fullname + ' <' + teams[team].Users[user].name + ',' + teams[team].Users[user].internal + '>';
463 if (_.includes(_.map(queues, 'id'), teams[team].Users[user].id)) {
464 option.setAttribute('selected', '');
465 var user = _.find(queues, {
466 id: teams[team].Users[user].id
468 option.text += user.hasOwnProperty('penalty') ? ' (' + user.penalty + ')' : '';
471 optgroup.appendChild(option);
473 select.appendChild(optgroup);
476 var div = document.getElementById('multi-select-team');
478 div.appendChild(select);
480 $(select).multiSelect({
481 selectableFooter: 'Agents available',
482 selectionFooter: 'Agents associated',
483 selectableOptgroup: true,
484 selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
485 selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
486 afterInit: function(ms) {
488 $selectableSearch = that.$selectableUl.prev(),
489 $selectionSearch = that.$selectionUl.prev(),
490 selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
491 selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
493 that.qs1 = $selectableSearch
494 .quicksearch(selectableSearchString)
495 .on('keydown', function(e) {
496 if (e.which === 40) {
497 that.$selectableUl.focus();
502 that.qs2 = $selectionSearch
503 .quicksearch(selectionSearchString)
504 .on('keydown', function(e) {
506 that.$selectionUl.focus();
511 afterSelect: function(values) {
514 agents().select(values);
516 afterDeselect: function(values) {
519 agents().deselect(values);
524 var bindPermit = function(dataQueues, dataPermits, channel, userId,
526 var select = document.createElement('select');
527 select.className = 'multi-select';
528 select.setAttribute('multiple', 'multiple');
530 _.forEach(dataQueues, function(queues) {
531 var optgroup = document.createElement('optgroup');
532 optgroup.label = 'ALL THE FOLLOWING:';
534 queues.forEach(function(queue) {
535 var option = document.createElement('option');
539 _query.id = queue.id;
541 _query.name = queue.name;
544 option.value = queue.id || queue.name;
545 option.text = queue.name;
547 if (_.find(dataPermits.queues, _query)) {
548 option.setAttribute('selected', '');
551 optgroup.appendChild(option);
554 select.appendChild(optgroup);
557 var div = document.getElementById('multi-select-permit');
559 div.appendChild(select);
561 $(select).multiSelect({
562 selectableFooter: 'Queues available',
563 selectionFooter: 'Queues associated',
564 selectableOptgroup: true,
565 selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
566 selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
567 afterInit: function(ms) {
569 $selectableSearch = that.$selectableUl.prev(),
570 $selectionSearch = that.$selectionUl.prev(),
571 selectableSearchString = '#' + that.$container.attr('id') +
572 ' .ms-elem-selectable:not(.ms-selected)',
573 selectionSearchString = '#' + that.$container.attr('id') +
574 ' .ms-elem-selection.ms-selected';
576 that.qs1 = $selectableSearch.quicksearch(
577 selectableSearchString)
578 .on('keydown', function(e) {
579 if (e.which === 40) {
580 that.$selectableUl.focus();
585 that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
586 .on('keydown', function(e) {
588 that.$selectionUl.focus();
593 afterSelect: function(values) {
599 controller: 'allowed',
600 controller2: channel,
601 controller3: 'queues'
604 .catch(function(err) {
608 afterDeselect: function(values) {
614 controller: 'allowed',
615 controller2: channel,
616 controller3: 'queues',
620 .catch(function(err) {
627 var bindAgent = function(dataQueues, dataAgent, channel, userId, Resource, id) {
628 var select = document.createElement('select');
629 select.className = 'multi-select';
630 select.setAttribute('multiple', 'multiple');
632 _.forEach(dataQueues, function(queues) {
633 var optgroup = document.createElement('optgroup');
634 optgroup.label = 'ALL THE FOLLOWING:';
636 queues.forEach(function(queue) {
637 var option = document.createElement('option');
641 _query.id = queue.id;
643 _query.name = queue.name;
646 option.value = queue.id || queue.name;
647 option.text = queue.name;
649 if (_.find(dataAgent.queues, _query)) {
650 option.setAttribute('selected', '');
653 optgroup.appendChild(option);
656 select.appendChild(optgroup);
659 var id = 'multi-select-queues-' + channel;
660 var div = document.getElementById(id);
662 div.appendChild(select);
664 $(select).multiSelect({
665 selectableFooter: 'Queues available',
666 selectionFooter: 'Queues associated',
667 selectableOptgroup: true,
668 selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
669 selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
670 afterInit: function(ms) {
672 $selectableSearch = that.$selectableUl.prev(),
673 $selectionSearch = that.$selectionUl.prev(),
674 selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
675 selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
677 that.qs1 = $selectableSearch.quicksearch(
678 selectableSearchString)
679 .on('keydown', function(e) {
680 if (e.which === 40) {
681 that.$selectableUl.focus();
686 that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
687 .on('keydown', function(e) {
689 that.$selectionUl.focus();
694 afterSelect: function(values) {
698 dataAgent.queues = _.union(dataAgent.queues, values);
705 controller2: 'queues',
708 .catch(function(err) {
713 afterDeselect: function(values) {
717 dataAgent.queues = _.difference(dataAgent.queues, values);
724 controller2: 'queues',
728 .catch(function(err) {
737 //main function to initiate the module
740 handleSelect2Modal();
742 handleBootstrapSelect();
745 bindMailQueue: bindQueue,
746 bindUserByRole: bindUserByRole,
747 bindChatQueue: bindQueue,
748 bindFaxQueue: bindQueue,
749 bindSmsQueue: bindQueue,
750 bindQueue: bindQueue,
751 bindPermit: bindPermit,