aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_gui/static/app/pdp/edit/pdp.edit.basic.dir.js
blob: 402422b6fb2929ead0d0113da819823372cc80ad (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
(function() {

    'use strict';

    angular
        .module('moon')
        .directive('moonPDPEditBasic', moonPDPEditBasic);

    moonPDPEditBasic.$inject = [];

    function moonPDPEditBasic() {

        return {
            templateUrl : 'html/pdp/edit/pdp-edit-basic.tpl.html',
            bindToController : true,
            controller : moonPDPEditBasicController,
            controllerAs : 'edit',
            scope : {
                pdp : '='
            },
            restrict : 'E',
            replace : true
        };
    }

    angular
        .module('moon')
        .controller('moonPDPEditBasicController', moonPDPEditBasicController);

    moonPDPEditBasicController.$inject = ['$scope', 'pdpService', 'formService', 'alertService', '$translate', 'utilService'];

    function moonPDPEditBasicController($scope, pdpService, formService, alertService, $translate, utilService){

        var edit = this;

        edit.editPdp = editPdp;
        edit.init = init;

        edit.form = {};

        activate();

        function activate(){

            edit.pdp = $scope.edit.pdp;

            edit.pdpToEdit = angular.copy(edit.pdp);

        }

        function editPdp(){

            if(formService.isInvalid(edit.form)) {

                formService.checkFieldsValidity(edit.form);

            }else{

                edit.loading = true;

                pdpService.update(edit.pdpToEdit, updateSuccess, updateError);

            }

            function updateSuccess(data) {

                var updatedPdp = utilService.transformOne(data, 'pdps');

                $translate('moon.pdp.edit.basic.success', { pdpName: updatedPdp.name }).then( function(translatedValue) {
                    alertService.alertSuccess(translatedValue);
                });

                edit.loading = false;

                $scope.$emit('event:pdpUpdatedSuccess', updatedPdp);

            }

            function updateError(reason) {

                $translate('moon.pdp.edit.basic.error', { pdpName: edit.pdp.name }).then( function(translatedValue) {
                    alertService.alertError(translatedValue);
                });

                edit.loading = false;

            }
        }

        function init(){

            edit.pdpToEdit = angular.copy(edit.pdp);

        }
    }

})();