github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/deck/static/common/common.ts (about)

     1  import {JobState} from "../api/prow";
     2  import moment from "moment";
     3  
     4  // The cell namespace exposes functions for constructing common table cells.
     5  export namespace cell {
     6  
     7  	export function text(text: string): HTMLTableDataCellElement {
     8  		const c = document.createElement("td");
     9  		c.appendChild(document.createTextNode(text));
    10  		return c;
    11  	};
    12  
    13  	export function time(id: string, time: moment.Moment): HTMLTableDataCellElement {
    14  		const tid = "time-cell-" + id;
    15  		const main = document.createElement("div");
    16  		const isADayOld = time.isBefore(moment().startOf('day'));
    17  		main.textContent = time.format(isADayOld ? 'MMM DD HH:mm:ss' : 'HH:mm:ss');
    18  		main.id = tid;
    19  
    20  		const tooltip = document.createElement("div");
    21  		tooltip.textContent = time.format('MMM DD YYYY, HH:mm:ss [UTC]ZZ');
    22  		tooltip.setAttribute("data-mdl-for", tid);
    23  		tooltip.classList.add("mdl-tooltip", "mdl-tooltip--large");
    24  
    25  		const c = document.createElement("td");
    26  		c.appendChild(main);
    27  		c.appendChild(tooltip);
    28  
    29  		return c;
    30  	};
    31  
    32  	export function link(text: string, url: string): HTMLTableDataCellElement {
    33  		const c = document.createElement("td");
    34  		const a = document.createElement("a");
    35  		a.href = url;
    36  		a.appendChild(document.createTextNode(text));
    37  		c.appendChild(a);
    38  		return c;
    39  	};
    40  
    41  	export function state(state: JobState): HTMLTableDataCellElement {
    42  		const c = document.createElement("td");
    43  		if (!state) {
    44  			c.appendChild(document.createTextNode(""));
    45  			return c;
    46  		}
    47  		c.classList.add("icon-cell");
    48  
    49  		let displayState = stateToAdj(state);
    50  		displayState = displayState[0].toUpperCase() + displayState.slice(1);
    51  		let displayIcon = "";
    52  		switch (state) {
    53  			case "triggered":
    54  				displayIcon = "schedule";
    55  				break;
    56  			case "pending":
    57  				displayIcon = "watch_later";
    58  				break;
    59  			case "success":
    60  				displayIcon = "check_circle";
    61  				break;
    62  			case "failure":
    63  				displayIcon = "error";
    64  				break;
    65  			case "aborted":
    66  				displayIcon = "remove_circle";
    67  				break;
    68  			case "error":
    69  				displayIcon = "warning";
    70  				break;
    71  		}
    72  		const stateIndicator = document.createElement("i");
    73  		stateIndicator.classList.add("material-icons", "state", state);
    74  		stateIndicator.innerText = displayIcon;
    75  		c.appendChild(stateIndicator);
    76  		c.title = displayState;
    77  
    78  		return c;
    79  	};
    80  
    81  	function stateToAdj(state: JobState): string {
    82  		switch (state) {
    83  			case "success":
    84  				return "succeeded";
    85  			case "failure":
    86  				return "failed";
    87  			default:
    88  				return state;
    89  		}
    90  	};
    91  
    92  	export function commitRevision(repo: string, ref: string, SHA: string): HTMLTableDataCellElement {
    93  		const c = document.createElement("td");
    94  		const bl = document.createElement("a");
    95  		bl.href = "https://github.com/" + repo + "/commit/" + SHA;
    96  		bl.text = ref + " (" + SHA.slice(0, 7) + ")";
    97  		c.appendChild(bl);
    98  		return c;
    99  	}
   100  
   101  	export function prRevision(repo: string, num: number, author: string, title: string, SHA: string): HTMLTableDataCellElement {
   102  		const td = document.createElement("td");
   103  		addPRRevision(td, repo, num, author, title, SHA);
   104  		return td;
   105  	}
   106  
   107  	let idCounter = 0;
   108  	function nextID(): String {
   109  	  idCounter++;
   110  	  return "tipID-" + String(idCounter);
   111  	};
   112  
   113  	export function addPRRevision(elem: Node, repo: string, num: number, author: string, title: string, SHA: string): void {
   114  		elem.appendChild(document.createTextNode("#"));
   115  		const pl = document.createElement("a");
   116  		pl.href = "https://github.com/" + repo + "/pull/" + num;
   117  		pl.text = num.toString();
   118  		if (title) {
   119  			pl.id = "pr-" + repo + "-" + num + "-" + nextID();
   120  			const tip = tooltip.forElem(pl.id, document.createTextNode(title));
   121  			pl.appendChild(tip);
   122  		}
   123  		elem.appendChild(pl);
   124  		if (SHA) {
   125  			elem.appendChild(document.createTextNode(" ("));
   126  			const cl = document.createElement("a");
   127  			cl.href = "https://github.com/" + repo + "/pull/" + num
   128  					+ '/commits/' + SHA;
   129  			cl.text = SHA.slice(0, 7);
   130  			elem.appendChild(cl);
   131  			elem.appendChild(document.createTextNode(")"));
   132  		}
   133  		if (author) {
   134  			elem.appendChild(document.createTextNode(" by "))
   135  			const al = document.createElement("a");
   136  			al.href = "https://github.com/" + author;
   137  			al.text = author;
   138  			elem.appendChild(al);
   139  		}
   140  	}
   141  }
   142  
   143  export namespace tooltip {
   144  	export function forElem(elemID: string, tipElem: Node): Node {
   145  		const tooltip = document.createElement("div");
   146  		tooltip.appendChild(tipElem);
   147  		tooltip.setAttribute("data-mdl-for", elemID);
   148  		tooltip.classList.add("mdl-tooltip", "mdl-tooltip--large");
   149  		return tooltip;
   150  	}
   151  }