environmentManager.js 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed 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.
 */
'use strict';

module.exports = {
19 20 21 22 23 24 25 26
  bindings: {
    env: '<environment',
    application: '<application',
    envChangedCallback: '&onEnvironmentChanged'
  },
  controller: function () {
    var ctrl = this;
    ctrl.error = null;
27

28 29 30 31 32
    ctrl.$onChanges = function () {
      if (ctrl.env) {
        ctrl.allProperties = ctrl.env.reduce(function (left, right) {
          return left.concat(right.value);
        }, []);
33

34 35 36 37
        var managerSource = ctrl.env.find(function (source) {
          return source.name === 'manager';
        });
        ctrl.newProperties = (managerSource ? managerSource.value.slice(0) : []);
38

39 40 41 42
        ctrl.newProperties.push({
          name: null,
          value: null
        });
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        ctrl.allPropertyNames = ctrl.allProperties.map(function (property) {
          return property.name;
        }).sort().filter(function (name, index, names) {
          return !name || name !== names[index - 1];
        });
      }
    };

    ctrl.onChangePropertyName = function (edited) {
      var property = ctrl.allProperties.find(function (p) {
        return p.name === edited.name;
      });
      if (property) {
        edited.value = property.value;
      }
      if (ctrl.newProperties[ctrl.newProperties.length - 1].name) {
        ctrl.newProperties.push({
          name: null,
          value: null
        });
      }
    };

    ctrl.updateEnvironment = function () {
      ctrl.error = null;
      var newPropertyMap = ctrl.newProperties.reduce(function (map, property) {
        if (property.name) {
          map[property.name] = property.value;
        }
        return map;
      }, {});
      ctrl.application.setEnv(newPropertyMap).then(function () {
        ctrl.envChangedCallback()();
      }).catch(function (error) {
        ctrl.error = error;
        ctrl.envChangedCallback()();
      });
    };

    ctrl.resetEnvironment = function () {
      ctrl.error = null;
      ctrl.application.resetEnv().then(function () {
        ctrl.envChangedCallback()();
      }).catch(function (error) {
        ctrl.error = error;
        ctrl.envChangedCallback()();
      });
    };
92

93 94 95 96 97 98 99
    ctrl.refreshContext = function () {
      ctrl.application.refresh().then(function () {
        ctrl.envChangedCallback()();
      }).catch(function (error) {
        ctrl.error = error;
        ctrl.envChangedCallback()();
      });
100
    };
101 102 103
  },
  template: require('./environmentManager.tpl.html')
};