| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| import cloneDeep from 'lodash/cloneDeep'; |
| import get from 'lodash/get'; |
| import {tap} from 'rxjs/operators'; |
| import {Menu} from 'app/types'; |
| |
| import LegacyConfirmFactory from 'app/services/Confirm.service'; |
| import Version from 'app/services/Version.service'; |
| import Caches from '../../../../services/Caches'; |
| import FormUtilsFactory from 'app/services/FormUtils.service'; |
| |
| export default class CacheEditFormController { |
| modelsMenu: Menu<string>; |
| |
| igfssMenu: Menu<string>; |
| |
| /** IGFS IDs to validate against. */ |
| igfsIDs: string[]; |
| |
| onSave: ng.ICompiledExpression; |
| |
| static $inject = ['IgniteConfirm', 'IgniteVersion', '$scope', 'Caches', 'IgniteFormUtils']; |
| |
| constructor( |
| private IgniteConfirm: ReturnType<typeof LegacyConfirmFactory>, |
| private IgniteVersion: Version, |
| private $scope: ng.IScope, |
| private Caches: Caches, |
| private IgniteFormUtils: ReturnType<typeof FormUtilsFactory> |
| ) {} |
| |
| $onInit() { |
| this.available = this.IgniteVersion.available.bind(this.IgniteVersion); |
| |
| const rebuildDropdowns = () => { |
| this.$scope.affinityFunction = [ |
| {value: 'Rendezvous', label: 'Rendezvous'}, |
| {value: 'Custom', label: 'Custom'}, |
| {value: null, label: 'Default'} |
| ]; |
| |
| if (this.available(['1.0.0', '2.0.0'])) |
| this.$scope.affinityFunction.splice(1, 0, {value: 'Fair', label: 'Fair'}); |
| }; |
| |
| rebuildDropdowns(); |
| |
| const filterModel = () => { |
| if ( |
| this.clonedCache && |
| this.available('2.0.0') && |
| get(this.clonedCache, 'affinity.kind') === 'Fair' |
| ) |
| this.clonedCache.affinity.kind = null; |
| |
| }; |
| |
| this.subscription = this.IgniteVersion.currentSbj.pipe( |
| tap(rebuildDropdowns), |
| tap(filterModel) |
| ) |
| .subscribe(); |
| |
| // TODO: Do we really need this? |
| this.$scope.ui = this.IgniteFormUtils.formUI(); |
| |
| this.formActions = [ |
| {text: 'Save', icon: 'checkmark', click: () => this.save()}, |
| {text: 'Save and Download', icon: 'download', click: () => this.save(true)} |
| ]; |
| } |
| $onDestroy() { |
| this.subscription.unsubscribe(); |
| } |
| $onChanges(changes) { |
| if ( |
| 'cache' in changes && get(this.clonedCache, '_id') !== get(this.cache, '_id') |
| ) { |
| this.clonedCache = cloneDeep(changes.cache.currentValue); |
| if (this.$scope.ui && this.$scope.ui.inputForm) { |
| this.$scope.ui.inputForm.$setPristine(); |
| this.$scope.ui.inputForm.$setUntouched(); |
| } |
| } |
| if ('models' in changes) |
| this.modelsMenu = (changes.models.currentValue || []).map((m) => ({value: m._id, label: m.valueType})); |
| if ('igfss' in changes) { |
| this.igfssMenu = (changes.igfss.currentValue || []).map((i) => ({value: i._id, label: i.name})); |
| this.igfsIDs = (changes.igfss.currentValue || []).map((i) => i._id); |
| } |
| } |
| getValuesToCompare() { |
| return [this.cache, this.clonedCache].map(this.Caches.normalize); |
| } |
| save(download) { |
| if (this.$scope.ui.inputForm.$invalid) |
| return this.IgniteFormUtils.triggerValidation(this.$scope.ui.inputForm, this.$scope); |
| this.onSave({$event: {cache: cloneDeep(this.clonedCache), download}}); |
| } |
| reset = (forReal) => forReal ? this.clonedCache = cloneDeep(this.cache) : void 0; |
| confirmAndReset() { |
| return this.IgniteConfirm.confirm('Are you sure you want to undo all changes for current cache?') |
| .then(this.reset); |
| } |
| } |