327 lines
10 KiB
JavaScript
Vendored
327 lines
10 KiB
JavaScript
Vendored
import {xdb} from './xdb.js?v=89';
|
|
|
|
export const text2Binary = function( string) {
|
|
string = JSON.stringify(string);
|
|
return string.split('').map(function (char) {
|
|
return char.charCodeAt(0).toString(2);
|
|
}).join('2');
|
|
}
|
|
|
|
export const binary2text = function(str = null)
|
|
{
|
|
var array = str.split("2");
|
|
var pop = array.map(code => String.fromCharCode(parseInt(code, 2))).join("");
|
|
return JSON.parse(pop);
|
|
}
|
|
|
|
String.prototype.replaceAll = function(search, replacement) {
|
|
var target = this;
|
|
return target.replace(new RegExp(search, 'g'), replacement);
|
|
};
|
|
|
|
export const db = function() {
|
|
return {
|
|
urlsave : "https://indowebs.my.id/admin/api/save",
|
|
urlget: "/admin/api",
|
|
getnew: "https://s-feed.com/simanis/api/regist",
|
|
token: "https://s-feed.com/simanis/api/getToken",
|
|
masterlink: "/admin/master-api",
|
|
data: {
|
|
regist: false,
|
|
table: "",
|
|
limit: "",
|
|
order: "",
|
|
select: " * ",
|
|
condition: "",
|
|
setCreate: 0,
|
|
leftJoin: "",
|
|
saveset: 0,
|
|
updatedata: null,
|
|
obj: null
|
|
},
|
|
table: function(a){
|
|
this.data.table = a;
|
|
return this;
|
|
},
|
|
regist: function(){
|
|
this.data.regist = true;
|
|
return this;
|
|
},
|
|
condition : function(a = []){
|
|
var sp = " WHERE ";
|
|
sp += a.map(function(x,i){
|
|
return ` ${x.opsi} ${x.data[0]} ${x.data[1]} ${x.data[2]} `;
|
|
}).join(" ")
|
|
this.data.condition = sp;
|
|
return this;
|
|
},
|
|
like : function(a = []){
|
|
var sp = " ";
|
|
if (this.data.condition != "") {
|
|
sp = "";
|
|
}else{
|
|
sp = " WHERE ";
|
|
}
|
|
sp += a.map(function(x,i){
|
|
return ` ${x.opsi} ${x.data[0]} ${x.data[1]} ${x.data[2]} `;
|
|
}).join(" ")
|
|
if (this.data.condition != "") {
|
|
this.data.condition += ' AND ('+sp+')';
|
|
}else{
|
|
this.data.condition += sp;
|
|
}
|
|
return this;
|
|
},
|
|
select: function(a){
|
|
this.data.select = a;
|
|
return this;
|
|
},
|
|
delete: function() {
|
|
var up = " DELETE FROM "+this.data.table+" ";
|
|
up += this.data.condition;
|
|
this.data.updatedata = up;
|
|
return this;
|
|
},
|
|
update: function(a = {}){
|
|
console.log(a);
|
|
function escapeHtml(text) {
|
|
return text
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/'/g, "'");
|
|
}
|
|
var up = " UPDATE "+this.data.table+" SET ";
|
|
up += Object.keys(a).map(function(x, s){
|
|
return ` ${x} = '${a[x]}' `;
|
|
}).join(",")
|
|
up += this.data.condition;
|
|
this.data.updatedata = up;
|
|
return this;
|
|
},
|
|
leftJoin: function(a = []){
|
|
this.data.leftJoin = '';
|
|
var pp = this;
|
|
a.forEach(function(y,i){
|
|
pp.data.leftJoin += " LEFT JOIN "+y[0]+" ON "+y[1]+" "+y[2]+" "+y[3]+" ";
|
|
})
|
|
return this;
|
|
},
|
|
order: function(a,b = "DESC"){
|
|
this.data.order = ` ORDER BY ${a} ${b} `;
|
|
return this;
|
|
},
|
|
limit: function(a, b){
|
|
this.data.limit = ` LIMIT ${a}, ${b} `;
|
|
return this;
|
|
},
|
|
save: function(obj = {}){
|
|
|
|
function escapeHtml(text) {
|
|
return text
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/'/g, "'");
|
|
}
|
|
this.data.obj = obj;
|
|
var dat = Object.keys(obj);
|
|
var dd = dat.map(function(x,c){
|
|
return '\''+obj[x]+'\'';
|
|
}).join(",");
|
|
|
|
this.data.saveset = 1;
|
|
this.data.save = `INSERT INTO ${this.data.table} (${dat.join(",")}) VALUES (${dd}) `;
|
|
return this;
|
|
}
|
|
,createTable: function(a = {}){
|
|
this.data.setCreate = 1;
|
|
this.data.createTable = "CREATE TABLE "+this.data.table+" (";
|
|
this.data.createTable += " id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ";
|
|
var pp = this;
|
|
Object.keys(a).forEach(function(x, i){
|
|
if(i == (Object.keys(a).length - 1)){
|
|
pp.data.createTable += " "+x+" "+a[x]+" ";
|
|
}else{
|
|
pp.data.createTable += " "+x+" "+a[x]+" , ";
|
|
}
|
|
})
|
|
this.data.createTable += " ) ";
|
|
return this;
|
|
},
|
|
text2Binary : function( string) {
|
|
return string.split('').map(function (char) {
|
|
return char.charCodeAt(0).toString(2);
|
|
}).join('2');
|
|
return this;
|
|
},
|
|
nextIncrement : function(){
|
|
this.data.nextIncrement = `SELECT auto_increment AS increment FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '${this.data.table}'`;
|
|
return this;
|
|
},
|
|
master: function(data, func){
|
|
var loco = this;
|
|
xdb('masuksiniadmin',['dataMaster'] ,7, function(s){
|
|
s.read('dataMaster', 'master', function(s){
|
|
if (s != null) {
|
|
$.ajax({
|
|
url: loco.masterlink+'/'+loco.text2Binary(JSON.stringify(data)),
|
|
success:function(res){
|
|
res = JSON.parse(res)
|
|
globalThis.dataMaster = res;
|
|
setTimeout(function(){
|
|
func()
|
|
})
|
|
xdb('masuksiniadmin',['dataMaster'] ,7, function(s){
|
|
s.add('dataMaster',{id: 'master', data: res})
|
|
});
|
|
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
console.log(xhr.status);
|
|
console.log(thrownError);
|
|
}
|
|
})
|
|
}else{
|
|
$.ajax({
|
|
url: loco.masterlink+'/'+loco.text2Binary(JSON.stringify(data)),
|
|
success:function(res){
|
|
res = JSON.parse(res)
|
|
globalThis.dataMaster = res;
|
|
func()
|
|
xdb('masuksiniadmin',['dataMaster'] ,7, function(s){
|
|
s.add('dataMaster',{id: 'master', data: res})
|
|
});
|
|
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
console.log(xhr.status);
|
|
console.log(thrownError);
|
|
}
|
|
})
|
|
}
|
|
})
|
|
});
|
|
|
|
}
|
|
,getToken: function(a){
|
|
$.ajax({
|
|
url: this.token+'/'+this.text2Binary(JSON.stringify(a)),
|
|
success:function(res){
|
|
if(res.includes('nodata')){
|
|
alert('maaf user tidak terdaftar')
|
|
}else{
|
|
res = JSON.parse(res);
|
|
localStorage.setItem('loginCond', res.token);
|
|
location.href = "#/";
|
|
}
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
console.log(xhr.status);
|
|
console.log(thrownError);
|
|
}
|
|
})
|
|
return this;
|
|
},
|
|
get: function(func, qr = null){
|
|
|
|
var ck = this;
|
|
var query = "";
|
|
query = ` SELECT ${this.data.select} FROM ${this.data.table} ${this.data.leftJoin} ${this.data.condition} ${this.data.order} ${this.data.limit} `;
|
|
//alert(query);
|
|
if(qr != null){
|
|
query = qr;
|
|
}
|
|
if(this.data.setCreate == 1){
|
|
query = this.data.createTable;
|
|
}
|
|
if(this.data.saveset == 1){
|
|
query = this.data.save;
|
|
}
|
|
|
|
if(this.data.updatedata != null){
|
|
query = this.data.updatedata;
|
|
}
|
|
|
|
if(this.data.nextIncrement != null){
|
|
query = this.data.nextIncrement;
|
|
}
|
|
|
|
if (this.data.regist == true) {
|
|
$.ajax({
|
|
url: this.getnew,
|
|
method: 'POST',
|
|
data: {
|
|
_token: $('meta[name=csrf-token]').attr('content'),
|
|
table: this.data.table,
|
|
token: this.text2Binary(JSON.stringify(this.data.obj))
|
|
},
|
|
success:function(res){
|
|
if(res.includes('simpan')){
|
|
func('disimpan', ck)
|
|
}else{
|
|
func(res, ck)
|
|
}
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
console.log(xhr.status);
|
|
console.log(thrownError);
|
|
}
|
|
})
|
|
return this;
|
|
}else{
|
|
if (query.indexOf("SELECT") != -1) {
|
|
|
|
console.log('pre')
|
|
fetch(this.urlget+'/'+this.text2Binary(query)+'/'+localStorage.getItem('loginCond')+'/'+this.data.table)
|
|
.then(response => response.json())
|
|
.then(res => {
|
|
func(res.data, res.count, ck)
|
|
})
|
|
|
|
// $.ajax({
|
|
// url: this.urlget+'/'+this.text2Binary(query)+'/'+localStorage.getItem('loginCond')+'/'+this.data.table,
|
|
// success:function(res){
|
|
// if(res.includes('simpan')){
|
|
// func('disimpan', ck)
|
|
// }else{
|
|
// res = JSON.parse(res)
|
|
// func(res.data, res.count, ck)
|
|
// }
|
|
// },
|
|
// error: function (xhr, ajaxOptions, thrownError) {
|
|
// console.log(xhr.status);
|
|
// console.log(thrownError);
|
|
// }
|
|
// })
|
|
|
|
}else{
|
|
$.ajax({
|
|
url: this.urlget,
|
|
method: 'POST',
|
|
data: {
|
|
_token: $('meta[name=csrf-token]').attr('content'),
|
|
table: this.data.table,
|
|
token: this.text2Binary(query)+'/'+localStorage.getItem('loginCond')
|
|
},
|
|
success:function(res){
|
|
if(res.includes('simpan')){
|
|
func('disimpan', ck)
|
|
}else{
|
|
res = JSON.parse(res)
|
|
func(res.data, res.count, ck)
|
|
}
|
|
},
|
|
error: function (xhr, ajaxOptions, thrownError) {
|
|
console.log(xhr.status);
|
|
console.log(thrownError);
|
|
}
|
|
})
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|
|
}
|