-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsidebar.js.html
186 lines (169 loc) · 5.05 KB
/
sidebar.js.html
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
<!--
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Libraries -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js"></script>
<!-- Custom JavaScript -->
<script>
// Constants.
var REFRESH_WAIT_SECONDS = 1;
var INACTIVITY_TIMEOUT_MINUTES = 2;
// Global variables.
var lastResult;
var refreshOn = true;
var lastChangedTime;
var timeoutId;
// On page load.
$(function() {
$('#loading').hide();
refreshState();
});
/**
* Refreshes the state information in the sidebar.
*/
function refreshState() {
google.script.run.withFailureHandler(function(error) {
showError(error);
tick(null);
}).withSuccessHandler(function(result) {
hideError();
if (!lastResult || !jsonEquals(result.cursor, lastResult.cursor)) {
updateCursor(result.cursor);
}
if (!lastResult || !jsonEquals(result.selection, lastResult.selection)) {
updateSelection(result.selection);
}
tick(result);
}).getDocumentInfo();
}
/**
* Updates the cursor information in the sidebar.
* @param {Object} cursor The cursor information.
*/
function updateCursor(cursor) {
if (cursor) {
$('#cursor input').removeAttr('disabled');
updateElement('element-type', cursor.element.type);
updateElement('offset', cursor.offset);
updateElement('surrounding-text', cursor.surroundingText);
updateElement('surrounding-text-offset', cursor.surroundingTextOffset);
} else {
$('#cursor input').val('').attr('disabled', 'true');
}
}
/**
* Updates the selection information in the sidebar.
* @param {Object} selection The selection information.
*/
function updateSelection(selection) {
var tableBody = $('#selection table tbody');
tableBody.children().remove();
if (selection) {
selection.selectedElements.forEach(function(selectedElement) {
var row = $('<tr>');
var data = [
selectedElement.element.type,
selectedElement.partial,
selectedElement.startOffset,
selectedElement.endOffsetInclusive
];
data.forEach(function(value) {
row.append($('<td>').text(value));
});
tableBody.append(row);
});
tableBody.effect("highlight", { duration: 1500 });
} else {
tableBody.append($('<tr><td class="na" colspan="4">None</td></tr>'));
}
}
/**
* Shows an error message in the sidebar.
* @param {string} error The error returned by the server.
*/
function showError(error) {
$('#error').text(error).show();
$('#results').css('color', 'gray');
}
/**
* Hides any error message in the sidebar.
*/
function hideError() {
$('#error').hide();
$('#results').css('color', 'inherit');
}
/**
* Updates the state of the document and sets up the next refresh.
* @param {Object} result The last result, if any.
*/
function tick(result) {
if (result) {
if (!jsonEquals(result, lastResult)) {
lastChangedTime = new Date();
}
lastResult = result;
}
$('#lastupdated').text(new Date().toLocaleTimeString());
if (isInactive()) {
toggleRefresh();
lastResult = null;
lastChangedTime = null;
}
if (refreshOn) {
timeoutId = window.setTimeout(refreshState, REFRESH_WAIT_SECONDS * 1000);
}
}
/**
* Determines if the document is inactive.
* @return {Boolean} True if the document is inactive, false otherwise.
*/
function isInactive() {
var now = new Date();
return lastChangedTime && now.getTime() - lastChangedTime.getTime() > INACTIVITY_TIMEOUT_MINUTES * 60 * 1000;
}
/**
* Toggles whether or not automatic refreshing is on, and updated the button.
*/
function toggleRefresh() {
var toggle = $('#toggle');
if (refreshOn) {
refreshOn = false;
window.clearTimeout(timeoutId);
toggle.text(toggle.data('resumetext'));
} else {
refreshOn = true;
refreshState();
toggle.text(toggle.data('stoptext'));
}
}
/**
* Updates an element with a new value and highlights it if there is a change.
* @param {string} elementId The ID of the element to update.
* @param {string} value The new value of the element.
*/
function updateElement(elementId, value) {
var element = $(document.getElementById(elementId));
if (String(element.val()) != String(value)) {
element.val(value).effect("highlight", { duration: 1500 });
}
}
/**
* Determines if two Objects have the JSON structure.
* @param {Object} a The first object.
* @param {Object} b The second object.
* @return {Boolean} True if both objects have the same JSON structure, false otherwise.
*/
function jsonEquals(a, b) {
return JSON.stringify(a) == JSON.stringify(b);
}
</script>