-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFavaGitSync.js
More file actions
103 lines (95 loc) · 2.98 KB
/
Copy pathFavaGitSync.js
File metadata and controls
103 lines (95 loc) · 2.98 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
export default {
guessURL() {
const re =
/^(.*)\/(?:income_statement|balance_sheet|trial_balance|journal|query|holdings|commodities|documents|events|statstics|editor|import|options|help)\/?$/;
const url = window.location.href;
const found = url.match(re);
if (!found) {
return "/beancount";
}
return found[1];
},
guessedURL: "",
init() {
this.guessedURL = this.guessURL();
this.remoteAheadCount = 0;
this.syncStatus = "";
const spacer = document.querySelector("header > span.spacer");
const header = document.querySelector("header");
const syncButton = document.createElement("Button");
syncButton.id = "git-sync-button";
syncButton.textContent = "sync";
syncButton.style.fontWeight = "bold";
syncButton.style.cursor = "pointer";
var timeout;
syncButton.onclick = async () => {
clearTimeout(timeout);
fetch(this.guessedURL + "/extension/FavaGitSync/sync").then(
(response) => {
if (response.ok) {
this.syncStatus = "✅";
this.remoteAheadCount = "0";
this.setButtonText();
// syncButton.textContent = "sync ✅";
syncButton.style.backgroundColor = "";
timeout = setTimeout(() => {
// syncButton.textContent = "sync";
this.syncStatus = "";
this.setButtonText();
}, 2000);
syncButton.blur();
} else {
// syncButton.textContent = "sync fail";
this.syncStatus = "fail";
this.setButtonText();
syncButton.style.backgroundColor = "red";
syncButton.blur();
}
}
);
};
header.insertBefore(syncButton, spacer);
},
onPageLoad() {
const syncButton = document.getElementById("git-sync-button");
if (syncButton == null) {
return;
}
fetch(this.guessedURL + "/extension/FavaGitSync/status").then(
(response) => {
if (response.status == 250) {
response.text().then((aheadCount) => {
if (aheadCount != "") {
this.remoteAheadCount = aheadCount;
}
this.syncStatus = "🟠";
this.setButtonText();
});
} else if (response.status == 200) {
response.text().then((aheadCount) => {
if (aheadCount != "") {
this.remoteAheadCount = aheadCount;
}
this.setButtonText();
});
} else {
this.syncStatus = "?";
this.setButtonText();
}
}
);
},
setButtonText() {
const syncButton = document.getElementById("git-sync-button");
if (syncButton == null) {
return;
}
console.log("I AM SETTING BUTTON TEXT");
var text = `sync ${this.syncStatus}`;
if (this.remoteAheadCount != "" && this.remoteAheadCount != "0") {
text = `${text} [${this.remoteAheadCount}]`;
}
syncButton.textContent = text;
},
onExtensionPageLoad() {},
};