aboutsummaryrefslogtreecommitdiff
path: root/app/static/package_create.js
blob: 25cdd3e25b785ed0f9a87329c9b850d6d88ae4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
$(function() {
	function finish() {
		$(".pkg_wiz_1").hide()
		$(".pkg_wiz_2").hide()
		$(".pkg_repo").show()
		$(".pkg_meta").show()
	}

	function getJSON(url) {
		return new Promise(function(resolve, reject) {
			fetch(url).then(function(response) {
				response.text().then(function(txt) {
					resolve(JSON.parse(txt))
				}).catch(reject)
			}).catch(reject)
		})
	}

	function performTask(url) {
		return new Promise(function(resolve, reject) {
			getJSON(url).then(function(startResult) {
				console.log(startResult)
				if (typeof startResult.poll_url == "string") {
					var tries = 0;
					function retry() {
						tries++;
						if (tries > 10) {
							reject("timeout")
						} else {
							console.log("Polling task in " + (tries*100) + "ms")
							setTimeout(step, tries*100)
						}
					}
					function step() {
						getJSON(startResult.poll_url).then(function(res) {
							if (res.status == "SUCCESS") {
								console.log("Got result")
								resolve(res.result)
							} else {
								retry()
							}
						}).catch(retry)
					}
					retry()
				} else {
					reject("Start task didn't return string!")
				}
			}).catch(reject)
		})
	}

	function repoIsSupported(url) {
		try {
			return URI(url).hostname() == "github.com"
		} catch(e) {
			return false
		}
	}

	$(".pkg_meta").hide()
	$(".pkg_wiz_1").show()
	$("#pkg_wiz_1_next").click(function() {
		const repoURL = $("#repo").val();
		if (repoIsSupported(repoURL)) {
			$(".pkg_wiz_1").hide()
			$(".pkg_wiz_2").show()
			$(".pkg_repo").hide()

			performTask("/tasks/getmeta/new/?url=" + encodeURI(repoURL)).then(function(result) {
				console.log(result)
				$("#name").val(result.name)
				const desc = result.description || ""
				if (desc.length > 0) {
					const idx = desc.indexOf(".")
					$("#shortDesc").val((idx < 5 || idx > 100) ? desc.substring(0, Math.min(desc.length, 100)) : desc.substring(0, idx))
					$("#desc").val(desc)
				}
				finish()
			}).catch(function(e) {
				alert(e)
				$(".pkg_wiz_1").show()
				$(".pkg_wiz_2").hide()
				$(".pkg_repo").show()
				// finish()
			})
		} else {
			finish()
		}
	})
})