migrate JavaScript to TypeScript

This commit is contained in:
tdurieux
2021-08-11 18:18:45 +02:00
parent ee4a20286d
commit caeff49ab0
58 changed files with 6034 additions and 3096 deletions
+2 -18
View File
@@ -335,8 +335,8 @@
name="mode"
ng-model="options.mode"
>
<option value="stream" selected>Stream</option>
<option value="download">Download</option>
<option value="GitHubStream" selected>Stream</option>
<option value="GitHubDownload">Download</option>
</select>
<small class="form-text text-muted"
>How the repository will be anonymized. Stream mode will
@@ -367,22 +367,6 @@
https://anonymous.4open.science/w/{{repoId}}</small
>
</div>
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="loc"
name="loc"
ng-model="options.loc"
/>
<label class="form-check-label" for="page"
>Line of code</label
>
<small id="termsHelp" class="form-text text-muted"
>Display the number of line of code in the
repository</small
>
</div>
</div>
</div>
</div>
+19 -14
View File
@@ -73,6 +73,8 @@
/>
<label class="form-check-label" for="removed"> Removed </label>
</div>
<h5>Quota</h5>
{{quota.used | humanFileSize}}/{{quota.total| humanFileSize}}
</div>
<div class="col-md h-100 overflow-auto body">
<div class="row">
@@ -95,6 +97,9 @@
<th scope="col" class="text-center d-none d-xl-table-cell">
Anonymize date
</th>
<th scope="col" class="text-center d-none d-xl-table-cell">
Size
</th>
<th scope="col" class="text-center d-none d-xl-table-cell">
# Views
</th>
@@ -113,32 +118,30 @@
{{$index + 1}}
</th>
<td class="align-middle">
<a href="/r/{{repo.repoId}}"
>{{repo.repoId}}</a
>
<a href="/r/{{repo.repoId}}">{{repo.repoId}}</a>
</td>
<td
class="align-middle"
title="Commit: {{repo.commit}}"
title="Commit: {{repo.source.branch.commit}}"
data-toggle="tooltip"
data-placement="bottom"
>
<a
href="https://github.com/{{repo.fullName}}/commit/{{repo.commit}}"
>{{repo.fullName}}</a
href="https://github.com/{{repo.source.fullName}}/commit/{{repo.source.branch.commit}}"
>{{repo.source.fullName}}</a
>
</td>
<td
title="Commit: {{repo.commit}}"
title="Commit: {{repo.source.branch.commit}}"
data-toggle="tooltip"
data-placement="bottom"
class="align-middle d-none d-lg-table-cell"
>
{{repo.branch}}
{{repo.source.branch.name}}
</td>
<!-- <td>{{repo.commit.substring(0, 6)}}</td> -->
<td class="text-center align-middle d-none d-lg-table-cell">
{{repo.terms.length}}
{{repo.options.terms.length}}
</td>
<td
class="text-center align-middle"
@@ -162,7 +165,12 @@
<td class="text-center align-middle d-none d-xl-table-cell">
{{repo.anonymizeDate | date}}
</td>
<td class="text-center align-middle d-none d-xl-table-cell">{{repo.pageView}}</td>
<td class="text-center align-middle d-none d-xl-table-cell">
{{repo.size | humanFileSize}}
</td>
<td class="text-center align-middle d-none d-xl-table-cell">
{{repo.pageView}}
</td>
<td class="text-center align-middle d-none d-xl-table-cell">
{{repo.lastView | date}}
</td>
@@ -214,10 +222,7 @@
>
<i class="fa fa-remove" aria-hidden="true"></i> Remove
</a>
<a
class="dropdown-item"
href="/r/{{repo.repoId}}/"
>
<a class="dropdown-item" href="/r/{{repo.repoId}}/">
<i class="fa fa-eye" aria-hidden="true"></i> View Repo
</a>
<a
+12 -2
View File
@@ -4,14 +4,24 @@
<tree class="files" file="files"></tree>
</div>
<div class="col-md h-100 overflow-auto p-0 d-flex flex-column">
<nav aria-label="breadcrumb">
<nav aria-label="repository menu">
<ol class="breadcrumb shadow paths">
<li class="breadcrumb-item" ng-repeat="p in paths" ng-bind="p">
Loading...
</li>
</ol>
<a ng-href="{{url}}" target="__self" class="btn btn-primary"
>Download file</a
>
<a
ng-href="/api/repo/{{repoId}}/zip"
target="__self"
class="btn btn-primary"
>Download Repository</a
>
</nav>
<loc stats="stats" ng-if="stats"></loc>
<div class="align-items-stretch h-100 w-100 overflow-auto">
<ng-include src="'./partials/pageView.htm'"></ng-include>
</div>
+72 -30
View File
@@ -81,6 +81,30 @@ angular
$locationProvider.html5Mode(true);
})
.run(["Analytics", function (Analytics) {}])
.filter("humanFileSize", function () {
return function humanFileSize(bytes, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
bytes = bytes / 8;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = si
? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
: ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let u = -1;
const r = 10 ** dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + " " + units[u];
};
})
.filter("title", function () {
return function (str) {
if (!str) return str;
@@ -370,7 +394,7 @@ angular
notebook: true,
loc: true,
link: true,
mode: "download",
mode: "GitHubDownload",
};
function getDefault() {
@@ -500,6 +524,13 @@ angular
}
getRepositories();
function getQuota() {
$http.get("/api/user/quota").then((res) => {
$scope.quota = res.data;
}, console.error);
}
getQuota();
$scope.removeRepository = (repo) => {
if (
confirm(
@@ -596,9 +627,8 @@ angular
image: true,
pdf: true,
notebook: true,
loc: true,
link: true,
mode: "download",
mode: "GitHubDownload",
};
$scope.options.expirationDate.setDate(
$scope.options.expirationDate.getDate() + 90
@@ -630,10 +660,10 @@ angular
$scope.repoId = $routeParams.repoId;
$http.get("/api/repo/" + $scope.repoId).then(
async (res) => {
$scope.repoUrl = "https://github.com/" + res.data.fullName;
$scope.repoUrl = "https://github.com/" + res.data.source.fullName;
$scope.terms = res.data.terms.join("\n");
$scope.branch = res.data.branch;
$scope.terms = res.data.options.terms.join("\n");
$scope.branch = res.data.source.branch.name;
$scope.options = res.data.options;
$scope.conference = res.data.conference;
if (res.data.options.expirationDate) {
@@ -648,11 +678,11 @@ angular
}
$scope.details = (
await $http.get(`/api/repo/${res.data.fullName}/`)
await $http.get(`/api/repo/${res.data.source.fullName}/`)
).data;
await getReadme();
await $scope.getBranches();
await getReadme();
anonymize();
$scope.$apply();
},
@@ -709,24 +739,32 @@ angular
};
$('[data-toggle="tooltip"]').tooltip();
$scope.$watch("branch", (v) => {
if ($scope.branches && $scope.branches[$scope.branch]) {
$scope.commit = $scope.branches[$scope.branch].commit.sha;
}
if ($scope.details && $scope.details.has_page) {
$scope.anonymize.page.disabled(false);
$scope.$watch("branch", async (v) => {
const selected = $scope.branches.filter(
(f) => f.name == $scope.branch
)[0];
if ($scope.details && $scope.details.hasPage) {
$scope.anonymize.page.$$element[0].disabled = false;
if ($scope.details.pageSource.branch != $scope.branch) {
$scope.anonymize.page.disabled(true);
$scope.anonymize.page.$$element[0].disabled = true;
}
}
if (selected) {
$scope.commit = selected.commit;
$scope.readme = selected.readme;
await getReadme();
anonymize();
$scope.$apply();
}
});
$scope.$watch("options.mode", (v) => {
if (v == "stream") {
$scope.options.loc = false;
$scope.anonymize.loc.$$element[0].disabled = true;
if (v == "GitHubStream") {
$scope.options.page = false;
$scope.anonymize.page.$$element[0].disabled = true;
} else {
$scope.anonymize.loc.$$element[0].disabled = false;
$scope.anonymize.page.$$element[0].disabled = false;
}
});
@@ -749,10 +787,12 @@ angular
);
$scope.branches = branches.data;
if (!$scope.branch) {
$scope.branch = $scope.details.default_branch;
$scope.branch = $scope.details.defaultBranch;
}
if ($scope.branches[$scope.branch]) {
$scope.commit = $scope.branches[$scope.branch].commit.sha;
const selected = $scope.branches.filter((b) => b.name == $scope.branch);
if (selected.length > 0) {
$scope.commit = selected[0].commit;
$scope.readme = selected[0].readme;
}
$scope.$apply();
};
@@ -771,12 +811,10 @@ angular
const res = await $http.get(`/api/repo/${o.owner}/${o.repo}/`);
$scope.details = res.data;
if ($scope.details.size > 1024 * 8) {
$scope.options.mode = "stream";
$scope.options.loc = false;
$scope.options.mode = "GitHubStream";
$scope.anonymize.mode.$$element[0].disabled = true;
$scope.anonymize.loc.$$element[0].disabled = true;
}
$scope.repoId = $scope.details.name + "-" + generateRandomId(4);
$scope.repoId = $scope.details.repo + "-" + generateRandomId(4);
await $scope.getBranches();
} catch (error) {
if (error.data) {
@@ -790,13 +828,16 @@ angular
}
}
async function getReadme() {
async function getReadme(force) {
if ($scope.readme) return $scope.readme;
const o = parseGithubUrl($scope.repoUrl);
const res = await $http.get(`/api/repo/${o.owner}/${o.repo}/readme`);
const res = await $http.get(`/api/repo/${o.owner}/${o.repo}/readme`, {
params: { force: force === true ? "1" : "0", branch: $scope.branch },
});
$scope.readme = res.data;
}
async function anonymize() {
function anonymize() {
const urlRegex =
/<?\b((https?|ftp|file):\/\/)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]\b\/?>?/g;
let content = $scope.readme;
@@ -890,6 +931,7 @@ angular
function getRepo() {
const o = parseGithubUrl($scope.repoUrl);
$scope.options.pageSource = $scope.details.pageSource;
return {
repoId: $scope.repoId,
terms: $scope.terms.trim().split("\n"),
@@ -1200,7 +1242,7 @@ angular
getFiles(() => {
updateContent();
if (options.mode == "download") {
if (options.mode == "GitHubDownload") {
getStats();
}
});