2
0
mirror of https://github.com/frappe/books.git synced 2025-01-22 22:58:28 +00:00

[new control] link

This commit is contained in:
Rushabh Mehta 2018-01-23 17:56:40 +05:30
parent 2f0c3574db
commit 8df658112f
10 changed files with 94 additions and 8 deletions

View File

@ -1,5 +1,6 @@
const frappe = require('frappejs'); const frappe = require('frappejs');
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose();
const debug = false;
class sqliteDatabase { class sqliteDatabase {
constructor({db_path}) { constructor({db_path}) {
@ -13,8 +14,9 @@ class sqliteDatabase {
} }
return new Promise(resolve => { return new Promise(resolve => {
this.conn = new sqlite3.Database(this.db_path, () => { this.conn = new sqlite3.Database(this.db_path, () => {
// debug if (debug) {
// this.conn.on('trace', (trace) => console.log(trace)); this.conn.on('trace', (trace) => console.log(trace));
}
resolve(); resolve();
}); });
}); });
@ -119,7 +121,10 @@ class sqliteDatabase {
return await this.run(`delete from ${frappe.slug(doctype)} where name=?`, name); return await this.run(`delete from ${frappe.slug(doctype)} where name=?`, name);
} }
get_all({doctype, fields=['name'], filters, start, limit, order_by='modified', order='desc'} = {}) { get_all({doctype, fields, filters, start, limit, order_by='modified', order='desc'} = {}) {
if (!fields) {
fields = frappe.get_meta(doctype).get_keyword_fields();
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let conditions = this.get_filter_conditions(filters); let conditions = this.get_filter_conditions(filters);
@ -147,6 +152,10 @@ class sqliteDatabase {
for (let key in filters) { for (let key in filters) {
const value = filters[key]; const value = filters[key];
if (value instanceof Array) { if (value instanceof Array) {
// if its like, we should add the wildcard "%" if the user has not added
if (value[0].toLowerCase()==='like' && !value[1].includes('%')) {
value[1] = `%${value[1]}%`;
}
conditions.push(`${key} ${value[0]} ?`); conditions.push(`${key} ${value[0]} ?`);
values.push(value[1]); values.push(value[1]);
} else { } else {

View File

@ -1,4 +1,5 @@
@import "node_modules/bootstrap/scss/bootstrap"; @import "node_modules/bootstrap/scss/bootstrap";
@import "node_modules/awesomplete/awesomplete";
html { html {
font-size: 14px; font-size: 14px;
@ -29,4 +30,38 @@ html {
.dropdown-menu-right { .dropdown-menu-right {
right: 0; right: 0;
left: auto; left: auto;
} }
.awesomplete {
display: block;
}
.awesomplete > ul > li {
padding: .75rem .375rem;
}
.awesomplete > ul > li:hover {
background: $gray-300;
color: $body-color;
}
.awesomplete > ul > li[aria-selected="true"] {
background: $gray-300;
color: $body-color;
}
.awesomplete > ul > li[aria-selected="true"]:hover {
background: $gray-300;
color: $body-color;
}
mark {
padding: none;
background: inherit;
}
.awesomplete li[aria-selected="true"] mark, .awesomplete li[aria-selected="false"] mark {
background: inherit;
color: inherit;
padding: 0px;
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/bundle.js"></script>
</body>
</html>

View File

@ -1,7 +1,8 @@
const control_classes = { const control_classes = {
Data: require('./data'), Data: require('./data'),
Text: require('./text'), Text: require('./text'),
Select: require('./select') Select: require('./select'),
Link: require('./link')
} }

View File

@ -0,0 +1,26 @@
const frappe = require('frappejs');
const BaseControl = require('./base');
const Awesomplete = require('awesomplete');
class LinkControl extends BaseControl {
make() {
super.make();
this.input.setAttribute('type', 'text');
this.awesomplete = new Awesomplete(this.input, {
autoFirst: true,
minChars: 0,
maxItems: 99
});
// rebuild the list on input
this.input.addEventListener('input', async (event) => {
this.awesomplete.list = (await frappe.db.get_all({
doctype: this.options,
filters: {keywords: ["like", this.input.value]},
limit: 50
})).map(d => d.name);
});
}
};
module.exports = LinkControl;

View File

@ -8,6 +8,7 @@
"start": "nodemon app.js" "start": "nodemon app.js"
}, },
"dependencies": { "dependencies": {
"awesomplete": "^1.1.2",
"body-parser": "^1.18.2", "body-parser": "^1.18.2",
"bootstrap": "^4.0.0", "bootstrap": "^4.0.0",
"express": "^4.16.2", "express": "^4.16.2",

View File

@ -16,7 +16,6 @@ module.exports = {
models.init_models(path.resolve('node_modules', 'frappejs', 'models')); models.init_models(path.resolve('node_modules', 'frappejs', 'models'));
models.init_models(models_path); models.init_models(models_path);
console.log(frappe.models.controllers);
await this.init_db({backend:backend, connection_params:connection_params}); await this.init_db({backend:backend, connection_params:connection_params});
// database // database

View File

@ -13,7 +13,7 @@ module.exports = {
let data = await frappe.db.get_all({ let data = await frappe.db.get_all({
doctype: request.params.doctype, doctype: request.params.doctype,
fields: request.query.fields || ['name', 'subject'], fields: request.query.fields,
filters: request.query.filters, filters: request.query.filters,
start: request.query.start || 0, start: request.query.start || 0,
limit: request.query.limit || 20, limit: request.query.limit || 20,

View File

@ -8,7 +8,7 @@ module.exports = {
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
// handle error // handle error
res.status(err ? err.status_code : 500).send({ error: err ? err.message : 'Unknown Error'}); res.status(err.status_code || 500).send({error: err.message});
}); });
}, },

View File

@ -216,6 +216,10 @@ autoprefixer@^6.3.1:
postcss "^5.2.16" postcss "^5.2.16"
postcss-value-parser "^3.2.3" postcss-value-parser "^3.2.3"
awesomplete@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/awesomplete/-/awesomplete-1.1.2.tgz#b6e253f73474e46278bba5ae7f81d4262160fb75"
aws-sign2@~0.6.0: aws-sign2@~0.6.0:
version "0.6.0" version "0.6.0"
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f" resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f"