-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebarJavaScript.html
More file actions
171 lines (146 loc) · 4.57 KB
/
Copy pathSidebarJavaScript.html
File metadata and controls
171 lines (146 loc) · 4.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
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var units, width, height, resolution;
var $units, $width, $height, $resolution, $custom, $customContainer;
/**
* Run initializations on sidebar load.
*/
$(function() {
$('#sidebar-all-button').click(onAllClick);
$units = $('#sidebar-units');
$units.change(onUnitsChange);
units = $units.val();
$width = $('#sidebar-width');
$width.change(onWidthChange);
width = Number($width.val());
$height = $('#sidebar-height');
$height.change(onHeightChange);
height = Number($height.val());
$resolution = $('#sidebar-resolution');
$resolution.change(onResolutionChange);
resolution = Number($resolution.val());
$customContainer = $('#sidebar-custom-block');
$custom = $('#sidebar-custom');
$custom.change(onResolutionChange);
updateResultingSize();
});
/**
* Handle units change
*/
function onUnitsChange() {
units = this.value;
if (units === 'pixels') {
width = (width * resolution).toFixed(0);
$width.val(width);
height = (height * resolution).toFixed(0);
$height.val(height);
} else if (units === 'inches') {
width = width / resolution;
height = height / resolution;
$width.val(width.toFixed(2));
$height.val(height.toFixed(2));
}
updateResultingSize();
}
/**
* Handle width change
*/
function onWidthChange() {
width = Number(this.value);
updateResultingSize();
}
/**
* Handle height change
*/
function onHeightChange() {
height = Number(this.value);
updateResultingSize();
}
/**
* Handle resolution change, show custom input when "Custom" is selected.
* Hide it when a preset resolution is set.
*/
function onResolutionChange() {
var newResolution = this.value;
if (newResolution === 'custom') {
$customContainer.show();
$custom.val(resolution);
return;
}
if ($.inArray(Number(newResolution), [72, 144, 300]) !== -1) {
$resolution.val(newResolution);
$customContainer.hide();
}
var oldResolution = resolution;
resolution = Number(newResolution);
if (units === 'pixels' && resolution !== 0) {
width = ((width / oldResolution) * resolution).toFixed(0);
$width.val(width);
height = ((height / oldResolution) * resolution).toFixed(0);
$height.val(height);
}
updateResultingSize();
}
/**
* Handle resolution change, show custom input when "Custom" is selected.
* Hide it when a preset resolution is set.
*/
function updateResultingSize() {
var widthInches, heightInches;
if (units === 'pixels') {
widthInches = units === 'pixels' ? width / resolution : width;
heightInches = units === 'pixels' ? height / resolution : height;
} else if (units === 'inches') {
widthInches = width;
heightInches = height;
}
$('#sidebar-width-inches').html(widthInches.toFixed(2));
$('#sidebar-height-inches').html(heightInches.toFixed(2));
}
/**
* Calls the server to retrieve information from the sheet.
* Gets the value in the active cell, which is then placed in the
* sidebar text field.
*/
function onAllClick() {
this.disabled = true;
if (width === 0 || height === 0 || resolution === 0) {
showStatus('All Width, Height, and Resolution are required', 'error');
this.disabled = false;
return;
}
var multiplier = 1;
if (units === 'inches') {
multiplier = resolution;
}
// Send the value to the server and handle the response.
google.script.run
.withSuccessHandler(
function(msg, element) {
// Respond to success conditions here.
showStatus('Charts resized successfully to ' + width + ' by ' + height + ' ' + units, 'success');
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
// Respond to failure conditions here.
showStatus(msg, 'error');
element.disabled = false;
})
.withUserObject(this)
.resizeAllCharts(width * multiplier, height * multiplier);
}
/**
* Displays the given status message in the sidebar.
*
* @param {String} msg The status message to display.
* @param {String} classId The message type (class id) that the message
* should be displayed as.
*/
function showStatus(msg, classId) {
$('#sidebar-status').removeClass().html(msg);
if (classId) {
$('#sidebar-status').addClass(classId);
}
}
</script>