bricks/bricks/dynamicaccordion.js

280 lines
6.7 KiB
JavaScript
Raw Normal View History

2024-02-26 18:22:18 +08:00
var bricks = window.bricks || {};
bricks.PageContent = class extends bricks.Layout {
constructor(opts){
super(opts);
}
}
bricks.AccordionItem = class extends bricks.Layout {
constructor(opts){
super(opts);
this.set_css('accordion-item');
}
}
2024-03-12 18:44:23 +08:00
bricks.AccordionInfo = class extends bricks.Layout {
2024-02-26 18:22:18 +08:00
constructor(opts){
super(opts);
2024-03-12 18:44:23 +08:00
this.set_css('accordion-item-info');
2024-02-26 18:22:18 +08:00
}
}
bricks.DynamicAccordion = class extends bricks.VScrollPanel {
/*
{
"data_url",
2024-03-11 15:22:03 +08:00
"data_method",
2024-02-26 18:22:18 +08:00
"cache_limit",
"page_rows",
"record_view"
2024-03-12 18:44:23 +08:00
"content_rely_on"
"content_rely_value"
"editable"
"fields":
"record_toolbar",
"record_toolbar_collapsable"
"header"
"content_view"
2024-02-26 18:22:18 +08:00
}
*/
constructor(opts){
super(opts);
this.element = this.dom_element;
2024-03-11 15:22:03 +08:00
this.loader = new bricks.PageDataLoader({
url:this.opts.data_url,
params:this.opts.data_params,
pagerows:this.opts.page_rows,
method:this.opts.data_method,
cache_pages:this.opts.cache_limit
});
2024-03-12 18:44:23 +08:00
this.old_params = null;
this.active_item = null;
2024-02-26 18:22:18 +08:00
this.active_content = null;
this.loading = false;
2024-03-12 18:44:23 +08:00
schedule_once(this.build_all.bind(this), 0.1);
}
async build_all(){
if (this.title){
this.build_title();
}
if (this.description){
this.build_description();
2024-02-26 18:22:18 +08:00
}
2024-03-12 18:44:23 +08:00
if (this.toolbar){
await this.build_toolbar();
2024-02-26 18:22:18 +08:00
}
2024-03-12 18:44:23 +08:00
if (this.editable && this.fields){
await this.build_new_form();
}
if (! this.noheader){
await this.build_header();
}
var filler = new bricks.Filler();
this.container = new bricks.VScrollPanel({ });
filler.add_widget(this.container);
this.add_widget(filler);
this.container.bind('min_threshold', this.load_previous_page.bind(this));
this.container.bind('max_threshold', this.load_next_page.bind(this));
await this.render();
}
async build_header(){
return await this.build_info();
2024-02-26 18:22:18 +08:00
}
2024-03-12 18:44:23 +08:00
async build_info(record){
if (! this.fields && !record){
return;
}
var w;
const info = new bricks.AccordionInfo({});
if (! record){
record = {};
for (var i=0;i<this.fields.length;i++){
var f = this.fields[i];
record[f.name] = f.label || f.name;
}
}
w = await bricks.widgetBuild(this.record_view, info, record);
if (w){
info.add_widget(w);
} else {
bricks.debug('bricks.widgetBuild(), tmpl=', this.record_view, 'record=', record);
}
this.add_widget(info);
return info;
}
async build_new_form(){
var desc = {
"widgettype":"VBox",
"options":{
"height":"auto",
},
"subwidgets":[
{
"id":"new",
"widgettype":"Icon",
"options":{
"url":this.editable.add_icon || bricks_resource('imgs/add.png')
}
},
{
"widgettype":"Form",
"options":{
"cheight":this.editable.form_cheight||7,
"submit_url":this.editable.new_data_url,
"fields":this.fields
}
}
]
};
var w = await bricks.widgetBuild(desc, this);
w.children[1].hide();
w.children[0].bind('click',function(){ w.children[1].toggle_hide()})
this.new_form = w.children[1];
this.new_form.bind('submited', this.render.bind(this));
this.add_widget(w);
}
2024-03-11 15:22:03 +08:00
async render(params) {
2024-02-26 18:22:18 +08:00
params = params || {};
2024-03-12 18:44:23 +08:00
if (params == this.old_params){
return;
}
this.old_params = params;
2024-03-11 15:22:03 +08:00
bricks.debug('params=', params, 'render() called');
var d = await this.loader.loadData(params);
if (d){
2024-03-12 18:44:23 +08:00
this.container.clear_widgets();
2024-03-11 15:22:03 +08:00
await this.dataHandle(d);
2024-03-08 10:54:16 +08:00
} else {
2024-03-11 15:22:03 +08:00
bricks.debug(params, 'load data return null');
2024-02-26 18:22:18 +08:00
}
2024-03-08 10:54:16 +08:00
}
2024-03-11 15:22:03 +08:00
async dataHandle(d){
var data = d.rows;
var page = d.add_page;
await this.renderAccordionItems(data, page);
if (d.delete_page){
this.delete_page(d.delete_page);
}
2024-03-08 10:54:16 +08:00
}
2024-02-26 18:22:18 +08:00
async renderAccordionItems(data, page) {
2024-03-08 10:54:16 +08:00
var pos;
2024-02-26 18:22:18 +08:00
for (var i=0;i<data.length;i++){
2024-03-11 15:22:03 +08:00
if (this.loader.is_max_page(page)){
2024-03-08 10:54:16 +08:00
pos = -1;
} else {
pos = i;
}
2024-02-26 18:22:18 +08:00
const record = data[i];
const item = new bricks.AccordionItem({});
2024-03-12 18:44:23 +08:00
var info = await this.build_info(record);
var build_content = true;
if (this.content_rely_on){
var v = this.content_rely_value || true;
if(record[this.content_rely_on] != v){
build_content = false;
2024-02-26 18:22:18 +08:00
}
}
2024-03-12 18:44:23 +08:00
content = null;
if (build_content){
var content = new bricks.VBox({
height:'auto',
display:'none'
});
content.set_css('accordion-item-content');
content.hide();
info.bind('click', this.toggle_content.bind(this, info, content, record));
}
item.add_widget(info);
if(content){
item.add_widget(content);
}
2024-03-08 10:54:16 +08:00
item.set_attribute('data-page', page);
2024-03-12 18:44:23 +08:00
this.container.add_widget(item, pos);
2024-02-26 18:22:18 +08:00
};
2024-03-08 10:54:16 +08:00
}
delete_page(page){
var items = this.dom_element.querySelectorAll('[data-page="' + page + '"]');
for (var i=0;i<items.length;i++) {
var w = items[i].bricks_widget;
2024-03-12 18:44:23 +08:00
this.container.remove_widget(w);
2024-02-26 18:22:18 +08:00
}
}
2024-03-12 18:44:23 +08:00
async toggle_content(info, content, record, event){
if (!this.content_view){
return;
}
var h = this.container.dom_element.offsetHeight - 3 * info.dom_element.offsetHeight;
2024-02-28 11:49:58 +08:00
content.set_style('height', h + 'px');
2024-02-26 18:22:18 +08:00
if (this.active_content){
if (this.active_content != content){
this.active_content.clear_widgets();
this.active_content.hide();
this.active_content = null;
}
}
content.toggle_hide();
if (content.dom_element.style.display == 'none'){
2024-03-08 10:54:16 +08:00
bricks.debug('content just collapsed');
2024-02-26 18:22:18 +08:00
content.clear_widgets();
this.active_content = null;
return;
}
var jc = new bricks.HttpJson();
var w;
2024-03-12 18:44:23 +08:00
w = await bricks.widgetBuild(this.content_view, content, record);
2024-02-26 18:22:18 +08:00
if (w){
content.add_widget(w);
this.active_content = content;
} else {
this.active_content = null;
content.hide();
}
}
async load_previous_page(){
2024-03-08 10:54:16 +08:00
if (this.loading){
bricks.debug('this.loading is set, do not thing');
return;
}
this.loading = true;
try {
2024-03-11 15:22:03 +08:00
var d = await this.loader.loadPreviousPage();
if (d){
this.dataHandle(d);
2024-03-12 18:44:23 +08:00
var total = this.container.dom_element.scrollHeight - this.container.dom_element.clientHeight;
this.container.dom_element.scrollTop = d.pos_rate * total;
2024-03-08 10:54:16 +08:00
} else {
2024-03-11 15:22:03 +08:00
bricks.debug(this.loader, 'load previous page error');
2024-03-08 10:54:16 +08:00
}
2024-03-11 15:22:03 +08:00
} catch (e) {
bricks.debug('e=', e);
2024-02-26 18:22:18 +08:00
}
2024-03-08 10:54:16 +08:00
this.loading = false;
2024-02-26 18:22:18 +08:00
}
async load_next_page(){
2024-03-08 10:54:16 +08:00
if (this.loading){
bricks.debug('this.loading is set, do not thing');
return;
2024-02-26 18:22:18 +08:00
}
2024-03-08 10:54:16 +08:00
this.loading = true;
try {
2024-03-11 15:22:03 +08:00
var d = await this.loader.loadNextPage();
if (d){
this.dataHandle(d);
2024-03-12 18:44:23 +08:00
var total = this.container.dom_element.scrollHeight - this.container.dom_element.clientHeight;
this.container.dom_element.scrollTop = d.pos_rate * total;
2024-03-08 10:54:16 +08:00
} else {
2024-03-11 15:22:03 +08:00
bricks.debug(this.loader, 'load next page error');
2024-03-08 10:54:16 +08:00
}
} catch (e){
bricks.debug('error happened', e);
}
this.loading = false;
bricks.debug('load_next_page() finished');
2024-02-26 18:22:18 +08:00
}
}
bricks.Factory.register('DynamicAccordion', bricks.DynamicAccordion);