From 0210217ec547700dbeea9d6c73de839590ca8bde Mon Sep 17 00:00:00 2001 From: Maxime Roux Date: Thu, 30 Apr 2015 18:04:08 -0700 Subject: [PATCH] UI-1393: Updated the user devices section to use the monsterListing common control to add spare devices --- i18n/en-US.json | 8 +- i18n/fr-FR.json | 8 +- submodules/users/users.js | 132 ++++++++++---------------------- views/users-devices.html | 54 ++++--------- views/users-rowSpareDevice.html | 4 +- 5 files changed, 71 insertions(+), 135 deletions(-) diff --git a/i18n/en-US.json b/i18n/en-US.json index add27ae..4536dd3 100644 --- a/i18n/en-US.json +++ b/i18n/en-US.json @@ -606,7 +606,13 @@ }, "__comment": "UI-1289: Masks should be in i18n", "__version": "v3.20_s3", - "defaultPhoneMask": "+1 (999) 999-9999" + "defaultPhoneMask": "+1 (999) 999-9999", + "__comment": "UI-1393: Updating user devices to use the monsterListing common control", + "__version": "v3.20_s4", + "devices": { + "newDevice": "New Device", + "spareDevice": "Add from Spare Devices" + } }, "strategy": { diff --git a/i18n/fr-FR.json b/i18n/fr-FR.json index e2925ea..8214894 100644 --- a/i18n/fr-FR.json +++ b/i18n/fr-FR.json @@ -570,7 +570,13 @@ }, "__comment": "UI-1289: Masks should be in i18n", "__version": "v3.20_s3", - "defaultPhoneMask": "+1 (999) 999-9999" + "defaultPhoneMask": "+1 (999) 999-9999", + "__comment": "UI-1393: Updating user devices to use the monsterListing common control", + "__version": "v3.20_s4", + "devices": { + "newDevice": "Nouveau Téléphone", + "spareDevice": "Ajouter depuis les Téléphones Disponibles" + } }, "strategy": { diff --git a/submodules/users/users.js b/submodules/users/users.js index 85a6f56..9fc2a3f 100644 --- a/submodules/users/users.js +++ b/submodules/users/users.js @@ -347,6 +347,7 @@ define(function(require){ extensionsToSave, numbersToSave, extraSpareNumbers, + unassignedDevices, toastrMessages = self.i18n.active().users.toastrMessages, mainDirectoryId, mainCallflowId, @@ -458,6 +459,8 @@ define(function(require){ } else if (type === 'devices') { setTimeout(function() { template.find('.search-query').focus(); }); + currentUser = userId; + unassignedDevices = {}; } row.find('.edit-user').append(template).slideDown(400, function() { @@ -837,24 +840,41 @@ define(function(require){ listAssigned = template.find('.list-assigned-items'); listAssigned.find('.empty-row').hide(); - - /* reset search */ - listAssigned.find('.empty-search-row').hide(); - template.find('.unassigned-list-header .search-query').val(''); - listAssigned.find('.item-row').show(); - - /* Add row */ - listAssigned.append(rowDevice) - .find('.item-row[data-id="' + device.id + '"]') - .toggleClass('updated') - .find('button') - .removeClass('add-device btn-primary') - .addClass('remove-device btn-danger') - .text(self.i18n.active().remove); + listAssigned.append(rowDevice); } }); }); + template.on('click', '.spare-devices:not(.disabled)', function() { + var currentlySelected = $.map(template.find('.device-list.list-assigned-items .item-row'), function(val) { return $(val).data('id') }); + self.usersGetDevicesData(function(devicesData) { + var spareDevices = {}; + _.each(devicesData, function(device) { + if( (!('owner_id' in device) || device.owner_id === '' || device.owner_id === currentUser) && currentlySelected.indexOf(device.id) === -1 ) { + spareDevices[device.id] = device; + } + }); + + monster.pub('common.monsterListing.render', { + dataList: spareDevices, + dataType: 'devices', + okCallback: function(devices) { + _.each(devices, function(device) { + var rowDevice = monster.template(self, 'users-rowSpareDevice', device), + listAssigned = template.find('.list-assigned-items'); + + listAssigned.find('.empty-row').hide(); + listAssigned.append(rowDevice); + + if(device.owner_id) { + delete unassignedDevices[device.id]; + } + }); + } + }); + }); + }); + template.on('click', '.save-devices', function() { var dataDevices = { new: [], @@ -863,12 +883,10 @@ define(function(require){ name = $(this).parents('.grid-row').find('.grid-cell.name').text(), userId = $(this).parents('.grid-row').data('id'); - template.find('.detail-devices .list-assigned-items .item-row.updated').each(function(k, row) { + template.find('.detail-devices .list-assigned-items .item-row:not(.assigned)').each(function(k, row) { dataDevices.new.push($(row).data('id')); }); - template.find('.detail-devices .list-unassigned-items .item-row.updated').each(function(k, row) { - dataDevices.old.push($(row).data('id')); - }); + dataDevices.old = Object.keys(unassignedDevices); self.usersUpdateDevices(dataDevices, userId, function() { toastr.success(monster.template(self, '!' + toastrMessages.devicesUpdated, { name: name })); @@ -893,85 +911,17 @@ define(function(require){ ); }); - template.on('click', '.detail-devices .list-unassigned-items .add-device', function() { - - var row = $(this).parents('.item-row'), - spare = template.find('.count-spare'), - countSpare = spare.data('count') - 1, - assignedList = template.find('.detail-devices .list-assigned-items'); - - spare - .html(countSpare) - .data('count', countSpare); - - row.toggleClass('updated') - .find('button') - .removeClass('add-device btn-primary') - .addClass('remove-device btn-danger') - .text(self.i18n.active().remove); - - assignedList.find('.empty-row').hide(); - assignedList.append(row); - - var rows = template.find('.list-unassigned-items .item-row'); - - if(rows.size() === 0) { - template.find('.detail-devices .list-unassigned-items .empty-row').show(); - } - else if(rows.is(':visible') === false) { - template.find('.detail-devices .list-unassigned-items .empty-search-row').show(); - } - }); - template.on('click', '.detail-devices .list-assigned-items .remove-device', function() { - var row = $(this).parents('.item-row'), - spare = template.find('.count-spare'), - countSpare = spare.data('count') + 1, - unassignedList = template.find('.detail-devices .list-unassigned-items'); - - /* Alter the html */ - row.hide(); - - row.toggleClass('updated') - .find('button') - .removeClass('remove-device btn-danger') - .addClass('add-device btn-primary') - .text(self.i18n.active().add); - - unassignedList.append(row); - unassignedList.find('.empty-row').hide(); - - spare - .html(countSpare) - .data('count', countSpare); + var row = $(this).parents('.item-row'); + if(row.hasClass('assigned')) { + unassignedDevices[row.data('id')] = true; + } + row.remove(); var rows = template.find('.detail-devices .list-assigned-items .item-row'); - /* If no rows beside the clicked one, display empty row */ if(rows.is(':visible') === false) { template.find('.detail-devices .list-assigned-items .empty-row').show(); } - - /* If it matches the search string, show it */ - if(row.data('search').indexOf(currentNumberSearch) >= 0) { - row.show(); - unassignedList.find('.empty-search-row').hide(); - } - }); - - template.on('keyup', '.detail-devices .search-query', function() { - var searchString = $(this).val().toLowerCase(), - rows = template.find('.list-unassigned-items .item-row'), - emptySearch = template.find('.list-unassigned-items .empty-search-row'); - - _.each(rows, function(row) { - var row = $(row); - - row.data('search').toLowerCase().indexOf(searchString) < 0 ? row.hide() : row.show(); - }); - - if(rows.size() > 0) { - rows.is(':visible') ? emptySearch.hide() : emptySearch.show(); - } }); /* Events for Numbers in Users */ diff --git a/views/users-devices.html b/views/users-devices.html index 4473113..13f7475 100644 --- a/views/users-devices.html +++ b/views/users-devices.html @@ -5,7 +5,7 @@ {{ i18n.users.noAssignedDevices }} {{#each assignedDevices}} -
+
{{ this.name }} @@ -15,49 +15,23 @@ {{/each}}
- -
-
{{i18n.users.addFromSpareDevices}} ({{countSpare}})
- -
- -
-
- {{ i18n.users.noMatchingDevices }} -
-
- {{ i18n.users.noMoreSpareDevices }} -
- {{#each unassignedDevices}} -
-
- - {{ this.name }} -
-
- {{/each}} -
+ {{ i18n.users.devices.spareDevice }}
{{ i18n.cancel }} diff --git a/views/users-rowSpareDevice.html b/views/users-rowSpareDevice.html index c5c2921..3216a9f 100644 --- a/views/users-rowSpareDevice.html +++ b/views/users-rowSpareDevice.html @@ -1,6 +1,6 @@ -
+
- + {{ this.name }}