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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
// Globals
// -----------------------------------------------------------------------------
var petitions = [];
var table;
// Supplamentary functions
// -----------------------------------------------------------------------------
// array.indexOf polyfill
// --- -- -- - - -
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
// return petitions record by petition-id
// --- -- -- - - -
function petition_record(id) {
var record = false;
petitions.forEach(el => { if (el.id == id) record = el; });
return record;
}
// create actions html (edit and delete buttons)
// --- -- -- - - -
function create_actions_html(id, signature) {
var del, edit, show;
del = "";
edit = [
'<button type="button" class="btn btn-xs btn-primary"',
'data-bs-toggle="modal" data-bs-target="#managePetition"',
'data-id="'+ id +'">',
'<i class="fa-solid fa-stamp"></i>',
'</button>'
].join('\n');
show = [
'<a type="button" target="_blank" class="btn btn-xs btn-warning"',
'href="/petition/signature/'+ signature +'">',
'<i class="fa-regular fa-circle-down"></i>',
'</a>'
].join('\n');
return show +' '+ edit +' '+ del;
}
$(document).ready(function() {
// When document is ready
// -------------------------------------------------------------------------
/** init_table
* --- -- -- - - -
* get categories
* constuct categories array
* render dataTable
*/
function init_table() {
// get all categories from server (ajax)
$.getJSON( "/admin/get/petitions")
.done(function( json ) {
// then ...
console.log(json);
// reset categories
petitions.length = 0;
// construct petitions data
json.data.forEach( el => {
petitions.push({
id: parseInt(el.id),
subject: el.subject,
protocol: el.protocol,
username: el.UserName,
signature: el.signature,
datepost: el.creation_date,
actions: create_actions_html(el.id, el.signature)
});
});
// destroy previous datatable table instances
$('#dt-petitions').dataTable().fnClearTable();
$('#dt-petitions').dataTable().fnDestroy();
// (re-)create categories datatable
table = $('#dt-petitions').DataTable({
language: { url: '/assets/js/DataTables/localization/Greek.json' },
data: petitions,
// ordering: false,
columns: [
{ data: 'subject', width: '42%' },
{ data: 'protocol' },
{ data: 'username' },
// { data: 'signature' },
{ data: 'datepost' },
{ data: 'actions', class: 'dt-actions' }
]
});
})
.fail(function( jqxhr, textStatus, error ) {
console.log( "Request Failed (" + error +")" );
});
}
init_table(); // init table on first run
/** When modal show-up
* -------------------------------------------------------------------------
* ... prepare the manage-category form
* ... update select2 with possible parents
*/
$('#managePetition').on('show.bs.modal', event => {
var button = $(event.relatedTarget); // Button that triggered the modal
var id = parseInt( button.data('id') ); // category_id from data-id
// var modal = $(this); // modal object
var petition = petition_record(id)
$('#managePetition form input[name=protocol]').val(
petition.protocol ? petition.protocol : ''
);
$('#managePetition form input[name=id]').val( id ); // category id (hidden)
})
// When form is submited
// -------------------------------------------------------------------------
$('#managePetition form').submit( event => {
event.preventDefault();
// prepare data to POST
var data = {
id: parseInt($('#managePetition form input[name=id]').val()),
protocol: $('#managePetition form input[name=protocol]').val() };
// select action url (add or update)
var request = '/admin/set/protocol';
// send POST request
$.post(request, data)
.done(function( data ) {
$('#managePetition').modal('hide'); // when done, close modal
init_table(); // reload table of categories
});
});
});
|