Friendiqa/source-android/js/helper.js
2017-11-07 21:57:40 +01:00

139 lines
5.3 KiB
JavaScript

.pragma library
.import QtQuick.LocalStorage 2.0 as Sql
function friendicaRequest(login,api,rootwindow,callback) {
var xhrequest= new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
//print(api+JSON.stringify(login));
if(xhrequest.readyState === XMLHttpRequest.DONE) {
try{ if (xhrequest.status=200){ //if (xhrequest.responseText!=""){
callback(xhrequest.responseText)
}else{
showMessage("Error","API:" +login.server+api+"\n NO RESPONSE"+xhrequest.statusText,rootwindow);
//callback(xhrequest.responseText)
}
}
catch (e){print(e);
showMessage("Error", xhrequest.responseText,rootwindow)
}
}
}
xhrequest.open("GET", login.server+api,true,login.username,Qt.atob(login.password));
xhrequest.send();
}
function friendicaPostRequest(login,api,data,method,rootwindow,callback) {
var xhrequest= new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
//print(api+JSON.stringify(login)+Qt.atob(login.password));
if (xhrequest.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
} else if(xhrequest.readyState === XMLHttpRequest.DONE) {
try{ if (xhrequest.responseText!=""){
callback(xhrequest.responseText)
}else{
showMessage("Error",api+" NO RESPONSE",rootwindow)
callback(xhrequest.responseText)
}
}
catch (e){
showMessage("Error", api+" "+e,rootwindow)
}
}
}
xhrequest.open(method, login.server+api,true,login.username,Qt.atob(login.password));
xhrequest.send(data);
}
function getCount(database,login,table,field,countvalue){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var count=0;
db.transaction( function(tx) {
var countrs = tx.executeSql('SELECT COUNT(*) from '+table+' WHERE username= "'+ login.username +'" AND '+field+' = "'+countvalue+'"');
count = parseInt(countrs.rows.item(0)["COUNT(*)"])
})
return count
}
function friendicaWebRequest(url,rootwindow,callback) {
var xhrequest = new XMLHttpRequest();
xhrequest.onreadystatechange = function() {
if (xhrequest.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
} else if(xhrequest.readyState === XMLHttpRequest.DONE) {
try{ callback(xhrequest.responseText);
}
catch (e){print(e);
showMessage("Error",url+" "+e, rootwindow)
}
}
}
xhrequest.open("GET", url,true);
xhrequest.send();
}
function readData(database,table,username,callback,filter,filtervalue, sort) { // reads and applies data from DB
if (filter){
var where = " AND "+ filter +" = '" + filtervalue+"'";
} else { var where="";}
if (username){
var user = ' where username= "'+ username +'"';
} else { var user='';}
if (sort){
var sortparam = " ORDER BY "+ sort;
} else { var sortparam="";}
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
if(!db) { return; }
db.transaction( function(tx) {
//print('select * from '+table+user+where+sortparam);
var rsArray=[];
var rs = tx.executeSql('select * from '+table+user+where+sortparam);
for(var i = 0; i < rs.rows.length; i++) {
rsArray.push(rs.rows.item(i))
}
callback(rsArray);
});
}
function readField(field,database,table, username, callback,filter,filtervalue) { // reads and applies data from DB
if (filter){
var where = " AND "+ filter +" = '" + filtervalue+"'";
} else { var where="";}
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
if(!db) { return; }
db.transaction( function(tx) {
//print('... read from database ' + field)
var rsArray=[];
//print('select DISTINCT '+field+' from '+table+' WHERE username="'+username+'"'+where+' ORDER BY '+field+' ASC');
var rs = tx.executeSql('select DISTINCT '+field+' from '+table+' WHERE username="'+username+'"'+where+' ORDER BY '+field+' ASC');
for(var i = 0; i < rs.rows.length; i++) {
rsArray.push(rs.rows.item(i)[field])
}
callback(rsArray);
});
}
function showMessage(header,message,rootwindow){//print("message: "+message);
var cleanmessage=message.replace(/"/g,"-");
var messageString='import QtQuick 2.0; import QtQuick.Dialogs 1.2; MessageDialog{ visible: true; title:"'+header+'";standardButtons: StandardButton.Ok; text:" '+cleanmessage+'"}';
var messageObject=Qt.createQmlObject(messageString,rootwindow,"messageOutput");
}
function inArray(list, prop, val) {
if (list.length > 0 ) {
for (var i in list) { if (list[i][prop] == val) {
return i;
}
}
} return -1;
}
function cleanArray(array) {
var arraystring=JSON.stringify(array);
arraystring=arraystring.replace(/[\[\]]/g , '');
return arraystring;
}
function cleanDate(date){
var cleanedDate= date.slice(0,3)+", "+date.slice(8,11)+date.slice(4,7)+date.slice(25,30)+date.slice(10,25);
return cleanedDate
}