From b7780e17421bf9491bca2e62b316f9ee2726e355 Mon Sep 17 00:00:00 2001 From: charithag Date: Wed, 5 Aug 2015 19:47:50 +0530 Subject: [PATCH] Add groups in device list --- .../units/device-listing/device-listing.hbs | 16 ++- .../units/device-listing/device-listing.js | 3 +- .../public/js/device-listing.js | 107 ++++++++++-------- .../public/templates/device-listing.hbs | 8 +- .../public/templates/group-listing.hbs | 19 ++++ 5 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/group-listing.hbs diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.hbs index 82afb848..da18ac9d 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.hbs @@ -1,8 +1,8 @@ {{#zone "main"}} - + -
+
@@ -52,6 +52,16 @@
+
+ +
+
+

You don't have any associated devices here.

+
+
+ +
+
@@ -248,6 +258,8 @@
{{/zone}} {{#zone "bottomJs"}} + diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.js index 3db7b52b..d4cdaca9 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/device-listing.js @@ -1,6 +1,7 @@ var log = new Log("modules/device-listing.js"); function onRequest(context){ + var constants = require("/modules/constants.js"); var userModule = require("/modules/user.js").userModule; var permissions = []; if(userModule.isAuthorized("/permission/device-mgt/admin/devices/list")){ @@ -19,6 +20,6 @@ function onRequest(context){ }else{ context.groupId = 0; } - + context.user = session.get(constants.USER_SESSION_KEY).username; return context; } \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/js/device-listing.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/js/device-listing.js index c3b570ae..48494228 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/js/device-listing.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/js/device-listing.js @@ -16,12 +16,17 @@ * under the License. */ +var groupId, user; + (function () { var cache = {}; var permissionSet = {}; var validateAndReturn = function (value) { return (value == undefined || value == null) ? "Unspecified" : value; }; + Handlebars.registerHelper("groupMap", function (group) { + group.id = validateAndReturn(group.id); + }); Handlebars.registerHelper("deviceMap", function (device) { device.owner = validateAndReturn(device.owner); device.ownership = validateAndReturn(device.ownership); @@ -33,6 +38,13 @@ }, {}); } }); + Handlebars.registerHelper("if_owner", function (owner, opts) { + if (owner == user){ + return opts.fn(this); + }else{ + opts.inverse(this); + } + }); //This method is used to setup permission for device listing $.setPermission = function (permission) { @@ -47,13 +59,12 @@ /* * Setting-up global variables. */ -var deviceCheckbox = "#ast-container .ctrl-wr-asset .itm-select input[type='checkbox']"; -var assetContainer = "#ast-container"; +var deviceCheckbox = "#ast-container-parent .ctrl-wr-asset .itm-select input[type='checkbox']"; +var assetContainerParent = "#ast-container-parent"; /* * DOM ready functions. */ -var groupId; $(document).ready(function () { /* Adding selected class for selected devices */ $(deviceCheckbox).each(function () { @@ -61,6 +72,7 @@ $(document).ready(function () { }); var i; + user = $("#permission").data("user"); var permissionList = $("#permission").data("permission"); for (i = 0; i < permissionList.length; i++) { $.setPermission(permissionList[i]); @@ -113,9 +125,9 @@ function changeDeviceView(view, selection) { }); $(selection).addClass("selected"); if (view == "list") { - $(assetContainer).addClass("list-view"); + $(assetContainerParent).addClass("list-view"); } else { - $(assetContainer).removeClass("list-view"); + $(assetContainerParent).removeClass("list-view"); } } @@ -131,63 +143,60 @@ function addDeviceSelectedClass(checkbox) { $(checkbox).closest(".ctrl-wr-asset").removeClass("selected device-select"); } } -function loadDevices(searchType, searchParam) { - var deviceListing = $("#device-listing"); - var deviceListingSrc = deviceListing.attr("src"); - var imageResource = deviceListing.data("image-resource"); - $.template("device-listing", deviceListingSrc, function (template) { + +function loadDevices() { + var groupListing = $("#group-listing"); + var groupListingSrc = groupListing.attr("src"); + $.template("group-listing", groupListingSrc, function (template) { var serviceURL; - if ($.hasPermission("LIST_DEVICES")) { - if (groupId && groupId != "0") { - serviceURL = "/iotserver/api/group/id/" + groupId + "/device/all"; - } else { - serviceURL = "/iotserver/api/devices/all"; - } - } else if ($.hasPermission("LIST_OWN_DEVICES")) { - //Get authenticated users devices - if (groupId && groupId != "0") { - serviceURL = "/iotserver/api/group/id/" + groupId + "/device/all"; - } else { - serviceURL = "/iotserver/api/devices/all"; - } + if (groupId && groupId != "0") { + serviceURL = "/iotserver/api/group/id/" + groupId + "/device/all"; } else { - $("#ast-container").html("Permission denied"); - return; - } - if (searchParam) { - if (searchType == "users") { - serviceURL = serviceURL + "?user=" + searchParam; - } else if (searchType == "user-roles") { - serviceURL = serviceURL + "?role=" + searchParam; - } else { - serviceURL = serviceURL + "?type=" + searchParam; - } + serviceURL = "/iotserver/api/devices/all"; } var successCallback = function (data) { data = JSON.parse(data); var viewModel = {}; + var groups; if (groupId && groupId != "0") { - data.data = data; + groups = data; + } else { + groups = data.data; } - viewModel.devices = data.data; - viewModel.imageLocation = imageResource; - if (!data.data || data.data.length <= 0) { + viewModel.groups = groups; + if (!groups || groups.length <= 0) { if (groupId && groupId != "0") { - $("#ast-container").html($("#no-grouped-devices-div-content").html()); + $("#ast-container-parent").html($("#no-grouped-devices-div-content").html()); } else { - $("#ast-container").html($("#no-devices-div-content").html()); + $("#ast-container-parent").html($("#no-devices-div-content").html()); } } else { var content = template(viewModel); - $("#ast-container").html(content); - /* - * On device checkbox select add parent selected style class - */ - $(deviceCheckbox).click(function () { - addDeviceSelectedClass(this); + $("#ast-container-parent").html(content); + var deviceListing = $("#device-listing"); + var deviceListingSrc = deviceListing.attr("src"); + var imageResource = deviceListing.data("image-resource"); + $.template("device-listing", deviceListingSrc, function (template) { + for (var g in groups) { + if (groups[g].devices && groups[g].devices.length > 0){ + viewModel = {}; + viewModel.devices = groups[g].devices; + viewModel.imageLocation = imageResource; + content = template(viewModel); + }else{ + content = $("#no-devices-in-group-div-content").html(); + } + $("#ast-container-" + groups[g].id).html(content); + } + /* + * On device checkbox select add parent selected style class + */ + $(deviceCheckbox).click(function () { + addDeviceSelectedClass(this); + }); + attachEvents(); + formatDates(); }); - attachEvents(); - formatDates(); } }; invokerUtil.get(serviceURL, @@ -376,7 +385,7 @@ function attachEvents() { */ if (groupId && groupId != "0") { $("a.group-device-link").remove(); - }else{ + } else { $("a.group-device-link").click(function () { var deviceId = $(this).data("deviceid"); var deviceType = $(this).data("devicetype"); diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/device-listing.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/device-listing.hbs index 23eb0086..faef2959 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/device-listing.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/device-listing.hbs @@ -28,13 +28,15 @@ View - + {{#if_owner enrolmentInfo.owner }} + - Group - + Group + + {{/if_owner}} diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/group-listing.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/group-listing.hbs new file mode 100644 index 00000000..3db79968 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/device-listing/public/templates/group-listing.hbs @@ -0,0 +1,19 @@ +{{#each groups}} + {{groupMap this}} +
+
+
+ +
+
+
+
+
+{{/each}} +