summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/ui/shared/alerts/confirmModalFactory.js
blob: 0286341aaf2fb030902ff90084858491a34bb7a8 (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
(function () {
    'use strict';

    angular
        .module('testapiApp')
        .factory('confirmModal', confirmModal);

    confirmModal.$inject = ['$uibModal'];

    /**
     * Opens confirm modal dialog with input textbox
     */
    function confirmModal($uibModal) {
        return function(text, resource, successHandler, name) {
            $uibModal.open({
                templateUrl: 'testapi-ui/shared/alerts/confirmModal.html',
                controller: 'CustomConfirmModalController as confirmModal',
                size: 'md',
                resolve: {
                    data: function () {
                        return {
                            text: text,
                            resource: resource,
                            successHandler: successHandler,
                            name: name

                        };
                    }
                }
            });
        };
    }

    angular
        .module('testapiApp')
        .controller('CustomConfirmModalController',
                    CustomConfirmModalController);

    CustomConfirmModalController.$inject = ['$uibModalInstance', 'data'];

    /**
     * This is the controller for the alert pop-up.
     */
    function CustomConfirmModalController($uibModalInstance, data) {
        var ctrl = this;

        ctrl.confirm = confirm;
        ctrl.cancel = cancel;
        ctrl.buildDeleteObjects = buildDeleteObjects;
        ctrl.data = angular.copy(data);

        function buildDeleteObjects(){
            ctrl.deleteObjects = '';
            if (typeof ctrl.data.name === 'string') {
                ctrl.deleteObjects = ctrl.data.name
            }
            else if (ctrl.data.name instanceof Array){
                for(var index in ctrl.data.name){
                    if(index==0){
                        ctrl.deleteObjects += ctrl.data.name[index]
                    }
                    else{
                        ctrl.deleteObjects += ", "+ ctrl.data.name[index]
                    }

                }
            }
            else{
                for(var index in ctrl.data.name[ctrl.data.resource]){
                    if(index==0){
                        ctrl.deleteObjects += ctrl.data.name[ctrl.data.resource][index]
                    }
                    else{
                        ctrl.deleteObjects += ", "+ ctrl.data.name[ctrl.data.resource][index]
                    }

                }
            }
        }
        /**
         * Initiate confirmation and call the success handler with the
         * input text.
         */
        function confirm() {
            $uibModalInstance.close();
            if (angular.isDefined(ctrl.data.successHandler)) {
                ctrl.data.successHandler(ctrl.data.name);
            }
        }

        /**
         * Close the confirm modal without initiating changes.
         */
        function cancel() {
            $uibModalInstance.dismiss('cancel');
        }

        ctrl.buildDeleteObjects();
    }
})();