@@ -158,7 +158,10 @@ var executeDataAndCode = function () {
158158 }
159159 $ ( '.sidemenu button' ) . removeClass ( 'active' ) ;
160160 $menu . addClass ( 'active' ) ;
161- $ ( '#btn_desc' ) . click ( ) ;
161+
162+ var requestedTab = getAlgorithmHash ( 'algorithm' ) [ 'tab' ] ;
163+ if ( requestedTab === 'trace' ) $ ( '#btn_trace' ) . click ( ) ;
164+ else $ ( '#btn_desc' ) . click ( ) ;
162165
163166 $ ( '#category' ) . html ( category_name ) ;
164167 $ ( '#algorithm' ) . html ( algorithm_name ) ;
@@ -249,10 +252,19 @@ var executeDataAndCode = function () {
249252 showDescription ( data ) ;
250253 showFiles ( category , algorithm , files ) ;
251254 } ) ;
255+ var hash = isScratchPaper ( category , algorithm ) ? algorithm : category + '/' + algorithm ;
256+ setHashValue ( 'algorithm' , hash ) ;
252257 } ;
253258 var list = { } ;
254259 var anyOpened = false ;
255260 $ . getJSON ( './algorithm/category.json' , function ( data ) {
261+ var algorithmHash = getAlgorithmHash ( ) ;
262+ console . log ( algorithmHash ) ;
263+ var requestedCategory = algorithmHash [ 'category' ] ,
264+ requestedAlgorithm = algorithmHash [ 'algorithm' ] ;
265+ var anyRequested = requestedCategory && requestedAlgorithm ;
266+ anyOpened = anyRequested ;
267+
256268 list = data ;
257269 for ( var category in list ) {
258270 ( function ( category ) {
@@ -283,6 +295,16 @@ var executeDataAndCode = function () {
283295 }
284296 } ) ( category ) ;
285297 }
298+
299+ if ( anyRequested ) {
300+ if ( ! list [ requestedCategory ] || ! list [ requestedCategory ] . list [ requestedAlgorithm ] ) {
301+ showErrorToast ( 'Oops! This link appears to be broken.' ) ;
302+ $ ( '#scratch-paper' ) . click ( ) ;
303+ } else {
304+ $ ( '[data-category="' + requestedCategory + '"]' ) . toggleClass ( 'collapse' ) ;
305+ loadAlgorithm ( requestedCategory , requestedAlgorithm ) ;
306+ }
307+ }
286308 } ) ;
287309 $ ( '#powered-by' ) . click ( function ( ) {
288310 $ ( '#powered-by-list button' ) . toggleClass ( 'collapse' ) ;
@@ -334,6 +356,7 @@ var executeDataAndCode = function () {
334356 $ ( '#btn_share' ) . click ( function ( ) {
335357 var $icon = $ ( this ) . find ( '.fa-share' ) ;
336358 $icon . addClass ( 'fa-spin fa-spin-faster' ) ;
359+
337360 shareScratchPaper ( function ( url ) {
338361 $icon . removeClass ( 'fa-spin fa-spin-faster' ) ;
339362 $ ( '#shared' ) . removeClass ( 'collapse' ) ;
@@ -372,12 +395,16 @@ var executeDataAndCode = function () {
372395 $ ( '#tab_desc' ) . addClass ( 'active' ) ;
373396 $ ( '.tab_bar > button' ) . removeClass ( 'active' ) ;
374397 $ ( this ) . addClass ( 'active' ) ;
398+ var algorithmHash = getAlgorithmHash ( ) ;
399+ setHashValue ( 'algorithm' , algorithmHash [ 'category' ] + '/' + algorithmHash [ 'algorithm' ] ) ;
375400 } ) ;
376401 $ ( '#btn_trace' ) . click ( function ( ) {
377402 $ ( '.tab_container > .tab' ) . removeClass ( 'active' ) ;
378403 $ ( '#tab_module' ) . addClass ( 'active' ) ;
379404 $ ( '.tab_bar > button' ) . removeClass ( 'active' ) ;
380405 $ ( this ) . addClass ( 'active' ) ;
406+ var algorithmHash = getAlgorithmHash ( ) ;
407+ setHashValue ( 'algorithm' , algorithmHash [ 'category' ] + '/' + algorithmHash [ 'algorithm' ] + '/trace' ) ;
381408 } ) ;
382409
383410 $ ( window ) . resize ( function ( ) {
@@ -476,6 +503,74 @@ var executeDataAndCode = function () {
476503 tracerManager . findOwner ( this ) . mousewheel ( e ) ;
477504 } ) ;
478505
506+ var getHashValue = function ( key ) {
507+ if ( ! key ) return null ;
508+ var hash = window . location . hash . substr ( 1 ) ;
509+ var params = hash ? hash . split ( '&' ) : [ ] ;
510+ for ( var i = 0 ; i < params . length ; i ++ ) {
511+ var pair = params [ i ] . split ( '=' ) ;
512+ if ( pair [ 0 ] === key ) {
513+ return pair [ 1 ] ;
514+ }
515+ }
516+ return null ;
517+ }
518+
519+ var setHashValue = function ( key , value ) {
520+ if ( ! key || ! value ) return ;
521+ var hash = window . location . hash . substr ( 1 ) ;
522+ var params = hash ? hash . split ( '&' ) : [ ] ;
523+
524+ var found = false ;
525+ for ( var i = 0 ; i < params . length && ! found ; i ++ ) {
526+ var pair = params [ i ] . split ( '=' ) ;
527+ if ( pair [ 0 ] === key ) {
528+ pair [ 1 ] = value ;
529+ params [ i ] = pair . join ( '=' ) ;
530+ found = true ;
531+ }
532+ }
533+ if ( ! found ) {
534+ params . push ( [ key , value ] . join ( '=' ) ) ;
535+ }
536+
537+ var newHash = params . join ( '&' ) ;
538+ window . location . hash = '#' + newHash ;
539+ }
540+
541+ var removeHashValue = function ( key ) {
542+ if ( ! key ) return ;
543+ var hash = window . location . hash . substr ( 1 ) ;
544+ var params = hash ? hash . split ( '&' ) : [ ] ;
545+
546+ for ( var i = 0 ; i < params . length ; i ++ ) {
547+ var pair = params [ i ] . split ( '=' ) ;
548+ if ( pair [ 0 ] === key ) {
549+ params . splice ( i , 1 ) ;
550+ break ;
551+ }
552+ }
553+
554+ var newHash = params . join ( '&' ) ;
555+ window . location . hash = '#' + newHash ;
556+ }
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+
479574// Share scratch paper
480575
481576 var getParameterByName = function ( name ) {
@@ -499,7 +594,7 @@ var executeDataAndCode = function () {
499594 } ;
500595 $ . post ( 'https://api.github.com/gists' , JSON . stringify ( gist ) , function ( res ) {
501596 var data = JSON . parse ( res ) ;
502- 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 ) ;
503598 } ) ;
504599 } ;
505600
@@ -519,8 +614,8 @@ var executeDataAndCode = function () {
519614 } ) ;
520615 } ;
521616
522- var gistID = getParameterByName ( 'scratch-paper' ) ;
617+ var gistID = getHashValue ( 'scratch-paper' ) ;
523618 if ( gistID ) {
524619 loadScratchPaper ( gistID ) ;
525620 }
526- } ) ( ) ;
621+ } ) ( ) ;
0 commit comments