Skip to content

Commit 470dbf4

Browse files
committed
improve horizontal scrolling of files
1 parent 3d34b06 commit 470dbf4

File tree

4 files changed

+70
-62
lines changed

4 files changed

+70
-62
lines changed

algorithm/graph_search/dfs/desc.json

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
"<a href='https://en.wikipedia.org/wiki/Depth-first_search'>Wikipedia</a>"
2222
],
2323
"files": {
24+
"11111111111": "",
25+
"22222222222": "",
26+
"33333333333": "",
27+
"44444444444": "",
28+
"55555555555": "",
29+
"66666666666": "",
2430
"tree": "Searching a tree",
2531
"all_paths": "Going through all possible paths without making any circuit",
2632
"weighted_graph": "DFS of Weighted Graph",

css/stylesheet.css

+21-9
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,32 @@ section {
249249
height: 30px;
250250
}
251251

252-
.files_bar_right_button {
252+
.files_bar {
253253
height: 30px;
254-
float: right;
255-
position: relative;
256254
}
257255

258-
.files_bar_left_button {
259-
height: 30px;
260-
float: left;
261-
position: relative;
256+
.files_bar > * {
257+
position: absolute;
258+
height: 100%
262259
}
263260

264-
.files_bar {
265-
height: 30px;
261+
.files_bar > .button-left {
262+
left: 0;
263+
}
264+
265+
.files_bar > .button-right {
266+
right: 0;
267+
}
268+
269+
.files_bar > .wrapper {
270+
left: 30px;
271+
right: 30px;
272+
overflow: scroll;
273+
white-space: nowrap;
274+
}
275+
276+
.files_bar > .wrapper > button {
277+
max-width: 80%;
266278
}
267279

268280
.explanation_container {

index.html

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ <h3>
8989
</div>
9090
<div class="editor_container">
9191
<section class="files_bar">
92+
<button class="button-left">&lt;</button>
93+
<div class="wrapper"></div>
94+
<button class="button-right">&gt;</button>
9295
</section>
9396
<section class="explanation_container">
9497
<span id="explanation"></span>

js/script.js

+40-53
Original file line numberDiff line numberDiff line change
@@ -145,70 +145,58 @@ var showAlgorithm = function (category, algorithm) {
145145
$('#category').html(category_name);
146146
$('#algorithm').html(algorithm_name);
147147
$('#tab_desc > .wrapper').empty();
148-
$('.files_bar').empty();
148+
$('.files_bar > .wrapper').empty();
149149
$('#explanation').html('');
150150
lastFile = null;
151151
dataEditor.setValue('');
152152
codeEditor.setValue('');
153153
};
154154
var showFiles = function (category, algorithm, files) {
155-
var maxButtonCount = 3, fileNames = Object.keys(files), lastFile = -1;
155+
$('.files_bar > .wrapper').empty();
156156
var init = false;
157-
var currentFileBarButtons = [];
158-
159-
function addFileButton(file, explanation) {
160-
var $file = $('<button>').append(file).click(function () {
161-
loadFile(category, algorithm, file, explanation);
162-
$('.files_bar > button').removeClass('active');
163-
$(this).addClass('active');
164-
});
165-
$('.files_bar').append($file);
166-
currentFileBarButtons.push($file);
167-
168-
if (!init) {
169-
init = true;
170-
$file.click();
171-
}
172-
}
173-
174-
function clearFileBar() {
175-
currentFileBarButtons.forEach(function (f) {
176-
f.remove();
177-
});
157+
for (var file in files) {
158+
(function (file, explanation) {
159+
var $file = $('<button>').append(file).click(function () {
160+
loadFile(category, algorithm, file, explanation);
161+
$('.files_bar > .wrapper > button').removeClass('active');
162+
$(this).addClass('active');
163+
});
164+
$('.files_bar > .wrapper').append($file);
165+
if (!init) {
166+
init = true;
167+
$file.click();
168+
}
169+
})(file, files[file]);
178170
}
179-
180-
$('.files_bar').empty();
181-
182-
var $nextButton = $('<button>').append('<b>&gt;</b>').addClass('files_bar_right_button').click(function () {
183-
if (lastFile >= fileNames.length) {
184-
return;
171+
};
172+
$('.files_bar > .button-left').click(function () {
173+
var $parent = $('.files_bar > .wrapper');
174+
var clipWidth = $parent.width();
175+
var scrollLeft = $parent.scrollLeft();
176+
$($parent.children('button').get().reverse()).each(function () {
177+
var left = $(this).position().left;
178+
var right = left + $(this).outerWidth();
179+
if (0 > left) {
180+
$parent.scrollLeft(scrollLeft + right - clipWidth);
181+
return false;
185182
}
186-
clearFileBar(); //first, remove the previously visible buttons
187-
188-
init = false;
189-
fileNames.slice(lastFile + 1, lastFile + 1 + maxButtonCount).forEach(function (file) {
190-
file && addFileButton(file, files [file]);
191-
});
192-
lastFile += maxButtonCount;
193183
});
194-
$('.files_bar').append($nextButton);
195-
196-
var $prevButton = $('<button>').append('<b>&lt;</b>').addClass('files_bar_left_button').click(function () {
197-
if (lastFile === maxButtonCount - 1) {
198-
return;
184+
});
185+
$('.files_bar > .button-right').click(function () {
186+
var $parent = $('.files_bar > .wrapper');
187+
var clipWidth = $parent.width();
188+
var scrollLeft = $parent.scrollLeft();
189+
$parent.children('button').each(function () {
190+
var left = $(this).position().left;
191+
var right = left + $(this).outerWidth();
192+
console.log(left);
193+
console.log(right);
194+
if (clipWidth < right) {
195+
$parent.scrollLeft(scrollLeft + left);
196+
return false;
199197
}
200-
201-
clearFileBar(); //first, remove the previously visible buttons
202-
lastFile -= maxButtonCount;
203-
init = false;
204-
fileNames.slice(lastFile + 1 - maxButtonCount, lastFile + 1).forEach(function (file) {
205-
addFileButton(file, files [file]);
206-
});
207198
});
208-
$('.files_bar').append($prevButton);
209-
210-
$nextButton.click(); //initialize the file bar with the First 3 Buttons
211-
};
199+
});
212200
var loadAlgorithm = function (category, algorithm) {
213201
if (checkLoading()) return;
214202
showAlgorithm(category, algorithm);
@@ -497,7 +485,6 @@ var loadScratchPaper = function (gistID) {
497485

498486
var gistID = getParameterByName('scratch-paper');
499487
if (gistID) {
500-
console.log(gistID);
501488
loadScratchPaper(gistID);
502489
}
503490

0 commit comments

Comments
 (0)