-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
145 lines (115 loc) · 4.54 KB
/
script.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
function openTab(tabName) {
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace("active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
selectedTab = document.getElementById(tabName)
selectedTab.style.display = "block";
}
function changeFont() {
const fonts = document.getElementById("fontList");
const selectedFont = fonts.options[fonts.selectedIndex].text;
document.getElementById("home").style.fontFamily = selectedFont;
document.getElementById("fontList").style.fontFamily = selectedFont;
}
function setFont(fontName) {
document.getElementById("fontList").value = fontName;
document.getElementById("home").style.fontFamily = fontName;
document.getElementById("fontList").style.fontFamily = fontName;
}
function changeBackground() {
const colors = document.getElementById("colorList");
const selectedColor = colors.options[colors.selectedIndex].text;
let bg;
if (selectedColor === "Blue")
bg = "#DDEBF8"
else if (selectedColor === "Green")
bg = "#E0ECEA"
else if (selectedColor === "Violet")
bg = "#E6E4F6"
else if (selectedColor === "Grey") bg = "#EBECED"
else bg = "#fff"
document.getElementById("home").style.background = bg;
return bg;
}
function setBackground(color) {
document.getElementById("colorList").value = color;
document.getElementById("home").style.background = color;
}
function addNewGroup() {
let i = 0;
const original = document.getElementById("group");
const clone = original.cloneNode(true);
clone.id = "group" + ++i;
original.parentNode.appendChild(clone);
}
function saveGroupFields() {
var options = document.getElementById('multipleSelect').options,
result = [];
for (var i = 0, len = options.length; i < len; i++) {
var opt = options[i];
if (opt.selected) {
result.push(opt.value);
}
}
alert(result);
console.log("saveGroupFields")
}
const columnsToMap = ['Title', 'Subtitle', 'Image', 'Text1', 'Text2']
const htmlReferences = ['title', 'subtitle', 'image', 'text1', 'text2']
grist.ready({
columns: columnsToMap,
// Register configuration handler to show configuration panel.
onEditOptions() {
document.getElementById("tab").style.display = 'block';
},
// Inform about required access level.
requiredAccess: 'read table'
});
grist.onOptions(function (options, interaction) {
if (options) {
setFont(options.font)
setBackground(options.backgroundColor)
} else {
// No widget options were saved, fallback to default ones.
}
});
grist.onRecord(async (record, mappings) => {
console.log("record = " + JSON.stringify(record))
const mapped = grist.mapColumnNames(record);
console.log("mapped = " + JSON.stringify(mapped))
// First check if all columns were mapped by user
if (Object.keys(mapped).length !== 0) {
// Map columns and html elements
columnsToMap.forEach(function (item, index) {
document.getElementById(htmlReferences[index]).innerText = mapped[item];
});
//getting img
const tokenInfo = await grist.docApi.getAccessToken({readOnly: true});
const id = mapped.Image[0]; // only get the first attachment (there could be several)
const src = `${tokenInfo.baseUrl}/attachments/${id}/download?auth=${tokenInfo.token}`;
document.getElementById('image').setAttribute('src', src);
} else {
// Not all required columns were mapped.
console.error("Please map at least one column");
}
});
// Define handler for the Save button.
async function saveOptions() {
const fonts = document.getElementById("fontList");
const selectedFont = fonts.options[fonts.selectedIndex].text;
await grist.widgetApi.setOption('font', selectedFont);
const bgColor = changeBackground()
await grist.widgetApi.setOption('backgroundColor', bgColor);
// There is no need to update visible options, as Grist will send us a new message that will
// be handled by the onOptions handler.
showPanel('main');
}