content/static: rewrite unit.js with typescript

For golang/go#43359

Change-Id: I4256144908205463a70a6bd1b6c8b5b751ec2cc6
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/299260
Trust: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
This commit is contained in:
Jamal Carvalho 2021-03-05 16:14:56 -05:00
Родитель c8014eb244
Коммит 9a9d5ef0fc
3 изменённых файлов: 92 добавлений и 22 удалений

Просмотреть файл

@ -1,31 +1,20 @@
/**
/*!
* @license
* Copyright 2020 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
import { CopyToClipboardController } from './clipboard.js';
import './toggle-tip.js';
import { ExpandableRowsTableController } from '/static/js/table.js';
import { ExpandableRowsTableController } from './table.js';
document
.querySelectorAll('.js-expandableTable')
.forEach(
el => new ExpandableRowsTableController(el, document.querySelector('.js-expandAllDirectories'))
);
/**
* Instantiates CopyToClipboardController controller copy buttons
* on the unit page.
*/
document.querySelectorAll('.js-copyToClipboard').forEach(el => {
new CopyToClipboardController(el);
});
/**
* Event handlers for expanding and collapsing the readme section.
*/
const readme = document.querySelector('.js-readme');
const readmeContent = document.querySelector('.js-readmeContent');
const readmeOutline = document.querySelector('.js-readmeOutline');
@ -49,13 +38,13 @@ if (readme && readmeContent && readmeOutline && readmeExpand.length && readmeCol
readmeExpand[1].scrollIntoView({ block: 'center' });
}
});
readmeContent.addEventListener('keyup', e => {
readmeContent.addEventListener('keyup', () => {
readme.classList.add('UnitReadme--expanded');
});
readmeContent.addEventListener('click', e => {
readmeContent.addEventListener('click', () => {
readme.classList.add('UnitReadme--expanded');
});
readmeOutline.addEventListener('click', e => {
readmeOutline.addEventListener('click', () => {
readme.classList.add('UnitReadme--expanded');
});
document.addEventListener('keydown', e => {
@ -64,16 +53,13 @@ if (readme && readmeContent && readmeOutline && readmeExpand.length && readmeCol
}
});
}
/**
* Disable unavailable sections in navigation dropdown on mobile.
*/
const readmeOption = document.querySelector('.js-readmeOption');
if (readmeOption && !readme) {
readmeOption.setAttribute('disabled', true);
readmeOption.setAttribute('disabled', 'true');
}
const unitDirectories = document.querySelector('.js-unitDirectories');
const directoriesOption = document.querySelector('.js-directoriesOption');
if (!unitDirectories && directoriesOption) {
directoriesOption.setAttribute('disabled', true);
directoriesOption.setAttribute('disabled', 'true');
}
//# sourceMappingURL=unit.js.map

Просмотреть файл

@ -0,0 +1 @@
{"version":3,"file":"unit.js","sourceRoot":"","sources":["unit.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,iBAAiB,CAAC;AACzB,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAE3D,QAAQ;KACL,gBAAgB,CAAmB,qBAAqB,CAAC;KACzD,OAAO,CACN,EAAE,CAAC,EAAE,CACH,IAAI,6BAA6B,CAC/B,EAAE,EACF,QAAQ,CAAC,aAAa,CAAoB,0BAA0B,CAAC,CACtE,CACJ,CAAC;AAMJ,QAAQ,CAAC,gBAAgB,CAAoB,qBAAqB,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;IAC/E,IAAI,yBAAyB,CAAC,EAAE,CAAC,CAAC;AACpC,CAAC,CAAC,CAAC;AAKH,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;AACpD,MAAM,aAAa,GAAG,QAAQ,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;AAClE,MAAM,aAAa,GAAG,QAAQ,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;AAClE,MAAM,YAAY,GAAG,QAAQ,CAAC,gBAAgB,CAAC,kBAAkB,CAAC,CAAC;AACnE,MAAM,cAAc,GAAG,QAAQ,CAAC,aAAa,CAAC,oBAAoB,CAAC,CAAC;AACpE,IAAI,MAAM,IAAI,aAAa,IAAI,aAAa,IAAI,YAAY,CAAC,MAAM,IAAI,cA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S,EAAE,CAAC,CAAC,EAAE;QACvC,IAAI,CAAC,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,EAAE;YAC7C,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;SAC9C;IACH,CAAC,CAAC,CAAC;CACJ;AAKD,MAAM,YAAY,GAAG,QAAQ,CAAC,aAAa,CAAC,kBAAkB,CAAC,CAAC;AAChE,IAAI,YAAY,IAAI,CAAC,MAAM,EAAE;IAC3B,YAAY,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;CAC/C;AACD,MAAM,eAAe,GAAG,QAAQ,CAAC,aAAa,CAAC,qBAAqB,CAAC,CAAC;AACtE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,aAAa,CAAC,uBAAuB,CAAC,CAAC;AAC1E,IAAI,CAAC,eAAe,IAAI,iBAAiB,EAAE;IACzC,iBAAiB,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;CACpD"}

83
content/static/js/unit.ts Normal file
Просмотреть файл

@ -0,0 +1,83 @@
/*!
* @license
* Copyright 2021 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
import { CopyToClipboardController } from './clipboard.js';
import './toggle-tip.js';
import { ExpandableRowsTableController } from './table.js';
document
.querySelectorAll<HTMLTableElement>('.js-expandableTable')
.forEach(
el =>
new ExpandableRowsTableController(
el,
document.querySelector<HTMLButtonElement>('.js-expandAllDirectories')
)
);
/**
* Instantiates CopyToClipboardController controller copy buttons
* on the unit page.
*/
document.querySelectorAll<HTMLButtonElement>('.js-copyToClipboard').forEach(el => {
new CopyToClipboardController(el);
});
/**
* Event handlers for expanding and collapsing the readme section.
*/
const readme = document.querySelector('.js-readme');
const readmeContent = document.querySelector('.js-readmeContent');
const readmeOutline = document.querySelector('.js-readmeOutline');
const readmeExpand = document.querySelectorAll('.js-readmeExpand');
const readmeCollapse = document.querySelector('.js-readmeCollapse');
if (readme && readmeContent && readmeOutline && readmeExpand.length && readmeCollapse) {
if (window.location.hash.includes('readme')) {
readme.classList.add('UnitReadme--expanded');
}
readmeExpand.forEach(el =>
el.addEventListener('click', e => {
e.preventDefault();
readme.classList.add('UnitReadme--expanded');
readme.scrollIntoView();
})
);
readmeCollapse.addEventListener('click', e => {
e.preventDefault();
readme.classList.remove('UnitReadme--expanded');
if (readmeExpand[1]) {
readmeExpand[1].scrollIntoView({ block: 'center' });
}
});
readmeContent.addEventListener('keyup', () => {
readme.classList.add('UnitReadme--expanded');
});
readmeContent.addEventListener('click', () => {
readme.classList.add('UnitReadme--expanded');
});
readmeOutline.addEventListener('click', () => {
readme.classList.add('UnitReadme--expanded');
});
document.addEventListener('keydown', e => {
if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
readme.classList.add('UnitReadme--expanded');
}
});
}
/**
* Disable unavailable sections in navigation dropdown on mobile.
*/
const readmeOption = document.querySelector('.js-readmeOption');
if (readmeOption && !readme) {
readmeOption.setAttribute('disabled', 'true');
}
const unitDirectories = document.querySelector('.js-unitDirectories');
const directoriesOption = document.querySelector('.js-directoriesOption');
if (!unitDirectories && directoriesOption) {
directoriesOption.setAttribute('disabled', 'true');
}