345 lines
9.7 KiB
JavaScript
Vendored
345 lines
9.7 KiB
JavaScript
Vendored
var __id = function(a){
|
|
return document.getElementById(a);
|
|
}
|
|
|
|
const xdb = function(v = 0, func){
|
|
var listdbname = ['dbmask'];
|
|
var dbname = 'masuksini';
|
|
if (localStorage.getItem('dbv') != undefined) {
|
|
if (localStorage.getItem('dbv') != v) {
|
|
indexedDB.deleteDatabase("toDoList");
|
|
}
|
|
}
|
|
|
|
var dbs = null;
|
|
|
|
var request = window.indexedDB.open(dbname, 4);
|
|
|
|
request.onsuccess = function(event) {
|
|
dbs = request.result;
|
|
func({
|
|
add: function(obj){
|
|
var table = 'dbmask';
|
|
// cek data
|
|
var id = obj.id;
|
|
this.read(id, function(aw){
|
|
if (aw == null) {
|
|
var rq = dbs.transaction([table], "readwrite")
|
|
.objectStore(table)
|
|
.add(obj);
|
|
}else{
|
|
var request = dbs.transaction([table], "readwrite")
|
|
.objectStore(table)
|
|
.delete(id)
|
|
var rq = dbs.transaction([table], "readwrite")
|
|
.objectStore(table)
|
|
.add(obj);
|
|
}
|
|
})
|
|
|
|
return this;
|
|
},
|
|
read:function(id, func) {
|
|
var table = 'dbmask';
|
|
var transaction = dbs.transaction([table]);
|
|
var objectStore = transaction.objectStore(table);
|
|
var request = objectStore.get(id);
|
|
request.onerror = function(event) {
|
|
console.log("Unable to retrieve daa from database!");
|
|
};
|
|
request.onsuccess = function(event) {
|
|
// Do something with the request.result!
|
|
if(request.result) {
|
|
func(request.result)
|
|
} else {
|
|
func(null);
|
|
}
|
|
};
|
|
return this;
|
|
}
|
|
})
|
|
};
|
|
request.onupgradeneeded = function(event) {
|
|
dbs = event.target.result;
|
|
dbs.onerror = function(event) {
|
|
note.innerHTML += "<li>Error loading database.</li>";
|
|
};
|
|
// Create an objectStore for this database
|
|
for (var i = 0; i < listdbname.length; i++) {
|
|
dbs.createObjectStore(listdbname[i], {keyPath: "id"});
|
|
}
|
|
};
|
|
localStorage.setItem('dbv', v);
|
|
}
|
|
|
|
|
|
|
|
// start db
|
|
|
|
|
|
$(document).ready(function(){
|
|
Array.from(document.querySelectorAll('.nav-item .nav-link')).forEach(function(alp){
|
|
if (location.pathname == "/" && alp.getAttribute('href') == '/') {
|
|
alp.className += ' active';
|
|
}else{
|
|
if(location.pathname.includes(alp.getAttribute('href')) && alp.getAttribute('href') != '/'){
|
|
alp.className += ' active';
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
function formatRupiah(angka, prefix) {
|
|
var number_string = angka.toString().replace(/\./g, ",").replace(/[^,\d]/g, ""),
|
|
split = number_string.split(","),
|
|
sisa = split[0].length % 3,
|
|
rupiah = split[0].substr(0, sisa),
|
|
ribuan = split[0].substr(sisa).match(/\d{3}/gi);
|
|
// tambahkan titik jika yang di input sudah menjadi angka ribuan
|
|
if (ribuan) {
|
|
separator = sisa ? "." : "";
|
|
rupiah += separator + ribuan.join(".");
|
|
}
|
|
rupiah = split[1] != undefined ? rupiah + "," + split[1] : rupiah;
|
|
return prefix == undefined ? rupiah : rupiah ? "" + rupiah : "";
|
|
}
|
|
|
|
const db = function() {
|
|
return {
|
|
urlsave : "https://indowebs.my.id/admin/api/save",
|
|
urlget: "/api",
|
|
// urlget: "http://localhost:8080/api/okb",
|
|
data: {
|
|
table: "",
|
|
master: false,
|
|
limit: "",
|
|
order: "",
|
|
select: " * ",
|
|
condition: "",
|
|
setCreate: 0,
|
|
leftJoin: "",
|
|
saveset: 0,
|
|
updatedata: null
|
|
},
|
|
table: function(a){
|
|
this.data.table = a;
|
|
return this;
|
|
},
|
|
master:function(n){
|
|
this.data.master = n;
|
|
return this;
|
|
},
|
|
condition : function(a = []){
|
|
var sp = " WHERE ";
|
|
var table = this.data.table;
|
|
sp += a.map(function(x,i){
|
|
return ` ${x.opsi} ${table}.${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;
|
|
},
|
|
update: function(a = {}){
|
|
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 = "ASC"){
|
|
this.data.order = ` ORDER BY ${a} ${b} `;
|
|
return this;
|
|
},
|
|
limit: function(a, b){
|
|
this.data.limit = ` LIMIT ${a}, ${b} `;
|
|
return this;
|
|
},
|
|
save: function(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;
|
|
},
|
|
get: function(func, qr = null, cekm = 1){
|
|
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;
|
|
}
|
|
|
|
xdb(3, function(a){
|
|
a.read(ck.text2Binary(query), function(c){
|
|
if (c != null) {
|
|
if(c.content.data != undefined){
|
|
func(c.content.data, ck, query, c.content.count);
|
|
}
|
|
if (ck.data.master == false) {
|
|
|
|
fetch(ck.urlget+'/'+ck.text2Binary(query))
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
|
|
xdb(3, function(a){
|
|
a.add({
|
|
id: ck.text2Binary(query),
|
|
content: data
|
|
})
|
|
})
|
|
|
|
if(JSON.stringify(c.content) != JSON.stringify(data)){
|
|
func(data.data, ck, query, data.count)
|
|
}
|
|
|
|
})
|
|
}
|
|
}else{
|
|
fetch(ck.urlget+'/'+ck.text2Binary(query))
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
xdb(3, function(a){
|
|
a.add({
|
|
id: ck.text2Binary(query),
|
|
content: data
|
|
})
|
|
})
|
|
func(data.data, ck, query, data.count);
|
|
})
|
|
}
|
|
})
|
|
});
|
|
return this;
|
|
}
|
|
}
|
|
}
|
|
|
|
// bcari data terdekat dengan lokasi
|
|
function getLocation() {
|
|
if (navigator.geolocation) {
|
|
navigator.geolocation.getCurrentPosition(showPosition);
|
|
} else {
|
|
view.innerHTML = "Yah browsernya ngga support Geolocation bro!";
|
|
}
|
|
}
|
|
|
|
function showPosition(position) {
|
|
var obj = {
|
|
latitude: position.coords.latitude,
|
|
longtitude: position.coords.longitude
|
|
}
|
|
|
|
alert(obj)
|
|
}
|
|
|
|
function get_location() {
|
|
if (Modernizr.geolocation) {
|
|
navigator.geolocation.getCurrentPosition(show_map);
|
|
} else {
|
|
globalThis.glocation = null;
|
|
}
|
|
}
|
|
|
|
function show_map(r){
|
|
console.log(r)
|
|
}
|
|
|
|
get_location()
|
|
|
|
String.prototype.replaceAll = function(str1, str2, ignore)
|
|
{
|
|
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
|
|
}
|
|
|
|
async function getFileFromUrl(url, name, defaultType = 'image/jpeg'){
|
|
const response = await fetch(url);
|
|
const data = await response.blob();
|
|
return new File([data], name, {
|
|
type: response.headers.get('content-type') || defaultType,
|
|
});
|
|
}
|
|
|
|
const toBase64 = file => new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.readAsDataURL(file);
|
|
reader.onload = () => resolve(reader.result);
|
|
reader.onerror = error => reject(error);
|
|
});
|
|
|
|
|
|
$(document).ready(function(){
|
|
$(".owl-carousel").owlCarousel({
|
|
margin:30,
|
|
loop: true,
|
|
autoWidth:true,
|
|
items:4,
|
|
autoHeight:true
|
|
})
|
|
});
|