Completed
Push — master ( 689e98...bb1346 )
by Craig
15:41
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 1
f 0
1
// Copyright Zikula Foundation, licensed MIT.
2
3
var currentDelete;
4
(function ($) {
5
    $(document).ready(function () {
6
7
        /* --- init --------------------------------------------------------------------------------------------------------- */
8
        var $sortable = $('#permission-list > tbody');
9
10
        // Return a helper with preserved width of cells
11
        var fixHelper = function (e, ui) {
12
            ui.children().each(function () {
13
                jQuery(this).css({width: jQuery(this).width()});
14
            });
15
            return ui;
16
        };
17
        $sortable.sortable({
18
            helper: fixHelper,
19
            items: 'tr:not(.warning)',
20
            update: function (event, ui) {
0 ignored issues
show
Unused Code introduced by
The parameter ui is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
                var parameters = [];
22
                $('#permission-list > tbody > tr').each(function () {
23
                    parameters.push($(this).data('id'));
24
                });
25
                $.ajax({
26
                    url: Routing.generate('zikulapermissionsmodule_permission_changeorder'),
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
                    dataType: 'json',
28
                    type: 'POST',
29
                    data: {
30
                        permorder: parameters
31
                    },
32
                    success: function (result) {
33
                        console.log(result);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
34
                    }
35
                });
36
            }
37
        });
38
        $sortable.disableSelection();
39
40
        /* --- edit or create permission ---------------------------------------------------------------------------------------------- */
41
        /* Open modal to edit permission */
42
        function editPermissionHandler(event) {
43
            event.preventDefault();
44
            $(this).find('.fa').addClass('fa-spin');
45
            var pars = {};
46
            var id = event.data.action == 'edit' ? $(this).parents("tr").data('id') : 'undefined';
47
            if (event.data.action == 'new') {
48
                pars.sequence = $(this).hasClass('insertBefore') ? $(this).parents("tr").data("id") : -1;
49
            }
50
            $.ajax({
51
                type: 'POST',
52
                url: Routing.generate('zikulapermissionsmodule_permission_edit', {pid: id}),
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
53
                data: pars
54
            }).done(function(result) {
55
                var modal = $('#editModal');
56
                modal.find('.modal-body').html(result.data.view);
57
                modal.modal();
58
            }).fail(function(result) {
59
                alert(result.status + ': ' + result.statusText);
60
            }).always(function() {
61
            });
62
        }
63
64
        function updateEditForm(view) {
65
            $('#edit-form-container').replaceWith(view).show();
66
        }
67
68
        /* Save permission changes */
69
        $('#save-permission').click(function () {
70
            var pid = $('#zikulapermissionsmodule_permission_pid').val();
71
            if (pid == '') {
72
                pid = '-1';
73
            } else if (pid == adminpermission && lockadmin == 1) {
0 ignored issues
show
Bug introduced by
The variable lockadmin seems to be never declared. If this is a global, consider adding a /** global: lockadmin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable adminpermission seems to be never declared. If this is a global, consider adding a /** global: adminpermission */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
74
                return;
75
            }
76
            // fetch each input and hidden field and store the value to POST
77
            var pars = {};
78
            $.each($(':input, :hidden').serializeArray(), function(i, field) {
79
                pars[field.name] = field.value;
80
            });
81
            $.ajax({
82
                type: 'POST',
83
                url: Routing.generate('zikulapermissionsmodule_permission_edit', {
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
84
                    pid: pid
85
                }),
86
                data: pars
87
            }).done(function(result) {
88
                var data = result.data;
89
                if (data.view) {
90
                    // validation failed
91
                    updateEditForm(data.view);
92
                } else {
93
                    if (pid !== '-1') {
94
                        // update existing row
95
                        $('#permission-component-' + pid).text(data.permission.component);
96
                        $('#permission-instance-' + pid).text(data.permission.instance);
97
                        $('#permission-group-' + pid).data('id', data.permission.gid);
98
                        $('#permission-group-' + pid).text($('#zikulapermissionsmodule_permission_gid').find('option:selected').text());
99
                        $('#permission-level-' + pid).data('id', data.permission.level);
100
                        $('#permission-level-' + pid).text($('#zikulapermissionsmodule_permission_level').find('option:selected').text());
101
                    } else {
102
                        var existingIndexRow = $('#permission-list tr').eq(data.permission.sequence);
103
                        if (existingIndexRow.length !== 0) {
104
                            // insert new row above it
105
                            existingIndexRow.before(data.row);
106
                        } else {
107
                            // append new row
108
                            $('#permission-list').append(data.row);
109
                        }
110
                        initRowHandlers();
111
                    }
112
                }
113
            }).fail(function(result) {
114
                alert(result.status + ': ' + result.statusText);
115
            }).always(function() {
116
                $('#editModal').modal('hide');
117
            });
118
        });
119
120
        /* --- delete permission -------------------------------------------------------------------------------------------- */
121
        /* Open modal  */
122
        function startDeletePermission(event) {
123
            event.preventDefault();
124
            $(this).find('.fa').addClass('fa-spin');
125
            currentDelete = $(this).parents('tr');
126
            $('#deleteModal').modal();
127
        }
128
129
        /* Delete a permission */
130
        $('#confirm-delete-permission').click(function () {
131
            $.ajax({
132
                url: Routing.generate('zikulapermissionsmodule_permission_delete', {pid: currentDelete.data('id')}),
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
133
                type: 'POST',
134
                success: function () {
135
                    currentDelete.remove();
136
                }
137
            });
138
        });
139
140
        /* --- test permission ---------------------------------------------------------------------------------------------- */
141
        /* Copies the component, instance and level to the permission test form */
142
        function startTestPermission() {
143
            var pid = $(this).parents('tr').data('id');
144
            $('#zikulapermissionsmodule_permissioncheck_user').val('');
145
            $('#zikulapermissionsmodule_permissioncheck_component').val($('#permission-component-' + pid).text());
146
            $('#zikulapermissionsmodule_permissioncheck_instance').val($('#permission-instance-' + pid).text());
147
            $('#permission-test-info').html(' ');
148
            $('html, body').animate({
149
                scrollTop: $('#testpermform').offset().top
150
            }, 500);
151
        }
152
153
        /* Test a permission for a user */
154
        $('#zikulapermissionsmodule_permissioncheck_check').click(function (event) {
155
            event.preventDefault();
156
            var $permissionTestInfo = $('#permission-test-info');
157
            $permissionTestInfo.text($permissionTestInfo.data('testing'));
158
            // fetch each input and hidden field and store the value to POST
159
            var pars = {};
160
            $.each($(':input, :hidden').serializeArray(), function(i, field) {
161
                pars[field.name] = field.value;
162
            });
163
            $.ajax({
164
                url: Routing.generate('zikulapermissionsmodule_permission_test'),
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
165
                dataType: 'json',
166
                type: 'POST',
167
                data: pars,
168
                success: function (result) {
169
                    $permissionTestInfo.html(result.data.testresult);
170
                }
171
            });
172
        });
173
        $('#zikulapermissionsmodule_permissioncheck_reset').click(function (event) {
174
            event.preventDefault();
175
            $('#zikulapermissionsmodule_permissioncheck_user').val('');
176
            $('#zikulapermissionsmodule_permissioncheck_component').val('');
177
            $('#zikulapermissionsmodule_permissioncheck_instance').val('');
178
        });
179
180
        function initRowHandlers() {
181
            $('.edit-permission').unbind('click').on('click', {action: 'edit'}, editPermissionHandler);
182
            $('.create-new-permission').unbind('click').on('click', {action: 'new'}, editPermissionHandler);
183
            $('.delete-permission').unbind('click').click(startDeletePermission);
184
            $('.test-permission').unbind('click').click(startTestPermission);
185
        }
186
187
        initRowHandlers();
188
189
        /* --- View instance info ------------------------------------------------------------------------------------------- */
190
        /* Open modal */
191
        $('#view-instance-info').click(function (event) {
192
            event.preventDefault();
193
            $('#instanceInfoModal').modal();
194
        });
195
196
        /* --- Filter permissions ------------------------------------------------------------------------------------------- */
197
        $('#zikulapermissionsmodule_filterlist_filterGroup, #zikulapermissionsmodule_filterlist_filterComponent').change(function () {
198
            var group = $('#zikulapermissionsmodule_filterlist_filterGroup').val();
199
            var component = $('#zikulapermissionsmodule_filterlist_filterComponent').val();
200
201
            // toggle warnings
202
            if (group == -1) {
203
                $('#filter-warning-group').hide();
204
            } else {
205
                $('#filter-warning-group').show();
206
            }
207
            if (component == "-1") {
208
                $('#filter-warning-component').hide();
209
            } else {
210
                $('#filter-warning-component').show();
211
            }
212
213
            $('#permission-list > tbody > tr').each(function () {
214
                var $this = $(this);
215
                var pid = $this.data('id');
216
                var show = true;
217
                if (group != -1 && group != $('#permission-group-' + pid).data('id')) {
218
                    show = false;
219
                }
220
                if (component != "-1" && $('#permission-component-' + pid).text().indexOf(component) == -1) {
221
                    show = false;
222
                }
223
                if (show) {
224
                    $this.show();
225
                } else {
226
                    $this.hide();
227
                }
228
            });
229
        });
230
231
        $('#zikulapermissionsmodule_filterlist_reset').click(function () {
232
            $('#zikulapermissionsmodule_filterlist_filterComponent').val(-1);
233
            $('#zikulapermissionsmodule_filterlist_filterGroup').val(-1).trigger('change');
234
        });
235
236
        // on modal close, stop all spinning icons
237
        $('.modal').on('hidden.bs.modal', function (event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
238
            $('.fa').removeClass('fa-spin');
239
        });
240
    });
241
})(jQuery);
242