diff --git a/js/script.js b/js/script.js index 5859c8db3b00396808864e2da596353b97adbc23..282b87395288c53a514626e27167d8be5b5e75b0 100644 --- a/js/script.js +++ b/js/script.js @@ -158,15 +158,10 @@ var executeDataAndCode = function () { } $('.sidemenu button').removeClass('active'); $menu.addClass('active'); - var requestedTab = getHashValue('tab'); - if(requestedTab) { - if(requestedTab === 'trace') - $('#btn_trace').click(); - else - $('#btn_desc').click(); - } else { - $('#btn_desc').click(); - } + + var requestedTab = getAlgorithmHash('algorithm')['tab']; + if(requestedTab === 'trace') $('#btn_trace').click(); + else $('#btn_desc').click(); $('#category').html(category_name); $('#algorithm').html(algorithm_name); @@ -257,12 +252,16 @@ var executeDataAndCode = function () { showDescription(data); showFiles(category, algorithm, files); }); + var hash = isScratchPaper(category, algorithm) ? algorithm : category + '/' + algorithm; + setHashValue('algorithm', hash); }; var list = {}; var anyOpened = false; $.getJSON('./algorithm/category.json', function (data) { - var requestedCategory = getHashValue('category'), - requestedAlgorithm = getHashValue('algorithm'); + var algorithmHash = getAlgorithmHash(); + console.log(algorithmHash); + var requestedCategory = algorithmHash['category'], + requestedAlgorithm = algorithmHash['algorithm']; var anyRequested = requestedCategory && requestedAlgorithm; anyOpened = anyRequested; @@ -285,9 +284,6 @@ var executeDataAndCode = function () { .attr('data-algorithm', algorithm) .attr('data-category', category) .click(function () { - setHashValue('category', category); - setHashValue('algorithm', algorithm); - setHashValue('tab', 'desc'); loadAlgorithm(category, algorithm); }); $('#list').append($algorithm); @@ -304,9 +300,6 @@ var executeDataAndCode = function () { if(!list[requestedCategory] || !list[requestedCategory].list[requestedAlgorithm]) { showErrorToast('Oops! This link appears to be broken.'); $('#scratch-paper').click(); - removeHashValue('category'); - removeHashValue('algorithm'); - removeHashValue('tab'); } else { $('[data-category="' + requestedCategory + '"]').toggleClass('collapse'); loadAlgorithm(requestedCategory, requestedAlgorithm); @@ -363,6 +356,7 @@ var executeDataAndCode = function () { $('#btn_share').click(function () { var $icon = $(this).find('.fa-share'); $icon.addClass('fa-spin fa-spin-faster'); + shareScratchPaper(function (url) { $icon.removeClass('fa-spin fa-spin-faster'); $('#shared').removeClass('collapse'); @@ -401,14 +395,16 @@ var executeDataAndCode = function () { $('#tab_desc').addClass('active'); $('.tab_bar > button').removeClass('active'); $(this).addClass('active'); - setHashValue('tab', 'desc'); + var algorithmHash = getAlgorithmHash(); + setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm']); }); $('#btn_trace').click(function () { $('.tab_container > .tab').removeClass('active'); $('#tab_module').addClass('active'); $('.tab_bar > button').removeClass('active'); $(this).addClass('active'); - setHashValue('tab', 'trace'); + var algorithmHash = getAlgorithmHash(); + setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm'] + '/trace'); }); $(window).resize(function () { @@ -519,6 +515,7 @@ var executeDataAndCode = function () { } return null; } + var setHashValue = function (key, value) { if(!key || !value) return; var hash = window.location.hash.substr(1); @@ -540,6 +537,7 @@ var executeDataAndCode = function () { var newHash = params.join('&'); window.location.hash = '#' + newHash; } + var removeHashValue = function (key) { if(!key) return; var hash = window.location.hash.substr(1); @@ -556,6 +554,23 @@ var executeDataAndCode = function () { var newHash = params.join('&'); window.location.hash = '#' + newHash; } + + var getAlgorithmHash = function () { + var hash = getHashValue('algorithm'); + if(hash){ + var regex = /(?:[^\/\\]+|\\.)+/g; + var tmp = null, algorithmHash = {}, i = 0; + while(tmp = regex.exec(hash)){ + if(i === 0) algorithmHash['category'] = tmp[0]; + if(i === 1) algorithmHash['algorithm'] = tmp[0]; + if(i === 2) algorithmHash['tab'] = tmp[0]; + i++; + } + return algorithmHash; + } else + return false; + } + // Share scratch paper var getParameterByName = function (name) { @@ -579,7 +594,7 @@ var executeDataAndCode = function () { }; $.post('https://api.github.com/gists', JSON.stringify(gist), function (res) { var data = JSON.parse(res); - if (callback) callback(location.protocol + '//' + location.host + location.pathname + '?scratch-paper=' + data.id); + if (callback) callback(location.protocol + '//' + location.host + location.pathname + '#scratch-paper=' + data.id); }); }; @@ -599,7 +614,7 @@ var executeDataAndCode = function () { }); }; - var gistID = getParameterByName('scratch-paper'); + var gistID = getHashValue('scratch-paper'); if (gistID) { loadScratchPaper(gistID); }