Skip to content

Commit d8ee6bd

Browse files
authored
Merge pull request #1111 from gusthoff/topic/issues/1100
Widget: fix dangerous menu options
2 parents 9b22f64 + 22898fb commit d8ee6bd

File tree

5 files changed

+52
-63
lines changed

5 files changed

+52
-63
lines changed

frontend/sphinx/widget/templates/widget.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@
1111
{% for f in w.files %}
1212
<div class="file" data-basename="{{ f.basename }}">{{ f.content }}</div>
1313
{% endfor %}
14-
<div id="{{ w.id }}.tab" class="tab">
15-
{% for t in w.files %}
16-
<button id="{{ w.id }}.tab.{{ t.basename }}" class="tab-links">{{ t.basename }}</button>
17-
{% endfor %}
18-
</div>
19-
<div id="{{ w.id }}.editors">
20-
<div id="{{ w.id }}.editors.editor" class="editor-container"></div>
21-
<div id="{{ w.id }}.editors.non-tabbed-editor" class="non-tabbed-editor-container" hidden>
22-
{% for t in w.files %}
23-
<div id="{{ w.id }}.editors.non-tabbed-editor.{{ t.basename }}" class="non-tabbed-links"></div>
24-
{% endfor %}
25-
</div>
26-
</div>
2714
<div id="{{ w.id }}.settings-bar" class="settings-bar">
2815
<div class="dropdown-container settingsbar-item">
2916
<button class="dropdown-btn">
@@ -78,6 +65,19 @@
7865
Download
7966
</button>
8067
</div>
68+
<div id="{{ w.id }}.editors">
69+
<div id="{{ w.id }}.editors.editor" class="editor-container"></div>
70+
<div id="{{ w.id }}.editors.non-tabbed-editor" class="non-tabbed-editor-container" hidden>
71+
{% for t in w.files %}
72+
<div id="{{ w.id }}.editors.non-tabbed-editor.{{ t.basename }}" class="non-tabbed-links"></div>
73+
{% endfor %}
74+
</div>
75+
</div>
76+
<div id="{{ w.id }}.tab" class="tab">
77+
{% for t in w.files %}
78+
<button id="{{ w.id }}.tab.{{ t.basename }}" class="tab-links">{{ t.basename }}</button>
79+
{% endfor %}
80+
</div>
8181
<div class="row output-row">
8282
<div id="{{ w.id }}.button-group" class="col-md-3 button-group">
8383
{% for b in w.button_group %}

frontend/src/styles/learn.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ pre.widget {
160160
flex-wrap: nowrap;
161161
overflow-x: auto;
162162
-webkit-overflow-scrolling: touch;
163+
margin-bottom: 10px;
164+
163165
button {
164166
background-color: inherit;
165167
float: left;
@@ -196,7 +198,6 @@ pre.widget {
196198
padding-left: 13px;
197199
display: block;
198200
margin-top: 0px;
199-
margin-bottom: 10px;
200201

201202
button {
202203
background-color: transparent;

frontend/src/ts/strings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export const RESET_CONFIRM_MSG =
55
'Your changes will be lost after reset. ' +
66
'Are you sure you want to reset the editor and the compiler options?';
77

8+
export const RELOAD_CONFIRM_MSG =
9+
'Changing this setting requires reloading the page: ' +
10+
'your changes will be lost. ' +
11+
'Are you sure you want to proceed?';
12+
813
export const DOWNLOAD_TOOLTIP = 'Download source files';
914
export const DOWNLOAD_MAINTENANCE =
1015
'The download functionilty is currently undergoing maintenance';

frontend/src/ts/widget.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,23 @@ class Widget {
151151
this.setTabbedView(tabbedView);
152152

153153
tabSetting.addEventListener('change', () => {
154-
let tabbedView = 'true';
155-
if (!tabSetting.checked) {
156-
tabbedView = 'false';
157-
}
158-
cookies.set('tabbed_view', tabbedView, {expires: 3650});
159-
this.setTabbedView(tabSetting.checked);
154+
if (window.confirm(Strings.RELOAD_CONFIRM_MSG)) {
155+
let tabbedView = 'true';
156+
if (!tabSetting.checked) {
157+
tabbedView = 'false';
158+
}
159+
cookies.set('tabbed_view', tabbedView, {expires: 3650});
160+
this.setTabbedView(tabSetting.checked);
160161

161-
// Current approach: just reload the page to
162-
// set the correct theme for all widgets.
163-
location.reload();
162+
// Current approach: just reload the page to
163+
// set the correct theme for all widgets.
164+
location.reload();
165+
}
166+
else
167+
{
168+
// Revert if user chooses "no change"
169+
tabSetting.checked = !tabSetting.checked;
170+
}
164171
});
165172

166173
this.initCompilerSwitches();
@@ -175,16 +182,23 @@ class Widget {
175182
this.setTheme(cookieTheme);
176183

177184
themeSetting.addEventListener('change', () => {
178-
let theme = 'light';
179-
if (themeSetting.checked) {
180-
theme = 'dark';
181-
}
182-
cookies.set('theme', theme, {expires: 3650});
183-
this.setTheme(theme);
185+
if (window.confirm(Strings.RELOAD_CONFIRM_MSG)) {
186+
let theme = 'light';
187+
if (themeSetting.checked) {
188+
theme = 'dark';
189+
}
190+
cookies.set('theme', theme, {expires: 3650});
191+
this.setTheme(theme);
184192

185-
// Current approach: just reload the page to
186-
// set the correct theme for all widgets.
187-
location.reload();
193+
// Current approach: just reload the page to
194+
// set the correct theme for all widgets.
195+
location.reload();
196+
}
197+
else
198+
{
199+
// Revert if user chooses "no change"
200+
themeSetting.checked = !themeSetting.checked;
201+
}
188202
});
189203

190204
const resetButton =

frontend/tests/ts/widget.spec.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -404,37 +404,6 @@ describe('Widget', () => {
404404
editor = ace.edit(editorDiv);
405405
});
406406

407-
it('should have a checkbox that switches editor theme', () => {
408-
const box = getElemById(root.id + '.settings-bar.theme-setting') as
409-
HTMLInputElement;
410-
const origTheme = editor.getTheme();
411-
box.checked = !box.checked;
412-
triggerEvent(box, 'change');
413-
expect(editor.getTheme()).to.not.equal(origTheme);
414-
});
415-
416-
it('should have a checkbox that switches tab setting', () => {
417-
const nonTabbedEditorDiv: HTMLElement = getElemById(root.id + '.editors.non-tabbed-editor');
418-
const box = getElemById(root.id + '.settings-bar.tab-setting') as
419-
HTMLInputElement;
420-
421-
const origIsTabbedView : boolean = !editorDiv.hidden;
422-
expect(nonTabbedEditorDiv.hidden).to.equal(origIsTabbedView);
423-
424-
box.checked = !box.checked;
425-
triggerEvent(box, 'change');
426-
expect(editorDiv.hidden).to.equal(origIsTabbedView);
427-
expect(nonTabbedEditorDiv.hidden).to.not.equal(origIsTabbedView);
428-
429-
// Test hiding / showing buttons for tabbed view
430-
const tabs = getElemById(root.id + '.tab');
431-
const headers = getElemsByTag(tabs, 'button');
432-
433-
for (const h of headers) {
434-
expect(h.hidden).to.equal(origIsTabbedView);
435-
}
436-
});
437-
438407
it('should have a button that resets the editor', () => {
439408
const btn = getElemById(root.id + '.settings-bar.reset-btn') as
440409
HTMLButtonElement;

0 commit comments

Comments
 (0)