2020-11-13 20:27:14 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {TemplateView} from "./TemplateView.js";
|
2021-05-31 22:53:00 +05:30
|
|
|
import { tag } from "./html.js";
|
2020-11-13 20:27:14 +05:30
|
|
|
|
|
|
|
export class Menu extends TemplateView {
|
2021-05-31 22:53:00 +05:30
|
|
|
static optionWithButton(label, callback) {
|
|
|
|
const option = new MenuOption(label);
|
|
|
|
option.setCallback(callback);
|
|
|
|
return option;
|
|
|
|
}
|
|
|
|
|
|
|
|
static optionWithLink(label, link) {
|
|
|
|
const option = new MenuOption(label);
|
|
|
|
option.setLink(link);
|
|
|
|
return option;
|
2020-11-13 20:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
super();
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:53:00 +05:30
|
|
|
_convertToDOM(option) {
|
|
|
|
if (option.callback) {
|
|
|
|
return tag.button({ onClick: option.callback }, option.label);
|
|
|
|
}
|
|
|
|
else if (option.link) {
|
|
|
|
return tag.a({ href: option.link }, option.label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 20:27:14 +05:30
|
|
|
render(t) {
|
|
|
|
return t.ul({className: "menu", role: "menu"}, this._options.map(o => {
|
2021-05-28 19:55:23 +05:30
|
|
|
const className = {
|
|
|
|
destructive: o.destructive,
|
|
|
|
};
|
|
|
|
if (o.icon) {
|
|
|
|
className.icon = true;
|
|
|
|
className[o.icon] = true;
|
|
|
|
}
|
2020-11-13 20:27:14 +05:30
|
|
|
return t.li({
|
2021-05-28 19:55:23 +05:30
|
|
|
className,
|
2021-05-31 22:53:00 +05:30
|
|
|
}, this._convertToDOM(o));
|
2020-11-13 20:27:14 +05:30
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MenuOption {
|
2021-05-31 22:53:00 +05:30
|
|
|
constructor(label) {
|
2020-11-13 20:27:14 +05:30
|
|
|
this.label = label;
|
|
|
|
this.icon = null;
|
2021-05-28 19:55:23 +05:30
|
|
|
this.destructive = false;
|
2020-11-13 20:27:14 +05:30
|
|
|
}
|
|
|
|
|
2021-05-31 22:53:00 +05:30
|
|
|
setCallback(callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLink(link) {
|
|
|
|
this.link = link;
|
|
|
|
}
|
|
|
|
|
2020-11-13 20:27:14 +05:30
|
|
|
setIcon(className) {
|
|
|
|
this.icon = className;
|
|
|
|
return this;
|
|
|
|
}
|
2021-05-28 19:55:23 +05:30
|
|
|
|
|
|
|
setDestructive() {
|
|
|
|
this.destructive = true;
|
|
|
|
return this;
|
|
|
|
}
|
2020-11-13 20:27:14 +05:30
|
|
|
}
|