Skip to content

Commit 2aced3b

Browse files
committed
Version 4.0.0
N.B. This Version is NOT backward compatible. If you upgrade to this version then you must upgrade the Server Side code to implement the strict JSON-RPC specifications. Constant C_API_VERSION added for reference Method api.ajax.jsonrpc.request amended to follow the strict JSON-RPC specifications >> callbackFunctionName_onSuccess accepts the specific parameter "response.result" rather than "response" >> callbackFunctionName_onError accepts the specific parameter "response.error" rather than "response" The plugin ClipboardJs is now required as a dependency Function String.prototype.s2ab added
1 parent 278bea4 commit 2aced3b

File tree

4 files changed

+99
-35
lines changed

4 files changed

+99
-35
lines changed

src/js/api.constant.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*******************************************************************************
2+
API - Constant
3+
*******************************************************************************/
4+
const C_API_VERSION = '4.0.0';
5+
16
/*******************************************************************************
27
API - Constant - Selector
38
*******************************************************************************/

src/js/api.library.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,49 @@ api.ajax.jsonrpc.request = function (pAPI_URL, pAPI_Method, pAPI_Params, callbac
388388
// Validate the JSON-RPC Call ID
389389
if (pAJAX_Params.dataType == 'json' && response.id != callID) {
390390
// Pop the exception in the Bootstrap Modal
391-
api.modal.exception("Invalid JSON-RPC Identifier");
392-
} else if (callbackFunctionName_onSuccess)
393-
api.ajax.callback(callbackFunctionName_onSuccess, response, callbackParams_onSuccess);
391+
api.modal.exception("An invalid JSON-RPC Identifier has been detected. Please try again.");
392+
return;
393+
}
394394

395+
if (response.error) {
396+
// Init the erro output
397+
var errorOutput = null;
398+
399+
// Check response.error.data exist
400+
if (response.error.data) {
401+
// Format the structured data, either array or object
402+
if (($.isArray(response.error.data) && response.error.data.length)
403+
|| ($.isPlainObject(response.error.data) && !$.isEmptyObject(response.error.data))) {
404+
errorOutput = $("<ul>", {
405+
class: "list-group"
406+
});
407+
$.each(response.error.data, function (_index, value) {
408+
var error = $("<li>", {
409+
class: "list-group-item",
410+
html: value.toString()
411+
});
412+
errorOutput.append(error);
413+
});
414+
} else
415+
// Plain error
416+
errorOutput = response.error.data;
417+
} else {
418+
// Get the simple message otherwise
419+
errorOutput = response.error.message;
420+
}
421+
422+
// Pop the error in the Bootstrap Modal
423+
api.modal.error(errorOutput);
424+
425+
if (callbackFunctionName_onError) {
426+
api.ajax.callback(callbackFunctionName_onError, response.error, callbackParams_onError);
427+
}
428+
} else if (response.result !== undefined) {
429+
// Check if the response.result property exist
430+
if (callbackFunctionName_onSuccess)
431+
api.ajax.callback(callbackFunctionName_onSuccess, response.result, callbackParams_onSuccess);
432+
}
433+
else api.modal.exception("An unexpected error has occurred. Please try again.");
395434
},
396435
error: function (jqXHR, textStatus, errorThrown) {
397436
if (callbackFunctionName_onError) {
@@ -402,7 +441,7 @@ api.ajax.jsonrpc.request = function (pAPI_URL, pAPI_Method, pAPI_Params, callbac
402441
}
403442
else {
404443
// Pop the exception in the Bootstrap Modal
405-
api.modal.exception("Server or Network Error. Please try again or contact the Administrator if the problem persists.");
444+
api.modal.exception("A Server or Network Error has occurred. Please try again.");
406445
}
407446
},
408447
complete: function () {
@@ -432,7 +471,7 @@ api.ajax.jsonrpc.request = function (pAPI_URL, pAPI_Method, pAPI_Params, callbac
432471
return $.ajax(extendedAJAXParams);
433472
} catch (error) {
434473
// Pop the exception in the Bootstrap Modal
435-
api.modal.exception("Ajax Error. Please try again or contact the Administrator if the problem persists.");
474+
api.modal.exception("An unhandled Ajax exception has occurred. Please try again.");
436475
return false;
437476
}
438477
};

src/js/api.plugin.js

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,66 @@ api.plugin = api.plugin || {};
1010
/*******************************************************************************
1111
API - Plugin - Return To Top
1212
*******************************************************************************/
13-
1413
$(document).ready(function () {
15-
$(window).scroll(function () {
16-
if ($(this).scrollTop() >= 50) {
17-
$(C_API_SELECTOR_RETURN_TO_TOP).fadeIn('slow');
18-
} else {
19-
$(C_API_SELECTOR_RETURN_TO_TOP).fadeOut('slow');
20-
}
21-
});
22-
23-
$(C_API_SELECTOR_RETURN_TO_TOP).click(function () {
24-
$('body,html').stop().animate({
25-
scrollTop: 0
26-
}, 500);
27-
});
14+
$(window).scroll(function () {
15+
if ($(this).scrollTop() >= 50) {
16+
$(C_API_SELECTOR_RETURN_TO_TOP).fadeIn('slow');
17+
} else {
18+
$(C_API_SELECTOR_RETURN_TO_TOP).fadeOut('slow');
19+
}
20+
});
21+
22+
$(C_API_SELECTOR_RETURN_TO_TOP).click(function () {
23+
$('body,html').stop().animate({
24+
scrollTop: 0
25+
}, 500);
26+
});
2827
});
2928

3029
/*******************************************************************************
3130
Application - Plugin - sprintf
3231
*******************************************************************************/
3332
String.prototype.sprintf = String.prototype.sprintf || function (params) {
34-
params = params || [];
33+
params = params || [];
3534

36-
var sprintfRegex = /\{(\d+)\}/g;
37-
var sprintf = function (match, number) {
38-
return number in params ? params[number] : match;
39-
};
35+
var sprintfRegex = /\{(\d+)\}/g;
36+
var sprintf = function (match, number) {
37+
return number in params ? params[number] : match;
38+
};
4039

41-
if (Array.isArray(params) && params.length)
42-
return this.replace(sprintfRegex, sprintf);
43-
else
44-
return this;
40+
if (Array.isArray(params) && params.length)
41+
return this.replace(sprintfRegex, sprintf);
42+
else
43+
return this;
4544
};
4645

4746
/*******************************************************************************
4847
Application - Plugin - ucwords
4948
*******************************************************************************/
5049
String.prototype.ucwords = String.prototype.ucwords || function () {
51-
str = this.toLowerCase();
52-
return str.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g, function (s) { return s.toUpperCase(); });
50+
str = this.toLowerCase();
51+
return str.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g, function (s) { return s.toUpperCase(); });
5352
};
53+
54+
55+
/*******************************************************************************
56+
Application - Plugin - ClipboardJs on Modal Error shown
57+
*******************************************************************************/
58+
$(document).ready(function () {
59+
$(document).on('show.bs.modal', '#modal-error', function (e) {
60+
// Init the ClipboardJS plugin
61+
new ClipboardJS('#modal-error [name=clipboard]');
62+
});
63+
});
64+
65+
/*******************************************************************************
66+
Application - Plugin - s2ab (String To Array Buffer)
67+
https://stackoverflow.com/questions/34993292/how-to-save-xlsx-data-to-file-as-a-blob
68+
*******************************************************************************/
69+
String.prototype.s2ab = String.prototype.s2ab || function () {
70+
var buffer = new Uint8Array(new ArrayBuffer(this.length));
71+
for (var i = 0; i != this.length; ++i) {
72+
buffer[i] = this.charCodeAt(i) & 0xFF;
73+
}
74+
return buffer;
75+
};

test/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<!-- URI.js - https://github.com/medialize/URI.js/ -->
4444
<script src="https://cdn.jsdelivr.net/gh/medialize/URI.js@v1.19.2/src/URI.min.js"></script>
4545

46+
<!-- ClipboardJS - https://github.com/zenorocha/clipboard.js/ -->
47+
<script src="https://cdn.jsdelivr.net/gh/zenorocha/clipboard.js@v2.0.4/dist/clipboard.min.js"></script>
48+
4649
<!-- **************************************************************************** -->
4750
<!-- Resources - API -->
4851
<!-- **************************************************************************** -->
@@ -136,11 +139,6 @@
136139
<script src="https://cdn.jsdelivr.net/gh/mholt/PapaParse@5.1.0/papaparse.min.js"></script>
137140
-->
138141

139-
<!-- ClipboardJS - https://github.com/zenorocha/clipboard.js/ -->
140-
<!--
141-
<script src="https://cdn.jsdelivr.net/gh/zenorocha/clipboard.js@v2.0.4/dist/clipboard.min.js"></script>
142-
-->
143-
144142
<!-- Select2 - https://github.com/select2/select2/ -->
145143
<!--
146144
<link href="https://cdn.jsdelivr.net/gh/select2/select2@4.0.11/dist/css/select2.min.css" rel="stylesheet">

0 commit comments

Comments
 (0)