This commit is contained in:
yumoqing 2024-04-07 17:33:24 +08:00
parent c2b73a5a08
commit 7901d7018d
3 changed files with 86 additions and 27 deletions

View File

@ -127,7 +127,7 @@ bricks.buildEventBind = function(from_widget, widget, event, desc){
}
bricks.universal_handler = function(from_widget, widget, desc, event){
var f = bricks.buildEventHandler(from_widget, desc);
var f = bricks.buildEventHandler(from_widget, desc, event);
if (f){
return f(event);
}
@ -137,23 +137,27 @@ bricks.universal_handler = function(from_widget, widget, desc, event){
'desc=', desc,
event);
}
bricks.buildEventHandler = function(w, desc){
bricks.buildEventHandler = function(w, desc, event){
var target = bricks.getWidgetById(desc.target, w);
if (! target){
bricks.debug('target miss desc=', desc, 'w=', w);
return null
}
var rtdata = null;
var rtdata = {};
if (desc.rtdata) rtdata = desc.rtdata;
else if (desc.datawidget){
var data_desc = {
widget:desc.datawidget,
method:desc.datamethod,
method:desc.datamethod || 'getValue',
params:desc.dataparams,
script:desc.datascript
}
rtdata = bricks.getRealtimeData(w, data_desc);
}
if (typeof event.params == typeof {}){
rtdata = bricks.extend(rtdata, event.params);
}
switch (desc.actiontype){
case 'urlwidget':
return bricks.buildUrlwidgetHandler(w, target, rtdata, desc);
@ -234,6 +238,7 @@ bricks.buildUrlwidgetHandler = function(w, target, rtdata, desc){
}
}
var options = objcopy(desc.options||{});
options = bricks.apply_data(options, rtdata);
bricks.extend(options.params, rtdata);
var opts = {
"widgettype":"urlwidget",
@ -255,7 +260,7 @@ bricks.buildBricksHandler = function(w, target, rtdata, desc){
target.add_widget(w);
}
var options = objcopy(desc.options||{});
bricks.extend(options.options, rtdata);
options = bricks.apply_data(options, rtdata);
return _buildWidget.bind(w, target, target, desc.mode || 'replace', options);
}
bricks.buildRegisterFunctionHandler = function(w, target, rtdata, desc){
@ -271,6 +276,7 @@ bricks.buildRegisterFunctionHandler = function(w, target, rtdata, desc){
if (rtdata){
bricks.extend(params, rtdata);
}
params = bricks.apply_data(params, rtdata);
return f.bind(target, params);
}
bricks.buildMethodHandler = function(w, target, rtdata, desc){
@ -282,12 +288,14 @@ bricks.buildMethodHandler = function(w, target, rtdata, desc){
var params = {};
bricks.extend(params, desc.params)
bricks.extend(params, rtdata);
params = bricks.apply_data(params, rtdata);
return f.bind(target, params);
}
bricks.buildScriptHandler = function(w, target, rtdata, desc){
var params = {};
bricks.extend(params, desc.params)
bricks.extend(params, rtdata);
params = bricks.apply_data(params, rtdata);
var f = new Function('target', 'params', 'event', desc.script);
bricks.debug('params=', params, 'buildScriptHandler() ..........');
return f.bind(target, target, params);
@ -296,6 +304,7 @@ bricks.buildDispatchEventHandler = function(w, target, rtdata, desc){
var params = {};
bricks.extend(params, desc.params)
bricks.extend(params, rtdata);
params = bricks.apply_data(params, rtdata);
var f = function(target, event_name, params){
target.dispatch(event_name, params);
}

View File

@ -131,9 +131,7 @@ bricks.DynamicAccordion = class extends bricks.VScrollPanel {
var info = new bricks.AccordionInfo({});
info.data = record;
item.add_widget(info);
if (this.editable){
tb = this.build_record_toolbar(info, record);
}
tb = this.build_record_toolbar(info, record);
if (! record){
record = {};
for (var i=0;i<this.fields.length;i++){
@ -308,10 +306,26 @@ bricks.DynamicAccordion = class extends bricks.VScrollPanel {
build_record_toolbar(info, record){
var c = new bricks.Layout({cwidth:2.5, height:'auto'})
var desc;
if (! record){
var edit_tools = [];
if (this.record_toolbar){
desc = bricks.extend({}, this.record_toolbar);
} else {
desc = {
cwidth:2.5,
tools:[
tools:[]
}
}
var event_names = [];
for (var i=0;i<desc.tools.length;i++){
event_names.push(desc.tools[i].name);
}
if (! record){
var mytools = [];
for (var i=0;i<desc.tools.length;i++){
mytools.push({ name:'blankicon'});
}
if (this.editable){
edit_tools = [
{
name:'add',
tip:'add new record',
@ -320,16 +334,20 @@ bricks.DynamicAccordion = class extends bricks.VScrollPanel {
{
name:'blankicon'
}
]
];
desc.tools = edit_tools.concat(mytools);
} else {
desc.tools = mytools;
}
var w = new bricks.IconBar(desc);
w.bind('add', this.add_record.bind(this, info, record));
if (this.editable){
w.bind('add', this.add_record.bind(this, info, record));
}
c.add_widget(w);
return c;
}
var desc = {
cwidth:2.5,
tools:[
if (this.editable){
var edit_tools = [
{
name:"update",
tip:"update record in this line",
@ -341,13 +359,22 @@ bricks.DynamicAccordion = class extends bricks.VScrollPanel {
icon:this.editable.delete_icon || bricks_resource('imgs/delete.png')
}
]
desc.tools = edit_tools.concat(desc.tools);
}
var w = new bricks.IconBar(desc);
w.bind('update', this.update_record.bind(this, info, record));
w.bind('delete', this.delete_record.bind(this, info, record));
if (this.editable){
w.bind('update', this.update_record.bind(this, info, record));
w.bind('delete', this.delete_record.bind(this, info, record));
}
for (var i=0;i<event_names.length;i++){
w.bind(event_names[i], this.fire_event.bind(this, event_names[i], record));
}
c.add_widget(w);
return c;
}
fire_event(event_name, data){
this.dispatch(event_name, data);
}
async renderAccordionItems(data, page) {
var pos;
for (var i=0;i<data.length;i++){

View File

@ -5,9 +5,10 @@ add following lines before 'bricks.js'
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
*/
bricks.MdText = class extends bricks.JsWidget {
bricks.MdWidget = class extends bricks.JsWidget {
/* options
{
"mdtext":
"md_url":
"method":"GET"
"params":{}
@ -16,11 +17,16 @@ bricks.MdText = class extends bricks.JsWidget {
constructor(options){
super(options);
var f = this.build.bind(this);
this.load_event = new Event('loaded');
this.dom_element.style.overFlow='auto';
window.addEventListener('scroll', this.show_scroll.bind(this));
schedule_once(f, 0.01);
if (this.mdtext){
this.md_content = this.mdtext;
this._build1();
} else {
var f = this.build.bind(this);
this.load_event = new Event('loaded');
this.dom_element.style.overFlow='auto';
window.addEventListener('scroll', this.show_scroll.bind(this));
schedule_once(f, 0.01);
}
}
show_scroll(event){
bricks.debug('scrollY=', window.scrollY);
@ -30,8 +36,12 @@ bricks.MdText = class extends bricks.JsWidget {
}
_build = async function(md_url){
var md_content = await (bricks.tget(md_url));
this.dom_element.innerHTML = marked.parse(md_content);
this.md_content = md_content;
this._build1();
this.dispatch('loaded', {'url':md_url});
}
_build1(){
this.dom_element.innerHTML = marked.parse(this.md_content);
/* change links in markdown to a bricks action */
var links = this.dom_element.getElementsByTagName('a');
for (var i=0; i<links.length; i ++){
@ -39,7 +49,15 @@ bricks.MdText = class extends bricks.JsWidget {
links[i].href = '#';
links[i].onclick=this._build.bind(this, url);
}
this.dispatch('loaded', {'url':md_url});
}
getname(){
return this.name || 'mdtext';
}
getValue(){
return {this.getname():this.md_content};
}
setValue(v){
this.md_content = v;
}
}
bricks.MarkdownViewer = class extends bricks.VBox {
@ -48,6 +66,7 @@ bricks.MarkdownViewer = class extends bricks.VBox {
navigator:true
recommentable:false
md_url:
mdtext:
method:"GET",
params:{}
}
@ -59,7 +78,10 @@ bricks.MarkdownViewer = class extends bricks.VBox {
if (this.opts.navigator){
this.createBackButton();
}
this.mdtext = new bricks.MdText({
this.mdtext = new bricks.MdWidget({
mdtext:this.mdtext,
method:this.method,
params:this.params,
md_url:this.md_url
});
this.add_widget(this.mdtext);
@ -127,3 +149,4 @@ bricks.MarkdownViewer = class extends bricks.VBox {
}
bricks.Factory.register('MarkdownViewer', bricks.MarkdownViewer);
bricks.Factory.register('MdWidget', bricks.MdWidget);