aboutsummaryrefslogtreecommitdiffstats
path: root/moon_dashboard/moon/static/moon/js/util.service.js
blob: 18ae901d1d2bfaa8248a98de9f85eaecb5449183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function () {

    'use strict';

    angular
        .module('moon')
        .factory('moon.util.service', utilService);

    utilService.$inject = ['horizon.framework.widgets.toast.service'];

    function utilService(toast) {


        return {
            mapToArray: function mapToArray(map, action) {
                var result = []
                for (var key in map) {
                    if (map.hasOwnProperty(key)) {
                        var item = map[key];
                        item.id = key;
                        if (action != null) {
                            action(item);
                        }
                        result.push(item);
                    }
                }
                return result;
            },

            mapIdToItem: function mapIdToItem(array, map) {
                if (array) {
                    for (var index = 0; index < array.length; index++) {
                        var id = array[index];
                        array[index] = map[id];
                    }
                }
            },

            mapItemToId: function mapItemToId(array) {
                if (array) {
                    for (var index = 0; index < array.length; index++) {
                        var item = array[index];
                        array[index] = item.id;
                    }
                }
            },

            addToMap: function addToMap(array, map) {
                if (array) {
                    for (var index = 0; index < array.length; index++) {
                        var item = array[index];
                        map[item.id] = item;
                    }
                }
            },

            updateObject: function updateObject(object, newObject) {
                for (var key in newObject) {
                    if (newObject.hasOwnProperty(key)) {
                        object[key] = newObject[key];
                    }
                }
            },

            cleanObject: function cleanObject(object) {
                for (var key in object) {
                    if (object.hasOwnProperty(key)) {
                        delete object[key];
                    }
                }
            },

            pushAll: function pushAll(array, arrayToPush) {
                Array.prototype.push.apply(array, arrayToPush);
            },

            indexOf: function indexOf(array, property, value) {
                for (var i = 0; i < array.length; i += 1) {
                    if (array[i][property] === value) {
                        return i;
                    }
                }
                return -1;
            },

            createInternal: function createInternal(data, array, map, action) {
                var added = this.mapToArray(data, action)
                this.addToMap(added, map);
                this.pushAll(array, added);
                return added;
            },

            updateInternal: function updateInternal(data, map, action) {
                var updated = this.mapToArray(data, action)
                var result = []
                for (var index = 0; index < updated.length; index++) {
                    var item = updated[index];
                    this.updateObject(map[item.id], item)
                    result.push(map[item.id])
                }
                return result;
            },

            removeInternal: function removeInternal(id, array, map) {
                var old = map[id];
                delete map[old.id];
                array.splice(array.indexOf(old), 1);
                return old;
            },

            arrayToTitleMap: function arrayToTitleMap(array) {
                return array.map(function (item) {
                    return { value: item.id, name: item.name }
                }).sort(function (itemA, itemB) {
                    return itemA.name.localeCompare(itemB.name);
                })
            },

            displayErrorFunction: function displayErrorFunction(message) {
                return function() {
                    toast.add('error', gettext(message));
                }
            },
    
            displaySuccess: function displaySuccess(message) {
                toast.add('success', gettext(message));
            },

            displayError: function displayError(message) {
                toast.add('error', gettext(message));
            },

        }

    }
})();