forked from abak-press/apress-assets-admin_table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_table.js
99 lines (87 loc) · 2.63 KB
/
admin_table.js
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
//= require_tree ./templates
/*
* Модуль отвечает за работу с таблицей в админке
*
*/
app.modules.adminTable = (function(self) {
var _$rootEl, _$tableRow, _$declineForm;
function _showForm($form, $row) {
if ($row) {
_$tableRow.find('.js-admin-table-cell').append($form);
$row.after(_$tableRow);
} else {
_$rootEl.find('.js-above-admin-table').append($form);
}
$form.show().find('textarea, input').trigger('keyup');
}
function _createTableRow() {
var colspan = _$rootEl.find('.js-admin-table th').length;
_$tableRow = $(HandlebarsTemplates['table_row']());
_$tableRow.find('.js-admin-table-cell').attr({colspan: colspan});
}
function _acceptGroup(ids) {
$.ajax({
url: _$rootEl.data('accept-url'),
type: 'put',
data: {ids: ids.join(',')},
success: function() {
location.reload();
}
});
}
function _getCheckedArray(){
return _$rootEl.find('.js-admin-table-checkbox:checked').map(function() {
return this.value;
}).get();
}
function _setIds($el, ids) {
$el.find('#ids_').val(ids.join(','));
}
function _listener() {
_$rootEl
.find('.js-accept-link').on('ajax:success', function() {
location.reload();
}).end()
.on('click', '.js-close-form', function() {
$(this).closest('.js-admin-table-row').remove();
})
.find('.js-decline-link').click(function(event) {
var $row = $(this).closest('.js-admin-table-row');
_setIds(_$declineForm, [$row.find('.js-admin-table-checkbox').val()]);
_showForm(_$declineForm, $row);
event.preventDefault();
}).end()
.find('.js-accept-group-link').click(function(event) {
var ids = _getCheckedArray();
if (!ids.length) {
alert(_$rootEl.data('error-nothing-checked'));
return;
}
_acceptGroup(ids);
event.preventDefault();
}).end()
.find('.js-decline-group-link').click(function(event) {
var ids = _getCheckedArray();
if (!ids.length) {
alert(_$rootEl.data('error-nothing-checked'));
return;
}
_setIds(_$declineForm, ids);
_showForm(_$declineForm);
event.preventDefault();
});
_$declineForm.on('ajax:success', function() {
location.reload();
});
}
self.load = function() {
_$rootEl = $('.js-admin-page');
if (!_$rootEl.length) {
return;
}
_$declineForm = _$rootEl.find('.js-decline-form');
_createTableRow();
_listener();
};
return self;
})(app.modules.adminTable || {});