|
| 1 | +"use strict"; |
| 2 | +(function() { |
| 3 | + angular.module("openshiftConsole").component('editEnvironmentFrom', { |
| 4 | + controller: [ |
| 5 | + '$attrs', |
| 6 | + '$filter', |
| 7 | + '$scope', |
| 8 | + 'keyValueEditorUtils', |
| 9 | + EditEnvironmentFrom |
| 10 | + ], |
| 11 | + //controllerAs: 'ctrl', |
| 12 | + bindings: { |
| 13 | + addRowLink: '@', |
| 14 | + entries: '=', |
| 15 | + envFromSelectorOptions: '<', |
| 16 | + selectorPlaceholder: '@' |
| 17 | + }, |
| 18 | + templateUrl: 'views/directives/edit-environment-from.html' |
| 19 | + }); |
| 20 | + |
| 21 | + function EditEnvironmentFrom($attrs, |
| 22 | + $filter, |
| 23 | + $scope, |
| 24 | + utils) { |
| 25 | + var ctrl = this; |
| 26 | + |
| 27 | + var canI = $filter('canI'); |
| 28 | + var humanizeKind = $filter('humanizeKind'); |
| 29 | + |
| 30 | + ctrl.$id = $scope.$id; |
| 31 | + ctrl.setFocusClass = 'edit-environment-from-set-focus-' + ctrl.$id; |
| 32 | + |
| 33 | + var addEntry = function(entries, entry) { |
| 34 | + entries && entries.push(entry || {}); |
| 35 | + }; |
| 36 | + |
| 37 | + ctrl.onAddRow = function() { |
| 38 | + addEntry(ctrl.envFromEntries); |
| 39 | + utils.setFocusOn('.'+ ctrl.setFocusClass); |
| 40 | + }; |
| 41 | + |
| 42 | + ctrl.deleteEntry = function(start, deleteCount) { |
| 43 | + if(ctrl.entries && !ctrl.entries.length) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + ctrl.envFromEntries.splice(start, deleteCount); |
| 48 | + if(!ctrl.envFromEntries.length && ctrl.addRowLink) { |
| 49 | + addEntry(ctrl.envFromEntries); |
| 50 | + } |
| 51 | + |
| 52 | + ctrl.updateEntries(ctrl.envFromEntries); |
| 53 | + $scope.forms.editEnvironmentFrom.$setDirty(); |
| 54 | + }; |
| 55 | + |
| 56 | + ctrl.isEnvFromReadonly = function(entry) { |
| 57 | + return ctrl.isReadonlyAny || |
| 58 | + entry.isReadonlyValue === true || |
| 59 | + ((entry.secretRef || entry.configMapRef) && !entry.selectedEnvFrom) || |
| 60 | + _.isEmpty(ctrl.envFromSelectorOptions); |
| 61 | + }; |
| 62 | + |
| 63 | + ctrl.groupByKind = function(object) { |
| 64 | + return humanizeKind(object.kind); |
| 65 | + }; |
| 66 | + |
| 67 | + ctrl.envFromObjectSelected = function(index, entry, selected) { |
| 68 | + var newEnvFrom = {}; |
| 69 | + |
| 70 | + switch (selected.kind) { |
| 71 | + case 'Secret': |
| 72 | + newEnvFrom.secretRef = { |
| 73 | + name: selected.metadata.name |
| 74 | + }; |
| 75 | + delete ctrl.envFromEntries[index].configMapRef; |
| 76 | + break; |
| 77 | + case 'ConfigMap': |
| 78 | + newEnvFrom.configMapRef = { |
| 79 | + name: selected.metadata.name |
| 80 | + }; |
| 81 | + delete ctrl.envFromEntries[index].secretRef; |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + _.assign(ctrl.envFromEntries[index], newEnvFrom); |
| 86 | + ctrl.updateEntries(ctrl.envFromEntries); |
| 87 | + }; |
| 88 | + |
| 89 | + ctrl.updateEntries = function(entries) { |
| 90 | + ctrl.entries = _.filter(_.clone(entries), function (val) { |
| 91 | + return val.secretRef || val.configMapRef; |
| 92 | + }); |
| 93 | + }; |
| 94 | + |
| 95 | + ctrl.updateEnvFromEntries = function(entries) { |
| 96 | + ctrl.envFromEntries = _.clone(entries || []); |
| 97 | + |
| 98 | + if(!ctrl.envFromEntries.length) { |
| 99 | + addEntry(ctrl.envFromEntries); |
| 100 | + } |
| 101 | + |
| 102 | + _.each(ctrl.envFromEntries, function(entry) { |
| 103 | + if(entry) { |
| 104 | + if (entry.configMapRef) { |
| 105 | + entry.isReadonlyValue = !canI('configmaps', 'get'); |
| 106 | + } |
| 107 | + |
| 108 | + if (entry.secretRef) { |
| 109 | + entry.isReadonlyValue = !canI('secrets', 'get'); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | + }; |
| 114 | + |
| 115 | + var getReferenceValue = function(option) { |
| 116 | + var referenceValue; |
| 117 | + |
| 118 | + switch(option.kind) { |
| 119 | + case 'ConfigMap': |
| 120 | + referenceValue = _.find(ctrl.envFromEntries, {configMapRef: {name: option.metadata.name}}); |
| 121 | + break; |
| 122 | + case 'Secret': |
| 123 | + referenceValue = _.find(ctrl.envFromEntries, {secretRef: {name: option.metadata.name}}); |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + return referenceValue; |
| 128 | + }; |
| 129 | + |
| 130 | + ctrl.checkEntries = function(option) { |
| 131 | + return !!(getReferenceValue(option)); |
| 132 | + }; |
| 133 | + |
| 134 | + var findReferenceValueForEntries = function(entries, envFromSelectorOptions) { |
| 135 | + _.each(envFromSelectorOptions, function(option) { |
| 136 | + var referenceValue = getReferenceValue(option); |
| 137 | + |
| 138 | + if (referenceValue) { |
| 139 | + _.set(referenceValue, 'selectedEnvFrom', option); |
| 140 | + } |
| 141 | + }); |
| 142 | + }; |
| 143 | + |
| 144 | + angular.extend($scope, { |
| 145 | + dragControlListeners: { |
| 146 | + accept: function (sourceItemHandleScope, destSortableScope) { |
| 147 | + return sourceItemHandleScope.itemScope.sortableScope.$id === destSortableScope.$id; |
| 148 | + }, |
| 149 | + orderChanged: function() { |
| 150 | + $scope.forms.editEnvironmentFrom.$setDirty(); |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + ctrl.$onInit = function() { |
| 156 | + ctrl.updateEnvFromEntries(ctrl.entries); |
| 157 | + findReferenceValueForEntries(ctrl.envFromEntries, ctrl.envFromSelectorOptions); |
| 158 | + |
| 159 | + if('cannotAdd' in $attrs) { |
| 160 | + ctrl.cannotAdd = true; |
| 161 | + } |
| 162 | + |
| 163 | + if('cannotDelete' in $attrs) { |
| 164 | + ctrl.cannotDeleteAny = true; |
| 165 | + } |
| 166 | + |
| 167 | + if('cannotSort' in $attrs) { |
| 168 | + ctrl.cannotSort = true; |
| 169 | + } |
| 170 | + |
| 171 | + if('isReadonly' in $attrs) { |
| 172 | + ctrl.isReadonlyAny = true; |
| 173 | + } |
| 174 | + |
| 175 | + if('showHeader' in $attrs) { |
| 176 | + ctrl.showHeader = true; |
| 177 | + } |
| 178 | + |
| 179 | + if(ctrl.envFromEntries && !ctrl.envFromEntries.length) { |
| 180 | + addEntry(ctrl.envFromEntries); |
| 181 | + } |
| 182 | + }; |
| 183 | + |
| 184 | + ctrl.$onChanges = function(changes) { |
| 185 | + if(changes.entries) { |
| 186 | + ctrl.updateEnvFromEntries(changes.entries.currentValue); |
| 187 | + } |
| 188 | + |
| 189 | + if(changes.envFromSelectorOptions) { |
| 190 | + findReferenceValueForEntries(ctrl.envFromEntries, changes.envFromSelectorOptions.currentValue); |
| 191 | + } |
| 192 | + }; |
| 193 | + } |
| 194 | +})(); |
| 195 | + |
0 commit comments