@@ -453,18 +453,14 @@ <h5 class="variable-wrap" data-id="{id}" data-repo="{repo}" data-url="{url}" dat
453453 var langs = localStorage . get ( persistLangsName ) , langQuery ;
454454 var page = 0 ;
455455 var lastVal ;
456- var lastCodeId ;
456+ var cacheSourceCodes = { } ;
457457
458458 genLangQuery ( langs ) ;
459459
460460 this . resetPage = function ( ) {
461461 page = 0 ;
462462 }
463463
464- this . getPage = function ( ) {
465- return page ;
466- }
467-
468464 this . setLang = function ( val ) {
469465 langs = val || null ;
470466 genLangQuery ( val ) ;
@@ -513,12 +509,16 @@ <h5 class="variable-wrap" data-id="{id}" data-repo="{repo}" data-url="{url}" dat
513509
514510 //get source code by id
515511 this . requestSourceCode = function ( id , callback ) {
516- lastCodeId = id ;
517- lastCodeId && $ . ajax ( {
512+ if ( cacheSourceCodes [ id ] ) {
513+ callback && callback ( cacheSourceCodes [ id ] ) ;
514+ return ;
515+ }
516+ id && $ . ajax ( {
518517 type : 'GET' ,
519518 dataType : 'json' ,
520- url : 'https://searchcode.com/api/result/' + lastCodeId + '/' ,
519+ url : 'https://searchcode.com/api/result/' + id + '/' ,
521520 success : function ( data ) {
521+ cacheSourceCodes [ id ] = data ;
522522 callback && callback ( data ) ;
523523 }
524524 } ) ;
@@ -633,17 +633,19 @@ <h5 class="variable-wrap" data-id="{id}" data-repo="{repo}" data-url="{url}" dat
633633 } ) ;
634634 els . body . on ( 'click' , '.js-variable-button__code' , showSourceCode ) ;
635635 els . body . on ( 'click' , removeVariableMenus ) ;
636+ els . sourceCodeModal . on ( 'hidden.bs.modal' , function ( ) {
637+ els . sourceCodeContent . text ( '' ) ;
638+ } ) ;
636639 }
637640 function init ( ) {
638641 bindEvent ( ) ;
639642 renderTitle ( ) ;
640643 renderLangMunu ( ) ;
641644 onLocationHashChanged ( ) ;
642645 renderAnalytics ( ) ;
643- // hljs.highlightBlock(els.sourceCodeContent[0]);
644646 }
645647
646- function showSourceCode ( e ) {
648+ function showSourceCode ( ) {
647649 searchcode . requestSourceCode ( this . dataset . id , renderSourceCode ) ;
648650 els . sourceCodeModal . modal ( 'toggle' ) ;
649651 }
0 commit comments