(function () { 'use strict'; /** * MODULE DESCRIPTION: * * TODO: Write description here. */ angular.module('portal.dashboard.scriptBank', ['ui.router']) .controller('ScriptBankCtrl', ScriptBankCtrlController); function ScriptBankCtrlController($scope, cache, scriptStores, scriptProvider, accountStores, $mdDialog, uiGridConstants, portalUtils, $log, Triggers, Constants) { $scope.confirmQuestionBox = true; $scope.scripts = []; $scope.userRole = ''; $scope.Constants = Constants; $scope.mCache = cache.request([ new accountStores.Account(), new scriptStores.Scripts() ]).then(function(){ $scope.acct = $scope.mCache.account.data; $scope.scripts = $scope.mCache.scripts.data.values; for (var i = 0; i < $scope.scripts.length; i++) { $scope.scripts[i].subGridOptions = { data: $scope.scripts[i].schedule, columnDefs: [ {field: 'jobId', displayName: 'Job Id', width: '**'}, {field: 'startDatetime', displayName: 'Start Date/Time', width: '*', cellTemplate: '
{{grid.appScope.formatTimeSub(row.entity.startDatetime)}}
'}, {field: 'endDatetime', displayName: 'End Date/Time', width: '*', cellTemplate: '
{{grid.appScope.formatTimeSub(row.entity.endDatetime)}}
'}, {field: 'frequency', displayName: 'Frequency', width: '*'}, {field: 'maxExecutions', displayName: 'Max Executions', width: '*'}, {field: 'currentExecutionCount', displayName: 'Current Count', width: '*'}, {field: 'lastRun', displayName: 'Last Ran', width: '*', cellTemplate: '
{{grid.appScope.formatTimeSub(row.entity.lastRun)}}
'}, {field: 'executionStatus', displayName: 'Status', width: '*', cellTemplate: 'main/script-bank/cell-templates/script-sched-status.html'}, {field: 'id', displayName: '', width: '100', enableColumnMenu: false, enableFiltering: false, enableHiding: false, cellTemplate: 'main/script-bank/cell-templates/script-sched-actions.html'}, ] }; } }); $scope.scriptGrid = { data: 'scripts', expandableRowTemplate:'
', expandableRowScope: { Constants : $scope.Constants, formatTimeSub : function(value) { if (value === null || value === undefined || value === 0) { return 'None'; } return $scope.formatTime(value); }, deleteSchedule: function(entity) { $scope.deleteSchedule(entity); }, formatStatus: function(entity) { return $scope.formatScriptStatus(entity); }, viewStatus: function(entity) { $scope.viewScriptStatus(entity); } }, paginationPageSizes: [25, 50, 75, 100, 125, 150, 175, 200], paginationPageSize: 25, enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER, toggleModifierKeysToMultiSelect : true, enableRowHeaderSelection: true, enableColumnMenus: true, enableGridMenu: true, exporterMenuCsv: true, multiSelect: false, noUnselect: false, onRegisterApi: function (gridApi) { $scope.scriptGridApi = gridApi; }, columnDefs: [ {field: 'name', displayName: 'Name', width: '*'}, {field: 'command', displayName: 'Command', width: '**' }, {field: 'parameters', displayName: 'Parameters', width: '**', cellTemplate: '
{{grid.appScope.formatParameters(row.entity.parameters)}}
'}, {field: 'schedule', displayName: 'Schedule Count', width: '*', cellTemplate: '
{{row.entity.schedule.length}}
'}, {field: 'id', displayName: 'Actions', width: '125', enableColumnMenu: false, enableFiltering: false, enableHiding: false, cellTemplate: 'main/script-bank/cell-templates/script-actions.html' } ], }; $scope.formatScriptStatus = function(entity) { if (entity.currentExecutionCount === null) { return 'Not Ran'; } if (entity.executionStatus !== 0) { return 'Failed'; } return 'Success'; }; $scope.formatParameters = function(parameters) { var params = ''; for (var i = 0; i < parameters.length; i++) { params += parameters[i].parameter + ' '; } return params; }; $scope.formatTime = function(value) { return moment(value).format('MMM D, YYYY hh:mm:ss A'); }; $scope.run = function(entity) { portalUtils.confirmBox('run', 'Run Script', ['Are you sure you want to manually run the script', entity.name], true, function(value) { if (value.confirm) { $scope.confirmQuestionBox = !value.checkbox; $scope.runScript(entity); } }); }; $scope.runScript = function(entity) { scriptProvider.runScript(entity).then(function() { portalUtils.toast('Script scheduled to run', ['top', 'left'], 2000); }, function(err){ portalUtils.toast('Unable to run script: ' + err.message, ['top', 'left'], 4000); new scriptStores.Scripts().refresh(); }); }; $scope.deleteSchedule = function(entity) { portalUtils.confirmBox('deleteSched', 'Delete Schedule', ['Are you sure you want to delete the schedule', entity.jobId], false, function(value) { if (value.confirm) { $scope.deleteSched(entity); } }); }; $scope.deleteSched = function(entity) { scriptProvider.deleteScriptSchedule(entity).then(function(){ portalUtils.toast('Script schedule deleted successfully', ['top', 'left'], 2000); }, function(err){ portalUtils.toast('Unable to delete script : ' + err.message, ['top', 'left'], 4000); }); }; $scope.delete = function(entity) { if ($scope.confirmQuestionBox) { portalUtils.confirmBox('deleteScript', 'Delete Script', ['Are you sure you want to delete the script', entity.name], true, function(value) { if (value.confirm) { $scope.confirmQuestionBox = !value.checkbox; $scope.deleteScript(entity); } }); } else { $scope.deleteScript(entity); } }; $scope.deleteScript = function(entity) { scriptProvider.deleteScript(entity).then(function() { portalUtils.toast('Script deleted successfully', ['top', 'left'], 2000); },function(err){ portalUtils.toast('Unable to delete script: ' + err.message, ['top', 'left'], 4000); }); }; $scope.edit = function(entity) { $mdDialog.show({ templateUrl: 'common/partials/add-script/add-script.html', controller: 'AddScriptCtrl', escapeToClose: false, locals: { modalInstance: { header: 'Edit Script', script: entity } } }); }; $scope.addScript = function() { $mdDialog.show({ templateUrl: 'common/partials/add-script/add-script.html', controller: 'AddScriptCtrl', escapeToClose: false, locals: { modalInstance: { header: 'Add New Script', } } }); }; $scope.viewScriptStatus = function(entity) { $mdDialog.show({ templateUrl: 'common/partials/script-status/view-script-status.html', controller: 'ScriptStatusCtrl', escapeToClose: false, locals: { modalInstance: { jobId: entity.jobId, header: 'Script Status' } } }); }; $scope.toggleFiltering = function() { $scope.scriptGrid.enableFiltering = !$scope.scriptGrid.enableFiltering; $scope.scriptGridApi.core.notifyDataChange( uiGridConstants.dataChange.COLUMN ); }; $scope.$on('$destroy', function () { $scope.mCache.unlisten(); }); } //end ScriptBankCtrlController function })();