environmentCtrl.js 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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';

18
module.exports = function ($scope, application) {
19
    $scope.application = application;
20
    $scope.overrides = { values: [{key: '', value: '' }], error: null};
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    var toArray = function(map) {
        var array = [];
        for (var key in map) {
            var value = map[key];
            if (value instanceof Object) {
                value = toArray(value);
            }
            var entry = {key: key, value: value};
            array.push(entry);
        }
        return array.length > 0 ? array : undefined;
    };

    var collectKeys = function(envArray) {
        var allKeys = {};
        for (var i = 0; i < envArray.length; i++) {
            for (var j = 0; envArray[i].value && j < envArray[i].value.length; j++) {
                allKeys[envArray[i].value[j].key] = true;
            }
        }
        return Object.getOwnPropertyNames(allKeys);
    };

    $scope.reload = function() {
        $scope.env = {};
        $scope.envArray = [];
        $scope.allKeys = [];

        application.getEnv()
51
          .then(function (env) {
52
            $scope.env = env;
53 54
            $scope.envArray = toArray(env);
            $scope.allKeys = collectKeys($scope.envArray);
55
        })
56
        .catch(function (error) {
57 58
            $scope.error = error;
        });
59 60 61 62 63 64
    };

    $scope.reload();

    var getValue = function(item) {
        if (item.key && $scope.allKeys.indexOf(item.key) >= 0) {
65
            application.getEnv(item.key).then(function(value) {
66 67 68 69 70 71 72 73
                item.value = value;
            });
        }
    };

    $scope.onChangeOverrideItem = function(item) {
        getValue(item);

74 75
        if ($scope.overrides.values[$scope.overrides.values.length - 1].key) {
            $scope.overrides.values.push({key: '', value: '' });
76 77 78
        }
    };

79
    $scope.override = function() {
80
        var map = {};
81 82 83
        for (var i = 0; i < $scope.overrides.values.length; i++) {
            if ($scope.overrides.values[i].key) {
                map[$scope.overrides.values[i].key] = $scope.overrides.values[i].value;
84 85 86
            }
        }

87
        $scope.overrides.error = null;
88
        application.setEnv(map).then(function () {
89
            $scope.overrides = { values: [{key: '', value: '' }], error: null, changes: null};
90 91
            $scope.reload();
         })
92
        .catch(function (error) {
93 94
            $scope.overrides.error = error;
            $scope.overrides.changes = null;
95 96 97 98 99
            $scope.reload();
        });
    };

    $scope.reset = function() {
100 101
        $scope.overrides.error = null;
        $scope.overrides.changes = null;
102
        application.resetEnv().then(function () {
103 104
            $scope.reload();
         })
105
        .catch(function (error) {
106 107 108 109 110 111 112 113 114
            $scope.overrides.error = error;
            $scope.reload();
        });

    };

    $scope.refresh = function() {
        $scope.overrides.error = null;
        $scope.overrides.changes = null;
115
        application.refresh().then(function (changes) {
116 117 118
            $scope.overrides.changes = changes;
            $scope.reload();
         })
119
        .catch(function (error) {
120
            $scope.overrides.error = error;
121 122 123 124
            $scope.reload();
        });

    };
125
};