Line 0
Link Here
|
0 |
- |
1 |
// domain_limits.js: Code for the corresponding admin form |
|
|
2 |
|
3 |
var domain_limits_table; |
4 |
$(document).ready(function() { |
5 |
domain_limits_table = init_table(); |
6 |
$('#toolbar').on( "click", 'a.add', function () { |
7 |
start_modal( 'add' ); |
8 |
}); |
9 |
$('#domain_limits').on( "click", 'a.add_member', function () { |
10 |
start_modal( 'add_member', { belongs_to: $(this).parents('tr').attr('id') } ); |
11 |
}); |
12 |
$('#domain_limits').on( "click", 'a.edit, a.edit_member', function () { |
13 |
hide_dialogs(); |
14 |
var limit_id = $(this).parents('tr').attr('id'); |
15 |
api_get(limit_id); |
16 |
}); |
17 |
$('#domain_limits').on( "click", 'a.delete', function () { |
18 |
hide_dialogs(); |
19 |
if ( window.confirm( _("Delete this limit?") ) ) { |
20 |
var limit_id = $(this).parents('tr').attr('id'); |
21 |
api_delete( limit_id ); |
22 |
} |
23 |
}); |
24 |
$('#domain_limits_modal').on( 'keydown', function(ev) { |
25 |
if ( ev.which == 27 ) { // respond to escape key |
26 |
$('#domain_limits_modal').hide(); |
27 |
return false; |
28 |
} |
29 |
}); |
30 |
$('#domain_limits_modal button.save').on( 'click', function() { |
31 |
if ( !$('#domain_limits_modal form').get(0).checkValidity() ) return; // Do not return false here to allow validation. |
32 |
var limit_id = $('#domain_limits_modal .modal-form').data('id'); |
33 |
var row = get_modal_inputs(); |
34 |
if ( limit_id ) api_put(limit_id, row); else api_post(row); |
35 |
return false; |
36 |
}); |
37 |
$('#domain_limits_modal button.cancel').on( 'click', function() { |
38 |
$('#domain_limits_modal').hide(); |
39 |
return false; |
40 |
}); |
41 |
}); |
42 |
|
43 |
function api_get (limit_id) { |
44 |
var row = domain_limits_table.DataTable().row('#'+limit_id).data(); |
45 |
$.ajax({ |
46 |
method: "GET", |
47 |
url: '/api/v1/smtp/domain_limits/' + limit_id, |
48 |
}).done(function (data) { |
49 |
start_modal( row.belongs_to ? 'edit_member' : 'edit', data ); |
50 |
}).fail(function (xhr) { |
51 |
status_message( 'get', xhr.status, row ); |
52 |
}); |
53 |
} |
54 |
|
55 |
function api_delete (limit_id) { |
56 |
var limit_name = domain_limits_table.DataTable().row('#'+limit_id).data().domain_name; |
57 |
$.ajax({ |
58 |
method: "DELETE", |
59 |
url: '/api/v1/smtp/domain_limits/' + limit_id, |
60 |
}).done(function () { |
61 |
domain_limits_table.api().ajax.reload(); |
62 |
$('#domain_limit_message').html( _("Limit for '%s' deleted.").format(limit_name) ).show(); |
63 |
}).fail(function (xhr) { |
64 |
status_message( 'delete', xhr.status, { domain_name: limit_name } ); |
65 |
}); |
66 |
} |
67 |
|
68 |
function api_post (row) { |
69 |
$.ajax({ |
70 |
method: "POST", |
71 |
url: '/api/v1/smtp/domain_limits', |
72 |
data: JSON.stringify(row), |
73 |
contentType: 'application/json', |
74 |
}).done(function () { |
75 |
domain_limits_table.api().ajax.reload(); |
76 |
$('#domain_limit_message').html( _("Limit for '%s' added.").format(row['domain_name']) ).show(); |
77 |
}).fail(function (xhr, status, error) { |
78 |
status_message( 'post', xhr.status, row ); |
79 |
}).always(function () { |
80 |
$('#domain_limits_modal').hide(); |
81 |
});; |
82 |
} |
83 |
|
84 |
function api_put (limit_id, row) { |
85 |
$.ajax({ |
86 |
method: "PUT", |
87 |
url: '/api/v1/smtp/domain_limits/' + limit_id, |
88 |
data: JSON.stringify(row), |
89 |
contentType: 'application/json', |
90 |
}).done(function () { |
91 |
domain_limits_table.api().ajax.reload(); |
92 |
$('#domain_limit_message').html( _("Limit for '%s' modified.").format(row.domain_name) ).show(); |
93 |
}).fail(function (xhr, status, error) { |
94 |
status_message( 'put', xhr.status, row ); |
95 |
}).always(function () { |
96 |
$('#domain_limits_modal').hide(); |
97 |
}); |
98 |
} |
99 |
|
100 |
function status_message (method, status, row) { // only used in ajax.fail |
101 |
// Extending with parsing xhr.responseText seems not needed here (now). |
102 |
// POST and PUT may trigger similar exceptions, GET and DELETE are trivial. |
103 |
var messages = { |
104 |
delete : _( "Deleting limit for '%s' failed." ), |
105 |
get : _( "Retrieving limit for '%s' failed." ), |
106 |
put : _( "Modifying limit for '%s' failed." ), |
107 |
post : _( "Adding limit for '%s' failed." ), |
108 |
status_400 : _( " (Status 400 - Bad request: expected columns were empty.)" ), |
109 |
status_401 : _( " (Status 401 - Unauthorized: session may have expired.)" ), |
110 |
status_403 : _( " (Status 403 - Forbidden: check permissions.)" ), |
111 |
status_404 : _( " (Status 404 - Not found)" ), |
112 |
status_404_put : _( " (Status 404 - Not found: group domain does not exist)" ), |
113 |
status_404_post : _( " (Status 404 - Not found: group domain does not exist)" ), |
114 |
status_409 : _( " (Status 409 - Conflict: duplicate domain name.)" ), |
115 |
}; |
116 |
var str = messages['status_'+status+'_'+method] || messages['status_'+status] || ''; |
117 |
$( '#domain_limit_alert' ).html( messages[method].format(row.domain_name) + str ).show(); |
118 |
} |
119 |
|
120 |
function hide_dialogs () { |
121 |
$('div.dialog').html('').hide(); |
122 |
} |
123 |
|
124 |
function get_actions_html (row) { |
125 |
var result = ''; |
126 |
if ( row.belongs_to == undefined ) { |
127 |
result += '<a class="btn btn-default btn-xs edit" href="#"><i class="fa fa-pencil-alt"></i> '+_("Edit")+'</a>'+"\n"; |
128 |
result += '<a class="btn btn-default btn-xs delete" href="#"><i class="fa fa-trash-can"></i> '+_("Delete")+'</a>'+"\n"; |
129 |
result += '<a class="btn btn-default btn-xs add_member" href="#"><i class="fa fa-plus"></i> '+_("New member")+'</a>'+"\n"; |
130 |
} else { |
131 |
result += '<a class="btn btn-default btn-xs edit_member" href="#"><i class="fa fa-pencil-alt"></i> '+_("Edit")+'</a>'+"\n"; |
132 |
result += '<a class="btn btn-default btn-xs delete" href="#"><i class="fa fa-trash-can"></i> '+_("Delete")+'</a>'+"\n"; |
133 |
} |
134 |
return result; |
135 |
} |
136 |
|
137 |
function get_modal_inputs () { |
138 |
var row = {}; |
139 |
row.domain_name = $('input[name="domain_name"]').val(); |
140 |
if( $('input[name="group_domain"]:visible').length ) { |
141 |
row.belongs_to = $('#domain_limits_modal .modal-form').data('belongs_to'); |
142 |
} else { |
143 |
row.domain_limit = $('input[name="domain_limit"]').val(); |
144 |
row.units = $('input[name="units"]').val(); |
145 |
row.unit_type = $('select[name="unit_type"]').val(); |
146 |
} |
147 |
return row; |
148 |
} |
149 |
|
150 |
function toggle_modal_inputs (action) { |
151 |
hide_dialogs(); |
152 |
// Show one title |
153 |
$('h3.modal-title').hide().filter('.'+action).show() |
154 |
// Hide and disable some inputs (disable needed to skip validation) |
155 |
$('.modal-body li').hide().filter('.'+action).show(); |
156 |
$('.modal-body .toggle_validation').prop('disabled', true); |
157 |
$('.modal-body li.'+action+' .toggle_validation').prop('disabled', false); |
158 |
} |
159 |
|
160 |
function fill_modal_inputs (action, row) { |
161 |
// Start by clearing |
162 |
$('#domain_limits_modal input').val(''); |
163 |
$('#domain_limits_modal select').val(''); |
164 |
|
165 |
if ( action=='edit' ) { |
166 |
for ( const field_name of [ 'domain_name', 'domain_limit', 'units' ] ) { |
167 |
$('#domain_limits_modal input[name="'+field_name+'"]').val(row[field_name]); |
168 |
} |
169 |
$('#domain_limits_modal select').val(row['unit_type']); |
170 |
} else if ( action=='edit_member' ) { |
171 |
$('#domain_limits_modal input[name="domain_name"]').val(row.domain_name); |
172 |
$('#domain_limits_modal input[name="group_domain"]').val(row.group_domain); |
173 |
} else if( action=='add_member' ) { |
174 |
$('#domain_limits_modal input[name="group_domain"]').val(row.group_domain); |
175 |
} |
176 |
} |
177 |
|
178 |
function start_modal (action, row) { |
179 |
toggle_modal_inputs(action); |
180 |
$('#domain_limits_modal .modal-form').data('id', row ? row.domain_limit_id : null).data('belongs_to', row ? row.belongs_to : null); |
181 |
if( action=='add_member' ) { |
182 |
// fetch group domain name from corresponding row |
183 |
var group_domain = domain_limits_table.DataTable().row('#' + row.belongs_to).data().domain_name; |
184 |
fill_modal_inputs( action, { group_domain: group_domain } ); |
185 |
} else { |
186 |
fill_modal_inputs(action, row); |
187 |
} |
188 |
$('#domain_limits_modal').show().find('input')[0].focus(); |
189 |
} |
190 |
|
191 |
function init_table () { |
192 |
return $("#domain_limits").kohaTable({ |
193 |
"ajax": { |
194 |
"url": '/api/v1/smtp/domain_limits', |
195 |
}, |
196 |
'language': { |
197 |
'emptyTable': '<div>'+_("There are no domain limits defined.")+'</div>' |
198 |
}, |
199 |
"columnDefs": [ { |
200 |
"targets": [0,1,2,3,4], |
201 |
"render": function (data, type, row, meta) { |
202 |
if ( type == 'display' && data != null ) { |
203 |
return data.escapeHtml(); |
204 |
} |
205 |
return data; |
206 |
} |
207 |
} ], |
208 |
"columns": [ |
209 |
{ |
210 |
"data": "domain_name", |
211 |
"searchable": true, |
212 |
"orderable": true, |
213 |
}, |
214 |
{ |
215 |
"data": "domain_limit", |
216 |
"searchable": true, |
217 |
"orderable": true, |
218 |
}, |
219 |
{ |
220 |
"data": "units", |
221 |
"searchable": true, |
222 |
"orderable": true, |
223 |
}, |
224 |
{ |
225 |
"data": "unit_type", |
226 |
"searchable": true, |
227 |
"orderable": true, |
228 |
}, |
229 |
{ |
230 |
"data": "group_domain", |
231 |
"searchable": false, |
232 |
"orderable": false, |
233 |
}, |
234 |
{ |
235 |
"data": function( row, type, val, meta ) { return get_actions_html(row) }, |
236 |
"searchable": false, |
237 |
"orderable": false |
238 |
} |
239 |
], |
240 |
rowId: 'domain_limit_id', |
241 |
}); |
242 |
} |