2015-02-02 20:55:33 +00:00
|
|
|
/**
|
2015-07-11 12:16:11 +00:00
|
|
|
* @description This module communicates with Lychee's API
|
|
|
|
* @copyright 2015 by Tobias Reich
|
2015-02-02 20:55:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
api = {
|
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
path : 'php/index.php',
|
2015-07-11 12:16:11 +00:00
|
|
|
onError : null
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
api.post = function(fn, params, callback) {
|
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
loadingBar.show()
|
2015-02-02 20:55:33 +00:00
|
|
|
|
2016-01-24 14:16:03 +00:00
|
|
|
params = $.extend({ function: fn }, params)
|
2015-02-02 20:55:33 +00:00
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
const success = (data) => {
|
2015-02-02 20:55:33 +00:00
|
|
|
|
2016-02-13 22:32:13 +00:00
|
|
|
setTimeout(loadingBar.hide, 100)
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
// Catch errors
|
2015-07-11 12:16:11 +00:00
|
|
|
if (typeof data==='string' && data.substring(0, 7)==='Error: ') {
|
|
|
|
api.onError(data.substring(7, data.length), params, data)
|
|
|
|
return false
|
2015-02-02 20:55:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:16:33 +00:00
|
|
|
// Convert true and false to real booleans
|
2016-02-13 22:38:04 +00:00
|
|
|
if (data==='true') data = true
|
|
|
|
else if (data==='false') data = false
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
// Convert to JSON if string start with '{' and ends with '}'
|
2016-01-24 14:16:03 +00:00
|
|
|
if (typeof data==='string' && data.substring(0, 1)==='{' && data.substring(data.length - 1, data.length)==='}') {
|
|
|
|
data = $.parseJSON(data)
|
|
|
|
}
|
2015-02-02 20:55:33 +00:00
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
callback(data)
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
const error = (jqXHR, textStatus, errorThrown) => {
|
2015-02-02 20:55:33 +00:00
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
api.onError('Server error or API not found.', params, errorThrown)
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: api.path,
|
|
|
|
data: params,
|
|
|
|
dataType: 'text',
|
|
|
|
success,
|
|
|
|
error
|
2015-07-11 12:16:11 +00:00
|
|
|
})
|
2015-02-02 20:55:33 +00:00
|
|
|
|
|
|
|
}
|