Skip to content

Commit 647f333

Browse files
authored
Merge pull request #184 from StackFocus/cleanup-jquery
Cleanup JavaScript code
2 parents b1598eb + db09c8d commit 647f333

7 files changed

Lines changed: 194 additions & 126 deletions

File tree

postmaster/static/js/admins.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function newAdmin(username, password, name) {
1818
},
1919

2020
error: function (response) {
21-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
21+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
2222
}
2323
});
2424
}
@@ -37,7 +37,7 @@ function deleteAdmin(id) {
3737
},
3838

3939
error: function (response) {
40-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
40+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
4141
}
4242
});
4343
}
@@ -46,16 +46,16 @@ function deleteAdmin(id) {
4646
function unlockAdmin(id, targetLink) {
4747

4848
$.ajax({
49-
url: '/api/v1/admins/' + id + 'unlock/',
49+
url: '/api/v1/admins/' + id + '/unlock',
5050
type: 'put',
5151

5252
success: function (response) {
5353
addStatusMessage('success', 'The administrator was unlocked successfully');
54-
targetLink.parent('td').html('Unlocked');
54+
targetLink.parent('td').text('Unlocked');
5555
},
5656

5757
error: function (response) {
58-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
58+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
5959
}
6060
});
6161

@@ -64,21 +64,21 @@ function unlockAdmin(id, targetLink) {
6464
// Sets the event listeners for x-editable
6565
function editableAdminEventListeners() {
6666

67-
var adminUsername = $('a.adminUsername');
67+
var adminEmail = $('a.adminEmail');
6868
var adminPassword = $('a.adminPassword');
6969
var adminName = $('a.adminName');
7070
var adminLocked = $('a.adminLocked')
7171

72-
adminUsername.unbind();
72+
adminEmail.unbind();
7373
adminPassword.unbind();
7474
adminName.unbind();
7575
adminLocked.unbind();
76-
adminUsername.tooltip();
76+
adminEmail.tooltip();
7777
adminPassword.tooltip();
7878
adminName.tooltip();
7979
adminLocked.tooltip();
8080

81-
adminUsername.editable({
81+
adminEmail.editable({
8282
type: 'text',
8383
mode: 'inline',
8484
anim: 100,
@@ -94,11 +94,11 @@ function editableAdminEventListeners() {
9494
},
9595

9696
display: function (value) {
97-
$(this).html(filterText(value.toLowerCase()));
97+
$(this).text(value.toLowerCase());
9898
},
9999

100100
error: function (response) {
101-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
101+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
102102
},
103103

104104
success: function () {
@@ -122,11 +122,11 @@ function editableAdminEventListeners() {
122122
},
123123

124124
display: function () {
125-
$(this).html('●●●●●●●●');
125+
$(this).text('●●●●●●●●');
126126
},
127127

128128
error: function (response) {
129-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
129+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
130130
},
131131

132132
success: function () {
@@ -150,11 +150,11 @@ function editableAdminEventListeners() {
150150
},
151151

152152
display: function (value) {
153-
$(this).html(filterText(value));
153+
$(this).text(value);
154154
},
155155

156156
error: function (response) {
157-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
157+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
158158
},
159159

160160
success: function () {
@@ -272,17 +272,46 @@ function fillInTable () {
272272
var i = 1;
273273
// For each item, add a row, but if the row exists, just change the value
274274
$.each(result['items'], function (j, item) {
275+
// Query the existing table row
275276
var tableRow = $('#dynamicTableRow' + String(i));
276-
var html = '';
277-
278-
tableRow.length == 0 ? html += '<tr id="dynamicTableRow' + String(i) + '">' : null;
279-
html += '<td data-title="Username: "><a href="#" class="adminUsername" data-pk="' + item.id + '" data-url="/api/v1/admins/' + item.id + '" title="Click to change the username">' + filterText(item.username) + '</a></td>\
280-
<td data-title="Password: "><a href="#" class="adminPassword" data-pk="' + item.id + '" data-url="/api/v1/admins/' + item.id + '" title="Click to change the password">●●●●●●●●</a></td>\
281-
<td data-title="Name: "><a href="#" class="adminName" data-pk="' + item.id + '" data-url="/api/v1/admins/' + item.id + '" title="Click to change the name">' + filterText(item.name) + '</a></td>\
282-
<td data-title="Locked: ">' + (item.locked ? ('<a href="#" class="adminLocked" data-pk="' + item.id + '" title="Click to unlock the administrator">Locked</a>') : 'Unlocked') + '</td>\
283-
<td data-title="Action: "><a href="#" class="deleteAnchor" data-pk="' + item.id + '" data-toggle="modal" data-target="#deleteModal">Delete</a></td>';
284-
tableRow.length == 0 ? html += '</tr>' : null;
285-
tableRow.length == 0 ? insertTableRow(html) : tableRow.html(html);
277+
// Create a new table row to be inserted or replace the current one
278+
var newTableRow = $('<tr />', {'id': 'dynamicTableRow' + String(i)});
279+
var columnDataUrl = '/api/v1/admins/' + item.id;
280+
var email_td = $('<td />', {'data-title': 'Email: '}).append(
281+
$('<a />', {'href': '#', 'class': 'adminEmail', 'data-pk': item.id, 'data-url': columnDataUrl,
282+
'title': 'Click to change the username'}).text(item.username)
283+
);
284+
var password_td = $('<td />', {'data-title': 'Password: '}).append(
285+
$('<a />', {'href': '#', 'class': 'adminPassword', 'data-pk': item.id, 'data-url': columnDataUrl,
286+
'title': 'Click to change the password'}).text('●●●●●●●●')
287+
);
288+
var name_td = $('<td />', {'data-title': 'Name: '}).append(
289+
$('<a />', {'href': '#', 'class': 'adminName', 'data-pk': item.id, 'data-url': columnDataUrl,
290+
'title': 'Click to change the name'}).text(item.name)
291+
);
292+
var locked_td = $('<td />', {'data-title': 'Locked: '});
293+
if (item.locked) {
294+
locked_td.append(
295+
$('<a />', {'href': '#', 'class': 'adminLocked', 'data-pk': item.id,
296+
'title': 'Click to unlock the administrator'}).text('Locked')
297+
);
298+
}
299+
else {
300+
locked_td.text('Unlocked');
301+
}
302+
var action_td = $('<td />', {'data-title': 'Action: '}).append(
303+
$('<a />', {'href': '#', 'class': 'deleteAnchor', 'data-pk': item.id, 'data-toggle': 'modal',
304+
'data-target': '#deleteModal'}).text('Delete')
305+
);
306+
// Add the new columns to the new table row
307+
newTableRow.append(email_td).append(password_td).append(name_td).append(locked_td).append(action_td);
308+
// If the table row exists, then replace it, otherwise insert it
309+
if (tableRow.length > 0) {
310+
tableRow.replaceWith(newTableRow);
311+
}
312+
else {
313+
insertTableRow(newTableRow);
314+
}
286315

287316
i++;
288317
});

postmaster/static/js/aliases.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function newAlias(source, destination) {
1818
},
1919

2020
error: function (response) {
21-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
21+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
2222
}
2323
});
2424
}
@@ -37,7 +37,7 @@ function deleteAlias (id) {
3737
},
3838

3939
error: function (response) {
40-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
40+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
4141
}
4242
});
4343
}
@@ -167,15 +167,32 @@ function fillInTable () {
167167
var i = 1;
168168
// For each item, add a row, but if the row exists, just change the value
169169
$.each(result['items'], function (j, item) {
170+
// Query the existing table row
170171
var tableRow = $('#dynamicTableRow' + String(i));
171-
var html = '';
172-
173-
tableRow.length == 0 ? html += '<tr id="dynamicTableRow' + String(i) + '">' : null;
174-
html += '<td data-title="Source: "><a href="#" class="sourceAlias" data-pk="' + item.id + '" data-url="/api/v1/aliases/' + item.id + '" title="Click to change the source of the alias">' + filterText(item.source) + '</td>\
175-
<td data-title="Destination: "><a href="#" class="destinationAlias" data-pk="' + item.id + '" data-url="/api/v1/aliases/' + item.id + '" title="Click to change the destination of the alias">' + filterText(item.destination) + '</td>\
176-
<td data-title="Action: "><a href="#" class="deleteAnchor" data-pk="' + item.id + '">Delete</a></td>';
177-
tableRow.length == 0 ? html += '</tr>' : null;
178-
tableRow.length == 0 ? insertTableRow(html) : tableRow.html(html);
172+
// Create a new table row to be inserted or replace the current one
173+
var newTableRow = $('<tr />', {'id': 'dynamicTableRow' + String(i)});
174+
var columnDataUrl = '/api/v1/aliases/' + item.id;
175+
var source_td = $('<td />', {'data-title': 'Source: '}).append(
176+
$('<a />', {'href': '#', 'class': 'sourceAlias', 'data-pk': item.id, 'data-url': columnDataUrl,
177+
'title': 'Click to change the source of the alias'}).text(item.source)
178+
);
179+
var destination_td = $('<td />', {'data-title': 'Destination: '}).append(
180+
$('<a />', {'href': '#', 'class': 'destinationAlias', 'data-pk': item.id, 'data-url': columnDataUrl,
181+
'title': 'Click to change the destination of the alias'}).text(item.destination)
182+
);
183+
var delete_td = $('<td />', {'data-title': 'Action: '}).append(
184+
$('<a />', {'href': '#', 'class': 'deleteAnchor', 'data-pk': item.id}).text('Delete')
185+
);
186+
// Add the new columns to the new table row
187+
newTableRow.append(source_td).append(destination_td).append(delete_td);
188+
189+
// If the table row exists, then replace it, otherwise insert it
190+
if (tableRow.length > 0) {
191+
tableRow.replaceWith(newTableRow);
192+
}
193+
else {
194+
insertTableRow(newTableRow);
195+
}
179196

180197
i++;
181198
});
@@ -221,10 +238,10 @@ $(document).ready(function () {
221238
return JSON.stringify({ 'value': params.value })
222239
};
223240
$.fn.editable.defaults.error = function (response) {
224-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
241+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
225242
};
226243
$.fn.editable.defaults.display = function (value) {
227-
$(this).html(filterText(value.toLowerCase()));
244+
$(this).text(value.toLowerCase());
228245
};
229246

230247
// Populate the table

postmaster/static/js/configs.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function configEventListeners () {
1919
configTextItems.tooltip();
2020
configTextItems.editable({
2121
display: function (value) {
22-
$(this).html(filterText(value));
22+
$(this).text(value);
2323
}
2424
});
2525

@@ -51,8 +51,8 @@ function fillInTable () {
5151
var i = 1;
5252
// For each item, add a row, but if the row exists, just change the value
5353
$.each(result['items'], function (j, item) {
54+
// Query the existing table row
5455
var tableRow = $('#dynamicTableRow' + String(i));
55-
var html = '';
5656
var boolConfigItems = [
5757
'Login Auditing',
5858
'Mail Database Auditing',
@@ -69,11 +69,23 @@ function fillInTable () {
6969
var cssClass = 'configText';
7070
}
7171

72-
tableRow.length == 0 ? html += '<tr id="dynamicTableRow' + String(i) + '">' : null;
73-
html += '<td data-title="Setting: ">' + filterText(item.setting) + '</td>\
74-
<td data-title="Value: "><a href="#" class="' + cssClass + '" data-pk="' + item.id + '" data-url="/api/v1/configs/' + item.id + '" title="Click to change the setting value">' + (item.value != null ? filterText(item.value) : '') + '</a></td>';
75-
tableRow.length == 0 ? html += '</tr>' : null;
76-
tableRow.length == 0 ? appendTableRow(html) : tableRow.html(html);
72+
// Create a new table row to be inserted or replace the current one
73+
var newTableRow = $('<tr />', {'id': 'dynamicTableRow' + String(i)});
74+
var setting_td = $('<td />', {'data-title': 'Setting: '}).text(item.setting);
75+
var value_td = $('<td />', {'data-title': 'Value: '}).append(
76+
$('<a />', {'href': '#', 'class': cssClass, 'data-pk': item.id, 'data-url': '/api/v1/configs/' + item.id,
77+
'title': 'Click to change the setting value'}).text(item.value)
78+
);
79+
// Add the new columns to the new table row
80+
newTableRow.append(setting_td).append(value_td);
81+
82+
// If the table row exists, then replace it, otherwise insert it
83+
if (tableRow.length > 0) {
84+
tableRow.replaceWith(newTableRow);
85+
}
86+
else {
87+
appendTableRow(newTableRow);
88+
}
7789

7890
i++;
7991
});
@@ -117,7 +129,7 @@ $(document).ready(function () {
117129
return JSON.stringify({ 'value': params.value })
118130
};
119131
$.fn.editable.defaults.error = function (response) {
120-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
132+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
121133
};
122134
$.fn.editable.defaults.success = function () {
123135
addStatusMessage('success', 'The setting was changed successfully');

postmaster/static/js/domains.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function newDomain(name) {
1414
},
1515

1616
error: function (response) {
17-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
17+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
1818
}
1919
});
2020
}
@@ -38,7 +38,7 @@ function deleteDomain (id) {
3838
},
3939

4040
error: function (response) {
41-
addStatusMessage('error', filterText(jQuery.parseJSON(response.responseText).message));
41+
addStatusMessage('error', jQuery.parseJSON(response.responseText).message);
4242
}
4343
});
4444
}
@@ -121,7 +121,7 @@ function domainEventListeners() {
121121

122122

123123
// Loads the dynamic table and pagination
124-
function fillInTable(filter) {
124+
function fillInTable() {
125125
// Set the loading spinner
126126
manageSpinner(true);
127127

@@ -134,15 +134,25 @@ function fillInTable(filter) {
134134
var i = 1;
135135
// For each item, add a row, but if the row exists, just change the value
136136
$.each(result['items'], function (j, item) {
137+
// Query the existing table row
137138
var tableRow = $('#dynamicTableRow' + String(i));
138-
var html = '';
139-
140-
tableRow.length == 0 ? html += '<tr id="dynamicTableRow' + String(i) + '">' : null;
141-
html += '<td data-pk="' + item.id + '" data-title="Domain: ">' + filterText(item.name) + '</td>\
142-
<td data-title="Action: "><a href="#" class="deleteAnchor" data-pk="' + item.id + '" data-toggle="modal" data-target="#deleteModal">Delete</a></td>';
143-
tableRow.length == 0 ? html += '</tr>' : null;
144-
tableRow.length == 0 ? insertTableRow(html) : tableRow.html(html);
145-
139+
// Create a new table row to be inserted or replace the current one
140+
var newTableRow = $('<tr />', {'id': 'dynamicTableRow' + String(i)});
141+
var name_td = $('<td />', {'data-pk': item.id, 'data-title': 'Domain: '});
142+
name_td.text(item.name);
143+
var action_td = $('<td />', {'data-title': 'Action: '}).append(
144+
// Create the anchor inside the column
145+
$('<a />', {'href': '#', 'data-pk': item.id, 'data-toggle': 'modal', 'data-target': '#deleteModal'}).text('Delete')
146+
);
147+
// Add the new columns to the new table row
148+
newTableRow.append(name_td).append(action_td);
149+
// If the table row exists, then replace it, otherwise insert it
150+
if (tableRow.length > 0) {
151+
tableRow.replaceWith(newTableRow);
152+
}
153+
else {
154+
insertTableRow(newTableRow);
155+
}
146156
i++;
147157
});
148158

postmaster/static/js/logs.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ function fillInTable() {
2626
var i = 1;
2727
// For each item, add a row, but if the row exists, just change the value
2828
$.each(result['items'], function (j, item) {
29+
// Query the existing table row
2930
var tableRow = $('#dynamicTableRow' + String(i));
30-
var html = '';
31+
// Create a new table row to be inserted or replace the current one
32+
var newTableRow = $('<tr />', {'id': 'dynamicTableRow' + String(i)});
33+
var time_td = $('<td />', {'data-title': 'Time: '}).text(dateFormatFromISO(item.timestamp));
34+
var admin_td = $('<td />', {'data-title': 'Admin: '}).text(item.admin);
35+
var message_td = $('<td />', {'data-title': 'Message: '}).text(item.message);
36+
// Add the new columns to the new table row
37+
newTableRow.append(time_td).append(admin_td).append(message_td);
3138

32-
tableRow.length == 0 ? html += '<tr id="dynamicTableRow' + String(i) + '">' : null;
33-
html += '<td data-title="Time: ">' + filterText(dateFormatFromISO(item.timestamp)) + '</td>\
34-
<td data-title="Admin: ">' + filterText(item.admin) + '</td>\
35-
<td data-title="Message: ">' + filterText(item.message) + '</td>';
36-
tableRow.length == 0 ? html += '</tr>' : null;
37-
tableRow.length == 0 ? appendTableRow(html) : tableRow.html(html);
39+
// If the table row exists, then replace it, otherwise insert it
40+
if (tableRow.length > 0) {
41+
tableRow.replaceWith(newTableRow);
42+
}
43+
else {
44+
appendTableRow(newTableRow);
45+
}
3846

3947
i++;
4048
});
@@ -47,7 +55,7 @@ function fillInTable() {
4755
.fail(function (jqxhr, textStatus, error) {
4856
// Remove the loading spinner
4957
manageSpinner(false);
50-
addStatusMessage('error', filterText(JSON.parse(jqxhr.responseText)['message']));
58+
addStatusMessage('error', JSON.parse(jqxhr.responseText)['message']);
5159
});
5260
}
5361

0 commit comments

Comments
 (0)