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){
this.view_w = await bricks.widgetBuild(this.tree.node_view, widget, this.user_data);
if (this.view_w){
widget.add_widget(this.view_w);
}
} else {
var txt = this.user_data[this.tree.opts.textField]; var txt = this.user_data[this.tree.opts.textField];
this.str_w = new bricks.Text({text:txt}); this.str_w = new bricks.Text({text:txt});
this.input = new bricks.UiStr({name:'text', value:txt});
this.input.bind('blur', this.edit_handle.bind(this));
widget.add_widget(this.str_w); widget.add_widget(this.str_w);
} }
edit(){
this.node_widget.remove_widget(this.str_w);
this.input.setValue(this.str_w.text);
this.node_widget.add_widget(this.input);
} }
async edit_handle(){ async update_content(){
if (this.input.value==this.str_w.text) if (this.tree.node_view){
return; this.node_widget.remove_Widget(this.view_w);
var v = this.input.value; this.view_w = await bricks.widgetBuild(this.tree.node_view, widget, this.user_data);
r = await this.syncdata('edit'); if (this.view_w){
this.user_data[this.tree.opts.textField] = v; widget.add_widget(this.view_w);
this.str_w = new bricks.Text({text:v}); }
this.node_widget.remove_widget(this.input); } else {
this.node_widget.add_widget(this.str_w); this.str_w.set_text(this.user_data[this.tree.opts.textField]);
} }
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,6 +245,7 @@ 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();
} }
if (this.editable.add_url){
var jc = new bricks.HttpJson() var jc = new bricks.HttpJson()
var desc = await jc.post(this.editable.add_url, {params:d}); var desc = await jc.post(this.editable.add_url, {params:d});
if (desc.widgettype == 'Message'){ if (desc.widgettype == 'Message'){
@ -237,6 +254,10 @@ bricks.Tree = class extends bricks.VScrollPanel {
} }
var w = await bricks.widgetBuild(desc, this); var w = await bricks.widgetBuild(desc, this);
w.open(); w.open();
} else {
d[this.idField] = bricks.uuid();
this.append_new_subnode(node, d);
}
} }
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,6 +295,7 @@ 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();
if (this.editable.delete_url){
var jc = new bricks.HttpJson() var jc = new bricks.HttpJson()
var desc = await jc.post(this.editable.delete_url, {params:d}); var desc = await jc.post(this.editable.delete_url, {params:d});
if (desc.widgettype == 'Message'){ if (desc.widgettype == 'Message'){
@ -286,6 +308,13 @@ bricks.Tree = class extends bricks.VScrollPanel {
} }
var w = await bricks.widgetBuild(desc, this); var w = await bricks.widgetBuild(desc, this);
w.open(); w.open();
} else {
var pnode = this.selected_node.parent_node;
if (pnode && pnode.container){
pnode.container.remove_widget(this.selected_node);
}
this.selected_node = null;
}
} }
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();
if(this.editable.update_url){
var jc = new bricks.HttpJson() var jc = new bricks.HttpJson()
var desc = await jc.post(this.editable.update_url, {params:d}); var desc = await jc.post(this.editable.update_url, {params:d});
if (desc.widgettype == 'Message'){ if (desc.widgettype == 'Message'){
this.update_node_data(node, d); await this.update_node_data(node, d);
} }
var w = await bricks.widgetBuild(desc, this); var w = await bricks.widgetBuild(desc, this);
w.open(); w.open();
} else {
await this.update_node_data(node, d);
}
} }
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){