Projekt

Obecné

Profil

Stáhnout (5.29 KB) Statistiky
| Větev: | Revize:
1
<div id="edit-modal" class="modal fade" role="dialog">
2
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
3
        <div class="modal-content">
4
            <div class="modal-header">
5
                <h4 class="modal-title"></h4>
6
                <button type="button" class="close" data-dismiss="modal">&times;</button>
7
            </div>
8
            <div class="modal-body">
9
                <div class="container">
10
                    <div class="row my-1" id="username-wrapper">
11
                        <div class="col-xs-12 col-sm-4">
12
                            <label for="username">Uživatelské jméno: <b class="required">*</b></label>
13
                        </div>
14
                        <div class="col-xs-12 col-sm-8">
15
                            <input class="min-wdth" id="username"/>
16
                        </div>
17
                    </div>
18
                    <div class="row my-1" id="password-wrapper">
19
                        <div class="col-xs-12 col-sm-4">
20
                            <label for="password">Heslo:</label>
21
                        </div>
22
                        <div class="col-xs-12 col-sm-8">
23
                            <input type="password" class="min-wdth" id="password"/>
24
                        </div>
25
                    </div>
26
                    <div class="row my-1">
27
                        <div class="col-xs-12 col-sm-4">
28
                            <label for="rights">Práva:  <b class="required">*</b></label>
29
                        </div>
30
                        <div class="col-xs-12 col-sm-8">
31
                            <select class="min-wdth" id="rights">
32
                                <option value = "editor">Editor</option>
33
                                <option value = "admin">Admin</option>
34
                            </select>
35
                        </div>
36
                    </div>
37

    
38
                </div>
39
            </div>
40
            <div class="modal-footer">
41
                <button type="button" id="submit-but" class="btn btn-default" data-dismiss="modal"></button>
42
                <button type="button" class="btn btn-default" data-dismiss="modal">Zavřít</button>
43
            </div>
44
        </div>
45
    </div>
46
</div>
47

    
48
<script>
49
    $('#edit-modal').on('show.bs.modal', function (event) {
50
        const button = $(event.relatedTarget);
51
        const pseudo_id = button.data('pseudo-id');// Extract info from data-* attributes
52
        const title = button.data('title');// Extract info from data-* attributes
53

    
54
        let modal = $(this);
55
        modal.find('.modal-title').text(title);
56

    
57
        if(title === "Nový uživatel"){
58
            prefill(modal);
59
            modal.find('#submit-but').text("Založit");
60
            modal.find("#username-wrapper").show();
61
            modal.find("#password-wrapper").show();
62
            modal.find('#submit-but').click(() => createData(modal));
63

    
64
            enableSubmit(modal, true);
65
            modal.find("#rights").change(() => enableSubmit(modal, true));
66
            modal.find("#username").change(() => enableSubmit(modal, true));
67
        } else {
68
            const data_obj = users[pseudo_id];
69
            modal.find('#submit-but').text("Upravit");
70
            modal.find("#username-wrapper").hide();
71
            modal.find("#password-wrapper").hide();
72
            modal.find('#submit-but').click(() => editData(data_obj, modal));
73
            prefill(modal, data_obj);
74

    
75
            enableSubmit(modal, false);
76
            modal.find("#rights").change(() => enableSubmit(modal, true));
77
            modal.find("#username").change(() => enableSubmit(modal, true));
78
        }
79
    });
80

    
81
    function enableSubmit(modal, createNew){
82
        if(
83
            modal.find("#rights")[0].value === "" ||
84
            (modal.find("#username")[0].value === "" && !createNew)
85
        ){
86
            modal.find("#submit-but").prop( "disabled", true );
87
        } else {
88
            modal.find("#submit-but").prop( "disabled", false );
89
        }
90
    }
91

    
92
    function prefill(modal, data = null){
93
        modal.find("#username").val("");
94
        modal.find("#password").val("");
95
        modal.find("#rights").val("");
96

    
97
        if(data !== null){
98
            modal.find("#rights").val(data.role);
99
        }
100
    }
101

    
102
    function editData(data, modal){
103
        const formData = prepareFormData(modal);
104
        formData.append("userId", data.id);
105

    
106
        var xhr = new XMLHttpRequest();
107
        xhr.open("POST", "./controller/UpdateUserRoleController.php");
108
        xhr.send(formData);
109
        setTimeout(function(){
110
         fetchUsers(); //reload data after item delete
111
        }, 500);
112
    }
113

    
114
    function prepareFormData(modal) {
115
        const formData = new FormData();
116
        formData.append("role", modal.find("#rights")[0].value);
117

    
118
        return formData;
119
    }
120

    
121
    function createData(modal){
122
        const formData = prepareFormData(modal);
123
        const username = modal.find("#username")[0].value;
124
        if(username !== undefined && username !== null){
125
            formData.append("username", username);
126
            formData.append("password", modal.find("#password")[0].value);
127

    
128
            var xhr = new XMLHttpRequest();
129
            xhr.open("POST", "./controller/RegistrationController.php");
130
            xhr.send(formData);
131
            setTimeout(function(){
132
                fetchUsers(); //reload data after item delete
133
            }, 500);
134
        }
135
    }
136
</script>
(4-4/10)