diff --git a/js/module/array1d.js b/js/module/array1d.js index 2fea160713866f880fb3d36af1cae03313a7e0f2..3ffc981ec46d6eeb56da38c2ec562b1074e15fb4 100644 --- a/js/module/array1d.js +++ b/js/module/array1d.js @@ -28,6 +28,22 @@ Array1DTracer.prototype = $.extend(true, Object.create(Array2DTracer.prototype), } return this; }, + _separate: function (idx) { + this.manager.pushStep(this.capsule, { + type: 'separate', + x: 0, + y: idx + }); + return this; + }, + _deseparate: function (idx) { + this.manager.pushStep(this.capsule, { + type: 'deseparate', + x: 0, + y: idx + }); + return this; + }, setData: function (D) { return Array2DTracer.prototype.setData.call(this, [D]); } diff --git a/js/module/array2d.js b/js/module/array2d.js index 1054ce15c1d7ec919f2b814e636b026a3cb60310..869781d6ca992d5dcbd65b6cb32631981b02e981 100644 --- a/js/module/array2d.js +++ b/js/module/array2d.js @@ -130,7 +130,7 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), { processStep: function (step, options) { switch (step.type) { case 'notify': - if (step.v) { + if (step.v === 0 || step.v) { var $row = this.$table.find('.mtbl-row').eq(step.x); var $col = $row.find('.mtbl-col').eq(step.y); $col.text(TracerUtil.refineByType(step.v)); diff --git a/js/script.js b/js/script.js index 8a8be2a55d2484ae91c3a8bcaf7ffee29906af0f..282b87395288c53a514626e27167d8be5b5e75b0 100644 --- a/js/script.js +++ b/js/script.js @@ -158,7 +158,10 @@ var executeDataAndCode = function () { } $('.sidemenu button').removeClass('active'); $menu.addClass('active'); - $('#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); @@ -249,10 +252,19 @@ 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 algorithmHash = getAlgorithmHash(); + console.log(algorithmHash); + var requestedCategory = algorithmHash['category'], + requestedAlgorithm = algorithmHash['algorithm']; + var anyRequested = requestedCategory && requestedAlgorithm; + anyOpened = anyRequested; + list = data; for (var category in list) { (function (category) { @@ -283,6 +295,16 @@ var executeDataAndCode = function () { } })(category); } + + if(anyRequested) { + if(!list[requestedCategory] || !list[requestedCategory].list[requestedAlgorithm]) { + showErrorToast('Oops! This link appears to be broken.'); + $('#scratch-paper').click(); + } else { + $('[data-category="' + requestedCategory + '"]').toggleClass('collapse'); + loadAlgorithm(requestedCategory, requestedAlgorithm); + } + } }); $('#powered-by').click(function () { $('#powered-by-list button').toggleClass('collapse'); @@ -334,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'); @@ -372,12 +395,16 @@ var executeDataAndCode = function () { $('#tab_desc').addClass('active'); $('.tab_bar > button').removeClass('active'); $(this).addClass('active'); + 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'); + var algorithmHash = getAlgorithmHash(); + setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm'] + '/trace'); }); $(window).resize(function () { @@ -476,6 +503,74 @@ var executeDataAndCode = function () { tracerManager.findOwner(this).mousewheel(e); }); + var getHashValue = function (key) { + if(!key) return null; + var hash = window.location.hash.substr(1); + var params = hash ? hash.split('&') : []; + for(var i = 0; i < params.length; i++) { + var pair = params[i].split('='); + if(pair[0] === key) { + return pair[1]; + } + } + return null; + } + + var setHashValue = function (key, value) { + if(!key || !value) return; + var hash = window.location.hash.substr(1); + var params = hash ? hash.split('&') : []; + + var found = false; + for(var i = 0; i < params.length && !found; i++) { + var pair = params[i].split('='); + if(pair[0] === key) { + pair[1] = value; + params[i] = pair.join('='); + found = true; + } + } + if(!found) { + params.push([key, value].join('=')); + } + + var newHash = params.join('&'); + window.location.hash = '#' + newHash; + } + + var removeHashValue = function (key) { + if(!key) return; + var hash = window.location.hash.substr(1); + var params = hash ? hash.split('&') : []; + + for(var i = 0; i < params.length; i++) { + var pair = params[i].split('='); + if(pair[0] === key) { + params.splice(i, 1); + break; + } + } + + 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) { @@ -499,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); }); }; @@ -519,8 +614,8 @@ var executeDataAndCode = function () { }); }; - var gistID = getParameterByName('scratch-paper'); + var gistID = getHashValue('scratch-paper'); if (gistID) { loadScratchPaper(gistID); } -})(); \ No newline at end of file +})();