bricks/bricks/datarow.js

100 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-04-29 17:36:36 +08:00
var bricks = window.bricks || {};
bricks.DataRow = class extends bricks.HBox {
/*
{
toolbar:[
]
fields:[]
css
header_css
}
*/
constructor(opts){
super(opts);
}
render_header(editable, checkable){
this.render(editable, checkable, true);
}
render_data(editable, checkable){
this.render(editable, checkable, false);
}
render(editable, checkable, header){
if (checkable){
var w = new bricks.UiCheck({name:'c',value:false})
this.add_widget(w);
w.bind(changed, this.get_check_state.bind(this));
}
this.build_toolbar(editable, header);
this.build_fields(header);
}
get_check_state(e){
d = e.target.getValue()
this.dispatch('checked', d.c)
}
build_toolbar(editable, header){
var tools = [];
2024-05-06 11:19:10 +08:00
var rsvd = ['add', 'update', 'delete'];
2024-04-29 17:36:36 +08:00
if (editable){
if (header){
tools.push({
name:'add',
tip:'add new record',
2024-05-06 16:21:56 +08:00
icon:editable.add_icon || bricks_resource('imgs/add.png')
2024-04-29 17:36:36 +08:00
});
tools.push({
name:'blankicon'
});
} else {
tools.push({
});
tools.push({
});
}
}
if (header){
if (this.toolbar){
2024-05-06 11:19:10 +08:00
this.toolbar.tools.forEach(t => {
if (! rsvd.includes(t.name))
tools.push({name:'blankicon'});
});
2024-04-29 17:36:36 +08:00
}
} else {
if (this.toolbar){
2024-05-06 11:19:10 +08:00
this.toolbar.tools.forEach(t => {
if (! rsvd.includes(t.name))
tools.push(t)
});
2024-04-29 17:36:36 +08:00
}
}
var toolbar = bricks.extend({cwidth:2.5}, this.toolbar || {});
toolbar.tools = tools;
2024-05-06 11:19:10 +08:00
var w = new bricks.IconBar(toolbar);
2024-04-29 17:36:36 +08:00
this.add_widget(w);
this.event_names = []
for(var i=0;i<tools.length;i++){
this.event_names.push(tools[i].name);
w.bind(tools[i].name, this.dispatch(tools[i].name));
}
}
2024-05-06 11:19:10 +08:00
2024-04-29 17:36:36 +08:00
build_fields(header){
for (var i=0;i<this.fields.length;i++){
var f = this.fields[i]
2024-05-06 11:19:10 +08:00
var opts = bricks.extend({
margin:'3px'
}, f);
2024-04-29 17:36:36 +08:00
if (header){
2024-05-06 11:19:10 +08:00
opts.value = f.label || f.name;
2024-05-06 18:00:27 +08:00
}
2024-05-06 11:19:10 +08:00
var f = bricks.get_ViewBuilder(f.uitype);
if (!f) f = bricks.get_ViewBuilder('str');
var w = f(opts);
2024-04-29 17:36:36 +08:00
this.add_widget(w)
}
}
}
bricks.Factory.register('DataRow', bricks.DataRow);