aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_gui/static/app/model/edit/model.edit.basic.dir.js
blob: 54bb707163a825c15a1e55a806df61152c7382cc (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('moonModelEditBasic', moonModelEditBasic);

    moonModelEditBasic.$inject = [];

    function moonModelEditBasic() {

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

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

    moonModelEditBasicController.$inject = ['$scope', 'modelService', 'formService', 'alertService', '$translate', 'utilService'];

    function moonModelEditBasicController($scope, modelService, formService, alertService, $translate, utilService){

        var edit = this;

        edit.editModel = editModel;
        edit.init = init;

        edit.form = {};

        activate();

        function activate(){

            edit.model = $scope.edit.model;

            edit.modelToEdit = angular.copy(edit.model);

        }

        function editModel(){

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

                formService.checkFieldsValidity(edit.form);

            }else{

                edit.loading = true;

                modelService.update(edit.modelToEdit, updateSuccess, updateError);

            }

            function updateSuccess(data) {

                var updatedModel = utilService.transformOne(data, 'models');

                $translate('moon.model.edit.basic.success', { modelName: updatedModel.name }).then( function(translatedValue) {
                    alertService.alertSuccess(translatedValue);
                });

                edit.loading = false;

                $scope.$emit('event:modelUpdatedSuccess', updatedModel);

            }

            function updateError(reason) {

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

                edit.loading = false;

            }
        }

        function init(){

            edit.modelToEdit = angular.copy(edit.model);

        }
    }

})();