Skip to content

Instantly share code, notes, and snippets.

@AlexDoanTB
Created February 14, 2020 09:16
Show Gist options
  • Select an option

  • Save AlexDoanTB/6860026b21a823f956d7cf81f84b5762 to your computer and use it in GitHub Desktop.

Select an option

Save AlexDoanTB/6860026b21a823f956d7cf81f84b5762 to your computer and use it in GitHub Desktop.
Entity info button code (ThingsBoard Custom action)
JavaScript
var $injector = widgetContext.$scope.$injector;
var types = $injector.get('types'),
attributeService = $injector.get('attributeService'),
$q = $injector.get('$q'),
$mdDialog = $injector.get('$mdDialog'),
$filter = $injector.get('$filter'),
$document = $injector.get('$document');
getInfoAttributes().then(
(attributes) => {
showInfoDialog(attributes);
},
() => {
showFailedDialog('Unable to get info attributes.');
}
);
function showInfoDialog(attributes) {
$mdDialog.show({
controller: ['attributes', '$mdDialog', InfoDialogController],
controllerAs: 'vm',
template: infoDialogTemplate,
parent: angular.element($document[0].body),
locals: { attributes: attributes },
targetEvent: $event,
clickOutsideToClose: false,
fullscreen: true
}).then(
() => {
openDistrictDetails();
}
);
}
function InfoDialogController(attributes, $mdDialog) {
let vm = this;
vm.entityTitle = entityName;
vm.image = getAttributeValue(attributes, 'image');
vm.address = getAttributeValue(attributes, 'address');
vm.close = () => {
$mdDialog.cancel();
}
vm.openDetails = () => {
$mdDialog.hide();
}
}
function getAttributeValue(attributes, key) {
var foundAttributes = $filter('filter')(attributes, {key: key}, true);
if (foundAttributes.length > 0) {
return foundAttributes[0].value;
} else {
return null;
}
}
function openDistrictDetails() {
var params = {
entityId: entityId,
entityName: entityName
}
widgetContext.stateController.openState('district_view', params, false);
}
function getInfoAttributes() {
var deferred = $q.defer();
attributeService.getEntityAttributesValues(entityId.entityType, entityId.id,
types.attributesScope.server.value, 'image,address',
{ignoreLoading: true}).then(
(attributes) => {
if (attributes && attributes.length) {
deferred.resolve(attributes);
} else {
deferred.reject();
}
},
() => {
deferred.reject();
}
);
return deferred.promise;
}
function showFailedDialog(content) {
$mdDialog.show(
$mdDialog.alert()
.parent(angular.element($document[0].body))
.clickOutsideToClose(true)
.title(`Unable to display info for ${entityName}`)
.textContent(content)
.ariaLabel('Action Demo Dialog')
.ok('Close')
.targetEvent($event)
);
}
HTML
<md-dialog aria-label="Info">
<form name="theForm">
<md-toolbar>
<div class="md-toolbar-tools">
<h2>{{vm.entityTitle}}</h2>
<span flex></span>
<md-button class="md-icon-button" ng-click="vm.close()">
<ng-md-icon icon="close" aria-label="Close"></ng-md-icon
</md-button>
</div>
</md-toolbar>
<md-dialog-content>
<md-content class="md-padding">
<div layout="column" flex>
<div><h3>{{vm.address}}</h3></div>
<div style="padding-top: 20px;"><img src="{{vm.image}}"></img></div>
<div layout="row">
<md-button class="md-raised md-primary" ng-click="vm.openDetails()">
District details
</md-button>
<span flex></span>
</div>
</div>
<md-content>
</<md-dialog-content>
<md-dialog-actions layout="row">
<span flex></span>
<md-button ng-click="vm.close()" style="margin-right:20px;">Close</md-button>
</md-dialog-actions>
</form>
</md-dialog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment