function setDbFilter() {
const entityMap = {
function: 'pfunction',
contract: 'pclass',
}
dtype.t.setEnum('entity', Object.keys(entityMap));
dtype.t.setEnum('io', ['inputs', 'outputs']);
let inputs = {
value: [
'uint256',
'function',
'inputs',
'',
],
type: {
type: 'enum:types',
collection: 'enum:entity',
'i/o': 'enum:io',
name: 'string',
},
name: 'filter',
}
const guiOptions = {
gui: {align: 'left'},
onChange: (typed) => {
inputs = typed;
inputs.type = inputs.type;
},
buttons: [
{type: "button", label: "search", action: async () => {
console.log('-- filter inputs', inputs);
const results = await callDb(inputs);
}}
]
}
async function callDb(newinputs) {
const [type, collection, io, nameRegex, page] = newinputs.value;
const query = {};
const filter = {limit: 50};
let response = {};
query[`${entityMap[collection]}.gapi.${io}.type`] = type;
// {name: {like: value, options: 'i'}}
if (nameRegex !== '') {
const key = collection === 'function' ? 'pfunction.gapi.name' : 'name';
query[key] = {like: nameRegex, options: 'i'};
}
if (
page &&
type === inputs.value[0] &&
collection === inputs.value[1] &&
io === inputs.value[2] &&
nameRegex === inputs.value[3]
) {
filter.skip = (page - 1) * filter.limit;
} else {
filter.skip = 0;
}
if (collection === 'function') {
response = await searchPFunctions(query, filter);
}
if (collection === 'contract') {
filter.limit = 5;
response = await searchPClasses(query, filter);
}
showDbOptions(response.pclassMap, 'contractsSelectDb', 'nodesSelectDb', 'addNodeDb');
const count = response.count;
console.log('count', count);
if (count > filter.limit) {
const pages = Math.ceil(count / filter.limit);
dtype.t.setType('upage', x => x >= 1 && x <= pages, [dtype.natural]);
dtype.controls.upage = {
min: () => 0,
max: () => pages,
showControl: function (typed, folder, gui, onChange) {
gui.Register({
type: 'range', label: typed.name+":"+typed.type,
min: dtype.controls[typed.type].min(),
max: dtype.controls[typed.type].max(),
step: 1,
folder: folder,
initial: typed.value,
onChange: (data) => {
typed.value = data;
if (onChange) onChange(typed);
}
})
return typed
}
};
if (!inputs.type.page) {
inputs.value.push(1);
inputs.type.page = 'upage';
}
console.log('inputs', inputs);
showControl(inputs, "controlDbFilter", guiOptions);
}
}
showControl(inputs, "controlDbFilter", guiOptions);
}