-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScripts.html
More file actions
390 lines (364 loc) · 9.57 KB
/
Scripts.html
File metadata and controls
390 lines (364 loc) · 9.57 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
console.log("SciAI Loading");
/***** ACCORDION SETUP *****/
// display the first div by default
$("#accordion div").first().css('display', 'block');
var link = $("#accordion a");
link.on('click', function(e) {
e.preventDefault();
var a = $(this).attr("href");
if ($(this).hasClass('closed')) {
$(a).slideDown('fast')
$(this).removeClass('closed').addClass('opened');
$(this).children('.accordion-arrow').html('⤴');
$(".accordion-inner").not(a).slideUp('fast');
$(".accordion-arrow").not($(this).children('.accordion-arrow')).html('⤵');
} else {
$(a).slideUp('fast');
$(this).children('.accordion-arrow').html('⤵');
$(this).removeClass('opened').addClass('closed');
}
});
/***** END OF ACCORDION SETUP *****/
/***** BIND ACTIONS TO ALL BUTTONS *****/
$('#set-structure').click(setStructure);
$('#add-custom-tag').click(addCustomTag);
$('#element-types').change(runSearch);
$('#export-clear').click(exportClear);
$('#export-docx').click(exportDocx);
$('#export-pdf').click(exportPdf);
$('#export-sci').click(exportSci);
$('#update-tags').click(updateTags);
$('#remove-all').click(removeAllTags);
/***** END OF BIND ACTIONS TO ALL BUTTONS *****/
});
/***** FUNCTIONS *****/
function removeAllTags() {
google.script.run
.withSuccessHandler(
function(tags, element) {
element.disabled = false;
updateTags();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.removeAllTags();
}
/*
* Update list of existing tags in the document
*/
function updateTags() {
google.script.run
.withSuccessHandler(
function(tags, element) {
element.disabled = false;
console.log(tags);
$('.show-tags').html('');
for(var i = 0; i < tags.length; i++) {
var content = '<p><i class="fa fa-2 fa-bookmark-o" aria-hidden="true"></i> <b>Type: '
+ tags[i].type + '</b></p>';
content += '<p><i class="fa fa-2 fa-file-text-o" aria-hidden="true"></i> <b>Text:</b> '
+ tags[i].text + '</p>';
var buttons = "<button class='delete-tag' data-id='" + tags[i].namedRangeId
+ "'><i class='fa fa-3 fa-times' aria-hidden='true'></i></button>";
content = '<div class="tag-block"><div class="tag-block-left">'
+ content + '</div><div class="tag-block-right">'
+ buttons + '</div><hr class="clear"></div>';
$('.show-tags').append(content);
}
$('.delete-tag').unbind().click(deleteSelectedTag);
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.getAllTags();
}
/*
* Remove existing tag from the document
*/
function deleteSelectedTag(event) {
var name = $(event.currentTarget).attr('data-id');
var el = event.currentTarget;
google.script.run
.withSuccessHandler(
function(tags, element) {
element.disabled = false;
$(el).closest('.tag-block').remove();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.removeTag(name);
}
/*
* Wrap text into the custom tag
*/
function addCustomTag() {
var type = $('#custom-tag').val();
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.createNewTag(type);
}
/*
* Insert empty document structure for selected document type
*/
function setStructure() {
this.disabled = true;
removeErrors();
var documentType = $('#document-types').val();
if(documentType == 'select') {
showError('Please, select type of document', '#document-types');
this.disabled = false;
return false;
}
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
showError(msg, '#document-types');
element.disabled = false;
})
.withUserObject(this)
.insertDocumentStructure(documentType);
}
/*
* Runs a server-side function to search the user-selected text and update
* the sidebar UI with the resulting text.
*/
function runSearch() {
console.log("Searching...");
this.disabled = true;
var elementType = $('#element-types').val();
if(elementType == 'select') {
showError('Please, select type of document', '#element-types');
this.disabled = false;
return false;
}
$('#error').remove();
google.script.run
.withSuccessHandler(
function(searchResults, element) {
element.disabled = false;
showResults(searchResults);
$('#element-types').val('select');
$('.wrap').unbind().click(insertSelectedResult);
})
.withFailureHandler(
function(msg, element) {
showError(msg, 'element-types');
element.disabled = false;
})
.withUserObject(this)
.searchTerm(elementType);
}
/*
* Insert in document search result that user click on
*/
function insertSelectedResult(event) {
console.log("Clicked on " + event.currentTarget.id);
var id = $(event.currentTarget).attr('data-id');
var type = $(event.currentTarget).attr('data-type');
google.script.run
.withSuccessHandler(
function(processedText, element) {
$('#search-results').empty();
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.createNewTag(type, id);
}
/*
* Show search results in the sidebar
*/
function showResults(results) {
$('#search-results').empty();
if (results.length > 0) {
console.log("Number of results: " + results.length);
for (var i = 0, j = results.length; i < j; i++) {
var id = results[i]['id'];
var type = results[i]['type'];
var header = '<h3 class="inline">' + splitByWords(results[i]['name'], 40)
+ '<p class="inline">' + type + '</p></h3>';
var content = '<p class="no-margin">' + splitByWords(results[i]['desc'], 200) + '</p>';
var button = '<button class="blue wrap" data-id="' + id
+ '" data-type="' + type + '">Wrap</button>'
+ '<a class="margin-left-15 button white" href="'
+ results[i]['url'] + '" target="_blank">Open</a>'
$('#search-results').append('<div id="' + id + '"></div');
$('#' + id).append(header, content, button);
if (i !== j - 1) {
$('#' + id).append('<hr class="clear">');
}
}
} else {
console.log('Nothing found!');
$('#search-results').append('<p>Sorry, but there is no results...</p>');
}
}
/*
* Split string by words (spaces) with setted result length
*/
function splitByWords(str, len) {
if (str.length <= len) {
return str;
}
var strArr = str.split(' ');
var result = strArr[0];
var i = 0;
for(var i = 1; i < strArr.length; i++) {
if (result.length < len) {
result = result + ' ' + strArr[i];
}
else {
break;
}
}
result += '...';
return result;
}
/*
* Run server-side export script to DOCX format
*/
function exportDocx(){
toggleLoader();
this.disabled = true;
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
toggleLoader();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
toggleLoader();
})
.withUserObject(this)
.exportDocx();
}
/*
* Run server-side export script to PDF format
*/
function exportPdf(){
toggleLoader();
this.disabled = true;
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
toggleLoader();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
toggleLoader();
})
.withUserObject(this)
.exportPdf();
}
/*
* Run server-side export script to clear (without any tags) format
*/
function exportClear(){
toggleLoader();
this.disabled = true;
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
toggleLoader();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
toggleLoader();
})
.withUserObject(this)
.exportClear();
}
/*
* Run server-side export script to SCI format
*/
function exportSci(){
toggleLoader();
this.disabled = true;
google.script.run
.withSuccessHandler(
function(processedText, element) {
element.disabled = false;
toggleLoader();
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
toggleLoader();
})
.withUserObject(this)
.exportSci();
}
/*
* Show or hide loader
*/
function toggleLoader() {
var state = $(".loader-wrapper").css('display');
if(state == 'none') {
// show loader
$(".loader-wrapper").css("display","block");
$("body").addClass("body_with_opacity");
} else {
// hide loader
$(".loader-wrapper").css("display","none");
$("body").removeClass("body_with_opacity");
}
}
/*
* Inserts a div that contains an error message after a given element.
*
* @param msg The error message to display.
* @param element The element after which to display the error.
*/
function showError(msg, element) {
var div = $('<div id="error" class="error">' + msg + '</div>');
$(element).after(div);
}
/*
* Remove all errors
*/
function removeErrors() {
$('.error').remove();
}
/***** END OF FUNCTIONS *****/
</script>