From 3fe18ba4bb7ce5f3931b1abb170901fa1532768e Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 25 Nov 2016 08:17:39 +0530 Subject: [PATCH] Refactoring group sharing modal --- .../app/pages/cdmf.page.groups/groups.hbs | 136 +++++----- .../app/pages/cdmf.page.groups/groups.js | 4 + .../cdmf.page.groups/public/js/listing.js | 247 +++++++----------- 3 files changed, 169 insertions(+), 218 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.hbs index 5bc33e4377..ad4c096949 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.hbs @@ -80,7 +80,7 @@

- Enter user name to manage group sharing + Select user roles to manage group sharing

- - +
+
+ {{#each userRoles}} +
+ +
+ {{/each}} +

- - -
- - -
@@ -201,25 +171,10 @@

Enter Group Sharing Role Name

- -

- Role Permissions +

- - - {{#each groupPermissions}} - - - - - {{/each}} - -
{{this}} - - - -
+ +
+
+
+
+

New Role was successfully created.

+
+ Do you need to Add / Remove users to the chosen roles? + +
@@ -256,7 +247,8 @@ Yes - + Cancel
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.js index 0c04f9bb2d..f432193231 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/groups.js @@ -23,6 +23,10 @@ function onRequest(context) { var groupPermissions = require("/app/pages/cdmf.page.groups/public/group-permissions.json"); var currentUser = session.get(constants.USER_SESSION_KEY); var page = {}; + var rolesResult = userModule.getRoles(); + if (rolesResult.status == "success") { + page.userRoles = rolesResult.content; + } if (currentUser) { page.permissions = userModule.getUIPermissions(); page.permissions.list = stringify(page.permissions); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js index 1770cd26c4..1548ff4c71 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js @@ -110,12 +110,12 @@ function loadGroups() { var objects = []; $(data.deviceGroups).each(function (index) { objects.push({ - groupId: data.deviceGroups[index].id, - name: data.deviceGroups[index].name, - description: data.deviceGroups[index].description, - owner: data.deviceGroups[index].owner, - dateOfCreation: data.deviceGroups[index].dateOfCreation - }) + groupId: data.deviceGroups[index].id, + name: data.deviceGroups[index].name, + description: data.deviceGroups[index].description, + owner: data.deviceGroups[index].owner, + dateOfCreation: data.deviceGroups[index].dateOfCreation + }) }); var json = { "recordsTotal": data.count, @@ -154,34 +154,50 @@ function loadGroups() { class: 'text-right content-fill text-left-on-grid-view no-wrap', render: function (id, type, row, meta) { var html; - if ($.hasPermission("VIEW_GROUP_DEVICES")){ - html = '' + - '' + - ''; - - html += '' + - '' + - ''; + if ($.hasPermission("VIEW_GROUP_DEVICES")) { + html = '' + + '' + + + ''; + + html += '' + + '' + + + ''; } else { html = ''; } - if($.hasPermission("SHARE_GROUP")) { - html += '' + + if ($.hasPermission("SHARE_GROUP")) { + html += + '' + + ''; } else { html += ''; } - if($.hasPermission("UPDATE_GROUP")) { - html += '' + + if ($.hasPermission("UPDATE_GROUP")) { + html += + '' + ''; } else { html += ''; } if ($.hasPermission("REMOVE_GROUP")) { - html += '' + + html += + '' + + ''; } else { html += ''; @@ -336,7 +352,7 @@ function hidePopup() { $(modalPopupContent).html(""); $(modalPopupContent).removeClass("operation-data"); $(modalPopup).modal('hide'); - $('body').removeClass('modal-open').css('padding-right','0px'); + $('body').removeClass('modal-open').css('padding-right', '0px'); $('.modal-backdrop').remove(); } @@ -352,33 +368,37 @@ function attachEvents() { $("a.share-group-link").click(function () { var groupId = $(this).data("group-id"); var groupOwner = $(this).data("group-owner"); + $(modalPopupContent).html($('#share-group-w1-modal-content').html()); - $("a#share-group-next-link").show(); showPopup(); - $("a#share-group-next-link").click(function () { - var successCallback = function (data) { - if(data === 'true') { - getAllRoles(groupId, selectedUser); - } else { - var errorMsgWrapper = "#notification-error-msg"; - var errorMsg = "#notification-error-msg span"; - $(errorMsg).text("User does not exist."); - $(errorMsgWrapper).removeClass("hidden"); + + markAlreadySavedUsersRoles(groupId); + var shareGroupNextLink = $("a#share-group-next-link"); + shareGroupNextLink.click(function () { + var roles = []; + $('.modal .roleCheckBoxes').each( + function () { + if ($(this).is(':checked')) { + roles.push($(this).data('role-name')); + } } - } - var selectedUser = $('#share-user-selector').val(); - if (selectedUser == $("#group-listing").data("current-user")) { - $("#user-names").html("Please specify a user other than current user."); - $("a#share-group-next-link").hide(); - } else { - invokerUtil.get("/api/device-mgt/v1.0/users/checkUser?username=" + selectedUser, - successCallback, function (message) { - displayErrors(message); - }); - } + ); + updateGroupShare(groupId, roles); }); - $("a#share-group-w1-cancel-link").click(function () { - hidePopup(); + + var shareGroupNewRoleFromSelectionLink = $("a#share-group-new-role-from-selection"); + shareGroupNewRoleFromSelectionLink.click(function () { + var roles = []; + $('.modal .roleCheckBoxes').each( + function () { + if ($(this).is(':checked')) { + roles.push($(this).data('role-name')); + } + } + ); + addNewRole(roles); + // $(modalPopupContent).html($('#share-group-w3-modal-content').html()); + // createNewRole(roles); }); }); @@ -408,8 +428,8 @@ function attachEvents() { }; invokerUtil.delete("/api/device-mgt/v1.0/groups/id/" + groupId, - successCallback, function (message) { - displayErrors(message); + successCallback, function (message) { + displayErrors(message); }); }); @@ -453,8 +473,8 @@ function attachEvents() { }; invokerUtil.put("/api/device-mgt/v1.0/groups/id/" + groupId, group, - successCallback, function (message) { - displayErrors(message); + successCallback, function (message) { + displayErrors(message); }); }); @@ -464,16 +484,22 @@ function attachEvents() { }); } -function getAllRoles(groupId, selectedUser) { - $(modalPopupContent).html($('#share-group-w2-modal-content').html()); - $("a#share-group-yes-link").hide(); +function markAlreadySavedUsersRoles(groupId) { var successCallback = function (data, textStatus, xhr) { data = JSON.parse(data); if (xhr.status == 200) { if (data.roles.length > 0) { - generateRoleMap(groupId, selectedUser, data.roles); + for (var i = 0; i < data.roles.length; i++) { + $('.roleCheckBoxes').each( + function () { + if (data.roles[i] == $(this).data('role-name')) { + $(this).attr('checked', true); + } + } + ); + } } else { - $('#user-roles').html("There is no any roles for this group."); + return; } } else { displayErrors(xhr); @@ -481,98 +507,17 @@ function getAllRoles(groupId, selectedUser) { }; invokerUtil.get("/api/device-mgt/v1.0/groups/id/" + groupId + "/roles", - successCallback, function (message) { - displayErrors(message); - }); - - $("a#share-group-w2-cancel-link").click(function () { - hidePopup(); - }); -} - -function generateRoleMap(groupId, selectedUser, allRoles) { - var successCallback = function (data, textStatus, xhr) { - data = JSON.parse(data); - if (xhr.status == 200) { - var userRoles = []; - if(data != "EMPTY") { - userRoles = data.roles; - } - var str = $('#user-roles').html(); - - for (var i = 0; i < allRoles.length; i++) { - var isChecked = ''; - for (var j = 0; j < userRoles.length; j++) { - if (allRoles[i] == userRoles[j]) { - isChecked = 'checked'; - break; - } - } - str += '

'; - } - - $('#user-roles').html(str); - $("a#share-group-yes-link").show(); - $("a#share-group-yes-link").show(); - $("a#share-group-yes-link").click(function () { - var roles = []; - for (var i = 0; i < allRoles.length; i++) { - if ($('#user-role-' + allRoles[i]).is(':checked')) { - roles.push(allRoles[i]); - } - } - updateGroupShare(groupId, selectedUser, roles); - }); - $("a#share-group-w2-add-new-role-link").click(function () { - addNewRole(groupId, selectedUser, allRoles); - }); - } else { - displayErrors(xhr); - } - }; - - invokerUtil.get("/api/device-mgt/v1.0/groups/id/" + groupId + "/roles?userName=" + selectedUser, - successCallback, function (message) { + successCallback, function (message) { displayErrors(message); }); - - $("a#share-group-w2-cancel-link").click(function () { - hidePopup(); - }); } -function addNewRole(groupId, selectedUser, allRoles) { +function addNewRole(roles) { $(modalPopupContent).html($('#share-group-w3-modal-content').html()); - function getPermissions() { - var PERMISSION_PREFIX = '/permission/admin/'; - var permissions = []; - $('#permission-table-container').find('tr').each(function () { - var row = $(this).closest('tr'); - var permission = $(row).find('td:nth-child(1)').text(); - var check = $(row).find('td:nth-child(2) a').data('value'); - if(check === 'checked') { - permission = PERMISSION_PREFIX + permission; - permissions.push(permission); - } - }); - return permissions; - }; $("a#share-group-w3-yes-link").click(function () { - var successCallback = function (data, status, jqXHR) { - if(status == "success") { - getAllRoles(groupId, selectedUser); - } - } var roleName = $('#group-sharing-role-name').val(); - var users = []; - if(roleName) { - var groupRoleInfo = {"roleName": roleName, "permissions": getPermissions(), "users": users}; - var currentUser = $("#group-listing").data("current-user"); - invokerUtil.post("/api/device-mgt/v1.0/groups/id/" + groupId + "/roles/create?userName=" + currentUser, - groupRoleInfo, successCallback, function (message) { - displayErrors(message); - }); + if (roleName) { + createNewCombinedRole(roleName, roles); } else { var errorMsgWrapper = "#notification-error-msg"; var errorMsg = "#notification-error-msg span"; @@ -588,7 +533,7 @@ function addNewRole(groupId, selectedUser, allRoles) { function togglePermissionAction(element) { $(element).data('value', 'checked'); var icon = $(element).find("i")[1]; - if($(icon).hasClass('fw-minus')) { + if ($(icon).hasClass('fw-minus')) { $(icon).removeClass('fw-minus'); $(icon).addClass('fw-add'); $(element).data('value', 'unchecked'); @@ -599,7 +544,7 @@ function togglePermissionAction(element) { } } -function updateGroupShare(groupId, selectedUser, roles) { +function updateGroupShare(groupId, roles) { var successCallback = function (data) { $(modalPopupContent).html($('#share-group-200-content').html()); setTimeout(function () { @@ -608,11 +553,21 @@ function updateGroupShare(groupId, selectedUser, roles) { }, 2000); }; - var deviceGroupShare = {"username": selectedUser, "groupRoles": roles }; invokerUtil.post("/api/device-mgt/v1.0/groups/id/" + groupId + "/share", - deviceGroupShare, successCallback, function (message) { - displayErrors(message); - }); + roles, successCallback, function (message) { + displayErrors(message); + }); +} + +function createNewCombinedRole(roleName, roleList) { + var successCallback = function (data, status, jqXHR, isLast) { + $(modalPopupContent).html($('#create-combined-role-200-content').html()); + showPopup(); + }; + invokerUtil.post("/api/device-mgt/v1.0/roles/create-combined-role/" + roleName, roleList, + successCallback, function (message) { + displayErrors(message); + }); } function displayErrors(jqXHR) {