create-nexaui-electron
Version:
Create Nexa App - Tool untuk membuat aplikasi Nexa Electron
263 lines (234 loc) • 7.4 kB
JavaScript
import NEXA_CONFIG from "./config.js";
import { createNexaSync } from "./module/NexaSync.js";
import { createForm } from "./module/NexaForm.js";
import { NexaStorage } from './module/NexaStorage.js';
import { NexaModal } from './module/NexaModal.js';
import {NexaScriptName} from './module/NexaScriptName.js';
import {NexaOauth,NexaLogout} from './module/NexaOauth.js';
import {NexaFilePreview} from './module/NexaFilePreview.js';
import { NexaDom } from './module/NexaDom.js';
import { NexaVars } from './module/NexaVars.js';
import { NexaEvent } from './module/NexaEvent.js';
import {NexaClient} from './module/NexaWebSocket.js';
import {NexaElement} from './utilities/NexaElement.js';
export function NexaUI() {
return {
Client: function () {
return new NexaClient();
},
Components: function () {
return NexaElement();
},
Network: function (config) {
if (config.type === 'v2') {
return createNexaSync({
key: config.credensial,
secret: config.secret,
url: `${NEXA_CONFIG.endpoint}/v2`,
});
} else if (config.type === 'v1') {
return createNexaSync({
key: config.credensial,
url: `${NEXA_CONFIG.endpoint}/v1`,
});
} else if (config.type === 'ws') {
return new NexaClient().Buckets(config);
} else {
}
},
Storage: function () {
return new NexaStorage();
},
NexaEvent: function () {
return new NexaEvent();
},
FilePreview: function () {
return new NexaFilePreview();
},
Event: function () {
// Simpan listeners dalam closure untuk menghindari variabel global
const listeners = {
press: new Set(),
remove: new Set(),
onModal: new Set(),
submit: new Set(),
update: new Set(),
view: new Set()
};
// Buat fungsi global untuk handle onclick events
window.press = function(data) {
listeners.press.forEach(cb => cb(data));
};
window.remove = function (data) {
listeners.remove.forEach(cb => cb(data));
};
window.modal = function (data) {
listeners.modal.forEach(cb => cb(data));
};
window.submit = function (data) {
listeners.submit.forEach(cb => cb(data));
};
window.update = function (data) {
listeners.update.forEach(cb => cb(data));
};
window.view = function (data) {
listeners.view.forEach(cb => cb(data));
};
return {
onPress: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.press.add(callback);
},
onRemove: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.remove.add(callback);
},
onModal: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.onModal.add(callback);
},
onSubmit: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.remove.add(callback);
},
onUpdate: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.update.add(callback);
},
onView: function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback harus berupa function');
}
listeners.view.add(callback);
},
};
},
Modal: function () {
return {
init: function(callback) {
return NexaModal(callback);
},
close: function(newById) {
nxMdClose(newById);
},
active: function(callback) {
window.addEventListener("modalactiv", (event) => {
callback(event);
});
}
};
},
Reload: function () {
// this.Init().routeHandler.refresh();
},
Oauth:function () {
return {
Signin:function (userData) {
return NexaOauth(userData);
},
Logout:function (pathname) {
return NexaLogout(pathname)
},
user:async function () {
const storage = new NexaStorage();
const indexDB = storage.getIndexDB();
const result = await indexDB.get("Oauth");
return result;
}
}
},
createForm: function (data, onResponse) {
return createForm(data, onResponse);
},
ScriptKey:function (scriptName) {
return NexaScriptName(scriptName)
},
clientId: function () {
return getCookie('VID');
},
NexaDom: function (row) {
return new NexaDom(row);
},
NexaVars: function (row) {
return new NexaVars(row);
},
findById:function(data, id) {
if (!Array.isArray(data)) {
console.log("Data structure:", data);
return null;
}
return data.find((item) => item.id === parseInt(id));
},
idPassword: function (row) {
return idPassword(row);
},
Router: function (page) {
window.location.href = page;
},
// NexaRouter BATAS MODULE
};
}
if (typeof define === "function" && define.amd) {
// AMD
define([], () => NexaUI);
} else if (typeof module === "object" && module.exports) {
// CommonJS/Node.js
module.exports = NexaUI;
} else {
// Browser global
window.NexaUI = NexaUI;
}
export const filterRow = (data, propertyMap) => {
return data.map((item) => {
const newObj = {};
Object.entries(propertyMap).forEach(([oldKey, newKey]) => {
if (item.hasOwnProperty(oldKey)) {
newObj[newKey] = item[oldKey];
}
});
return newObj;
});
};
export function getCookie(name) {
const cookies = document.cookie.split("; ");
for (let cookie of cookies) {
const [cookieName, cookieValue] = cookie.split("=");
if (decodeURIComponent(cookieName) === name) {
return decodeURIComponent(cookieValue);
}
}
return null;
}
export function onCookie(name, value) {
// Membuat cookie dengan waktu kedaluwarsa sesi
document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(
value
)}; path=/`;
}
export function idPassword(id) {
const togglePassword = document.querySelector(
'[data-action="password-toggle"]'
);
const passwordInput = document.getElementById(id);
togglePassword.addEventListener("click", function () {
// Toggle tipe input antara "password" dan "text"
const type =
passwordInput.getAttribute("type") === "password"
? "text"
: "password";
passwordInput.setAttribute("type", type);
// Toggle icon mata
this.classList.toggle("fa-eye");
this.classList.toggle("fa-eye-slash");
});
}