bricks/bricks/toolbar.js

125 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-02-01 17:54:04 +08:00
var bricks = window.bricks || {};
2024-02-01 16:40:31 +08:00
bricks.Toolbar = class extends bricks.Layout {
2023-09-14 10:28:07 +08:00
/* toolbar options
{
orientation:
target:
interval::
tools:
}
tool options
{
icon:
name:
label:
css:
}
*/
constructor(options){
super(options);
this.toolList = [];
if (this.opts.orientation == 'vertical'){
2024-03-01 11:07:42 +08:00
this.bar = new bricks.VScrollPanel(options);
2023-09-14 10:28:07 +08:00
this.dom_element.classList.add('vtoolbar')
} else {
2024-03-01 11:07:42 +08:00
this.bar = new bricks.HScrollPanel(options);
2023-09-14 10:28:07 +08:00
this.dom_element.classList.add('htoolbar')
}
this.add_widget(this.bar);
this.clicked_btn = null;
this.preffix_css = this.opts.css || 'toolbar';
schedule_once(this.createTools.bind(this), 0.01);
}
add_interval_box(){
if (this.opts.orientation == 'vertical'){
2024-02-01 17:54:04 +08:00
this.bar.add_widget(new bricks.JsWidget({
2023-09-14 10:28:07 +08:00
height:this.opts.interval || '10px'
}));
} else {
2024-02-01 17:54:04 +08:00
this.bar.add_widget(new bricks.JsWidget({
2023-09-14 10:28:07 +08:00
width:this.opts.interval || '10px'
}));
}
}
createTools = async function(){
var l = this.opts.tools.length;
for (var i=0;i<l; i++){
await this.createTool(this.opts.tools[i]);
if (i < l -1 ){
this.add_interval_box();
}
}
}
createTool = async function(desc){
var options = {
"widgettype":"Button",
"options":{
width:"auto",
orientation:"horizontal",
icon:desc.icon,
label:desc.label,
name:desc.name,
css:desc.css
}
};
2024-02-20 15:56:35 +08:00
var w = await bricks.widgetBuild(options);
2023-09-14 10:28:07 +08:00
if (! w){
2024-03-08 10:54:16 +08:00
bricks.debug('Toolbar(): build widget failed', options);
2023-09-14 10:28:07 +08:00
return
}
w.bind('click', this.do_handle.bind(this, w));
w.tool_opts = desc;
this.add_removable(w);
this.toolList.push(w);
this.bar.add_widget(w);
return w;
}
remove_item(w, event){
this.bar.remove_widget(w);
this.toolList.remove(w);
w.unbind('click',this.do_handle.bind(this, w));
this.dispatch('remove', w.tool_opts);
event.preventDefault();
event.stopPropagation();
}
do_handle(tool, event){
// var tool = event.target;
2024-03-08 10:54:16 +08:00
bricks.debug('Toolbar() onclick,target=', event.target, tool);
2023-09-14 10:28:07 +08:00
var d = {};
2024-02-21 23:00:49 +08:00
bricks.extend(d, tool.tool_opts);
2023-09-14 10:28:07 +08:00
if (this.opts.target){
d.target = this.opts.target;
}
this.dispatch('command', d);
2024-03-01 11:07:42 +08:00
this.dispatch(tool.tool_opts.name, d);
2024-03-08 10:54:16 +08:00
bricks.debug('event ', tool.tool_opts.name, ' fired ...')
2023-09-14 10:28:07 +08:00
if (this.clicked_btn){
this.clicked_btn.set_css(this.preffix_css + '-button-active', true);
}
tool.set_css(this.preffix_css + '-button-active');
this.clicked_btn = tool;
}
add_removable(item){
if (! item.tool_opts.removable) return;
var img_src = bricks_resource('imgs/delete.png');
2024-02-01 17:54:04 +08:00
var image = new bricks.Icon({url:img_src});
2023-09-14 10:28:07 +08:00
if (image){
item.add_widget(image);
image.bind('click',this.remove_item.bind(this, item));
2024-03-08 10:54:16 +08:00
bricks.debug('Toolbar(): add_removable() for ', img_src);
2023-09-14 10:28:07 +08:00
} else {
2024-03-08 10:54:16 +08:00
bricks.debug('Toolbar(): Image create error', img_src);
2023-09-14 10:28:07 +08:00
}
}
click(name){
for (var i=0;i<this.toolList.length;i++){
if (name==this.toolList[i].tool_opts.name){
this.toolList[i].dom_element.click();
}
}
}
}
2024-02-01 16:40:31 +08:00
bricks.Factory.register('Toolbar', bricks.Toolbar);