(function () { 'use strict'; var app = angular.module('rulesAdmin'); var templatePath = modulesSharedResourcesUrl + 'Modules/RulesAdmin/Views/'; app.directive('rulesAdmin', ['config', 'common', '$location', '$modal', '$q', 'rulesAdminDataContext', '$timeout', 'rulesAdminService', 'user', function (config, common, $location, $modal, $q, rulesAdminDataContext, $timeout, rulesAdminService, user) { return { restrict: 'E', templateUrl: templatePath + 'rules.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('rulesAdmin'); var logSuccess = getLogFn('rulesAdmin', "success"); $scope.orderByPredicate = '-lastModified'; activate(); function activate() { NProgress.done(); log('Activated rules View'); getRules(); } function getRules() { rulesAdminDataContext.getRules().then(function (rules) { $scope.rules = rules; $scope.rulesLoaded = true; }); }; $scope.getUserDetails = function (rule) { user.getUserSimpleProfile(rule.lastModifiedBy).then(function (theUser) { rule.updater = theUser }); }; $scope.toggleEnabled = function (rule) { rulesAdminDataContext.toggleEnabled(rule).then(function (rules) { logSuccess("Rule updated successfully"); getRules(); }); } $scope.deleteRule = function (rule) { rulesAdminDataContext.deleteRule(rule).then(function (rules) { logSuccess("Rule deleted successfully"); getRules(); }); } $scope.manageRule = function (rule) { if (rule) { rulesAdminDataContext.getRuleById(rule).then(function (data) { rulesAdminService.setRule(data); $location.path('/rules/manage'); }); rulesAdminService.setRule(rule); } else { rule = rulesAdminService.createRule(); rulesAdminService.setRule(rule); $location.path('/rules/manage'); } } } }]); app.directive('manageRule', ['config', 'common', '$location', '$modal', '$q', 'OrganisationAdminService', 'rulesAdminDataContext', 'myBadgesAdminService', 'myBadgesAdminDataContext', '$timeout', 'rulesAdminService', function (config, common, $location, $modal, $q,OrganisationAdminService, rulesAdminDataContext, myBadgesAdminService, myBadgesAdminDataContext, $timeout, rulesAdminService) { return { restrict: 'E', templateUrl: templatePath + 'managerule.html', scope: { singleRule: '=', multiRule: '=' }, link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('rulesAdmin'); var logSuccess = getLogFn('rulesAdmin', "success"); activate(); function activate() { NProgress.done(); log('Activated rules View'); } $scope.rule = rulesAdminService.getRule(); if (!$scope.rule) { $location.path('/rules'); return; } if (!$scope.rule.rule.collection) { var ruleCollection = rulesAdminService.createRuleCollection(); ruleCollection.collection.push($scope.rule.rule) $scope.rule.rule = ruleCollection; } $scope.collectionTypes = rulesAdminService.collectionTypes(); $scope.logicTypes = rulesAdminService.logicTypes(); $scope.conditionTypes = rulesAdminService.conditionTypes(); $scope.targetTypes = rulesAdminService.targetTypes(); $scope.createCollection = function (collection, type) { collection.push(rulesAdminService.collection(type)); } $scope.createCondition = function (collection, type) { collection.push(rulesAdminService.conditionTypes(type)); } $scope.getTargets = function (collection, type) { collection.targetValues = rulesAdminService.targetValues(type); collection.comparisonValues = rulesAdminService.comparisonValues(type, collection.condition.target); } $scope.setTargets = function (collection, type) { collection.targetValues = rulesAdminService.targetValues(type); collection.comparisonValues = rulesAdminService.comparisonValues(type, collection.condition.target); collection.condition.comparison = null; collection.condition.data = null; collection.condition.min = null; collection.condition.max = null; collection.condition.targetType = null; collection.condition.target = null; } $scope.setTargetType = function (condition, collection) { if (condition.target == 'int1' || condition.target == 'int2') { condition.targetType = 'int'; } if (condition.target == 'text1' || condition.target == 'text2') { condition.targetType = 'string'; } if (condition.target == 'date1') { condition.targetType = 'dateTime'; } if (collection) { condition.comparison = null; condition.data = null; condition.min = null; condition.max = null; collection.comparisonValues = rulesAdminService.comparisonValues(condition.type, condition.target); } } $scope.saveRule = function () { $scope.isSaving = true; if (!$scope.rule.id) { rulesAdminDataContext.createRule($scope.rule).then(function (rules) { logSuccess("Rule created successfully"); $scope.isSaving = false; $location.path('/rules'); }); } else { rulesAdminDataContext.saveRule($scope.rule).then(function (rules) { logSuccess("Rule updated successfully"); $scope.isSaving = false; $location.path('/rules'); }); } } $scope.remove = function ($index, collection) { if (collection) { for (i in $scope.rule.rule.collection) { if ($scope.rule.rule.collection[i] == collection) { $scope.rule.rule.collection.splice(i, 1); return; } if ($scope.rule.rule.collection[i].collection) { removeCollection($scope.rule.rule.collection[i].collection, collection); } } } } function removeCollection(collections, collection) { for (i in collections) { if (collections[i] == collection) { collections.splice(i, 1); return; } if (collections[i].collection) { removeCollection(collections[i].collection, collection); } } } // Get the already selected badge details $scope.getBadgeDetails = function (id) { myBadgesAdminDataContext.getTemplate(id).then(function (data) { $scope.selectedBadge = data; }); } // Modal to preview a badge template $scope.selectBadge = function () { $modal.open({ templateUrl: templatePath + 'selectbadge.html', controller: selectBadgeController, size: 'lg', backdrop: 'static', resolve: { rule: function () { return $scope.rule; } } }); } // Controller for previewing badge template var selectBadgeController = function (common, $scope, $modalInstance, rule) { $scope.rule = rule; $scope.rule.outcomes[0].itemId = null; // Get all the badge templates accessible by the current user function getBadgeTemplates() { myBadgesAdminService.getBadgeTemplates().then(function (data) { $scope.badges = data; $scope.loadedyet = true; }); } $scope.selectBadge = function (badgeId) { $scope.rule.outcomes[0].itemId = badgeId; $modalInstance.dismiss('cancel'); } getBadgeTemplates(); // close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } } }]); })();