All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.dirigible.template-application-ui-angular.ui.perspective.master-manage.detail.controller.js.template Maven / Gradle / Ivy

There is a newer version: 10.6.27
Show newest version
#set($dollar = '$')
angular.module('page', ["ideUI", "ideView", "entityApi"])
	.config(["messageHubProvider", function (messageHubProvider) {
		messageHubProvider.eventIdPrefix = '${projectName}.${perspectiveName}.${name}';
	}])
	.config(["entityApiProvider", function (entityApiProvider) {
		entityApiProvider.baseUrl = "/services/ts/${projectName}/gen/${genFolderName}/api/${perspectiveName}/${name}Service.ts";
	}])
	.controller('PageController', ['$scope',#if($hasDropdowns) '${dollar}http',#end 'messageHub', 'entityApi', 'Extensions', function ($scope,#if($hasDropdowns) ${dollar}http,#end messageHub, entityApi, Extensions) {
		//-----------------Custom Actions-------------------//
		Extensions.get('dialogWindow', '${projectName}-custom-action').then(function (response) {
			$scope.pageActions = response.filter(e => e.perspective === "${perspectiveName}" && e.view === "${name}" && (e.type === "page" || e.type === undefined));
			$scope.entityActions = response.filter(e => e.perspective === "${perspectiveName}" && e.view === "${name}" && e.type === "entity");
		});

		$scope.triggerPageAction = function (action) {
			messageHub.showDialogWindow(
				action.id,
				{},
				null,
				true,
				action
			);
		};

		$scope.triggerEntityAction = function (action) {
			messageHub.showDialogWindow(
				action.id,
				{
					id: $scope.entity.${primaryKeysString}
				},
				null,
				true,
				action
			);
		};
		//-----------------Custom Actions-------------------//

		function resetPagination() {
			$scope.dataPage = 1;
			$scope.dataCount = 0;
			$scope.dataLimit = 10;
		}
		resetPagination();

		//-----------------Events-------------------//
		messageHub.onDidReceiveMessage("#if($hasReferencedProjection)${referencedProjectionProjectName}.${referencedProjectionPerspectiveName}#else${projectName}.${perspectiveName}#end.${masterEntity}.entitySelected", function (msg) {
			resetPagination();
			$scope.selectedMainEntityId = msg.data.selectedMainEntityId;
			$scope.loadPage($scope.dataPage);
		}, true);

		messageHub.onDidReceiveMessage("#if($hasReferencedProjection)${referencedProjectionProjectName}.${referencedProjectionPerspectiveName}#else${projectName}.${perspectiveName}#end.${masterEntity}.clearDetails", function (msg) {
			$scope.$apply(function () {
				resetPagination();
				$scope.selectedMainEntityId = null;
				$scope.data = null;
			});
		}, true);

		messageHub.onDidReceiveMessage("clearDetails", function (msg) {
			$scope.$apply(function () {
				$scope.entity = {};
				$scope.action = 'select';
			});
		});

		messageHub.onDidReceiveMessage("entityCreated", function (msg) {
			$scope.loadPage($scope.dataPage, $scope.filter);
		});

		messageHub.onDidReceiveMessage("entityUpdated", function (msg) {
			$scope.loadPage($scope.dataPage, $scope.filter);
		});

		messageHub.onDidReceiveMessage("entitySearch", function (msg) {
			resetPagination();
			$scope.filter = msg.data.filter;
			$scope.filterEntity = msg.data.entity;
			$scope.loadPage($scope.dataPage, $scope.filter);
		});
		//-----------------Events-------------------//

		$scope.loadPage = function (pageNumber, filter) {
			let ${masterEntityId} = ${dollar}scope.selectedMainEntityId;
			$scope.dataPage = pageNumber;
			if (!filter && $scope.filter) {
				filter = $scope.filter;
			}
			if (!filter) {
				filter = {};
			}
			if (!filter.$filter) {
				filter.$filter = {};
			}
			if (!filter.$filter.equals) {
				filter.$filter.equals = {};
			}
			filter.$filter.equals.${masterEntityId} = ${masterEntityId};
			entityApi.count(filter).then(function (response) {
				if (response.status != 200) {
					messageHub.showAlertError("${name}", `Unable to count ${name}: '${response.message}'`);
					return;
				}
				if (response.data) {
					$scope.dataCount = response.data;
				}
				filter.$offset = (pageNumber - 1) * $scope.dataLimit;
				filter.$limit = $scope.dataLimit;
				entityApi.search(filter).then(function (response) {
					if (response.status != 200) {
						messageHub.showAlertError("${name}", `Unable to list/filter ${name}: '${response.message}'`);
						return;
					}
#if($hasDates)

					response.data.forEach(e => {
#foreach ($property in $properties)
#if($property.isDateType)
						if (e.${property.name}) {
							e.${property.name} = new Date(e.${property.name});
						}
#end
#end
					});

#end
					$scope.data = response.data;
				});
			});
		};

		$scope.selectEntity = function (entity) {
			$scope.selectedEntity = entity;
		};

		$scope.openDetails = function (entity) {
			$scope.selectedEntity = entity;
			messageHub.showDialogWindow("${name}-details", {
				action: "select",
				entity: entity,
#if($hasDropdowns)
#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
				options${property.name}: $scope.options${property.name},
#end
#end
#end
			});
		};

		$scope.openFilter = function (entity) {
			messageHub.showDialogWindow("${name}-filter", {
				entity: $scope.filterEntity,
#if($hasDropdowns)
#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
				options${property.name}: $scope.options${property.name},
#end
#end
#end
			});
		};

		$scope.createEntity = function () {
			$scope.selectedEntity = null;
			messageHub.showDialogWindow("${name}-details", {
				action: "create",
				entity: {},
				selectedMainEntityKey: "${masterEntityId}",
				selectedMainEntityId: $scope.selectedMainEntityId,
#if($hasDropdowns)
#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
				options${property.name}: $scope.options${property.name},
#end
#end
#end
			}, null, false);
		};

		$scope.updateEntity = function (entity) {
			messageHub.showDialogWindow("${name}-details", {
				action: "update",
				entity: entity,
				selectedMainEntityKey: "${masterEntityId}",
				selectedMainEntityId: $scope.selectedMainEntityId,
#if($hasDropdowns)
#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
				options${property.name}: $scope.options${property.name},
#end
#end
#end			}, null, false);
		};

		$scope.deleteEntity = function (entity) {
			let id = entity.${primaryKeysString};
			messageHub.showDialogAsync(
				'Delete ${name}?',
				`Are you sure you want to delete ${name}? This action cannot be undone.`,
				[{
					id: "delete-btn-yes",
					type: "emphasized",
					label: "Yes",
				},
				{
					id: "delete-btn-no",
					type: "normal",
					label: "No",
				}],
			).then(function (msg) {
				if (msg.data === "delete-btn-yes") {
					entityApi.delete(id).then(function (response) {
						if (response.status != 204) {
							messageHub.showAlertError("${name}", `Unable to delete ${name}: '${response.message}'`);
							return;
						}
						$scope.loadPage($scope.dataPage, $scope.filter);
						messageHub.postMessage("clearDetails");
					});
				}
			});
		};
#if($hasDropdowns)

		//----------------Dropdowns-----------------//
#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
		$scope.options${property.name} = [];
#end
#end

#foreach ($property in $properties)
#if ($property.widgetType == "DROPDOWN")

		${dollar}http.get("${property.widgetDropdownUrl}").then(function (response) {
			${dollar}scope.options${property.name} = response.data.map(e => {
				return {
					value: e.${property.widgetDropDownKey},
					text: e.${property.widgetDropDownValue}
				}
			});
		});
#end
#end

#foreach ($property in $properties)
#if($property.widgetType == "DROPDOWN")
		${dollar}scope.options${property.name}Value = function (optionKey) {
			for (let i = 0; i < ${dollar}scope.options${property.name}.length; i++) {
				if (${dollar}scope.options${property.name}[i].value === optionKey) {
					return ${dollar}scope.options${property.name}[i].text;
				}
			}
			return null;
		};
#end
#end
		//----------------Dropdowns-----------------//
#end

	}]);




© 2015 - 2024 Weber Informatics LLC | Privacy Policy