Built motion from commit 2e74e5e.|0.0.138
[motion.git] / public / assets / scripts / components-dropdowns.js
index 2624791..cb93125 100644 (file)
@@ -268,6 +268,121 @@ var ComponentsDropdowns = function() {
     });
   }
 
+  var bindUserByRole = function(users, agents, telephones, add, remove, selected) {
+    var select = document.createElement('select');
+    select.className = 'multi-select';
+    select.setAttribute('multiple', 'multiple');
+
+    if (selected) {
+      selected = selected[0].split('&');
+      selected = _.map(selected, function(elm) {
+        return elm.split('/')[1];
+      });
+    }
+
+    // START AGENTS
+    var optgroup = document.createElement('optgroup');
+    optgroup.label = 'AGENTS';
+    for (var i = 0; i < agents.rows.length; i++) {
+      var option = document.createElement('option');
+      option.value = agents.rows[i].name;
+      option.text = agents.rows[i].fullname;
+
+      if (selected && _.includes(selected, agents.rows[i].name)) {
+        option.setAttribute('selected', '');
+      }
+      optgroup.appendChild(option);
+    }
+    select.appendChild(optgroup);
+    // END AGENTS
+
+    // START USERS
+    var optgroup = document.createElement('optgroup');
+    optgroup.label = 'USERS';
+    for (var i = 0; i < users.rows.length; i++) {
+      var option = document.createElement('option');
+      option.value = users.rows[i].name;
+      option.text = users.rows[i].fullname;
+
+      if (selected && _.includes(selected, users.rows[i].name)) {
+        option.setAttribute('selected', '');
+      }
+      optgroup.appendChild(option);
+    }
+    select.appendChild(optgroup);
+    // END USERS
+
+    // START TELEPHONES
+    var optgroup = document.createElement('optgroup');
+    optgroup.label = 'TELEPHONES';
+    for (var i = 0; i < telephones.rows.length; i++) {
+      var option = document.createElement('option');
+      option.value = telephones.rows[i].name;
+      option.text = telephones.rows[i].fullname;
+
+      if (selected && _.includes(selected, telephones.rows[i].name)) {
+        option.setAttribute('selected', '');
+      }
+      optgroup.appendChild(option);
+    }
+    select.appendChild(optgroup);
+    // END TELEPHONES
+
+    var div = document.getElementById('multi-select-user-by-role');
+    div.innerHTML = '';
+    div.appendChild(select);
+
+    //
+    $(select).multiSelect({
+      selectableFooter: 'Agents available',
+      selectionFooter: 'Agents associated',
+      selectableOptgroup: true,
+      selectableHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
+      selectionHeader: "<input type='text' class='search-input form-control' autocomplete='off' placeholder='Search...'>",
+      afterInit: function(ms) {
+        var that = this,
+          $selectableSearch = that.$selectableUl.prev(),
+          $selectionSearch = that.$selectionUl.prev(),
+          selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
+          selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
+
+        that.qs1 = $selectableSearch
+          .quicksearch(selectableSearchString)
+          .on('keydown', function(e) {
+            if (e.which === 40) {
+              that.$selectableUl.focus();
+              return false;
+            }
+          });
+
+        that.qs2 = $selectionSearch
+          .quicksearch(selectionSearchString)
+          .on('keydown', function(e) {
+            if (e.which == 40) {
+              that.$selectionUl.focus();
+              return false;
+            }
+          });
+      },
+      afterSelect: function(values) {
+        this.qs1.cache();
+        this.qs2.cache();
+        add(values);
+        // this.qs1.cache();
+        // this.qs2.cache();
+        // teams().select(values);
+      },
+      afterDeselect: function(values) {
+        this.qs1.cache();
+        this.qs2.cache();
+        remove(values);
+        // this.qs1.cache();
+        // this.qs2.cache();
+        // teams().deselect(values);
+      }
+    });
+  }
+
   var bindTeam = function(teamAgents, agents, teams) {
     var select = document.createElement('select');
     select.className = 'multi-select';
@@ -346,15 +461,14 @@ var ComponentsDropdowns = function() {
       for (var user in teams[team].Users) {
         var option = document.createElement('option');
         option.value = teams[team].Users[user].id;
-        option.text = teams[team].Users[user].fullname + ' <' + teams[team]
-          .Users[user].name + ',' + teams[team].Users[user].internal + '>';
+        option.text = teams[team].Users[user].fullname + ' <' + teams[team].Users[user].name + ',' + teams[team].Users[user].internal + '>';
+
         if (_.includes(_.map(queues, 'id'), teams[team].Users[user].id)) {
           option.setAttribute('selected', '');
           var user = _.find(queues, {
             id: teams[team].Users[user].id
           });
-          option.text += user.hasOwnProperty('penalty') ? ' (' + user.penalty +
-            ')' : '';
+          option.text += user.hasOwnProperty('penalty') ? ' (' + user.penalty + ')' : '';
         }
 
         optgroup.appendChild(option);
@@ -625,6 +739,7 @@ var ComponentsDropdowns = function() {
     },
     bindTeam: bindTeam,
     bindMailQueue: bindQueue,
+    bindUserByRole: bindUserByRole,
     bindChatQueue: bindQueue,
     bindFaxQueue: bindQueue,
     bindSmsQueue: bindQueue,