1111 < link rel ="apple-touch-icon " sizes ="144x144 " href ="resources/images/codelf_logo.png ">
1212 < link rel ="apple-touch-icon-precomposed " href ="resources/images/codelf_logo.png ">
1313 < link rel ="stylesheet " href ="resources/css/bootstrap.min.css ">
14+ < link rel ="stylesheet " href ="resources/css/monokai-sublime.css ">
1415 < style >
1516 html , body {
1617 -webkit-font-smoothing : antialiased;
156157 overflow-x : scroll;
157158 -webkit-overflow-scrolling : touch;
158159 }
159- . search-result . label-warp {
160+ . search-result . variable-wrap {
160161 display : inline-block;
161162 }
162163 . search-result a : hover {
178179 max-width : 100% ;
179180 height : auto;
180181 }
181-
182+ . popover--variable {
183+ background-color : transparent;
184+ border-left : 0 ;
185+ border-right : 0 ;
186+ border-top : 0 ;
187+ border-color : # ccc;
188+ }
189+ . popover--variable . popover-content {
190+ padding : 0 ;
191+ }
192+ . popover--variable . btn {
193+ border-bottom : 0 ;
194+ }
182195 @media ( min-width : 62em ) {
183196 . main-container {
184197 padding : 20px 150px ;
@@ -271,20 +284,41 @@ <h5><span class="label label-pill label-success">real-world usage</span> <span c
271284 < img src ="resources/images/twohardtings.jpg ">
272285 </ blockquote >
273286 < script type ="text/html ">
274- < h5 class = "label-warp" > < a class = "label label-{type }" data - toggle = "tooltip " data - placement = "top" title = "{val }" href = "# {val}"> { val} < / a > < / h5 >
287+ < h5 class = "variable-wrap" data - id = "{id}" data - repo = "{repo }" data - url = "{url} " data - val = "{val}" > < span class = "label label-{label_type }" title = " {val}"> { val} < / s p a n > < / h5 >
275288 </ script >
276289 </ section >
277290 </ div >
278291 </ div >
279292 </ section >
280293</ section >
281- < a href =""> </ a >
294+ < div class ="modal fade js-sourcecode-modal " tabindex ="-1 " role ="dialog " aria-labelledby ="sourcecodeModal " aria-hidden ="true ">
295+ < div class ="modal-dialog modal-lg ">
296+ < div class ="modal-content ">
297+ < div class ="modal-header ">
298+ < button type ="button " class ="close " data-dismiss ="modal " aria-label ="Close ">
299+ < span aria-hidden ="true "> ×</ span >
300+ </ button >
301+ </ div >
302+ < div class ="modal-body ">
303+ < pre > < code > </ code > </ pre >
304+ </ div >
305+ </ div >
306+ </ div >
307+ </ div >
308+ < script type ="text/html " template ="variableMenu ">
309+ < div class = "btn-group" role = "group" >
310+ < button type = "button" class = "btn btn-secondary js-variable-button__code " data-id = "{id}" > Codes</ button >
311+ < button type = "button" class = "btn btn-secondary" > < a href = "#{val}" > Search</ a > < / b u t t o n >
312+ < button type = "button" class = "btn btn-secondary" > < a href = "{repo}" target = "_blank" > Repo</ a > < / b u t t o n >
313+ </div >
314+ </ script >
282315<!-- http://tholman.com/github-corners/ -->
283316< a href ="https://github.com/unbug/codelf " class ="github-corner " target ="_blank " title ="Fork me on GitHub "> < svg width ="80 " height ="80 " viewBox ="0 0 250 250 " style ="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0; "> < path d ="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z "> </ path > < path d ="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2 " fill ="currentColor " style ="transform-origin: 130px 106px; " class ="octo-arm "> </ path > < path d ="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z " fill ="currentColor " class ="octo-body "> </ path > </ svg > </ a >
284317< style > . github-corner svg { transition : all 1s ease-in-out; } . github-corner : hover . octo-arm { animation : octocat-wave 560ms ease-in-out} @keyframes octocat-wave{ 0% , 100% { transform : rotate ( 0deg ) } 20% , 60% { transform : rotate ( -25deg ) } 40% , 80% { transform : rotate ( 10deg ) } } @media ( max-width : 500px ) { . github-corner : hover . octo-arm { animation : none} . github-corner . octo-arm { animation : octocat-wave 560ms ease-in-out} } </ style >
285318< script src ="src/lib/tether/js/tether.min.js "> </ script >
286319< script src ="src/lib/jquery.min.js "> </ script >
287320< script src ="src/lib/bootstrap.min.js "> </ script >
321+ < script src ="src/lib/highlight.pack.js "> </ script >
288322< script >
289323 $ ( function ( ) {
290324 //utils
@@ -415,11 +449,11 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
415449 //http://githut.info/
416450 var topProgramLan = [ { "id" :"22,106" , "language" :"JavaScript, CoffeeScript" } , { "id" :"133,135" , "language" :"CSS" } , { "id" :"3,39" , "language" :"HTML" } , { "id" :137 , "language" :"Swift" } , { "id" :35 , "language" :"Objective-C" } , { "id" :23 , "language" :"Java" } , { "id" :19 , "language" :"Python" } , { "id" :24 , "language" :"PHP" } , { "id" :32 , "language" :"Ruby" } , { "id" :28 , "language" :"C" } , { "id" :16 , "language" :"C++" } , { "id" :6 , "language" :"C#" } , { "id" :55 , "language" :"Go" } , { "id" :51 , "language" :"Perl" } , { "id" :"104,109" , "language" :"Clojure, ClojureScript" } , { "id" :40 , "language" :"Haskell" } , { "id" :54 , "language" :"Lua" } , { "id" :20 , "language" :"Matlab" } , { "id" :144 , "language" :"R" } , { "id" :47 , "language" :"Scala" } , { "id" :"69,78,146" , "language" :"Shell" } , { "id" :29 , "language" :"Lisp" } , { "id" :42 , "language" :"ActionScript" } ] ;
417451 var searchcode = new function ( ) {
418- var persistenceLangsName = 'codelf_langs_selected' ;
419- var langs = localStorage . get ( persistenceLangsName ) , langQuery ;
452+ var persistLangsName = 'codelf_langs_selected' ;
453+ var langs = localStorage . get ( persistLangsName ) , langQuery ;
420454 var page = 0 ;
421455 var lastVal ;
422- var searchRequestCallback ;
456+ var lastCodeId ;
423457
424458 genLangQuery ( langs ) ;
425459
@@ -435,7 +469,7 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
435469 langs = val || null ;
436470 genLangQuery ( val ) ;
437471 this . resetPage ( ) ;
438- localStorage [ langs ?'set' :'del' ] ( persistenceLangsName , langs ) ;
472+ localStorage [ langs ?'set' :'del' ] ( persistLangsName , langs ) ;
439473 }
440474
441475 this . getLang = function ( ) {
@@ -455,32 +489,36 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
455489 }
456490 }
457491
492+ //search code by query
458493 this . request = function ( val , callback ) {
459494 if ( val != lastVal ) {
460495 this . resetPage ( ) ;
461496 }
462497 lastVal = val ;
463- searchRequestCallback = callback ;
464- lastVal && $ . ajax ( {
465- type : 'GET' ,
466- url : 'https://searchcode.com/api/jsonp_codesearch_I/' + ( langQuery ?( '?' + langQuery ) :'' ) ,
467- dataType : 'jsonp' ,
468- jsonp : false ,
469- jsonpCallback : false ,
470- contentType : "application/json" ,
471- data : {
498+ lastVal && $ . get (
499+ 'https://searchcode.com/api/codesearch_I/' + ( langQuery ?( '?' + langQuery ) :'' ) ,
500+ {
472501 q : lastVal ,
473502 p : page ,
474- per_page : 100 ,
475- callback : 'afterSearchRequest'
503+ per_page : 100
504+ } ,
505+ function ( data ) {
506+ callback && callback ( data , page ) ;
507+ page ++ ;
476508 }
477- } ) ;
478- }
479- window . afterSearchRequest = function ( data ) {
480- if ( data ) {
481- searchRequestCallback && searchRequestCallback ( data , page ) ;
482- page ++ ;
483- }
509+ )
510+ } ;
511+
512+ //get source code by id
513+ this . requestSourceCode = function ( id , callback ) {
514+ lastCodeId = id ;
515+ lastCodeId && $ . get (
516+ 'https://searchcode.com/api/result/' + lastCodeId + '/' ,
517+ { } ,
518+ function ( data ) {
519+ callback && callback ( data ) ;
520+ }
521+ )
484522 }
485523 } ;
486524
@@ -541,6 +579,11 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
541579 searchResultHd : $ ( '.search-result .hd' ) ,
542580 searchResultBd : $ ( '.search-result .bd' ) ,
543581
582+ variableMenuTpl : $ ( 'script[template="variableMenu"]' ) . html ( ) ,
583+
584+ sourceCodeModal : $ ( '.js-sourcecode-modal' ) ,
585+ sourceCodeContent : $ ( '.js-sourcecode-modal pre code' ) ,
586+
544587 githubCorner : $ ( '.github-corner svg' ) ,
545588
546589 lastVal : ''
@@ -562,16 +605,44 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
562605 return false ;
563606 }
564607 } ) ;
565- els . searchResultBd . on ( 'mouseenter touchstart' , 'a' , function ( ) {
566- $ ( this ) . select ( ) ;
608+ els . searchResultBd . on ( 'click mouseenter' , '.variable-wrap' , function ( e ) {
609+ e . preventDefault ( ) ;
610+ e . stopPropagation ( ) ;
611+ removeVariableMenus ( ) ;
612+ $ ( this ) . popover ( {
613+ trigger : 'manual' ,
614+ html : true ,
615+ placement : 'top' ,
616+ offset : '-8 0' ,
617+ title : function ( ) {
618+ return false ;
619+ } ,
620+ content : function ( ) {
621+ return els . variableMenuTpl
622+ . replace ( '{id}' , this . dataset . id )
623+ . replace ( '{val}' , this . dataset . val )
624+ . replace ( '{repo}' , this . dataset . repo ) ;
625+ } ,
626+ template : '<div class="popover popover--variable" role="tooltip"><div class="popover-arrow"></div><div class="popover-content"></div></div>'
627+ } ) ;
628+ $ ( this ) . popover ( 'show' ) ;
629+ return false ;
567630 } ) ;
631+ els . body . on ( 'click' , '.js-variable-button__code' , showSourceCode ) ;
632+ els . body . on ( 'click' , removeVariableMenus ) ;
568633 }
569634 function init ( ) {
570635 bindEvent ( ) ;
571636 renderTitle ( ) ;
572637 renderLangMunu ( ) ;
573638 onLocationHashChanged ( ) ;
574639 renderAnalytics ( ) ;
640+ hljs . highlightBlock ( els . sourceCodeContent [ 0 ] ) ;
641+ }
642+
643+ function showSourceCode ( ) {
644+ searchcode . requestSourceCode ( this . dataset . id , renderSourceCode ) ;
645+ els . sourceCodeModal . modal ( 'toggle' ) ;
575646 }
576647
577648 function onLocationHashChanged ( e ) {
@@ -594,7 +665,7 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
594665 }
595666 function onSearch ( val ) {
596667 els . searchInput . blur ( ) ;
597- //els.body.find('.tooltip').remove ();
668+ removeVariableMenus ( ) ;
598669 if ( val && val == els . lastInputVal ) { return ; }
599670 val = val || els . searchInput . val ( ) . trim ( ) ;
600671 els . searchInput . val ( val ) ;
@@ -687,31 +758,41 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
687758 }
688759
689760 function renderSearchResult ( data , page ) {
690- var vals = [ ] , labels = [ ] , allStr = [ ] ;
761+ var vals = [ ] , labels = [ ] , lineStr ;
691762 data . results . forEach ( function ( rkey ) {
763+ //filter codes
764+ lineStr = [ ] ;
692765 for ( var lkey in rkey . lines ) {
693766 var lstr = rkey . lines [ lkey ] ;
694- ! ( / ; b a s e 6 4 , / g. test ( lstr ) && lstr . length > 256 ) //no base64
695- && allStr . push ( lstr ) ;
767+ if ( ! ( / ; b a s e 6 4 , / g. test ( lstr ) && lstr . length > 256 ) ) { //no base64
768+ lineStr . push ( lstr ) ;
769+ }
696770 }
697- } ) ;
698- allStr = allStr . join ( '' ) . replace ( / \r \n / g , ' ' ) ;
699- els . valRegs . forEach ( function ( key ) {
700- $ . each ( allStr . match ( key ) || [ ] , function ( i , el ) {
701- if (
702- ! / \/ / g. test ( el ) //exclude links
703- && $ . inArray ( el , vals ) === - 1
704- && $ . inArray ( el . toLowerCase ( ) , vals ) === - 1
705- && $ . inArray ( el . toUpperCase ( ) , vals ) === - 1
706- && el . length < 64 //too long
771+ lineStr = lineStr . join ( '' ) . replace ( / \r \n / g , ' ' ) ;
772+ //match variables
773+ els . valRegs . forEach ( function ( key ) {
774+ $ . each ( lineStr . match ( key ) || [ ] , function ( i , el ) {
775+ if (
776+ ! / \/ / g. test ( el ) //exclude links
777+ && $ . inArray ( el , vals ) === - 1
778+ && $ . inArray ( el . toLowerCase ( ) , vals ) === - 1
779+ && $ . inArray ( el . toUpperCase ( ) , vals ) === - 1
780+ && el . length < 64 //too long
707781 ) {
708- vals . push ( el )
709- } ;
782+ vals . push ( el ) ;
783+ //render variable labels
784+ labels . push ( els . searchResultTpl
785+ . replace ( '{label_type}' , getRandomLabelType ( ) )
786+ . replace ( / \{ v a l \} / g, el )
787+ . replace ( '{id}' , rkey . id )
788+ . replace ( '{url}' , rkey . url )
789+ . replace ( '{repo}' , rkey . repo )
790+ ) ;
791+ } ;
792+ } ) ;
710793 } ) ;
711794 } ) ;
712- vals . forEach ( function ( key ) {
713- labels . push ( els . searchResultTpl . replace ( '{type}' , getRandomLabelType ( ) ) . replace ( / \{ v a l \} / g, key ) ) ;
714- } ) ;
795+
715796 if ( labels . length ) {
716797 var blockquote = els . searchResultBd . find ( '.blockquote' ) ;
717798 if ( blockquote [ 0 ] ) {
@@ -747,7 +828,6 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
747828 els . searchBtn . tooltip ( 'dispose' ) ;
748829 } , 3000 ) ;
749830 }
750- //els.searchResult.find('[data-toggle="tooltip"]').tooltip();
751831 }
752832
753833 function renderHistory ( ) {
@@ -766,6 +846,12 @@ <h5 class="label-warp"><a class="label label-{type}" data-toggle="tooltip" data
766846 els . searchRelateBd . html ( '<span class="label label-default">Suggestions :</span>' + labels . join ( '' ) ) ;
767847 }
768848
849+ function renderSourceCode ( data ) {
850+ data && els . sourceCodeContent . text ( data . code ) ;
851+ }
852+ function removeVariableMenus ( ) {
853+ els . body . find ( '.popover--variable' ) . remove ( ) ;
854+ }
769855 function renderAnalytics ( param ) {
770856 setTimeout ( function ( ) {
771857 els . body . append ( '<iframe src="http://www.mihtool.com/analytics.html?codelf' + ( param ?( '&q=' + param ) :'' ) + '" style="display: none;width: 0;height: 0;border: 0;"></iframe>' ) ;
0 commit comments