Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable multiple zips per tutorial page #298

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions _layouts/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
layout: default
---


<div>
<article role="article">
{% if page.title %}
<header>
<h1 class="entry-title">{{ page.title }}</h1>
</header>
{% endif %}

{% if page.files %}
{% for zip in page.files %}
<a class="tutorial-filelist__heading" name="download-{{zip.zipname}}">Files for {{zip.zipname}}</a>
Copy link
Member

@octopusinvitro octopusinvitro Jul 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name attribute is obsolete in a tags (not in form elements, though).
You can use id="download-{{zip.zipname}}" instead.

Also, I personally wouldn't use an a element here, but a heading, so that there is an indication that this is a different section from what is strictly the tutorial copy. So something like, either:

<h2 class="tutorial-filelist__heading" id="download-{{zip.zipname}}">Files for {{zip.zipname}}</h2>

or maybe:

<section>
  <h1 class="tutorial-filelist__heading" id="download-{{zip.zipname}}">Files for {{zip.zipname}}</h1>
</section>

Or even:

<section>
  <h1>Download the tutorial files!</h1>

  <p class="tutorial-filelist__heading" id="download-{{zip.zipname}}">Files for {{zip.zipname}}</p>
  <!-- Files here... -->
  <p class="tutorial-filelist__heading" id="download-{{zip.zipname}}">Files for {{zip.zipname}}</p>
  <!-- Files here... -->
</section>

where zip.zipname is the corresponding name.

<ul class="tutorial-filelist__list compact-list">
{% assign pageurl = page.url | split: '/' | pop: | join: '/' %}
{% assign files = zip.files %}
{% for file in files %}
<li class="tutorial-filelist__file"><a href="{{ pageurl }}/{{ file }}">{{ pageurl }}/{{ file }}</a></li>
{% endfor %}
</ul>
{% endfor %}
{% endif %}

{{ content }}
<footer>
<a href='http://tutorials.codebar.io/'>Back to tutorials</a>
Expand All @@ -20,15 +35,3 @@ <h1 class="entry-title">{{ page.title }}</h1>



{% if page.files %}
{% assign pageurl = page.url | split: '/' | pop: | join: '/' %}
{% capture files %}[{% for file in page.files %}
"{{ pageurl }}/{{ file }}"{% if forloop.last %}{% else %},{% endif %}{% endfor %}
]{% endcapture %}
{% endif %}

<script>
var Files = {
all : {{ files }}
}
</script>
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ browsersync = require('browser-sync').create(),
'./javascripts-dev/vendor/FileSaver.js',
'./javascripts-dev/src/downloader.js',
'./javascripts-dev/src/zipper.js',
'./javascripts-dev/src/uitools.js',
'./javascripts-dev/src/ui.js',
'./javascripts-dev/main.js',
],
Expand Down
51 changes: 44 additions & 7 deletions javascripts-dev/src/ui.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
var UserInterface = {
selector: "a[href=download]"
selector: 'a[href|="#download"]'
};

UserInterface.setup = function(zipper, downloader) {

var downloadLink = document.body.querySelector(UserInterface.selector);
var downloadLinks = document.body.querySelectorAll(UserInterface.selector);

var createZip = function() {
zipper.createZip(downloader);
var createZip = function(files) {
zipper.createZip(downloader, files);
};

var registerListener = function(link) {
console.log("adding listener to", link);
Copy link
Member

@octopusinvitro octopusinvitro Jul 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally wouldn't include debugging code in commits that may end up in production, but that's maybe a personal choice.

You can use git add -p to granularly exclude the lines that you don't want to add to the commit.


link.addEventListener("click", function(event) {
event.preventDefault();
createZip();

// Get the file list on the fly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also usually don't commit comments... again, probably just a personal choice.

var files = getFileList(event.target);
console.log('files', files);

console.log('creating zip');
createZip(files);
}, false);
};

var checkIfDownloadLinkExist = function() {
if (downloadLink) {
registerListener(downloadLink);
if (downloadLinks) {
foreach(downloadLinks, function() {
registerListener(this);

hideAccessibleFileLinks(this);
});
}
};

var locateAnchorForDownloadLink = function(link) {
var anchorName = link.href.replace(/^[^#]*#/, '');
return document.body.querySelector('a[name="' + anchorName + '"')
}

var hideAccessibleFileLinks = function(link) {
var anchor = locateAnchorForDownloadLink(link);
var next = getNextSiblingInDom(anchor);

addClass(anchor, 'hidden'); // could be made an expander button
addClass(next, 'hidden');
};

var getFileList = function(link) {
var anchor = locateAnchorForDownloadLink(link);
var next = getNextSiblingInDom(anchor);

var fileLinks = next.querySelectorAll('a'),
files = [];
foreach(fileLinks, function() {
files.push(this.href);
});
return files;
}

checkIfDownloadLinkExist();
}
32 changes: 32 additions & 0 deletions javascripts-dev/src/uitools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function foreach(list, callback) {
for (var idx = 0; idx < list.length; idx++) {
if (callback.apply(list[idx]) === false) {
break;
}
}
}

function getNextSiblingInDom(element) {
var allChildrenOfParent = element.parentNode.children,
nextSibling = false,
foundCurrent = false;

foreach(allChildrenOfParent, function() {
if (this === element) {
foundCurrent = true;
} else {
if (foundCurrent === true) {
nextSibling = this;
return false;
}
}
});
return nextSibling;
}

function addClass(element, className) {
var classes = element.className.split(' ');
if (classes.indexOf(className) !== false) {
element.className += " " + className;
}
}
4 changes: 2 additions & 2 deletions javascripts-dev/src/zipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ var Zipper = {
zip: new JSZip()
};

Zipper.createZip = function(downloader) {
Zipper.createZip = function(downloader, fileList) {

var addFiles = function() {
Files.all.map(function(url) {
fileList.map(function(url) {
var fileurl = url;
var filename = fileurl.replace(/.*\//g, "");
Zipper.zip.file(filename, downloader.getFile(fileurl), {binary:true});
Expand Down
8 changes: 4 additions & 4 deletions javascripts/downloader-bundle.js

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions js/lesson3/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
layout: page
title: Introduction to jQuery
files:
- files/index.html
- files/jquery.js
- files/script.js
- files/style.css
- zipname: example1
files:
- files/index.html
- files/jquery.js
- zipname: example2
files:
- files/script.js
- files/style.css
---


Expand Down Expand Up @@ -46,7 +50,7 @@ Using jQuery and JavaScript functions, we are going to build a small
todo list.

Download the files that you will need to work through the example
[here](download).
[here](#download-example1). and [example2](#download-example2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Download the files that you will need to work through the example here. and example2" doesn't read very well to me.

But I would only download the files for Exercise 1 in the section for exercise 1. The files for exercise 2 should be downloaded from exercise 2.

Also, Exercise 2 still has a link in "Download the files required to begin working through the example." pointing to the gist file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only the test illustration, if you notice the file list is the exercise 1 zip split in 2

<!-- https://gist.github.com/despo/309f684b7a6e002aaf1f/download -->

Alternatively, if you've already learned how to use git and would like
Expand Down
12 changes: 12 additions & 0 deletions stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ footer {
min-width: 680px;
margin: 0 auto;
}

.tutorial-filelist__list {
}

.hidden {
display: none;
visibility: hidden;
Copy link
Member

@octopusinvitro octopusinvitro Jul 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

display:none is not very accessible. I personally prefer the more tested way used in the HTML5 boilerplate (the .visuallyhidden class) to hide elements visually.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I do want to hide it from screen readers if the JS is available because the zip links will work and are accessible, right?

}

.compact-list > * {
margin: 3px 0px;
}