-
Notifications
You must be signed in to change notification settings - Fork 0
/
tje.js
179 lines (151 loc) · 7.33 KB
/
tje.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* ******************************** */
/* Custom tje functions and loading */
/* NOTE TYPE SHOULD BE JS Frontend. */
/* ******************************** */
var currSpreadsheet = undefined;
var jsonNotes = undefined;
const jsonNotesData = document.getElementById("tjeNotes");
const sheetDiv = document.getElementById("tje_container");
const sheet = document.getElementsByClassName("x-spreadsheet-sheet");
const autosaveCheckbox = document.getElementById("tjeAutosaveCheckbox");
const noteName = document.getElementById("tjeNoteName");
async function init() {
var saveIcon = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNTc3MTc3MDkyOTg4IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjI2NzgiIHdpZHRoPSIxOCIgaGVpZ2h0PSIxOCIgeG1sbn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NyAwaDg1LjMzMzMzMy04NS4zMzMzMzN6IG0wIDU5Ny4zMzMzMzR2LTEyOEgzNDEuMzMzMzMzdjEyOEgyNTZ2LTE3MC42NjY2NjdhNDIuNjY2NjY3IDQyLjY2NjY2NyAwIDAgMSA0Mi42NjY2NjctNDIuNjY2NjY3aDQyNi42NjY2NjZhNDIuNjY2NjY3IDQyLjY2NjY2NyAwIDAgMSA0Mi42NjY2NjcgNDIuNjY2NjY3djE3MC42NjY2NjdoLTg1LjMzMzMzM3ogbTg1LjMzMzMzMyAwaC04NS4zMzMzMzMgODUuMzMzMzMzek0zNDEuMzMzMzMzIDgxMC42NjY2NjdIMjU2aDg1LjMzMzMzM3oiIHAtaWQ9IjI2NzkiIGZpbGw9IiMyYzJjMmMiPjwvcGF0aD48L3N2Zz4=';
currSpreadsheet = x_spreadsheet('#tje_container', { showTopBar: true, showBottomBar: true,extendToolbar: {left: [{ tip: 'Save current spreadsheet', icon: saveIcon, onClick: onSaveContents}]}});
jsonNotes = await api.runOnBackend(getJSONNotes);
addJSONNotesToList(jsonNotes);
jsonNotesData.addEventListener("change", onJSONNoteSelect);
autosaveCheckbox.addEventListener("change", onAutosaveChange);
showSpreadsheet(false);
}
init();
/* *************** */
/* UTILITY METHODS */
/* *************** */
function onSheetChange(event) {
if( autosaveCheckbox.checked) {
onSaveContents();
}
}
function onAutosaveChange(event) {
}
/* ************************************* */
/* Get all notes that are of type 'json' */
/* ************************************* */
function getJSONNotes() {
const jsonNotes = api.searchForNotes("note.type = code AND note.mime = 'application/json' AND #!tjeExclude");
return jsonNotes.map(n => ({
id: n.noteId,
title: n.title,
}));
}
/* ************************** */
/* Get contents of note by id */
/* ************************** */
async function getNoteContent(noteId) {
var jsonData = await api.runOnBackend((id) => api.getNote(id).getContent(), [noteId]);
if (jsonData == "") {
jsonData = "[{\"name\":\"sheet1\"}]";
}
return JSON.parse(jsonData);
}
/* ************************** */
/* Get path of note by id */
/* ************************** */
function getNoteLink(noteId) {
var notePath = api.runOnBackend((id) => api.getNote(id).getBestNotePath(), [noteId]);
return notePath;
}
/* ************************** */
/* Set contents of note by id */
/* ************************** */
function setNoteContent(noteId, content) {
var content = JSON.stringify(currSpreadsheet.getData());
api.runOnBackend((noteId, content) => api.getNote(noteId).setContent(content), [noteId, content]);
}
/* **************************** */
/* When a json note is selected */
/* **************************** */
function onJSONNoteSelect(e) {
const noteId = jsonNotesData.value;
resetTable();
if (noteId != "") {
loadContents(noteId);
}
}
/* ********************** */
/* When save is requested */
/* ********************** */
function onSaveContents(event) {
const showMessage = (event != undefined);
var content = JSON.stringify(currSpreadsheet.getData(), null, 4);
const noteId = jsonNotesData.value;
if (noteId) {
setNoteContent(noteId, content);
if (showMessage) { api.showMessage("Data saved."); }
console.log("Data saved.");
} else {
if (showMessage) { api.showWarning("Note not found. Data NOT saved."); }
console.log("Note not found. Data NOT saved.");
}
}
/* ************************************* */
/* Add json notes to the select control */
/* ************************************* */
function addJSONNotesToList(jsonObjs) {
resetJSONNotesList(jsonNotesData);
if (jsonObjs.length > 0) {
//addOptionToSelect(jsonNotesData, "Select a note...", "");
$(jsonObjs).each(function(){ addOptionToSelect(jsonNotesData, this.title, this.id); });
onJSONNoteSelect();
} else
{
addOptionToSelect(jsonNotesData, "No JSON Notes Found...", "");
}
}
/* ******************************************** */
/* Utility to add an option to the Notes Select */
/* ******************************************** */
function addOptionToSelect(selectElem, text, value) {
var elem = document.createElement("option");
elem.innerText = text;
elem.value = value;
selectElem.appendChild(elem);
}
/* ************************************** */
/* Zero out json note list and add prompt */
/* ************************************** */
function resetJSONNotesList(list) {
while (list.options.length > 0) {
list.options[0].remove();
}
}
/* *********************************************** */
/* Create table with contents from provided noteId */
/* *********************************************** */
async function loadContents(noteId) {
var jsonString = await getNoteContent(noteId);
var link = await getNoteLink(noteId);
link = "#" + link.join();
link = link.replaceAll(",", "/");
tjeNoteName.innerHTML = "<a href='" + link + "'>" + jsonNotesData.options[jsonNotesData.selectedIndex].text + "</a>";
currSpreadsheet.loadData(jsonString);
showSpreadsheet(true);
}
/* ******************** */
/* Clear existing table */
/* ******************** */
function resetTable() {
var newSheet = JSON.parse('[{"name":"New"}]');
currSpreadsheet.loadData(newSheet);
showSpreadsheet(false);
}
function showSpreadsheet(show) {
if (show) {
sheetDiv.style.display = "block";
sheet[0].addEventListener("keydown", onSheetChange);
} else {
sheetDiv.style.display = "none";
sheet[0].removeEventListener("keydown", onSheetChange);
}
}