This commit is contained in:
yumoqing 2024-05-22 12:03:44 +08:00
parent c3e8e499ac
commit 72173bef6a

View File

@ -23,7 +23,7 @@ bricks.TreeNode = class extends bricks.VBox {
this.add_widget(n); this.add_widget(n);
n.bind('click', this.tree.node_click_handle.bind(this.tree, this)); n.bind('click', this.tree.node_click_handle.bind(this.tree, this));
this.node_widget = n; this.node_widget = n;
this.create_node_content(n); schedule_once(this.create_node_content.bind(this,n), 0.01);
if (! this.user_data.is_leaf) { if (! this.user_data.is_leaf) {
this.container = new bricks.VBox({height:'auto'}); this.container = new bricks.VBox({height:'auto'});
this.add_widget(this.container); this.add_widget(this.container);
@ -35,7 +35,16 @@ bricks.TreeNode = class extends bricks.VBox {
} }
} }
getValue(){ getValue(){
return this.user_data; var v = this.user_data;
if (this.container){
var children = [];
for (var i=0; i<this.container.children.length;i++){
var sv = this.container.children[i].getValue();
children.push(sv);
}
v.children = children;
}
return v;
} }
get_id(){ get_id(){
return this.user_data[this.tree.idField]; return this.user_data[this.tree.idField];
@ -70,7 +79,7 @@ bricks.TreeNode = class extends bricks.VBox {
} }
this.container.hide(); this.container.hide();
} }
create_node_content(widget){ async create_node_content(widget){
var img_size = bricks.app.charsize; var img_size = bricks.app.charsize;
if (this.is_leaf){ if (this.is_leaf){
widget.add_widget(new bricks.BlankIcon({})); widget.add_widget(new bricks.BlankIcon({}));
@ -106,29 +115,27 @@ bricks.TreeNode = class extends bricks.VBox {
url:icon url:icon
}); });
widget.add_widget(img); widget.add_widget(img);
// console.log('info:textField=', this.tree.textField, 'data=', this.user_data, 'tree=', this.tree, 'tree opts=', this.tree.opts); if (this.tree.node_view){
var txt = this.user_data[this.tree.opts.textField]; this.view_w = await bricks.widgetBuild(this.tree.node_view, widget, this.user_data);
this.str_w = new bricks.Text({text:txt}); if (this.view_w){
this.input = new bricks.UiStr({name:'text', value:txt}); widget.add_widget(this.view_w);
this.input.bind('blur', this.edit_handle.bind(this)); }
widget.add_widget(this.str_w); } else {
var txt = this.user_data[this.tree.opts.textField];
this.str_w = new bricks.Text({text:txt});
widget.add_widget(this.str_w);
}
} }
edit(){ async update_content(){
this.node_widget.remove_widget(this.str_w); if (this.tree.node_view){
this.input.setValue(this.str_w.text); this.node_widget.remove_Widget(this.view_w);
this.node_widget.add_widget(this.input); this.view_w = await bricks.widgetBuild(this.tree.node_view, widget, this.user_data);
} if (this.view_w){
async edit_handle(){ widget.add_widget(this.view_w);
if (this.input.value==this.str_w.text) }
return; } else {
var v = this.input.value; this.str_w.set_text(this.user_data[this.tree.opts.textField]);
r = await this.syncdata('edit'); }
this.user_data[this.tree.opts.textField] = v;
this.str_w = new bricks.Text({text:v});
this.node_widget.remove_widget(this.input);
this.node_widget.add_widget(this.str_w);
}
async syncdata(mode){
} }
} }
@ -185,7 +192,16 @@ bricks.Tree = class extends bricks.VScrollPanel {
this.bind('node_selected',this.node_info_log.bind(this)); this.bind('node_selected',this.node_info_log.bind(this));
} }
getValue(){ getValue(){
return this.user_data; var v = this.user_data;
if (this.container){
var children = [];
for (var i=0; i<this.container.children.length;i++){
var sv = this.container.children[i].getValue();
children.push(sv);
}
v.children = children;
}
return v;
} }
get_id(){ get_id(){
return this.user_data[this.idField]; return this.user_data[this.idField];
@ -229,14 +245,19 @@ bricks.Tree = class extends bricks.VScrollPanel {
node = this.selected_node; node = this.selected_node;
d[this.parentField] = node.get_id(); d[this.parentField] = node.get_id();
} }
var jc = new bricks.HttpJson() if (this.editable.add_url){
var desc = await jc.post(this.editable.add_url, {params:d}); var jc = new bricks.HttpJson()
if (desc.widgettype == 'Message'){ var desc = await jc.post(this.editable.add_url, {params:d});
var data = desc.options.user_data; if (desc.widgettype == 'Message'){
this.append_new_subnode(node, data); var data = desc.options.user_data;
this.append_new_subnode(node, data);
}
var w = await bricks.widgetBuild(desc, this);
w.open();
} else {
d[this.idField] = bricks.uuid();
this.append_new_subnode(node, d);
} }
var w = await bricks.widgetBuild(desc, this);
w.open();
} }
async create_tree_nodes(node, records){ async create_tree_nodes(node, records){
for (var i=0;i<records.length;i++){ for (var i=0;i<records.length;i++){
@ -274,18 +295,26 @@ bricks.Tree = class extends bricks.VScrollPanel {
async delete_node_conformed(event){ async delete_node_conformed(event){
var d = {}; var d = {};
d[this.idField] = this.selected_node.get_id(); d[this.idField] = this.selected_node.get_id();
var jc = new bricks.HttpJson() if (this.editable.delete_url){
var desc = await jc.post(this.editable.delete_url, {params:d}); var jc = new bricks.HttpJson()
if (desc.widgettype == 'Message'){ var desc = await jc.post(this.editable.delete_url, {params:d});
if (desc.widgettype == 'Message'){
var pnode = this.selected_node.parent_node;
console.log('parent node=', pnode);
if (pnode && pnode.container){
pnode.container.remove_widget(this.selected_node);
}
this.selected_node = null;
}
var w = await bricks.widgetBuild(desc, this);
w.open();
} else {
var pnode = this.selected_node.parent_node; var pnode = this.selected_node.parent_node;
console.log('parent node=', pnode);
if (pnode && pnode.container){ if (pnode && pnode.container){
pnode.container.remove_widget(this.selected_node); pnode.container.remove_widget(this.selected_node);
} }
this.selected_node = null; this.selected_node = null;
} }
var w = await bricks.widgetBuild(desc, this);
w.open();
} }
delete_subnode(node, subnode){ delete_subnode(node, subnode){
var subid = subnode.user_data[this.idField]; var subid = subnode.user_data[this.idField];
@ -326,20 +355,24 @@ bricks.Tree = class extends bricks.VScrollPanel {
var d = event.params; var d = event.params;
var node = this.selected_node; var node = this.selected_node;
d[this.idField] = node.get_id(); d[this.idField] = node.get_id();
var jc = new bricks.HttpJson() if(this.editable.update_url){
var desc = await jc.post(this.editable.update_url, {params:d}); var jc = new bricks.HttpJson()
if (desc.widgettype == 'Message'){ var desc = await jc.post(this.editable.update_url, {params:d});
this.update_node_data(node, d); if (desc.widgettype == 'Message'){
await this.update_node_data(node, d);
}
var w = await bricks.widgetBuild(desc, this);
w.open();
} else {
await this.update_node_data(node, d);
} }
var w = await bricks.widgetBuild(desc, this);
w.open();
} }
update_node_data(node, data){ async update_node_data(node, data){
for (var name in Object.keys(data)){ for (var name in Object.keys(data)){
node.user_data[name] = data[name]; node.user_data[name] = data[name];
} }
node.str_w.set_text(data[this.textField]); await node.update_content();
} }
async get_children_data(node){ async get_children_data(node){