Commit c692699f7bda203731055c6777e1d8347d9b099c

Authored by Igor Kulikov
Committed by mp-loki
1 parent 58cbb116

TB-47: Show assigned customer title in device card.

... ... @@ -42,6 +42,20 @@ public class CustomerController extends BaseController {
42 42 }
43 43 }
44 44
  45 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
  46 + @RequestMapping(value = "/customer/{customerId}/title", method = RequestMethod.GET, produces = "application/text")
  47 + @ResponseBody
  48 + public String getCustomerTitleById(@PathVariable("customerId") String strCustomerId) throws ThingsboardException {
  49 + checkParameter("customerId", strCustomerId);
  50 + try {
  51 + CustomerId customerId = new CustomerId(toUUID(strCustomerId));
  52 + Customer customer = checkCustomerId(customerId);
  53 + return customer.getTitle();
  54 + } catch (Exception e) {
  55 + throw handleException(e);
  56 + }
  57 + }
  58 +
45 59 @PreAuthorize("hasAuthority('TENANT_ADMIN')")
46 60 @RequestMapping(value = "/customer", method = RequestMethod.POST)
47 61 @ResponseBody
... ...
... ... @@ -23,6 +23,7 @@ function CustomerService($http, $q) {
23 23 var service = {
24 24 getCustomers: getCustomers,
25 25 getCustomer: getCustomer,
  26 + getCustomerTitle: getCustomerTitle,
26 27 deleteCustomer: deleteCustomer,
27 28 saveCustomer: saveCustomer
28 29 }
... ... @@ -60,6 +61,17 @@ function CustomerService($http, $q) {
60 61 return deferred.promise;
61 62 }
62 63
  64 + function getCustomerTitle(customerId) {
  65 + var deferred = $q.defer();
  66 + var url = '/api/customer/' + customerId + '/title';
  67 + $http.get(url, null).then(function success(response) {
  68 + deferred.resolve(response.data);
  69 + }, function fail(response) {
  70 + deferred.reject(response.data);
  71 + });
  72 + return deferred.promise;
  73 + }
  74 +
63 75 function saveCustomer(customer) {
64 76 var deferred = $q.defer();
65 77 var url = '/api/customer';
... ...
... ... @@ -26,6 +26,7 @@ import gridTemplate from './grid.tpl.html';
26 26
27 27 export default angular.module('thingsboard.directives.grid', [thingsboardScopeElement, thingsboardDetailsSidenav])
28 28 .directive('tbGrid', Grid)
  29 + .controller('ItemCardController', ItemCardController)
29 30 .directive('tbGridCardContent', GridCardContent)
30 31 .filter('range', RangeFilter)
31 32 .name;
... ... @@ -44,14 +45,52 @@ function RangeFilter() {
44 45 }
45 46
46 47 /*@ngInject*/
47   -function GridCardContent($compile) {
  48 +function ItemCardController() {
  49 +
  50 + var vm = this; //eslint-disable-line
  51 +
  52 +}
  53 +
  54 +/*@ngInject*/
  55 +function GridCardContent($compile, $controller) {
48 56 var linker = function(scope, element) {
  57 +
  58 + var controllerInstance = null;
  59 +
49 60 scope.$watch('itemTemplate',
50   - function(value) {
51   - element.html(value);
52   - $compile(element.contents())(scope);
  61 + function() {
  62 + initContent();
  63 + }
  64 + );
  65 + scope.$watch('itemController',
  66 + function() {
  67 + initContent();
53 68 }
54 69 );
  70 + scope.$watch('parentCtl',
  71 + function() {
  72 + controllerInstance.parentCtl = scope.parentCtl;
  73 + }
  74 + );
  75 + scope.$watch('item',
  76 + function() {
  77 + controllerInstance.item = scope.item;
  78 + }
  79 + );
  80 +
  81 + function initContent() {
  82 + if (scope.itemTemplate && scope.itemController && !controllerInstance) {
  83 + element.html(scope.itemTemplate);
  84 + var locals = {};
  85 + angular.extend(locals, {$scope: scope, $element: element});
  86 + var controller = $controller(scope.itemController, locals, true, 'vm');
  87 + controller.instance = controller();
  88 + controllerInstance = controller.instance;
  89 + controllerInstance.item = scope.item;
  90 + controllerInstance.parentCtl = scope.parentCtl;
  91 + $compile(element.contents())(scope);
  92 + }
  93 + }
55 94 };
56 95
57 96 return {
... ... @@ -61,6 +100,7 @@ function GridCardContent($compile) {
61 100 parentCtl: "=parentCtl",
62 101 gridCtl: "=gridCtl",
63 102 itemTemplate: "=itemTemplate",
  103 + itemController: "=itemController",
64 104 item: "=item"
65 105 }
66 106 };
... ... @@ -291,6 +331,11 @@ function GridController($scope, $state, $mdDialog, $document, $q, $timeout, $tra
291 331 } else if (vm.config.itemCardTemplateUrl) {
292 332 vm.itemCardTemplate = $templateCache.get(vm.config.itemCardTemplateUrl);
293 333 }
  334 + if (vm.config.itemCardController) {
  335 + vm.itemCardController = vm.config.itemCardController;
  336 + } else {
  337 + vm.itemCardController = 'ItemCardController';
  338 + }
294 339
295 340 vm.parentCtl = vm.config.parentCtl || vm;
296 341
... ...
... ... @@ -43,7 +43,7 @@
43 43 </md-card-title>
44 44 </section>
45 45 <md-card-content flex>
46   - <tb-grid-card-content grid-ctl="vm" parent-ctl="vm.parentCtl" item-template="vm.itemCardTemplate" item="rowItem[n]"></tb-grid-card-content>
  46 + <tb-grid-card-content grid-ctl="vm" parent-ctl="vm.parentCtl" item-controller="vm.itemCardController" item-template="vm.itemCardTemplate" item="rowItem[n]"></tb-grid-card-content>
47 47 </md-card-content>
48 48 <md-card-actions layout="row" layout-align="end end">
49 49 <md-button ng-if="action.isEnabled(rowItem[n])" ng-disabled="loading" class="md-icon-button md-primary" ng-repeat="action in vm.actionsList"
... ...
... ... @@ -15,6 +15,4 @@
15 15 limitations under the License.
16 16
17 17 -->
18   -<div class="tb-small" ng-if="item &&
19   - item.customerId.id != parentCtl.types.id.nullUid &&
20   - parentCtl.devicesScope === 'tenant'" translate>device.assignedToCustomer</div>
  18 +<div class="tb-small" ng-show="vm.isAssignedToCustomer()">{{'device.assignedToCustomer' | translate}} '{{vm.customerTitle}}'</div>
... ...
... ... @@ -24,7 +24,36 @@ import deviceCredentialsTemplate from './device-credentials.tpl.html';
24 24 /* eslint-enable import/no-unresolved, import/default */
25 25
26 26 /*@ngInject*/
27   -export default function DeviceController(userService, deviceService, customerService, $scope, $controller, $state, $stateParams, $document, $mdDialog, $q, $translate, types) {
  27 +export function DeviceCardController($scope, types, customerService) {
  28 +
  29 + var vm = this;
  30 +
  31 + vm.types = types;
  32 +
  33 + vm.isAssignedToCustomer = function() {
  34 + if (vm.item && vm.item.customerId && vm.parentCtl.devicesScope === 'tenant',
  35 + vm.item.customerId.id != vm.types.id.nullUid) {
  36 + return true;
  37 + }
  38 + return false;
  39 + }
  40 +
  41 + $scope.$watch('vm.item',
  42 + function() {
  43 + if (vm.item && vm.item.customerId && vm.item.customerId.id != vm.types.id.nullUid) {
  44 + customerService.getCustomerTitle(vm.item.customerId.id).then(
  45 + function success(title) {
  46 + vm.customerTitle = title;
  47 + }
  48 + );
  49 + }
  50 + }
  51 + );
  52 +}
  53 +
  54 +
  55 +/*@ngInject*/
  56 +export function DeviceController(userService, deviceService, customerService, $scope, $controller, $state, $stateParams, $document, $mdDialog, $q, $translate, types) {
28 57
29 58 var customerId = $stateParams.customerId;
30 59
... ... @@ -47,6 +76,7 @@ export default function DeviceController(userService, deviceService, customerSer
47 76
48 77 getItemTitleFunc: getDeviceTitle,
49 78
  79 + itemCardController: 'DeviceCardController',
50 80 itemCardTemplateUrl: deviceCard,
51 81 parentCtl: vm,
52 82
... ...
... ... @@ -21,7 +21,7 @@ import thingsboardApiDevice from '../api/device.service';
21 21 import thingsboardApiCustomer from '../api/customer.service';
22 22
23 23 import DeviceRoutes from './device.routes';
24   -import DeviceController from './device.controller';
  24 +import {DeviceController, DeviceCardController} from './device.controller';
25 25 import AssignDeviceToCustomerController from './assign-to-customer.controller';
26 26 import AddDevicesToCustomerController from './add-devices-to-customer.controller';
27 27 import ManageDeviceCredentialsController from './device-credentials.controller';
... ... @@ -40,6 +40,7 @@ export default angular.module('thingsboard.device', [
40 40 ])
41 41 .config(DeviceRoutes)
42 42 .controller('DeviceController', DeviceController)
  43 + .controller('DeviceCardController', DeviceCardController)
43 44 .controller('AssignDeviceToCustomerController', AssignDeviceToCustomerController)
44 45 .controller('AddDevicesToCustomerController', AddDevicesToCustomerController)
45 46 .controller('ManageDeviceCredentialsController', ManageDeviceCredentialsController)
... ...