bricks/bricks/tabular.js

197 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-04-29 17:36:36 +08:00
var bricks = window.bricks || {};
2024-05-06 11:19:10 +08:00
bricks.Tabular = class extends bricks.DynamicAccordion {
2024-04-29 17:36:36 +08:00
constructor(opts){
super(opts);
2024-05-08 11:15:05 +08:00
this.get_edit_fields();
2024-06-03 19:04:30 +08:00
this.check_changed_row = null;
this.bind('row_check_changed', this.show_check_event_data.bind(this));
}
show_check_event_data(event){
var d = event.params;
console.log('row_check_changed event data=', d);
2024-04-29 17:36:36 +08:00
}
2024-05-30 16:06:46 +08:00
build_toolbar(){
var edit_names = [];
var tbdesc = {
width:'auto',
tools:[]
}
if (this.editable){
tbdesc.tools = [
{
name:'add',
tip:'add new record',
icon:this.editable.add_icon || bricks_resource('imgs/add.png')
},
{
name:'update',
tip:'update selected record',
selected_row:true,
icon:this.editable.update_icon || bricks_resource('imgs/update.png')
},
{
name:'delete',
tip:'delete selected record',
selected_row:true,
icon:this.editable.delete_icon || bricks_resource('imgs/delete.png')
}
];
tbdesc.tools.forEach(t => {
edit_names.push(t.name);
});
}
if (this.toolbar){
this.toolbar.tools.forEach(t => {
if (! edit_names.incloudes(t.name)){
tbdesc.tools.push(t);
}
});
}
if (tbdesc.tools.length == 0){
return;
}
this.toolbar_w = new bricks.IconTextBar(tbdesc);
this.add_widget(this.toolbar_w);
this.toolbar_w.bind('command', this.command_event_handle.bind(this));
}
command_event_handle(event){
var tdesc = event.params;
if (tdesc.selected_row && ! this.select_row){
bricks.show_error({title:'Error', message:'need select a row'});
return;
}
if (tdesc.name == 'add'){
this.add_record();
return;
}
if (tdesc.name == 'update'){
this.update_record(this.select_row);
return;
}
if (tdesc.name == 'delete'){
this.delete_record(this.select_row);
return;
}
var d = {
tabular:this,
row:this.select_row,
data:row.user_data
}
this.dispathc(t.name. d);
}
2024-05-08 11:15:05 +08:00
get_edit_fields(){
2024-05-14 17:14:30 +08:00
var fs = this.row_options.fields;
2024-05-07 18:15:06 +08:00
this.fields = [];
var exclouded = [];
2024-05-14 17:14:30 +08:00
if (this.row_options.editexclouded){
exclouded = this.row_options.editexclouded;
2024-05-07 18:15:06 +08:00
}
fs.forEach(f => {
2024-05-08 11:15:05 +08:00
if (!exclouded.includes(f.name)){
2024-05-07 18:15:06 +08:00
this.fields.push(f);
}
2024-05-08 11:15:05 +08:00
});
2024-05-07 18:15:06 +08:00
}
2024-04-29 17:36:36 +08:00
async build_info(item, record){
2024-05-14 17:14:30 +08:00
var header = true;
var options = bricks.extend({cheight:this.cheight}, this.row_options);
2024-04-29 17:36:36 +08:00
if (record){
2024-05-07 16:53:57 +08:00
options.user_data = record;
2024-05-14 17:14:30 +08:00
header = false;
2024-04-29 17:36:36 +08:00
}
2024-05-14 17:14:30 +08:00
var dr = new bricks.DataRow(options);
2024-05-30 16:06:46 +08:00
dr.render(header);
2024-05-14 17:14:30 +08:00
dr.event_names.forEach(e => {
dr.toolbar_w.bind(e, this.record_event_handle.bind(this, e, record, dr, item));
});
2024-05-24 16:07:34 +08:00
dr.bind('check_changed', this.record_check_changed.bind(this));
2024-05-06 11:19:10 +08:00
item.add_widget(dr);
return dr;
2024-04-29 17:47:29 +08:00
}
2024-05-24 16:07:34 +08:00
record_check_changed(event){
2024-06-03 19:04:30 +08:00
this.check_changed_row = event.params;
this.dispatch('row_check_changed', event.params.user_data);
2024-05-24 16:07:34 +08:00
}
2024-05-08 16:01:23 +08:00
async renew_record_view(form, info){
var d = form.getValue();
var record = bricks.extend(info.user_data, d);
info.renew(record);
}
2024-05-06 11:19:10 +08:00
record_event_handle(event_name, record, row, item){
2024-05-06 18:26:21 +08:00
console.log('event_name=', event_name, 'record=', record);
2024-05-30 16:06:46 +08:00
this.dispatch(event_name, record);
}
add_record(info){
var f = new bricks.ModalForm({
"width":"90%",
"height":"70%",
"submit_url":this.editable.new_data_url,
"fields":this.fields
});
f.open();
f.bind('submited', this.add_record_finish.bind(this, f));
}
async add_record_finish(f, event){
f.dismiss();
var resp = event.params;
var desc = await resp.json();
var w = await bricks.widgetBuild(desc);
this.render();
}
update_record(){
var record = this.select_row.user_data;
var fields = [];
for (var i=0; i< this.fields.length; i++){
var f = bricks.extend({}, this.fields[i]);
f.value = record[f.name];
fields.push(f);
}
var f = new bricks.ModalForm({
"width":"90%",
"height":"70%",
"submit_url":this.editable.update_data_url+'?id=' + record.id,
"fields":fields
});
f.open();
f.bind('submited', this.update_record_finish.bind(this, f));
}
update_cancel(f){
f.dismiss();
}
async update_record_finish(form, event){
var resp = event.params;
var desc = await resp.json();
var w = await bricks.widgetBuild(desc);
if (desc.widgettype == 'Message'){
await this.renew_record_view(form, this.select_row);
}
}
delete_record(info, record){
var conform_w = new bricks.Conform({
target:this,
title:'Delete conform',
message:'Are you sure to delete is record?'
});
conform_w.bind('conformed', this.delete_record_act.bind(this, info, record));
conform_w.bind('discard', conform_w.dismiss.bind(conform_w));
}
async delete_record_act(){
var id = this.select_row.user_data.id;
var url = this.editable.delete_data_url + '?id=' + id;
var hc = new bricks.HttpJson();
var desc = await hc.get(url);
var w = await bricks.widgetBuild(desc);
if (desc.widgettype == 'Message'){
this.container.remove_widget(this.select_row.parent);
this.select_row.unset_css('accordion-item-info-selected');
this.select_row = null;
this.render();
2024-05-07 16:53:57 +08:00
}
2024-04-29 17:36:36 +08:00
}
}
bricks.Factory.register('Tabular', bricks.Tabular);