diff --git a/src/injectors/gitlab-injector.ts b/src/injectors/gitlab-injector.ts index ab9bfa5..d399828 100644 --- a/src/injectors/gitlab-injector.ts +++ b/src/injectors/gitlab-injector.ts @@ -2,13 +2,18 @@ import * as domloaded from 'dom-loaded'; import * as select from 'select-dom'; import { ConfigProvider } from '../config'; import { ButtonInjector, InjectorBase, checkIsBtnUpToDate, rewritePeriodKeybindGitLab } from './injector'; -import { renderGitpodUrl, makeOpenInPopup, UrlInfo } from '../utils'; +import { renderGitpodUrl, makeOpenInPopup, UrlInfo, renderGitpodCustomEditor, createElementFromHTML, ideOptions } from '../utils'; +import { IDEOptions } from '@gitpod/gitpod-protocol/lib/ide-protocol'; namespace Gitpodify { export const BTN_ID = "gitpod-btn-nav"; export const BTN_CLASS = "gitpod-nav-btn"; } + +const DropdownID = "gitpod-dropdown" +const DropdownTriggerID = "gitpod-dropdown-trigger" + export class GitlabInjector extends InjectorBase { constructor(protected readonly configProvider: ConfigProvider) { @@ -49,6 +54,7 @@ export class GitlabInjector extends InjectorBase { class RepositoryInjector implements ButtonInjector { static readonly PARENT_SELECTOR = ".tree-controls"; + protected showDropdown: boolean = false; isApplicableToCurrentPage(): boolean { const result = !!select.exists(RepositoryInjector.PARENT_SELECTOR) @@ -71,8 +77,9 @@ class RepositoryInjector implements ButtonInjector { return; } - const btn = this.renderButton(currentUrl, openAsPopup); + const btn = this.renderButton(urlInfo, openAsPopup, useLatest); parent.firstElementChild.appendChild(btn); + this.bindDropdown() const primaryButtons = parent.firstElementChild.getElementsByClassName("btn-primary"); if (primaryButtons && primaryButtons.length > 1) { @@ -85,27 +92,103 @@ class RepositoryInjector implements ButtonInjector { } } - protected renderButton(url: string, openAsPopup: boolean): HTMLElement { - const container = document.createElement('div'); - container.className = "project-clone-holder d-none d-md-inline-block"; + genOptionsString(ideOptions: IDEOptions, urlInfo: UrlInfo, useLatest: boolean) { + if (!ideOptions.clients) { + return [] + } + return Object.entries(ideOptions.options ?? {}).map(([ide, ideOption]) => { + const url = renderGitpodCustomEditor(urlInfo.host, urlInfo.originUrl, ide, useLatest); + const title = ideOption.title + (ideOption.type === "desktop" ? "" : (" - " + ideOption.type.toUpperCase())); + const logo = ideOption.logo + return `` + }) + } - const container2ndLevel = document.createElement('div'); - container2ndLevel.className = "git-clone-holder js-git-clone-holder"; + bindDropdown() { + const element = document.querySelector('#' + DropdownTriggerID) as HTMLButtonElement + if (element == null) { + return + } + element.onblur = () => { this.triggerDropdown() } + element.onfocus = () => { this.triggerDropdown() } + } - const a = document.createElement('a'); - a.id = Gitpodify.BTN_ID; - a.title = "Gitpod"; - a.text = "Gitpod" - a.href = url; - a.target = "_blank"; - a.className = "gl-button btn btn-info"; + triggerDropdown() { + const element = document.querySelector('#' + DropdownID) + const show = !element?.classList.contains("show") + if (show) { + element?.classList.add("show") + } else { + element?.classList.remove("show") + } + this.showDropdown = show + } + + protected renderButton(urlInfo: UrlInfo, openAsPopup: boolean, useLatest: boolean) { + const element = createElementFromHTML(` + `) if (openAsPopup) { - makeOpenInPopup(a); + element.querySelectorAll('a').forEach(e => { + makeOpenInPopup(e); + }); } - - container2ndLevel.appendChild(a); - container.appendChild(container2ndLevel); - return container; + return element } + + // protected renderButton(url: string, openAsPopup: boolean): HTMLElement { + // const container = document.createElement('div'); + // container.className = "project-clone-holder d-none d-md-inline-block"; + + // const container2ndLevel = document.createElement('div'); + // container2ndLevel.className = "git-clone-holder js-git-clone-holder"; + + // const a = document.createElement('a'); + // a.id = Gitpodify.BTN_ID; + // a.title = "Gitpod"; + // a.text = "Gitpod" + // a.href = url; + // a.target = "_blank"; + // a.className = "gl-button btn btn-info"; + + // if (openAsPopup) { + // makeOpenInPopup(a); + // } + + // container2ndLevel.appendChild(a); + // container.appendChild(container2ndLevel); + // return container; + // } } \ No newline at end of file