|
11 | 11 | .main-container{ |
12 | 12 | padding: 40px; |
13 | 13 | } |
| 14 | + .search-form .title{ |
| 15 | + text-align: center; |
| 16 | + padding: 20px 100px; |
| 17 | + } |
| 18 | + .search-result{ |
| 19 | + padding: 40px 0; |
| 20 | + } |
| 21 | + blockquote{ |
| 22 | + text-align: center; |
| 23 | + } |
14 | 24 | </style> |
15 | 25 | </head> |
16 | 26 | <body> |
17 | 27 | <section class="main-container"> |
18 | | - <div class="row"> |
| 28 | + <section class="row search-form"> |
| 29 | + <header class="title"> |
| 30 | + <header><h1>Codelf</h1></header> |
| 31 | + <h5>Search over projects from Github, Bitbucket, Google Code, Codeplex, Sourceforge, Fedora Projec to find a <span class="label label-success">variable name</span> for you!</h5> |
| 32 | + </header> |
19 | 33 | <div class="col-lg-12"> |
20 | 34 | <div class="input-group"> |
21 | | - <input type="text" class="form-control" placeholder="Search for...e.g.product"> |
| 35 | + <input type="text" class="form-control" placeholder="Search a variable name...e.g.product"> |
22 | 36 | <span class="input-group-btn"> |
23 | 37 | <button class="btn btn-secondary" type="button">Search</button> |
24 | 38 | </span> |
25 | 39 | </div> |
26 | 40 | </div> |
27 | | - </div> |
| 41 | + </section> |
| 42 | + <section class="row search-result"> |
| 43 | + <div class="col-lg-12"> |
| 44 | + <div class="alert alert-success bd" role="alert"> |
| 45 | + <blockquote> |
| 46 | + <p>"There are only two hard things in Computer Science: cache invalidation and naming things." -- Phil Karlton</p> |
| 47 | + </blockquote> |
| 48 | + <script type="text/html"> |
| 49 | + <span class="label label-{type}" data-toggle="tooltip" data-placement="top" title="{val}">{val}</span> |
| 50 | + </script> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </section> |
28 | 54 | </section> |
| 55 | +<script src="src/lib/tether/js/tether.min.js"></script> |
29 | 56 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> |
30 | 57 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" |
31 | 58 | integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" |
32 | 59 | crossorigin="anonymous"></script> |
33 | 60 | <script> |
| 61 | + $(function () { |
| 62 | + function randomList(list, len, verify, ratio) { |
| 63 | + var rs = [], _list = list.slice(0); |
| 64 | + len = len || _list.length; |
| 65 | + ratio = ratio ? ratio : 0; |
| 66 | + function rd(_array) { |
| 67 | + _array = _array.sort(function () { |
| 68 | + return (0.5 - Math.random()); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + while (ratio) { |
| 73 | + rd(_list); |
| 74 | + ratio--; |
| 75 | + } |
| 76 | + if (_list.length <= len) { |
| 77 | + rs = _list; |
| 78 | + } else { |
| 79 | + while (rs.length < len) { |
| 80 | + var index = Math.floor(Math.random() * _list.length), |
| 81 | + item = _list[index]; |
| 82 | + if (( verify && verify.call(this, item, _list) ) || !verify) { |
| 83 | + rs.push(item); |
| 84 | + _list.splice(index, 1); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return rs; |
| 89 | + } |
| 90 | + function searchPage(){ |
| 91 | + var page = 1; |
| 92 | + var lastVal; |
| 93 | + var searchRequestCallback; |
| 94 | + this.resetPage = function(){ |
| 95 | + page = 1; |
| 96 | + } |
| 97 | + this.request = function(val,callback){ |
| 98 | + if(val==lastVal){ |
| 99 | + this.resetPage(); |
| 100 | + } |
| 101 | + lastVal = val; |
| 102 | + searchRequestCallback = callback; |
| 103 | + lastVal && $.ajax({ |
| 104 | + type: 'GET', |
| 105 | + url: 'https://searchcode.com/api/jsonp_codesearch_I/?q=soup&lan=html,sass,js&p=1&callback=afterSearch', |
| 106 | + dataType: 'jsonp', |
| 107 | + jsonp: false, |
| 108 | + jsonpCallback: false, |
| 109 | + contentType: "application/json", |
| 110 | + data: { |
| 111 | + q: lastVal, |
| 112 | + p: page, |
| 113 | + per_page: 100, |
| 114 | + callback: 'afterSearchRequest' |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + window.afterSearchRequest = function(data){ |
| 119 | + if(data){ |
| 120 | + page++; |
| 121 | + searchRequestCallback && searchRequestCallback(data); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + var els = { |
| 127 | + searchForm: $('.search-form'), |
| 128 | + searchInput: $('.search-form input'), |
| 129 | + searchBtn: $('.search-form button'), |
| 130 | + |
| 131 | + searchResult: $('.search-result'), |
| 132 | + searchResultTpl: $('.search-result script').html(), |
| 133 | + searchResultBd: $('.search-result .bd'), |
| 134 | + |
| 135 | + lastVal: '' |
| 136 | + }, |
| 137 | + searchObj; |
| 138 | + |
| 139 | + function bindEvent(){ |
| 140 | + els.searchInput.on('keyup',renderSearchBtn); |
| 141 | + els.searchBtn.on('click',onSearch); |
| 142 | + els.searchInput.keypress(function (e) { |
| 143 | + if (e.which == 13) { |
| 144 | + onSearch(); |
| 145 | + return false; |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + function init(){ |
| 150 | + bindEvent(); |
| 151 | + searchObj = new searchPage(renderSearchResult); |
| 152 | + } |
| 153 | + |
| 154 | + function onSearch(){ |
| 155 | + var val = els.searchInput.val().trim(); |
| 156 | + if(val){ |
| 157 | + els.lastVal = val; |
| 158 | + els.valReg = new RegExp('(([-_\w\d]*)?'+els.lastVal+'([-_\w\d]*)?)','ig'); |
| 159 | + searchObj.request(els.lastVal,renderSearchResult); |
| 160 | + renderSearchBtn(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + function renderSearchResult(data){ |
| 165 | + var vals = [],labels = [],allStr = []; |
| 166 | + data.results.forEach(function(rkey){ |
| 167 | + for(var lkey in rkey.lines){ |
| 168 | + allStr.push(rkey.lines[lkey]); |
| 169 | + } |
| 170 | + }); |
| 171 | + $.each([].slice.call(allStr.join('').match(els.valReg)), function(i, el){ |
| 172 | + if($.inArray(el, vals) === -1) vals.push(el); |
| 173 | + }); |
| 174 | + vals.forEach(function(key){ |
| 175 | + labels.push(els.searchResultTpl.replace('{type}',getRandomLabelType()).replace(/\{val\}/g,key)); |
| 176 | + }); |
| 177 | + if(labels.length){ |
| 178 | + els.searchResultBd.html(labels.join('')); |
| 179 | + els.searchResult.find('[data-toggle="tooltip"]').tooltip(); |
| 180 | + els.searchResult.show(); |
| 181 | + }else{ |
| 182 | + els.searchResult.hide(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + function renderSearchBtn(){ |
| 187 | + els.searchBtn.html(els.searchInput.val().trim()==els.lastVal?'Next Group':'Search'); |
| 188 | + } |
| 189 | + |
| 190 | + function getRandomLabelType(){ |
| 191 | + var types = ['default','primary','success','info','warning','warning','danger']; |
| 192 | + return randomList(types,1,null,10)[0]; |
| 193 | + } |
34 | 194 |
|
| 195 | + init(); |
| 196 | + }) |
35 | 197 | </script> |
36 | 198 | </body> |
37 | 199 | </html> |
0 commit comments