Built motion from commit 450cc5d.|0.0.139
[motion.git] / public / assets / scripts / components-dropdowns.js
1 var ComponentsDropdowns = function() {
2
3   var handleSelect2 = function() {
4
5     $('#select2_sample1').select2({
6       placeholder: 'Select an option',
7       allowClear: true
8     });
9
10     $('#select2_sample2').select2({
11       placeholder: 'Select a State',
12       allowClear: true
13     });
14
15     $('#select2_sample3').select2({
16       placeholder: 'Select...',
17       allowClear: true,
18       minimumInputLength: 1,
19       query: function(query) {
20         var data = {
21             results: []
22           },
23           i, j, s;
24         for (i = 1; i < 5; i++) {
25           s = '';
26           for (j = 0; j < i; j++) {
27             s = s + query.term;
28           }
29           data.results.push({
30             id: query.term + i,
31             text: s
32           });
33         }
34         query.callback(data);
35       }
36     });
37
38     function format(state) {
39       if (!state.id) {
40         return state.text;
41       } // optgroup
42       return "<img class='flag' src='" + Metronic.getGlobalImgPath() + "flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
43     }
44     $("#select2_sample4").select2({
45       placeholder: "Select a Country",
46       allowClear: true,
47       formatResult: format,
48       formatSelection: format,
49       escapeMarkup: function(m) {
50         return m;
51       }
52     });
53
54     $("#select2_sample5").select2({
55       tags: ["red", "green", "blue", "yellow", "pink"]
56     });
57
58
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>";
63       }
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>";
69       }
70       markup += "</td></tr></table>"
71       return markup;
72     }
73
74     function movieFormatSelection(movie) {
75       return movie.title;
76     }
77
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",
83         dataType: 'jsonp',
84         data: function(term, page) {
85           return {
86             q: term, // search term
87             page_limit: 10,
88             apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
89           };
90         },
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
93           return {
94             results: data.movies
95           };
96         }
97       },
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();
103         if (id !== "") {
104           $.ajax(
105             "http://api.rottentomatoes.com/api/public/v1.0/movies/" +
106             id + ".json", {
107               data: {
108                 apikey: "ju6z9mjyajq2djue3gbvv26t"
109               },
110               dataType: "jsonp"
111             }).done(function(data) {
112             callback(data);
113           });
114         }
115       },
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) {
120           return m;
121         } // we do not want to escape markup since we are displaying html in results
122     });
123   }
124
125   var handleSelect2Modal = function() {
126
127     $('#select2_sample_modal_1').select2({
128       placeholder: "Select an option",
129       allowClear: true
130     });
131
132     $('#select2_sample_modal_2').select2({
133       placeholder: "Select a State",
134       allowClear: true
135     });
136
137     $("#select2_sample_modal_3").select2({
138       allowClear: true,
139       minimumInputLength: 1,
140       query: function(query) {
141         var data = {
142             results: []
143           },
144           i, j, s;
145         for (i = 1; i < 5; i++) {
146           s = "";
147           for (j = 0; j < i; j++) {
148             s = s + query.term;
149           }
150           data.results.push({
151             id: query.term + i,
152             text: s
153           });
154         }
155         query.callback(data);
156       }
157     });
158
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'/>&nbsp;&nbsp;" + state.text;
163     }
164     $("#select2_sample_modal_4").select2({
165       allowClear: true,
166       formatResult: format,
167       formatSelection: format,
168       escapeMarkup: function(m) {
169         return m;
170       }
171     });
172
173     $("#select2_sample_modal_5").select2({
174       tags: ["red", "green", "blue", "yellow", "pink"]
175     });
176
177
178     function movieFormatResult(movie) {
179       var markup = "<table class='movie-result'><tr>";
180       if (movie.posters !== undefined && movie.posters.thumbnail !==
181         undefined) {
182         markup += "<td valign='top'><img src='" + movie.posters.thumbnail +
183           "'/></td>";
184       }
185       markup += "<td valign='top'><h5>" + movie.title + "</h5>";
186       if (movie.critics_consensus !== undefined) {
187         markup += "<div class='movie-synopsis'>" + movie.critics_consensus +
188           "</div>";
189       } else if (movie.synopsis !== undefined) {
190         markup += "<div class='movie-synopsis'>" + movie.synopsis +
191           "</div>";
192       }
193       markup += "</td></tr></table>"
194       return markup;
195     }
196
197     function movieFormatSelection(movie) {
198       return movie.title;
199     }
200
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",
206         dataType: 'jsonp',
207         data: function(term, page) {
208           return {
209             q: term, // search term
210             page_limit: 10,
211             apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
212           };
213         },
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
216           return {
217             results: data.movies
218           };
219         }
220       },
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();
226         if (id !== "") {
227           $.ajax(
228             "http://api.rottentomatoes.com/api/public/v1.0/movies/" +
229             id + ".json", {
230               data: {
231                 apikey: "ju6z9mjyajq2djue3gbvv26t"
232               },
233               dataType: "jsonp"
234             }).done(function(data) {
235             callback(data);
236           });
237         }
238       },
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) {
243           return m;
244         } // we do not want to escape markup since we are displaying html in results
245     });
246   }
247
248   var handleBootstrapSelect = function() {
249     $('.bs-select').selectpicker({
250       iconBase: 'fa',
251       tickIcon: 'fa-check'
252     });
253   }
254
255   var handleMultiSelect = function() {
256     $('#my_multi_select1').multiSelect();
257     $('#my_multi_select2').multiSelect({
258       selectableOptgroup: true,
259       afterSelect: function(values) {
260         console.log(values);
261       },
262       afterDeselect: function(values) {
263         console.log(values);
264       }
265     });
266   }
267
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');
272
273     if (selected) {
274       selected = selected[0].split('&');
275       selected = _.map(selected, function(elm) {
276         return elm.split('/')[1];
277       });
278     }
279
280     // START AGENTS
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;
287
288       if (selected && _.includes(selected, agents.rows[i].name)) {
289         option.setAttribute('selected', '');
290       }
291       optgroup.appendChild(option);
292     }
293     select.appendChild(optgroup);
294     // END AGENTS
295
296     // START USERS
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;
303
304       if (selected && _.includes(selected, users.rows[i].name)) {
305         option.setAttribute('selected', '');
306       }
307       optgroup.appendChild(option);
308     }
309     select.appendChild(optgroup);
310     // END USERS
311
312     // START TELEPHONES
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;
319
320       if (selected && _.includes(selected, telephones.rows[i].name)) {
321         option.setAttribute('selected', '');
322       }
323       optgroup.appendChild(option);
324     }
325     select.appendChild(optgroup);
326     // END TELEPHONES
327
328     var div = document.getElementById('multi-select-user-by-role');
329     div.innerHTML = '';
330     div.appendChild(select);
331
332     //
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) {
340         var that = this,
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';
345
346         that.qs1 = $selectableSearch
347           .quicksearch(selectableSearchString)
348           .on('keydown', function(e) {
349             if (e.which === 40) {
350               that.$selectableUl.focus();
351               return false;
352             }
353           });
354
355         that.qs2 = $selectionSearch
356           .quicksearch(selectionSearchString)
357           .on('keydown', function(e) {
358             if (e.which == 40) {
359               that.$selectionUl.focus();
360               return false;
361             }
362           });
363       },
364       afterSelect: function(values) {
365         this.qs1.cache();
366         this.qs2.cache();
367         add(values);
368         // this.qs1.cache();
369         // this.qs2.cache();
370         // teams().select(values);
371       },
372       afterDeselect: function(values) {
373         this.qs1.cache();
374         this.qs2.cache();
375         remove(values);
376         // this.qs1.cache();
377         // this.qs2.cache();
378         // teams().deselect(values);
379       }
380     });
381   }
382
383   var bindTeam = function(teamAgents, agents, teams) {
384     var select = document.createElement('select');
385     select.className = 'multi-select';
386     select.setAttribute('multiple', 'multiple');
387
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 + '>';
393
394       if (_.includes(teamAgents, agents[agent].id)) {
395         option.setAttribute('selected', '');
396       }
397
398       select.appendChild(option);
399     }
400
401     var div = document.getElementById('multi-select-team');
402     div.innerHTML = '';
403     div.appendChild(select);
404
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) {
411         var that = this,
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';
418
419         that.qs1 = $selectableSearch.quicksearch(
420             selectableSearchString)
421           .on('keydown', function(e) {
422             if (e.which === 40) {
423               that.$selectableUl.focus();
424               return false;
425             }
426           });
427
428         that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
429           .on('keydown', function(e) {
430             if (e.which == 40) {
431               that.$selectionUl.focus();
432               return false;
433             }
434           });
435       },
436       afterSelect: function(values) {
437         this.qs1.cache();
438         this.qs2.cache();
439         teams().select(values);
440       },
441       afterDeselect: function(values) {
442         this.qs1.cache();
443         this.qs2.cache();
444         teams().deselect(values);
445       }
446     });
447   }
448
449   var bindQueue = function(teams, queues, agents) {
450     var select = document.createElement('select');
451     select.className = 'multi-select';
452     select.setAttribute('multiple', 'multiple');
453
454     for (var team in teams) {
455       var optgroup = document.createElement('optgroup');
456       optgroup.label = teams[team].name;
457
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 + '>';
462
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
467           });
468           option.text += user.hasOwnProperty('penalty') ? ' (' + user.penalty + ')' : '';
469         }
470
471         optgroup.appendChild(option);
472       }
473       select.appendChild(optgroup);
474     }
475
476     var div = document.getElementById('multi-select-team');
477     div.innerHTML = '';
478     div.appendChild(select);
479
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) {
487         var that = this,
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';
492
493         that.qs1 = $selectableSearch
494           .quicksearch(selectableSearchString)
495           .on('keydown', function(e) {
496             if (e.which === 40) {
497               that.$selectableUl.focus();
498               return false;
499             }
500           });
501
502         that.qs2 = $selectionSearch
503           .quicksearch(selectionSearchString)
504           .on('keydown', function(e) {
505             if (e.which == 40) {
506               that.$selectionUl.focus();
507               return false;
508             }
509           });
510       },
511       afterSelect: function(values) {
512         this.qs1.cache();
513         this.qs2.cache();
514         agents().select(values);
515       },
516       afterDeselect: function(values) {
517         this.qs1.cache();
518         this.qs2.cache();
519         agents().deselect(values);
520       }
521     });
522   }
523
524   var bindPermit = function(dataQueues, dataPermits, channel, userId,
525     Resource) {
526     var select = document.createElement('select');
527     select.className = 'multi-select';
528     select.setAttribute('multiple', 'multiple');
529
530     _.forEach(dataQueues, function(queues) {
531       var optgroup = document.createElement('optgroup');
532       optgroup.label = 'ALL THE FOLLOWING:';
533
534       queues.forEach(function(queue) {
535         var option = document.createElement('option');
536         var _query = {};
537
538         if (queue.id) {
539           _query.id = queue.id;
540         } else {
541           _query.name = queue.name;
542         }
543
544         option.value = queue.id || queue.name;
545         option.text = queue.name;
546
547         if (_.find(dataPermits.queues, _query)) {
548           option.setAttribute('selected', '');
549         }
550
551         optgroup.appendChild(option);
552       });
553
554       select.appendChild(optgroup);
555     });
556
557     var div = document.getElementById('multi-select-permit');
558     div.innerHTML = '';
559     div.appendChild(select);
560
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) {
568         var that = this,
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';
575
576         that.qs1 = $selectableSearch.quicksearch(
577             selectableSearchString)
578           .on('keydown', function(e) {
579             if (e.which === 40) {
580               that.$selectableUl.focus();
581               return false;
582             }
583           });
584
585         that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
586           .on('keydown', function(e) {
587             if (e.which == 40) {
588               that.$selectionUl.focus();
589               return false;
590             }
591           });
592       },
593       afterSelect: function(values) {
594         this.qs1.cache();
595         this.qs2.cache();
596         return Resource
597           .save({
598             id: userId,
599             controller: 'allowed',
600             controller2: channel,
601             controller3: 'queues'
602           }, values)
603           .$promise
604           .catch(function(err) {
605             console.error(err);
606           });
607       },
608       afterDeselect: function(values) {
609         this.qs1.cache();
610         this.qs2.cache();
611         return Resource
612           .delete({
613             id: userId,
614             controller: 'allowed',
615             controller2: channel,
616             controller3: 'queues',
617             ids: values
618           })
619           .$promise
620           .catch(function(err) {
621             console.error(err);
622           });
623       }
624     });
625   }
626
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');
631
632     _.forEach(dataQueues, function(queues) {
633       var optgroup = document.createElement('optgroup');
634       optgroup.label = 'ALL THE FOLLOWING:';
635
636       queues.forEach(function(queue) {
637         var option = document.createElement('option');
638         var _query = {};
639
640         if (queue.id) {
641           _query.id = queue.id;
642         } else {
643           _query.name = queue.name;
644         }
645
646         option.value = queue.id || queue.name;
647         option.text = queue.name;
648
649         if (_.find(dataAgent.queues, _query)) {
650           option.setAttribute('selected', '');
651         }
652
653         optgroup.appendChild(option);
654       });
655
656       select.appendChild(optgroup);
657     });
658
659     var id = 'multi-select-queues-' + channel;
660     var div = document.getElementById(id);
661     div.innerHTML = '';
662     div.appendChild(select);
663
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) {
671         var that = this,
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';
676
677         that.qs1 = $selectableSearch.quicksearch(
678             selectableSearchString)
679           .on('keydown', function(e) {
680             if (e.which === 40) {
681               that.$selectableUl.focus();
682               return false;
683             }
684           });
685
686         that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
687           .on('keydown', function(e) {
688             if (e.which == 40) {
689               that.$selectionUl.focus();
690               return false;
691             }
692           });
693       },
694       afterSelect: function(values) {
695         this.qs1.cache();
696         this.qs2.cache();
697
698         dataAgent.queues = _.union(dataAgent.queues, values);
699
700         if (userId) {
701           return Resource
702             .save({
703               id: userId,
704               controller: channel,
705               controller2: 'queues',
706             }, values)
707             .$promise
708             .catch(function(err) {
709               console.error(err);
710             });
711         }
712       },
713       afterDeselect: function(values) {
714         this.qs1.cache();
715         this.qs2.cache();
716
717         dataAgent.queues = _.difference(dataAgent.queues, values);
718
719         if (userId) {
720           return Resource
721             .delete({
722               id: userId,
723               controller: channel,
724               controller2: 'queues',
725               ids: values
726             })
727             .$promise
728             .catch(function(err) {
729               console.error(err);
730             });
731         }
732       }
733     });
734   }
735
736   return {
737     //main function to initiate the module
738     init: function() {
739       handleSelect2();
740       handleSelect2Modal();
741       handleMultiSelect();
742       handleBootstrapSelect();
743     },
744     bindTeam: bindTeam,
745     bindMailQueue: bindQueue,
746     bindUserByRole: bindUserByRole,
747     bindChatQueue: bindQueue,
748     bindFaxQueue: bindQueue,
749     bindSmsQueue: bindQueue,
750     bindQueue: bindQueue,
751     bindPermit: bindPermit,
752     bindAgent: bindAgent
753   };
754
755 }();