declare var require: any require("../lib/bootstrap-contextmenu") require("../lib/bootstrap-treeview") export interface IControl{ render(e:Element); dispose?(); title():string; id(): string controlId?:string contextActions?:IContributionItem[] extraStyles?:CSSStyleDeclaration } export interface IContributionItem{ id?:string title?: string link?: string image?: string disabled?: boolean checked?:boolean danger?:boolean warning?:boolean primary?: boolean success?: boolean run?(args?:any):void items?:IContributionItem[] } export interface IMenu extends IContributionItem{ items:IContributionItem[] } export class ToolbarRenderer{ constructor(private menu:IMenu){} style: CSSStyleDeclaration={} render(host:Element){ this.menu.items.forEach(x=>{ var button=document.createElement("button"); button.classList.add("btn") button.classList.add("btn-xs") if (x.checked){ button.classList.add("btn-success"); } else { if (x.danger){ button.classList.add("btn-danger") } else { button.classList.add("btn-primary") } } copyProps(this.style,button.style); button.textContent=x.title if (x.image){ button.innerHTML=``+x.title; } if (x.run){ button.onclick=x.run } if (x.disabled){ button.disabled=true; } host.appendChild(button); }) } } export class DrowpdownMenu{ constructor(private menu:IMenu,private setRoles:boolean=true){} render(host:Element){ this.menu.items.forEach(x=>{ var li=document.createElement("li"); if (this.setRoles) { li.setAttribute("role", "presentation"); } if (x.disabled){ li.classList.add("disabled"); } var a=document.createElement("a"); a.setAttribute("href",x.link?x.link:"#") if (this.setRoles) { a.setAttribute("role", "menuitem") } a.style.cursor="hand"; if ((x).run){ a.onclick=function (e){ x.run(); }; } if (x.checked){ a.innerHTML=x.title+"" } else{ a.innerHTML=x.title; } li.appendChild(a); host.appendChild(li); }) } } export class Context{ constructor(private menu:IMenu){} render(host:Element){ this.menu.items.forEach(x=>{ var li=document.createElement("li"); //li.setAttribute("role","presentation"); if (x.disabled){ li.classList.add("disabled"); } var a=document.createElement("a"); //a.setAttribute("role","menuitem") if ((x).run){ a.onclick=(x).run; } a.innerHTML=x.title; li.appendChild(a); host.appendChild(li); }) } } var c=1; export abstract class AbstractComposite implements IControl{ private _title: string; children:IControl[]=[] protected _element:Element; protected _id:string; id(){ if (this._id){ return this._id; } this._id="c"+(c++); return this._id; } render(e:Element){ this._element=e; this.innerRender(e); } refresh(){ if (this._element){ this.innerRender(this._element); } } protected abstract innerRender(e:Element):Element|void add(c:IControl){ this.children.push(c); this.refresh(); } remove(c:IControl){ this.children=this.children.filter(x=>x!=c); this.refresh(); } dispose(){ this._element=null; this.children.forEach(x=>{ if (x.dispose){ x.dispose(); } }) } setTitle(title:string){ this._title=title; } title(){ return this._title; } } function copyProps(a:any,b:any){ Object.keys(a).forEach(k=>{ b[k]=a[k]; }) } export class Composite extends AbstractComposite{ constructor(private tagName:string){super()} _style:CSSStyleDeclaration={} _styleString: string; attrs:any={}; _className:string _classNames:string[]=[] _text:string addLabel(l:string){ var cnt=new Composite("span"); cnt._text=l; this.add(cnt); return cnt; } addClassName(c:string){ this._classNames.push(c); } style(){ return this._style; } withClass(c:string){ this._className=c; return this; } protected innerRender(e:Element){ var ch=document.createElement(this.tagName) e.appendChild(ch); this.renderContent(ch); } refresh(){ if (this._element) { this._element.innerHTML = null; this.renderContent(this._element); } } protected renderContent(ch: HTMLElement) { ch.id=this.id(); if (this._styleString){ ch.setAttribute("style",this._styleString); } else if (this._style){ copyProps(this._style,ch.style); } if (this._className){ ch.className=this._className; } Object.keys(this.attrs).forEach(k=>{ ch.setAttribute(k,this.attrs[k]) }) this._classNames.forEach(x=>{ch.classList.add(x)}) if (this._text){ ch.innerText=this._text; } this.extraRender(ch); this.renderChildren(ch); } protected renderChildren(ch: HTMLElement) { this.children.forEach(c=> { var w = this.wrap(ch,c); var el = c.render(w); if (el) { w.appendChild(el) } }) } protected extraRender(ch: HTMLElement){ } protected wrap(p:HTMLElement,c?:IControl){ return p; } } export class WrapComposite extends Composite{ wrapElement:string="div" protected wrap(p:HTMLElement){ var d=document.createElement(this.wrapElement); p.appendChild(d); return d; } } export class HorizontalFlex extends Composite{ constructor(){ super("div") this._style.display="flex"; this._style.flexDirection="row" } wrapStyle: CSSStyleDeclaration={} protected wrap(p:HTMLElement){ var d=document.createElement("div"); copyProps(this.wrapStyle,d.style); p.appendChild(d); return d; } } export class VerticalFlex extends Composite{ constructor(){ super("div") this._style.display="flex"; this._style.flexDirection="column" } wrapStyle: CSSStyleDeclaration={} protected wrap(p:HTMLElement){ var d=document.createElement("div"); copyProps(this.wrapStyle,d.style); p.appendChild(d); return d; } } var globalId=0; function nextId(){ return "el"+(globalId++); } export class Loading extends AbstractComposite{ protected innerRender(e:Element){ e.innerHTML=`
Loading...
` } } export class Label extends AbstractComposite{ constructor(title?:string,private content?:string){ super(); this.setTitle(title); } protected innerRender(e:Element){ if (this.content){ e.innerHTML = `${this.content}` } else { e.innerHTML = `${this.title()}` } } } export class Accordition extends AbstractComposite{ public expand(c:IControl){ var index=this.children.indexOf(c); this.expandIndex(index); } protected selectedIndex:number; getSelectedIndex(){ return this.selectedIndex; } getSelectedTitle(){ if (this.selectedIndex!=undefined){ return this.children[this.selectedIndex].title(); } } getSelectedTitleId(){ if (this.selectedIndex!=undefined){ var c=this.children[this.selectedIndex]; return c.controlId?c.controlId:c.title(); } } public expandIndex(index: number){ var bids=this.bids; var gids=this.gids; this.selectedIndex=index; for (var j=0;j
`; templates.push(s); } var content=`
${templates.join('')}
` e.innerHTML=content; for (var i=0;i{ var panelId=bids[i]; var containerId=gids[i] var k=i; if (this.children[i].contextActions){ var tH=document.getElementById("T"+x); new ToolbarRenderer({items: this.children[i].contextActions}).render(tH) } document.getElementById(x).onclick=()=> { if (!this.disabled[x]) { this.expandIndex(k); } } i++; }); } }