@@ -158,15 +158,10 @@ var executeDataAndCode = function () {
158158 }
159159 $ ( '.sidemenu button' ) . removeClass ( 'active' ) ;
160160 $menu . addClass ( 'active' ) ;
161- var requestedTab = getHashValue ( 'tab' ) ;
162- if ( requestedTab ) {
163- if ( requestedTab === 'trace' )
164- $ ( '#btn_trace' ) . click ( ) ;
165- else
166- $ ( '#btn_desc' ) . click ( ) ;
167- } else {
168- $ ( '#btn_desc' ) . click ( ) ;
169- }
161+
162+ var requestedTab = getAlgorithmHash ( 'algorithm' ) [ 'tab' ] ;
163+ if ( requestedTab === 'trace' ) $ ( '#btn_trace' ) . click ( ) ;
164+ else $ ( '#btn_desc' ) . click ( ) ;
170165
171166 $ ( '#category' ) . html ( category_name ) ;
172167 $ ( '#algorithm' ) . html ( algorithm_name ) ;
@@ -257,12 +252,16 @@ var executeDataAndCode = function () {
257252 showDescription ( data ) ;
258253 showFiles ( category , algorithm , files ) ;
259254 } ) ;
255+ var hash = isScratchPaper ( category , algorithm ) ? algorithm : category + '/' + algorithm ;
256+ setHashValue ( 'algorithm' , hash ) ;
260257 } ;
261258 var list = { } ;
262259 var anyOpened = false ;
263260 $ . getJSON ( './algorithm/category.json' , function ( data ) {
264- var requestedCategory = getHashValue ( 'category' ) ,
265- requestedAlgorithm = getHashValue ( 'algorithm' ) ;
261+ var algorithmHash = getAlgorithmHash ( ) ;
262+ console . log ( algorithmHash ) ;
263+ var requestedCategory = algorithmHash [ 'category' ] ,
264+ requestedAlgorithm = algorithmHash [ 'algorithm' ] ;
266265 var anyRequested = requestedCategory && requestedAlgorithm ;
267266 anyOpened = anyRequested ;
268267
@@ -285,9 +284,6 @@ var executeDataAndCode = function () {
285284 . attr ( 'data-algorithm' , algorithm )
286285 . attr ( 'data-category' , category )
287286 . click ( function ( ) {
288- setHashValue ( 'category' , category ) ;
289- setHashValue ( 'algorithm' , algorithm ) ;
290- setHashValue ( 'tab' , 'desc' ) ;
291287 loadAlgorithm ( category , algorithm ) ;
292288 } ) ;
293289 $ ( '#list' ) . append ( $algorithm ) ;
@@ -304,9 +300,6 @@ var executeDataAndCode = function () {
304300 if ( ! list [ requestedCategory ] || ! list [ requestedCategory ] . list [ requestedAlgorithm ] ) {
305301 showErrorToast ( 'Oops! This link appears to be broken.' ) ;
306302 $ ( '#scratch-paper' ) . click ( ) ;
307- removeHashValue ( 'category' ) ;
308- removeHashValue ( 'algorithm' ) ;
309- removeHashValue ( 'tab' ) ;
310303 } else {
311304 $ ( '[data-category="' + requestedCategory + '"]' ) . toggleClass ( 'collapse' ) ;
312305 loadAlgorithm ( requestedCategory , requestedAlgorithm ) ;
@@ -363,6 +356,7 @@ var executeDataAndCode = function () {
363356 $ ( '#btn_share' ) . click ( function ( ) {
364357 var $icon = $ ( this ) . find ( '.fa-share' ) ;
365358 $icon . addClass ( 'fa-spin fa-spin-faster' ) ;
359+
366360 shareScratchPaper ( function ( url ) {
367361 $icon . removeClass ( 'fa-spin fa-spin-faster' ) ;
368362 $ ( '#shared' ) . removeClass ( 'collapse' ) ;
@@ -401,14 +395,16 @@ var executeDataAndCode = function () {
401395 $ ( '#tab_desc' ) . addClass ( 'active' ) ;
402396 $ ( '.tab_bar > button' ) . removeClass ( 'active' ) ;
403397 $ ( this ) . addClass ( 'active' ) ;
404- setHashValue ( 'tab' , 'desc' ) ;
398+ var algorithmHash = getAlgorithmHash ( ) ;
399+ setHashValue ( 'algorithm' , algorithmHash [ 'category' ] + '/' + algorithmHash [ 'algorithm' ] ) ;
405400 } ) ;
406401 $ ( '#btn_trace' ) . click ( function ( ) {
407402 $ ( '.tab_container > .tab' ) . removeClass ( 'active' ) ;
408403 $ ( '#tab_module' ) . addClass ( 'active' ) ;
409404 $ ( '.tab_bar > button' ) . removeClass ( 'active' ) ;
410405 $ ( this ) . addClass ( 'active' ) ;
411- setHashValue ( 'tab' , 'trace' ) ;
406+ var algorithmHash = getAlgorithmHash ( ) ;
407+ setHashValue ( 'algorithm' , algorithmHash [ 'category' ] + '/' + algorithmHash [ 'algorithm' ] + '/trace' ) ;
412408 } ) ;
413409
414410 $ ( window ) . resize ( function ( ) {
@@ -519,6 +515,7 @@ var executeDataAndCode = function () {
519515 }
520516 return null ;
521517 }
518+
522519 var setHashValue = function ( key , value ) {
523520 if ( ! key || ! value ) return ;
524521 var hash = window . location . hash . substr ( 1 ) ;
@@ -540,6 +537,7 @@ var executeDataAndCode = function () {
540537 var newHash = params . join ( '&' ) ;
541538 window . location . hash = '#' + newHash ;
542539 }
540+
543541 var removeHashValue = function ( key ) {
544542 if ( ! key ) return ;
545543 var hash = window . location . hash . substr ( 1 ) ;
@@ -556,6 +554,23 @@ var executeDataAndCode = function () {
556554 var newHash = params . join ( '&' ) ;
557555 window . location . hash = '#' + newHash ;
558556 }
557+
558+ var getAlgorithmHash = function ( ) {
559+ var hash = getHashValue ( 'algorithm' ) ;
560+ if ( hash ) {
561+ var regex = / (?: [ ^ \/ \\ ] + | \\ .) + / g;
562+ var tmp = null , algorithmHash = { } , i = 0 ;
563+ while ( tmp = regex . exec ( hash ) ) {
564+ if ( i === 0 ) algorithmHash [ 'category' ] = tmp [ 0 ] ;
565+ if ( i === 1 ) algorithmHash [ 'algorithm' ] = tmp [ 0 ] ;
566+ if ( i === 2 ) algorithmHash [ 'tab' ] = tmp [ 0 ] ;
567+ i ++ ;
568+ }
569+ return algorithmHash ;
570+ } else
571+ return false ;
572+ }
573+
559574// Share scratch paper
560575
561576 var getParameterByName = function ( name ) {
@@ -579,7 +594,7 @@ var executeDataAndCode = function () {
579594 } ;
580595 $ . post ( 'https://api.github.com/gists' , JSON . stringify ( gist ) , function ( res ) {
581596 var data = JSON . parse ( res ) ;
582- if ( callback ) callback ( location . protocol + '//' + location . host + location . pathname + '? scratch-paper=' + data . id ) ;
597+ if ( callback ) callback ( location . protocol + '//' + location . host + location . pathname + '# scratch-paper=' + data . id ) ;
583598 } ) ;
584599 } ;
585600
@@ -599,7 +614,7 @@ var executeDataAndCode = function () {
599614 } ) ;
600615 } ;
601616
602- var gistID = getParameterByName ( 'scratch-paper' ) ;
617+ var gistID = getHashValue ( 'scratch-paper' ) ;
603618 if ( gistID ) {
604619 loadScratchPaper ( gistID ) ;
605620 }
0 commit comments