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 = {
|
|
|
|
|
2015-07-11 12:16:11 +00:00
|
|
|
path : 'php/api.php',
|
|
|
|
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
|
|
|
|
2015-07-11 12:16:11 +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
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 1 to true and an empty string to false
|
2015-07-11 12:16:11 +00:00
|
|
|
if (data==='1') data = true
|
|
|
|
else if (data==='') 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
|
|
|
|
|
|
|
// Output response when debug mode is enabled
|
2015-07-11 12:16:11 +00:00
|
|
|
if (lychee.debugMode) console.log(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
|
|
|
|
|
|
|
}
|