Add base for theming

This commit is contained in:
Samy Pessé
2014-04-04 14:10:56 -07:00
parent 07101deaef
commit 446d486a0f
112 changed files with 14 additions and 5 deletions
+50
View File
@@ -0,0 +1,50 @@
define([
"jQuery",
"utils/execute",
"utils/analytic",
"core/state"
], function($, execute, analytic, state){
// Bind an exercise
var prepareExercise = function($exercise) {
var codeSolution = $exercise.find(".code-solution").text();
var codeValidation = $exercise.find(".code-validation").text();
var editor = ace.edit($exercise.find(".editor").get(0));
editor.setTheme("ace/theme/tomorrow");
editor.getSession().setUseWorker(false);
editor.getSession().setMode("ace/mode/javascript");
// Submit: test code
$exercise.find(".action-submit").click(function(e) {
e.preventDefault();
analytic.track("exercise.submit");
execute(editor.getValue(), codeValidation, function(err, result) {
$exercise.toggleClass("return-error", err != null);
$exercise.toggleClass("return-success", err == null);
if (err) $exercise.find(".alert-danger").text(err.message || err);
});
});
// Set solution
$exercise.find(".action-solution").click(function(e) {
e.preventDefault();
editor.setValue(codeSolution);
editor.gotoLine(0);
});
};
// Prepare all exercise
var init = function() {
state().$book.find("section.exercise").each(function() {
prepareExercise($(this));
});
};
return {
init: init,
prepare: prepareExercise
};
});
+31
View File
@@ -0,0 +1,31 @@
define([
"jQuery",
"Mousetrap",
"core/navigation",
"core/sidebar"
], function($, Mousetrap, navigation, sidebar){
// Bind keyboard shortcuts
var init = function() {
// Next
Mousetrap.bind(['right'], function(e) {
navigation.goNext();
return false;
});
// Prev
Mousetrap.bind(['left'], function(e) {
navigation.goPrev();
return false;
});
// Toggle Summary
Mousetrap.bind(['s'], function(e) {
sidebar.toggle();
return false;
});
};
return {
init: init
};
});
+17
View File
@@ -0,0 +1,17 @@
define([
"jQuery"
], function($) {
var goNext = function() {
var url = $("link[rel='next']").attr("href");
if (url) location.href = url;
};
var goPrev = function() {
var url = $("link[rel='prev']").attr("href");
if (url) location.href = url;
};
return {
goNext: goNext,
goPrev: goPrev
};
});
+66
View File
@@ -0,0 +1,66 @@
define([
"lodash",
"jQuery",
"utils/storage",
"core/state"
], function(_, $, storage, state) {
// Get current level
var getCurrentLevel = function() {
return state().level;
};
// Return all levels
var getLevels = function() {
var levels = $(".book-summary li[data-level]");
return _.map(levels, function(level) {
return $(level).data("level").toString();
});
};
// Return a map chapter -> number (timestamp)
var getProgress = function() {
// Current level
var progress = storage.get("progress", {});
// Levels
var levels = getLevels();
_.each(levels, function(level) {
progress[level] = progress[level] || 0;
});
return progress;
};
// Change value of progress for a level
var markProgress = function(level, state) {
if (state == null) state = true;
var progress = getProgress();
progress[level] = state? Date.now() : 0;
storage.set("progress", progress);
};
// Show progress
var showProgress = function() {
// Update progress
var progress = getProgress();
var $summary = $(".book-summary");
_.each(progress, function(value, level) {
$summary.find("li[data-level='"+level+"']").toggleClass("done", value > 0);
});
// Mark current progress
markProgress(getCurrentLevel(), true);
};
return {
'current': getCurrentLevel,
'levels': getLevels,
'get': getProgress,
'mark': markProgress,
'show': showProgress
};
});
+42
View File
@@ -0,0 +1,42 @@
define([
"utils/storage",
"utils/platform",
"core/state"
], function(storage, platform, state) {
// Toggle sidebar with or withour animation
var toggleSidebar = function(_state, animation) {
if (-state != null && isOpen() == _state) return;
if (animation == null) animation = true;
var $book = state().$book;
$book.toggleClass("without-animation", !animation);
$book.toggleClass("with-summary", _state);
storage.set("sidebar", isOpen());
};
// Return true if sidebar is open
var isOpen = function() {
return state().$book.hasClass("with-summary");
};
// Prepare sidebar: state and toggle button
var init = function() {
var $book = state().$book;
// Toggle summary
$book.find(".book-header .toggle-summary").click(function(e) {
e.preventDefault();
toggleSidebar();
});
// Init last state if not mobile and not homepage
toggleSidebar(platform.isMobile ? false : storage.get("sidebar", true), false);
};
return {
init: init,
toggle: toggleSidebar
}
});
+14
View File
@@ -0,0 +1,14 @@
define([
"jQuery"
], function() {
return function() {
var $book = $(".book");
return {
'$book': $book,
'githubId': $book.data("github"),
'level': $book.data("level")
};
};
});