diff options
Diffstat (limited to 'theme/javascript')
-rw-r--r-- | theme/javascript/core/events.js | 7 | ||||
-rwxr-xr-x | theme/javascript/core/exercise.js | 6 | ||||
-rw-r--r-- | theme/javascript/core/font-settings.js | 198 | ||||
-rwxr-xr-x | theme/javascript/core/navigation.js | 16 | ||||
-rw-r--r-- | theme/javascript/core/quiz.js | 13 | ||||
-rwxr-xr-x | theme/javascript/gitbook.js (renamed from theme/javascript/app.js) | 21 | ||||
-rw-r--r-- | theme/javascript/utils/analytic.js | 33 | ||||
-rwxr-xr-x | theme/javascript/vendors/mixpanel.js | 2 |
8 files changed, 153 insertions, 143 deletions
diff --git a/theme/javascript/core/events.js b/theme/javascript/core/events.js new file mode 100644 index 0000000..855755f --- /dev/null +++ b/theme/javascript/core/events.js @@ -0,0 +1,7 @@ +define([ + "jQuery" +], function($) { + var events = $({}); + + return events; +});
\ No newline at end of file diff --git a/theme/javascript/core/exercise.js b/theme/javascript/core/exercise.js index e24ae0a..7dbe982 100755 --- a/theme/javascript/core/exercise.js +++ b/theme/javascript/core/exercise.js @@ -1,9 +1,9 @@ define([ "jQuery", "utils/execute", - "utils/analytic", + "core/events", "core/state" -], function($, execute, analytic, state){ +], function($, execute, events, state){ // Bind an exercise var prepareExercise = function($exercise) { var codeSolution = $exercise.find(".code-solution").text(); @@ -19,7 +19,7 @@ define([ $exercise.find(".action-submit").click(function(e) { e.preventDefault(); - analytic.track("exercise.submit", {type: "code"}); + events.trigger("exercise.submit", {type: "code"}); execute("javascript", editor.getValue(), codeValidation, codeContext, function(err, result) { $exercise.toggleClass("return-error", err != null); diff --git a/theme/javascript/core/font-settings.js b/theme/javascript/core/font-settings.js index 9a7e205..4ea1646 100644 --- a/theme/javascript/core/font-settings.js +++ b/theme/javascript/core/font-settings.js @@ -1,89 +1,115 @@ define([ - "jQuery", - "utils/storage" - ], function($, storage) { - var fontState, toggle,bookBody,dropdown, book; - - var togglePopover = function(e) { - dropdown.toggleClass("open"); - e.stopPropagation(); - e.preventDefault(); - }; - - var closePopover = function(e) { - dropdown.removeClass("open"); - } - - var enlargeFontSize = function(e){ - if (fontState.size < 4){ - bookBody.toggleClass("font-size-"+fontState.size); - fontState.size++; - bookBody.toggleClass("font-size-"+fontState.size); - fontState.save(); - } - }; - - var reduceFontSize = function(e){ - if (fontState.size > 0){ - bookBody.toggleClass("font-size-"+fontState.size); - fontState.size--; - bookBody.toggleClass("font-size-"+fontState.size); - fontState.save(); - } - }; - - var changeFontFamily = function(index,el){ - bookBody.toggleClass("font-family-"+fontState.family); - $($(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")")[0]) - .removeClass("active"); - - fontState.family = index; - bookBody.toggleClass("font-family-"+fontState.family); - el.addClass("active"); - fontState.save(); - } - - var changeColorTheme = function(index){ - if (fontState.theme !== 0) - book.removeClass("color-theme-"+fontState.theme); - fontState.theme = index; - if (fontState.theme !== 0) - book.addClass("color-theme-"+fontState.theme); - fontState.save(); - } - - var init = function() { - //Find and save DOM elements. - book = $($(".book")[0]); - toggle = $(book.find(".book-header .toggle-font-settings")[0]); - dropdown = $(book.find("#font-settings-wrapper .dropdown-menu")[0]); - bookBody = $(book.find(".book-body")[0]); - //Instantiate font state object - fontState = storage.get("fontState", {size:1,family:0,theme:0}); - bookBody.addClass("font-size-"+fontState.size); - bookBody.addClass("font-family-"+fontState.family); - $($(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")")[0]) - .addClass("active"); - if(fontState.theme !== 0) - book.addClass("color-theme-"+fontState.theme); - fontState.save = function(){ - storage.set("fontState",fontState); + "jQuery", + "utils/storage" +], function($, storage) { + var fontState; + + var togglePopover = function(e) { + var $dropdown = $("#font-settings-wrapper .dropdown-menu"); + + $dropdown.toggleClass("open"); + e.stopPropagation(); + e.preventDefault(); + }; + + var closePopover = function(e) { + var $dropdown = $("#font-settings-wrapper .dropdown-menu"); + + $dropdown.removeClass("open"); + }; + + var enlargeFontSize = function(e){ + var $bookBody = $(".book-body"); + + if (fontState.size < 4){ + $bookBody.toggleClass("font-size-"+fontState.size, false); + fontState.size++; + + $bookBody.toggleClass("font-size-"+fontState.size, true); + fontState.save(); + } + }; + + var reduceFontSize = function(e){ + var $bookBody = $(".book-body"); + + if (fontState.size > 0){ + $bookBody.toggleClass("font-size-"+fontState.size); + fontState.size--; + + $bookBody.toggleClass("font-size-"+fontState.size); + fontState.save(); + } + }; + + var changeFontFamily = function(){ + var $bookBody = $(".book-body"); + var index = $(this).data("font"); + + $bookBody.toggleClass("font-family-"+fontState.family); + $(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")") + .removeClass("active"); + + fontState.family = index; + $bookBody.toggleClass("font-family-"+fontState.family); + $(this).addClass("active"); + fontState.save(); + }; + + var changeColorTheme = function(){ + var $book = $(".book"); + var index = $(this).data("theme"); + + if (fontState.theme !== 0) + $book.removeClass("color-theme-"+fontState.theme); + + fontState.theme = index; + if (fontState.theme !== 0) + $book.addClass("color-theme-"+fontState.theme); + + fontState.save(); + }; + + var init = function() { + var $toggle, $bookBody, $dropdown, $book; + + //Find DOM elements. + $book = $(".book"); + $toggle = $(".book-header .toggle-font-settings"); + $dropdown = $("#font-settings-wrapper .dropdown-menu"); + $bookBody = $(".book-body"); + + //Instantiate font state object + fontState = storage.get("fontState", { + size:1, + family:0, + theme:0 + }); + fontState.save = function(){ + storage.set("fontState",fontState); + }; + + $bookBody.addClass("font-size-"+fontState.size); + $bookBody.addClass("font-family-"+fontState.family); + + $(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")").addClass("active"); + + if(fontState.theme !== 0) + $book.addClass("color-theme-"+fontState.theme); + + //Add event listeners + $(document).on('click', "#enlarge-font-size", enlargeFontSize); + $(document).on('click', "#reduce-font-size", reduceFontSize); + + $(document).on('click', "#font-settings-wrapper .font-family-list li", changeFontFamily); + $(document).on('click', "#font-settings-wrapper .color-theme-list button", changeColorTheme); + + $(document).on('click', ".book-header .toggle-font-settings", togglePopover); + $(document).on('click', "#font-settings-wrapper .dropdown-menu", function(e){ e.stopPropagation(); }); + $(document).on("click", closePopover); }; - //Add event listeners - $("#enlarge-font-size").on("click", enlargeFontSize); - $("#reduce-font-size").on("click", reduceFontSize); - $(dropdown.find(".font-family-list li")).each(function(i){ - $(this).on("click",function(){changeFontFamily(i,$(this))}); - }); - $(dropdown.find(".color-theme-list button")).each(function(i){ - $(this).on("click",function(){changeColorTheme(i)}); - }); - toggle.on("click", togglePopover); - dropdown.on("click",function(e){e.stopPropagation();}); - $(document).on("click", closePopover); - }; - - return { - init: init - } + + return { + init: init + } });
\ No newline at end of file diff --git a/theme/javascript/core/navigation.js b/theme/javascript/core/navigation.js index 84445b4..8f8b0a6 100755 --- a/theme/javascript/core/navigation.js +++ b/theme/javascript/core/navigation.js @@ -1,21 +1,23 @@ define([ "jQuery", - "utils/analytic", + "core/events", "core/state", "core/search", "core/progress", "core/exercise", "core/quiz" -], function($, analytic, state, search, progress, exercises, quiz) { +], function($, events, state, search, progress, exercises, quiz) { var prev, next; var githubCountStars, githubCountWatch; + var usePushState = !navigator.userAgent.match('CriOS') && (typeof history.pushState !== "undefined"); + var updateHistory = function(url, title) { history.pushState({ path: url }, title, url); }; var handleNavigation = function(url, push) { - if (typeof history.pushState === "undefined") { + if (!usePushState) { // Refresh the page to the new URL if pushState not supported location.href = url; return @@ -23,6 +25,8 @@ define([ return $.get(url) .done(function (html) { + if (push) updateHistory(url, null); + html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; }); @@ -48,9 +52,7 @@ define([ $('.book-summary .summary').scrollTop(scrollPosition); // Update state - state.update($page); - - if (push) updateHistory(url, null); + state.update($("html")); preparePage(); }) .fail(function () { @@ -108,7 +110,7 @@ define([ } // Send to mixpanel - analytic.track("page.view"); + events.trigger("page.change"); }; var handlePagination = function (e) { diff --git a/theme/javascript/core/quiz.js b/theme/javascript/core/quiz.js index bc1a16d..d34af48 100644 --- a/theme/javascript/core/quiz.js +++ b/theme/javascript/core/quiz.js @@ -1,9 +1,9 @@ define([ "jQuery", "utils/execute", - "utils/analytic", + "core/events", "core/state" -], function($, execute, analytic, state){ +], function($, execute, events, state){ // Bind an exercise var prepareQuiz = function($quiz) { @@ -14,14 +14,17 @@ define([ // Submit: test code $quiz.find(".action-submit").click(function(e) { e.preventDefault(); - analytic.track("exercise.submit", {type: "quiz"}); + events.trigger("exercise.submit", {type: "quiz"}); $quiz.find("tr.alert-danger,li.alert-danger").removeClass("alert-danger"); $quiz.find(".alert-success,.alert-danger").addClass("hidden"); $quiz.find(".question").each(function(q) { var result = true; - var $answers = $quiz.find(".question-answers").slice(q).find("input[type=radio], input[type=checkbox]"); - $(this).find("input[type=radio],input[type=checkbox]").each(function(i) { + + var $questions = $(this).find(".question-content").find("input[type=radio], input[type=checkbox]"); + var $answers = $(this).find(".question-answers").find("input[type=radio], input[type=checkbox]"); + + $questions.each(function(i) { var correct = $(this).is(":checked") === $answers.slice(i).first().is(":checked"); result = result && correct; if (!correct) { diff --git a/theme/javascript/app.js b/theme/javascript/gitbook.js index a53fb00..7dcb8d7 100755 --- a/theme/javascript/app.js +++ b/theme/javascript/gitbook.js @@ -1,18 +1,18 @@ -require([ +define([ "jQuery", "utils/storage", - "utils/analytic", "utils/sharing", + "core/events", + "core/font-settings", "core/state", "core/keyboard", "core/navigation", "core/progress", "core/sidebar", - "core/search", - "core/font-settings" -], function($, storage, analytic, sharing, state, keyboard, navigation, progress, sidebar, search, fontSettings){ - $(document).ready(function() { + "core/search" +], function($, storage, sharing, events, fontSettings, state, keyboard, navigation, progress, sidebar, search){ + var start = function(config) { var $book; $book = state.$book; @@ -38,5 +38,12 @@ require([ //Init font settings fontSettings.init(); - }); + + events.trigger("start", config); + } + + return { + start: start, + events: events + }; }); diff --git a/theme/javascript/utils/analytic.js b/theme/javascript/utils/analytic.js deleted file mode 100644 index 6c6155a..0000000 --- a/theme/javascript/utils/analytic.js +++ /dev/null @@ -1,33 +0,0 @@ -define([ - "lodash", - "mixpanel" -], function(_, mixpanel) { - mixpanel.init("01eb2b950ae09a5fdb15a98dcc5ff20e", { - loaded: function() { - track("page.start"); - } - }); - - var isAvailable = function() { - return ( - typeof mixpanel !== "undefined" && - typeof mixpanel.track === "function" - ); - }; - - var track = function(e, data) { - if (!isAvailable()) { - console.warn("tracking not available!"); - return; - } - console.log("track", e); - mixpanel.track(e, _.extend(data || {}, { - 'domain': window.location.host - })); - }; - - return { - isAvailable: isAvailable, - track: track - }; -});
\ No newline at end of file diff --git a/theme/javascript/vendors/mixpanel.js b/theme/javascript/vendors/mixpanel.js deleted file mode 100755 index 763a573..0000000 --- a/theme/javascript/vendors/mixpanel.js +++ /dev/null @@ -1,2 +0,0 @@ -(function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user".split(" "); -for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src=("https:"===e.location.protocol?"https:":"http:")+'//cdn.mxpnl.com/libs/mixpanel-2.2.min.js';f=e.getElementsByTagName("script")[0];f.parentNode.insertBefore(a,f)}})(document,window.mixpanel||[]);
\ No newline at end of file |