diff --git a/app/scripts/directives/editEnvironmentFrom.js b/app/scripts/directives/editEnvironmentFrom.js index 709ca35835..ffc3517d71 100644 --- a/app/scripts/directives/editEnvironmentFrom.js +++ b/app/scripts/directives/editEnvironmentFrom.js @@ -11,7 +11,8 @@ addRowLink: '@', // creates a link to "add row" and sets its text label entries: '=', // an array of objects containing configmaps and secrets envFromSelectorOptions: '<', // dropdown selector options, an array of objects - selectorPlaceholder: '@' // placeholder copy for dropdown selector + selectorPlaceholder: '@', // placeholder copy for dropdown selector + isReadonly: '' // display as read only values }, templateUrl: 'views/directives/edit-environment-from.html' }); @@ -20,7 +21,6 @@ $filter, utils) { var ctrl = this; - var canI = $filter('canI'); var humanizeKind = $filter('humanizeKind'); var uniqueId = _.uniqueId(); @@ -42,6 +42,7 @@ } ctrl.envFromEntries.splice(start, deleteCount); + if(!ctrl.envFromEntries.length && ctrl.addRowLink) { addEntry(ctrl.envFromEntries); } @@ -50,18 +51,22 @@ ctrl.editEnvironmentFromForm.$setDirty(); }; + ctrl.hasOptions = function() { + return !_.isEmpty(ctrl.envFromSelectorOptions); + }; + + ctrl.hasEntries = function() { + return angular.toJson(ctrl.entries) !== '[{}]' && ctrl.entries && ctrl.entries.length >= 1; + }; + ctrl.isEnvFromReadonly = function(entry) { - return ctrl.isReadonlyAny || - entry.isReadonlyValue === true || - ((entry.secretRef || entry.configMapRef) && !entry.selectedEnvFrom) || - _.isEmpty(ctrl.envFromSelectorOptions); + return ctrl.isReadonly === true || entry && entry.isReadonly === true; }; ctrl.groupByKind = function(object) { return humanizeKind(object.kind); }; - //ctrl.uniqueForValue = utils.uniqueForValue; ctrl.dragControlListeners = { accept: function (sourceItemHandleScope, destSortableScope) { return sourceItemHandleScope.itemScope.sortableScope.$id === destSortableScope.$id; @@ -99,8 +104,13 @@ }); }; - var updateEnvFromEntries = function(entries) { - ctrl.envFromEntries = entries || []; + var getReferenceValues = function(option) { + var kindRef = _.camelCase(option.kind) + 'Ref'; + return _.filter(ctrl.envFromEntries, [kindRef, {name:option.metadata.name}]); + }; + + var updateEnvFromEntries = function() { + ctrl.envFromEntries = ctrl.entries || []; if(!ctrl.envFromEntries.length) { addEntry(ctrl.envFromEntries); @@ -109,56 +119,28 @@ _.each(ctrl.envFromEntries, function(entry) { if(entry) { if(entry.configMapRef && !canI('configmaps', 'get')) { - entry.isReadonlyValue = true; + entry.isReadonly = true; } if(entry.secretRef && !canI('secrets', 'get')) { - entry.isReadonlyValue = true; + entry.isReadonly = true; } } }); - }; - - var getReferenceValue = function(option) { - var referenceValue; - switch(option.kind) { - case 'ConfigMap': - referenceValue = _.find(ctrl.envFromEntries, {configMapRef: {name: option.metadata.name}}); - break; - case 'Secret': - referenceValue = _.find(ctrl.envFromEntries, {secretRef: {name: option.metadata.name}}); - break; - } + _.each(ctrl.envFromSelectorOptions, function(option) { + _.each(getReferenceValues(option), function(val, i) { + _.set(val, 'selectedEnvFrom', option); - return referenceValue; - }; - - ctrl.checkEntries = function(option, entrySelectedEnvFrom) { - if(option === entrySelectedEnvFrom) { - return false; - } - - return !!(getReferenceValue(option)); - }; - - var findReferenceValueForEntries = function(entries, envFromSelectorOptions) { - ctrl.cannotAdd = (ctrl.isReadonlyAny || _.isEmpty(envFromSelectorOptions)); - - if(envFromSelectorOptions) { - _.each(envFromSelectorOptions, function(option) { - var referenceValue = getReferenceValue(option); - - if (referenceValue) { - _.set(referenceValue, 'selectedEnvFrom', option); + if (i > 0) { + _.set(val, 'duplicateEnvFrom', true); } }); - } + }); }; ctrl.$onInit = function() { - updateEnvFromEntries(ctrl.entries); - findReferenceValueForEntries(ctrl.entries, ctrl.envFromSelectorOptions); + updateEnvFromEntries(); if('cannotDelete' in $attrs) { ctrl.cannotDeleteAny = true; @@ -168,10 +150,6 @@ ctrl.cannotSort = true; } - if('isReadonly' in $attrs) { - ctrl.isReadonlyAny = true; - } - if('showHeader' in $attrs) { ctrl.showHeader = true; } @@ -182,12 +160,8 @@ }; ctrl.$onChanges = function(changes) { - if(changes.entries) { - updateEnvFromEntries(changes.entries.currentValue); - } - - if(changes.envFromSelectorOptions) { - findReferenceValueForEntries(ctrl.envFromEntries, changes.envFromSelectorOptions.currentValue); + if(changes.entries || changes.envFromSelectorOptions) { + updateEnvFromEntries(); } }; } diff --git a/app/views/directives/edit-environment-from.html b/app/views/directives/edit-environment-from.html index 2b607be32b..a610297fd5 100644 --- a/app/views/directives/edit-environment-from.html +++ b/app/views/directives/edit-environment-from.html @@ -8,7 +8,6 @@ -