-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.gs
317 lines (257 loc) · 8.69 KB
/
config.gs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* @fileoverview Code related to configuration of the Odo Add-on.
*/
/**
* Card builder that's called when user selects the Univeral action "Configure"
*
* @return {CardService.Card} Card to show for configuration interface.
*/
function onOdoConfig() {
let wasConfigured = true;
let config = getConfig();
if (!config.saved) {
wasConfigured = false;
}
// clear out any prior selections from the "Customize Integrations" card
let up = PropertiesService.getUserProperties();
up.deleteProperty(PROP_STRINGIFIED_INTEGRATION_CUSTOMIZATIONS);
let card = CardService.newCardBuilder();
let header = CardService.newCardHeader();
header
.setTitle('Configure Odo')
.setSubtitle('Configure your demo Add-on')
.setImageStyle(CardService.ImageStyle.SQUARE)
.setImageUrl(ODO_ICON);
card.setHeader(header);
card.setName('mainConfigurationCard');
//// General Config Section ////
let appearanceSection = CardService.newCardSection();
appearanceSection.setHeader('General');
let customerNameWidget = CardService.newTextInput()
.setFieldName('customerName')
.setValue(config.customerName)
.setTitle('Customer Name');
appearanceSection.addWidget(customerNameWidget);
let customerLogoWidget = CardService.newTextInput()
.setFieldName('customerLogoUrl')
.setValue(config.customerLogoUrl)
.setTitle('Customer Logo URL');
appearanceSection.addWidget(customerLogoWidget);
let toolNameWidget = CardService.newTextInput()
.setFieldName('toolName')
.setValue(config.toolName)
.setTitle('Tool Name');
appearanceSection.addWidget(toolNameWidget);
let welcomeMessageWidget = CardService.newTextInput()
.setFieldName('welcomeSplashMessage')
.setValue(config.welcomeSplashMessage)
.setTitle('Welcome Splash Card Message');
appearanceSection.addWidget(welcomeMessageWidget);
let dateFormatWidget = CardService.newSelectionInput()
.setFieldName('dateFormat')
.setType(CardService.SelectionInputType.DROPDOWN)
.setTitle('Date Format');
let supportedDateFormats = [
'MM/dd/yyyy',
'dd MMM, yyyy',
'dd-MM-yyyy',
'MM-dd-yyyy',
'dd/MM/yyyy',
'MMM dd, yyyy',
];
for (let i = 0; i < supportedDateFormats.length; i++) {
let selected = supportedDateFormats[i] === config.dateFormat;
dateFormatWidget.addItem(
supportedDateFormats[i],
supportedDateFormats[i],
selected
);
}
appearanceSection.addWidget(dateFormatWidget);
appearanceSection.addWidget(
CardService.newTextParagraph().setText('<br><br>')
);
card.addSection(appearanceSection);
//////////////////////
//// Integration Section ////
let integrationSection = CardService.newCardSection();
integrationSection.setHeader('Simulated Integration');
let integrationTypeWidget = CardService.newSelectionInput()
.setFieldName('integrationType')
.setType(CardService.SelectionInputType.DROPDOWN)
.setTitle('Integration Type');
for (integrationType in INTEGRATION_HOOKS) {
if (!INTEGRATION_HOOKS.hasOwnProperty(integrationType)) {
continue;
}
let hooks = INTEGRATION_HOOKS[integrationType];
let selected = integrationType === config.integrationType;
integrationTypeWidget.addItem(
integrationTypeToPrintableString(integrationType),
integrationType,
selected
);
}
integrationSection.addWidget(integrationTypeWidget);
let customizeIntegrationAction = CardService.newAction();
customizeIntegrationAction.setFunctionName(
'onUserSelectedCustomizeIntegration'
);
integrationSection.addWidget(
CardService.newTextButton()
.setText('Customize Integration →')
.setOnClickAction(customizeIntegrationAction)
);
card.addSection(integrationSection);
//////////////////////
//// Footer with Button(s) ////
let footer = CardService.newFixedFooter();
let saveAction = CardService.newAction().setFunctionName(
'onUserSelectedConfigSaveSelections'
);
footer.setPrimaryButton(
CardService.newTextButton().setText('Save').setOnClickAction(saveAction)
);
if (wasConfigured) {
let resetAction = CardService.newAction().setFunctionName(
'onUserSelectedConfigResetSelections'
);
footer.setSecondaryButton(
CardService.newTextButton()
.setText('Reset All')
.setOnClickAction(resetAction)
);
}
card.setFixedFooter(footer);
//////////////////////
return card.build();
}
/**
* Builds a card that shows the user options to customize their integration.
* Contents will vary based on the chosen integration type. Called when
* user clicks "Customize Integration" from "Configure Odo" card.
*
* @param {Object} event Event information passed in by Card framework.
*
* @return {CardService.Card} Card to show result.
*/
function onUserSelectedCustomizeIntegration(event) {
// get the user selected integration type to customize
let formInputs = event.commonEventObject.formInputs;
let selectedIntegrationType =
formInputs.integrationType.stringInputs.value[0];
return buildCustomizeIntegrationCard(selectedIntegrationType);
}
/**
* Saves the user selected configurations from the Configure Odo page
*
* @param {Object} event Event information passed in by Card framework.
*
* @return {CardService.Card} Card to show result.
*/
function onUserSelectedConfigSaveSelections(event) {
let config = {};
let formInputs = event.commonEventObject.formInputs;
config.saved = true;
config.customerName = formInputs.customerName.stringInputs.value[0];
config.customerLogoUrl = formInputs.customerLogoUrl.stringInputs.value[0];
config.toolName = formInputs.toolName.stringInputs.value[0];
config.integrationType = formInputs.integrationType.stringInputs.value[0];
config.dateFormat = formInputs.dateFormat.stringInputs.value[0];
config.welcomeSplashMessage =
formInputs.welcomeSplashMessage.stringInputs.value[0];
let up = PropertiesService.getUserProperties();
let integrationDataStr = up.getProperty(
PROP_STRINGIFIED_INTEGRATION_CUSTOMIZATIONS
);
if (integrationDataStr) {
let integrationData = JSON.parse(integrationDataStr);
console.log(JSON.stringify(integrationData))
config.integrationData = integrationData;
} else {
let hooks = INTEGRATION_HOOKS[config.integrationType];
config.integrationData = hooks.defaultIntegrationConfig();
}
saveConfig(config);
return buildRefreshNeededCard();
}
/**
* Resets the Odo configurations
*
* @return {CardService.Card} Card to show result
*/
function onUserSelectedConfigResetSelections() {
resetConfig();
return buildRefreshNeededCard();
}
/**
* Returns the current Odo configuration, which is an Object (struct) with
* various members related to Odo's config. Can be modified and saved via a
* call to saveConfig().
*
* @return {Object}
*/
function getConfig() {
let up = PropertiesService.getUserProperties();
let configStr = up.getProperty(USER_PROPERTY_CONFIG);
let config = null;
if (configStr) {
config = JSON.parse(configStr);
} else {
return _getDefaultConfig();
}
return config;
}
/**
* Takes a configuration Object (struct) and saves it. Will
* be returned with the same data the next time getConfig() is called.
* @param {Object} config - The configuration object to be saved.
*
*/
function saveConfig(config) {
config.saved = true;
let configStr = JSON.stringify(config);
let up = PropertiesService.getUserProperties();
up.setProperty(USER_PROPERTY_CONFIG, configStr);
}
/**
* Function that fully resets the configuration.
*
*/
function resetConfig() {
let up = PropertiesService.getUserProperties();
// Delete all stored properties. This will include the USER_PROPERTY_CONFIG,
// as well as any integration specific properties.
up.deleteAllProperties();
}
/**
* Dump contents of config to console. For debugging purposes only.
*/
function _dumpConfig() {
let config = getConfig();
console.log(JSON.stringify(config));
}
/**
* Private function that constructs and returns a default config (i.e. user
* has not configured Odo yet). Used internally by getConfig().
*
* @return {Object}
*/
function _getDefaultConfig() {
let defaultIntegrationType = INTEGRATION_TYPE.RECORDS_BASED;
let config = {
saved: false, // set to true first time configi saved by user,
welcomeSplashShhown: false,
customerName: DEFAULT_CUSTOMER_NAME,
customerLogoUrl: DEFAULT_CUSTOMER_LOGO_URL,
toolName: DEFAULT_CUSTOMER_TOOL_NAME,
integrationType: defaultIntegrationType,
dateFormat: 'MMM dd, yyyy',
welcomeSplashMessage:
'Welcome to {{toolName}} for Workspace. Click the button below '
+ 'to get going!',
};
let hooks = INTEGRATION_HOOKS[defaultIntegrationType];
config.integrationData = hooks.defaultIntegrationConfig();
return config;
}