summaryrefslogtreecommitdiff
path: root/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/mistral/static/mistral/js/mistral.workbook.controllers.js')
-rw-r--r--extensions/mistral/static/mistral/js/mistral.workbook.controllers.js141
1 files changed, 73 insertions, 68 deletions
diff --git a/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js b/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
index 77d15e9..f4830dd 100644
--- a/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
+++ b/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
@@ -4,82 +4,87 @@
4(function() { 4(function() {
5 'use strict'; 5 'use strict';
6 6
7 angular.module('mistral') 7 angular
8 .module('mistral')
8 .value('baseActionID', 'action') 9 .value('baseActionID', 'action')
9 .value('baseWorkflowID', 'workflow') 10 .value('baseWorkflowID', 'workflow')
10 .controller('workbookCtrl', 11 .controller('WorkbookController', WorkbookController);
11 ['$scope', 'mistral.workbook.models', '$http',
12 'baseActionID', 'baseWorkflowID',
13 function($scope, models, $http, baseActionId, baseWorkflowId) {
14 $scope.init = function(id, yaml, commitUrl, discardUrl) {
15 $scope.workbookID = id;
16 $scope.commitUrl = commitUrl;
17 $scope.discardUrl = discardUrl;
18 if ( id !== undefined ) {
19 $scope.workbook = models.Workbook.create(jsyaml.safeLoad(yaml));
20 } else {
21 $scope.workbook = models.Workbook.create({name: 'My Workbook'});
22 }
23 $scope.root = models.Root.create();
24 $scope.root.set('workbook', $scope.workbook);
25 12
26 $scope.root.set('standardActions', { 13 WorkbookController.$inject = ['mistral.workbook.models', '$http',
27 'nova.create_server': ['image', 'flavor', 'network_id'], 14 '$window', 'baseActionID', 'baseWorkflowID'];
28 'neutron.create_network': ['name', 'create_subnet'],
29 'glance.create_image': ['image_url']
30 });
31 };
32 15
33 function getNextIDSuffix(container, regexp) { 16 function WorkbookController(models, $http, $window,
34 var max = Math.max.apply(Math, container.getIDs().map(function(id) { 17 baseActionId, baseWorkflowId) {
35 var match = regexp.exec(id); 18 var vm = this;
36 return match && +match[2]; 19 vm.init = function(id, yaml, commitUrl, discardUrl) {
37 })); 20 vm.workbookID = id;
38 return max > 0 ? max + 1 : 1; 21 vm.commitUrl = commitUrl;
39 } 22 vm.discardUrl = discardUrl;
23 if (angular.isDefined(id)) {
24 vm.workbook = models.Workbook.create(jsyaml.safeLoad(yaml));
25 } else {
26 vm.workbook = models.Workbook.create({name: 'My Workbook'});
27 }
28 vm.root = models.Root.create();
29 vm.root.set('workbook', vm.workbook);
40 30
41 function getWorkbookNextIDSuffix(base) { 31 vm.root.set('standardActions', {
42 var containerName = base + 's', 32 'nova.create_server': ['image', 'flavor', 'network_id'],
43 regexp = /(workflow|action)([0-9]+)/, 33 'neutron.create_network': ['name', 'create_subnet'],
44 container = $scope.workbook.get(containerName); 34 'glance.create_image': ['image_url']
45 if ( !container ) { 35 });
46 throw 'Base should be either "action" or "workflow"!'; 36 };
47 }
48 return getNextIDSuffix(container, regexp);
49 }
50 37
51 $scope.addAction = function() { 38 function getNextIDSuffix(container, regexp) {
52 var nextSuffix = getWorkbookNextIDSuffix(baseActionId), 39 var max = Math.max.apply(Math, container.getIDs().map(function(id) {
53 newID = baseActionId + nextSuffix; 40 var match = regexp.exec(id);
54 $scope.workbook.get('actions').push( 41 return match && +match[2];
55 {name: 'Action ' + nextSuffix}, {id: newID}); 42 }));
56 }; 43 return max > 0 ? max + 1 : 1;
44 }
57 45
58 $scope.addWorkflow = function() { 46 function getWorkbookNextIDSuffix(base) {
59 var nextSuffix = getWorkbookNextIDSuffix(baseWorkflowId), 47 var containerName = base + 's';
60 newID = baseWorkflowId + nextSuffix; 48 var regexp = /(workflow|action)([0-9]+)/;
61 $scope.workbook.get('workflows').push( 49 var container = vm.workbook.get(containerName);
62 {name: 'Workflow ' + nextSuffix}, {id: newID}); 50 if ( !container ) {
63 }; 51 throw new Error('Base should be either "action" or "workflow"!');
52 }
53 return getNextIDSuffix(container, regexp);
54 }
64 55
65 $scope.commitWorkbook = function() { 56 vm.addAction = function() {
66 var data = { 57 var nextSuffix = getWorkbookNextIDSuffix(baseActionId);
67 name: $scope.workbook.get('name').get(), 58 var newID = baseActionId + nextSuffix;
68 yaml: $scope.workbook.toYAML() 59 vm.workbook.get('actions').push(
69 }; 60 {name: 'Action ' + nextSuffix}, {id: newID});
61 };
70 62
71 $http({ 63 vm.addWorkflow = function() {
72 url: $scope.commitUrl, 64 var nextSuffix = getWorkbookNextIDSuffix(baseWorkflowId);
73 method: 'POST', 65 var newID = baseWorkflowId + nextSuffix;
74 data: data 66 vm.workbook.get('workflows').push(
75 }).success(function(data, status, headers, config) { 67 {name: 'Workflow ' + nextSuffix}, {id: newID});
76 document.location = $scope.discardUrl; 68 };
77 });
78 };
79 69
80 $scope.discardWorkbook = function() { 70 vm.commitWorkbook = function() {
81 document.location = $scope.discardUrl; 71 var data = {
82 }; 72 name: vm.workbook.get('name').get(),
73 yaml: vm.workbook.toYAML()
74 };
83 75
84 }]) 76 $http({
85})(); \ No newline at end of file 77 url: vm.commitUrl,
78 method: 'POST',
79 data: data
80 }).success(function() {
81 $window.location.href = vm.discardUrl;
82 });
83 };
84
85 vm.discardWorkbook = function() {
86 $window.location.href = vm.discardUrl;
87 };
88
89 }
90})();