1app.controller("searchController",function($scope,$rootScope,$location,$http,$timeout){ 2 $rootScope.articleTypes = articleTypes; 3 4 5 //articleCategories['planes'] = {safename: 'planes',name: 'Planes', order: 0}; 6 //articleCategories['helicopters'] = {safename: 'helicopters',name: 'Helicopters', order: 1}; 7 //articleCategories['fpv'] = {safename: 'fpv',name: 'Fpv', order: 2}; 8 //articleCategories['scratch-builds'] = {safename: 'scratch-builds',name: 'Scratch-builds', order: 3}; 9 //articleCategories['multi-rotor'] = {safename: 'multi-rotor',name: 'Multi-rotor', order: 4}; 10 //articleCategories['workbench'] = {safename: 'workbench',name: 'Workbench', order: 5}; 11 console.log(articleCategories); 12 13 14 $rootScope.articleCategories = articleCategories; 15 16 //articleCategories['planes']['order'] = 0; 17 //articleCategories['helicopters']['order'] = 1; 18 //articleCategories['fpv']['order'] = 2; 19 //articleCategories['scratch-builds']['order'] = 3; 20 //articleCategories['multi-rotor']['order'] = 4; 21 //articleCategories['workbench']['order'] = 5; 22 23 articleCategories['ft-articles'] = {safename: 'ft-articles',name: 'FT Articles', order: 6}; 24 articleCategories['news'] = {safename: 'news',name: 'News', order: 7}; 25 26// console.log(articleCategories); 27 28 //search vars 29 $scope.types = []; 30 $scope.cats = []; 31 $scope.dateFilter = 'all'; 32 $scope.curPage = 1; 33 $scope.perPage = parseInt(localStorage.getItem('perPage')) || 16; 34 $scope.perPageOptions = [16, 32, 48, 64, 80, 96]; 35 $scope.pages = []; 36 $scope.count = 0; 37 $scope.curStart = 1; 38 $scope.curEnd = 8; 39 $scope.prepopulated = false; 40 $scope.listFormat = 'details'; 41 $scope.textSearch = []; 42 $scope.listSort = false;//'date_high_to_low'; 43 $scope.searchInitiatedBySort = false; 44 $scope.user = false;
45 $scope.url = {}; 46 47 //display vars 48 $scope.textSearchEntry = ''; 49 $scope.showTextEntries = false; 50 $scope.showTypeEntries = false; 51 $scope.showCatEntries = false; 52 $scope.textDisabled = false; 53 $scope.typesDisabled = false; 54 $scope.catsDisabled = false; 55 $scope.fromInit = false; 56 57 $scope.showPagesStart = false; 58 $scope.showPagesEnd = false; 59 60 $scope.selectedCategories = []; 61 $scope.loading = false; 62 $scope.listSortField = 'date'; 63 $scope.listSortDirection = 'high_to_low'; 64 65 $scope.endDisplayCount = function(){ 66 endCount = (($scope.curPage-1)*$scope.perPage)+$scope.perPage; 67 if ( endCount > $scope.count ){ 68 return $scope.count; 69 } else { 70 return endCount; 71 } 72 } 73 74 $scope.toggleCategory = function(cat){ 75 var currentSearchParams = $("#searchBox").select2("val"); 76 var catSearchString = "cat__"+cat.safename; 77 var currentCatPos = $scope.selectedCategories.indexOf(catSearchString); 78 if ( currentCatPos >= 0 ){ 79 $scope.selectedCategories.splice(currentCatPos, 1); 80 currentSearchParams.splice(currentSearchParams.indexOf(catSearchString),1); 81 $scope.cats.splice($scope.cats.indexOf(catSearchString),1); 82 } else { 83 $scope.selectedCategories.push(catSearchString); 84 currentSearchParams.push(catSearchString); 85 $scope.cats.push(cat.safename); 86 } 87 88 $("#searchBox").select2("val",currentSearchParams) 89 $scope.updateSearch(true) 90 } 91 92 $scope.inSelectedCategories = function(cat){ 93 if ( $scope.selectedCategories.indexOf("cat__"+cat.safename) >= 0 ){ 94 return 'active'; 95 } 96 return ''; 97 } 98 99 $scope.validateSearchBtns = function(){ 100 var currentSearchParams = $("#searchBox").select2("val"); 101 for ( var i in $scope.selectedCategories ){ 102 cat = $scope.selectedCategories[i]; 103 if ( currentSearchParams.indexOf(cat) == -1 ){ 104 $scope.selectedCategories.splice(i,1) 105 } 106 } 107 } 108 109 $scope.populateSearch = function(vals){ 110 $scope.types = []; 111 $scope.cats = []; 112 $scope.textSearch = []; 113 vals = vals.split(','); 114 for ( var i in vals ){ 115 val = vals[i]; 116 if ( val.indexOf('cat__') != -1 ){ 117 $scope.cats.push(val.replace('cat__','')); 118 } else if ( val.indexOf('type__') != -1 ){ 119 $scope.types.push(val.replace('type__','')); 120 } else { 121 $scope.textSearch.push(val); 122 } 123 } 124 $scope.updateSearch(true); 125 } 126 127 $scope.goToPage = function(page){ 128 $scope.curPage = page; 129 $("#featuredContent").collapse('hide'); 130 var toolbar = document.getElementById("searchContainer"); 131 toolbar.scrollIntoView(true); 132 $scope.updateSearch(false); 133 } 134 135 $scope.addTextSearch = function(){ 136 if ( $scope.textSearchEntry == '' ){ 137 return false; 138 } 139 140 $scope.textSearch.push($scope.textSearchEntry); 141 $scope.textSearchEntry = ''; 142 $scope.showTextEntries = true; 143 if ( $scope.textSearch.length == 5 ){ 144 $scope.textDisabled = true; 145 } 146 $scope.updateSearch(true); 147 } 148 149 $scope.removeTextSearch = function(index){ 150 $scope.textSearch.splice(index,1); 151 if ( $scope.textSearch.length == 0 ){ 152 $scope.showTextEntries = false; 153 } 154 $scope.textDisabled = false; 155 $scope.updateSearch(true); 156 } 157 158 $scope.addType = function(type){ 159 if ( $scope.typeChosen(type) ){ 160 return false; 161 } 162 $scope.types.push(type); 163 $scope.showTypeEntries = true; 164 $scope.updateSearch(true); 165 } 166 167 $scope.removeType = function(index){ 168 $scope.types.splice(index,1); 169 if ( $scope.types.length == 0 ){ 170 $scope.showTypeEntries = false; 171 } 172 $scope.updateSearch(true); 173 $scope.typesDisabled = false; 174 } 175 176 $scope.typeChosen = function(type){ 177 if ( $scope.types.indexOf(type) == -1 ){ 178 return false; 179 } 180 return true; 181 } 182 183 $scope.addCat = function(cat){ 184 if ( $scope.catChosen(cat) ){ 185 return false; 186 } 187 $scope.cats.push(cat); 188 $scope.showCatEntries = true; 189 $scope.updateSearch(true); 190 } 191 192 $scope.initCat = function(cat){ 193 if ( cat == 'news' || cat == 'podcasts'){ 194 $scope.types.push(cat); 195 var searchStr = 'type__'+cat; 196 } else { 197 $scope.cats.push(cat); 198 var searchStr = 'cat__'+cat; 199 } 200 $().ready(function(){ 201 $("#searchBox").select2("val",[searchStr]); 202 }) 203 204 205 //update sort 206 urlSections = $location.path().replace(/\//g,'').split("&"); //get url sections 207 sections = {}; 208 for ( var i in urlSections ){ //put url sections in object 209 section = urlSections[i].split('='); 210 211 sectionTitle = section[0]; 212 sections[sectionTitle] = section[1]; 213 } 214 215 //if ( !sections['dateFilter'] && $scope.types.indexOf('podcasts') == -1 ){ //if the date filter is false, overwrite it unless we're in podcasts 216 // $scope.dateFilter = 'published+30+Days'; 217 //} 218 219 $scope.fromInit = true; 220 $scope.updateSearch(false); 221 } 222 223 $scope.initUser = function(user){ 224 $scope.user = user; 225 $scope.fromInit = true; 226 $scope.updateSearch(true); 227 } 228 229 $scope.removeCat = function(index){ 230 $scope.cats.splice(index,1); 231 if ( $scope.cats.length == 0 ){ 232 $scope.showCatEntries = false;
233 } 234 $scope.updateSearch(true); 235 $scope.catsDisabled = false; 236 } 237 238 $scope.catChosen = function(cat){ 239 if ( $scope.cats.indexOf(cat) == -1 ){ 240 return false; 241 } 242 return true; 243 } 244 245 $scope.clearAll = function(type){ 246 $scope[type] = []; 247 248 if ( type == 'types' ){ 249 $scope.showTypeEntries = false; 250 } else if ( type == 'cats' ){ 251 $scope.showCatEntries = false; 252 } else if ( type == 'textSearch' ){ 253 $scope.showTextEntries = false; 254 $scope.textDisabled = false; 255 } 256 257 $scope.updateSearch(true); 258 } 259 260 $scope.updateListFormat = function(format){ 261 $scope.listFormat = format; 262 } 263 264 $scope.updateListSort = function(){ 265 localStorage.setItem('perPage', $scope.perPage); 266 $scope.listSort = $scope.listSortField + "_"+$scope.listSortDirection; 267 $scope.searchInitiatedBySort = true; 268 $scope.updateSearch(true); 269 } 270 271 $scope.oldUpdateListSort = function(sort){ 272 if ( sort == 'date' && $scope.listSort.indexOf('date') != -1 ){ 273 curDirection = $scope.listSort.replace('date_',''); 274 if ( curDirection.indexOf('high') == 0 ){ //if high is first, as in 'high_to_low' 275 direction = 'low_to_high'; 276 } else { 277 direction = 'high_to_low'; 278 } 279 $scope.listSort = 'date_'+direction; 280 } else if ( sort == 'views' && $scope.listSort.indexOf('views') != -1 ){ 281 curDirection = $scope.listSort.replace('views_',''); 282 if ( curDirection.indexOf('high') == 0 ){ //if high is first, as in 'high_to_low' 283 direction = 'low_to_high'; 284 } else { 285 direction = 'high_to_low'; 286 } 287 $scope.listSort = 'views_'+direction; 288 } else if ( sort == 'rating' && $scope.listSort.indexOf('rating') != -1 ){ 289 curDirection = $scope.listSort.replace('rating_',''); 290 if ( curDirection.indexOf('high') == 0 ){ //if high is first, as in 'high_to_low' 291 direction = 'low_to_high'; 292 } else { 293 direction = 'high_to_low'; 294 } 295 $scope.listSort = 'rating_'+direction; 296 } else { 297 $scope.listSort = sort+"_high_to_low"; 298 } 299 300 $scope.searchInitiatedBySort = true; 301 302 303 $scope.updateSearch(true); 304 } 305 306 $scope.getStars = function(article){ 307 return Math.floor(article.average_rating); 308 } 309 310 //var randomNum = Math.floor((Math.random()*100)+1); 311 $scope.getTemplateUrl = function(){ 312 return "/angular_templates/list-article-"+$scope.listFormat+".html?id=02042013"; 313 } 314 315 $scope.gridStyle = function(index){ 316 styles = {} 317 if ( index % 4 == 0 ){ 318 styles['clear'] = 'both'; 319 styles['margin-left'] = 0; 320 } 321 322 if ( index > 3 ){ 323 styles['margin-top'] = '2%'; 324 } 325 326 return styles; 327 } 328 329 $scope.decreasePage = function(){ 330 if ( $scope.curPage == 1 ){ 331 return; 332 } 333 $scope.curPage--; 334 $("#featuredContent").collapse('hide'); 335 var toolbar = document.getElementById("searchContainer"); 336 toolbar.scrollIntoView(true); 337 $scope.updateSearch(false); 338 } 339 340 $scope.increasePage = function(){ 341 if ( $scope.curPage == $scope.pages ){ 342 return; 343 } 344 $scope.curPage++; 345 $("#featuredContent").collapse('hide'); 346 var toolbar = document.getElementById("searchContainer"); 347 toolbar.scrollIntoView(true); 348 $scope.updateSearch(false); 349 } 350 351 $scope.updateSearch = function(to_page_one){ 352 353 var updatePath = true; 354 //when coming to the page, we need to repopulate the search items if there are any in the url 355 if ( !$scope.prepopulated && $location.path() != '/' && $location.path() != '' ){ 356 var searchBoxVals = []; 357 sections = $location.path().replace(/\//g,'').split("&"); 358 for ( var i in sections ){ 359 section = sections[i].split('='); 360 sectionTitle = section[0]; 361 var values = section[1].split(","); 362 363 if ( sectionTitle == 'page' ){ 364 $scope.curPage = values; 365 to_page_one = false; 366 } 367 368 $scope[sectionTitle] = values; 369 $scope.url[sectionTitle] = values; 370 371 372 for ( var i in values ){ 373 if ( sectionTitle == 'types'){ 374 searchBoxVals.push('type__'+values[i]); 375 } else if ( sectionTitle == 'cats' ){ 376 searchBoxVals.push('cat__'+values[i]); 377 $scope.selectedCategories.push('cat__'+values[i]); 378 } else if ( sectionTitle == 'listSort' ){ 379 sortString = values[0]; 380 sortStringSplit = sortString.split('_'); 381 $scope.listSortField = sortStringSplit[0]; 382 $scope.listSortDirection = 'high_to_low'; 383 if ( sortStringSplit[1] == 'low' ){ 384 $scope.listSortDirection = 'low_to_high'; 385 } 386 } else if ( sectionTitle != 'dateFilter' && sectionTitle != 'page') { 387 if ( values[i] != '' ){ 388 searchBoxVals.push(values[i]); 389 } 390 } 391 } 392 393 if ( sectionTitle == 'cats' ){ 394 $scope.showCatEntries = true; 395 } else if ( sectionTitle == 'types' ){ 396 $scope.showTypeEntries = true; 397 } else if ( sectionTitle == 'dateFilter' ){ 398 $scope.dateFilter = values[0]; 399 } else if ( sectionTitle == 'textSearch' ){ 400 $scope.showTextEntries = true; 401 } 402 if ( $scope.textSearch.length == 5 ){ 403 $scope.textDisabled = true; 404 } 405 406 }
407 updatePath = false; 408 if ( searchBoxVals.length > 0 ){ 409 $("#searchBox").select2("val",searchBoxVals); 410 } 411 412 var thisIsPrepopulation = true; 413 414 } 415 $scope.prepopulated = true; 416 417 418 if ( to_page_one ){ 419 $scope.curPage = 1; 420 } 421 422 423 var search = {}; 424 var address=""; 425 if ( $scope.types.length > 0 ){ 426 search.types = []; 427 address += "types="; 428 for ( var i in $scope.types ){ 429 search.types.push($scope.types[i]) 430 if ( i > 0 ) 431 address+=","; 432 address+=$scope.types[i]; 433 } 434 } 435 436 if ( $scope.cats.length > 0 ){ 437 search.cats = []; 438 catsAddress = ''; 439 for ( var i in $scope.cats ){ 440 search.cats.push($scope.cats[i]) 441 if ( i > 0 ) 442 catsAddress+=","; 443 addressSplit = $location.absUrl().split("#"); 444 if ( addressSplit[0].indexOf($scope.cats[i]) == -1 ){ 445 catsAddress+=$scope.cats[i]; 446 } 447 } 448 449 if ( catsAddress != '' ){ 450 if ( address != '' ) 451 address += "&"; 452 address += "cats="+catsAddress; 453 } 454 } 455 456 if ( $scope.curPage > 1 ){ 457 address += address!=''?'&':''; 458 address += "page="+$scope.curPage; 459 } 460 461 if ( $scope.textSearch.length > 0 ){ 462 //if there is only one item and the item is blank, set back to empty array. This happens when coming in with a blank text string as the value on the URL param. 463 if ( $scope.textSearch.length == 1 && $scope.textSearch[0] == '' ){ 464 $scope.textSearch = []; 465 } else { 466 address += address!=''?'&':''; 467 search.textSearch = $scope.textSearch; 468 for ( var i in $scope.textSearch ){ 469 if ( i == 0 ){ 470 address += 'textSearch='; 471 } else { 472 address+=","; 473 } 474 address += $scope.textSearch[i]; 475 } 476 } 477 } 478 479 // if we have text, categories, or types (basically, if the search isn't empty) 480 // AND the search was NOT initiated by sort 481 if ( ( $scope.textSearch.length > 0 || $scope.cats.length > 0 || $scope.types.length > 0 ) && $scope.searchInitiatedBySort == false && $scope.types.indexOf('podcasts') == -1 ){ 482 if ( thisIsPrepopulation && $scope.listSort ){ //if we're on the initial prepopulation AND the sort isn't blank 483 //leave things as they are 484 } else { 485 $scope.listSort = 'date_high_to_low'; 486 } 487 } 488 489 490 491 //if our search is blank and we were not initiated by sort 492 if ( ( $scope.textSearch.length == 0 && $scope.cats.length == 0 && $scope.types.length == 0 ) && $scope.searchInitiatedBySort == false ){ 493 if ( thisIsPrepopulation && $scope.listSort ){ //if we're on the initial prepopulation AND the sort isn't blank 494 //leave things as they are 495 } else { 496 //alert($scope.listSort); 497 //$scope.listSort = false; //set back to default 498 } 499 } 500 501 if ( $scope.listSort != false && $scope.types.indexOf('podcasts') == -1 ){ 502 address += address!=''?'&':''; 503 address += 'listSort='+$scope.listSort; 504 } else { 505 $scope.listSort = 'date_high_to_low'; 506 } 507 508 509 510 $scope.searchInitiatedBySort = false; //reset for next search 511 512 if ( $scope.dateFilter != 'all' ){ 513 search.dateFilter = $scope.dateFilter; 514 address += address!=''?'&':''; 515 address += 'dateFilter='+$scope.dateFilter; 516 } 517 518 if ( $scope.user ){ 519 search.user = $scope.user; 520 } 521 522 if ( updatePath ){ 523 $timeout(function(){ 524 if ( address != '' ){ 525 $location.path(address); 526 } 527 }); 528 } 529 530 if ( address != '' ){ 531 //$("#searchContainer").ScrollTo({offset:function(){ 532 // alert("HERE"); 533 //}} ); 534 //$.scrollTo($("#searchContainer"),500,{offset: -100}); 535 } 536 537 538 $scope.fromInit = false; //reset fromInit 539 //$("#articleListCurtain").css('opacity','0').show().animate({opacity: .8},500,function(){ 540 start = $scope.curPage > 1 ? ($scope.perPage*($scope.curPage-1)):0; 541 542 //hack for ft news and author 543 if ( search.cats != undefined ){ 544 if ( search.cats.indexOf('news') >= 0 ){ 545 search.cats.splice(search.cats.indexOf('news'),1); 546 if ( search.types != undefined ){ 547 search.types.push('news'); 548 } else { 549 search.types = ['news']; 550 } 551 } 552 553 if ( search.cats.indexOf('ft-articles') >= 0 ){ 554 search.cats.splice(search.cats.indexOf('ft-articles'),1); 555 search.user = '5101fbb332d9bf22060008e1'; 556 } else { 557 delete(search.user); 558 } 559 } 560 //console.log(search); 561 562 563 // DO THE SEARCH 564 $scope.loading = true; 565 $http.get("/articles/search/?search="+JSON.stringify(search)+"&skip="+start+"&limit="+$scope.perPage+"&sort="+$scope.listSort).success(function(data){ 566 //console.log(data); 567 $scope.loading = false;
568 //pages 569 $scope.count = data.count; 570 pages = Math.ceil($scope.count/$scope.perPage); 571 $scope.pages = []; 572 $scope.totalPages = pages; 573 if($scope.curPage.length){ 574 $scope.curPage = +$scope.curPage[0]; 575 } 576 577 curPage = $scope.curPage; 578 for ( var page = 1; page <= pages; page++ ){ 579 /*if(page <= 2 || page > (pages - 2) || (page <= (curPage + 1) && page >= (curPage - 1))){*/ 580 if(page <= 2 || page > (pages - 1) || (page <= (curPage + 2) && page >= (curPage - 2))){ 581 $scope.pages.push(page); 582 } 583 } 584 for (var i = 1; i < $scope.pages.length; i++){ 585 var curNum = $scope.pages[i]; 586 var prevNum = $scope.pages[i-1]; 587 if((curNum != false && prevNum != false) && (curNum - 1 != prevNum)){ 588 $scope.pages.splice(i, 0, false); 589 } 590 591 } 592 //console.log($scope.pages); 593 594 595 $scope.curStart = (($scope.curPage-1)*$scope.perPage)+1; 596 $scope.curEnd = $scope.perPage+(($scope.curPage-1)*$scope.perPage); 597 if ( $scope.curEnd > $scope.count ){ 598 $scope.curEnd = $scope.count; 599 } 600 //end pages 601 602 $scope.users = data.users; 603 $scope.articles = data.articles; 604 $timeout(function(){ 605 $scope.$apply(function (){ 606 $scope.curPage = $scope.curPage++; 607 }); 608 609 }) 610 611 for ( var i in data.ads ){ 612 $(".adZone[rel="+i+"]").html(data.ads[i]); 613 } 614 615 $("#articleListCurtain").fadeOut(500); 616 }) 617 //}); 618 }; 619 620 $timeout(function(){ 621 if ( !$scope.user && $scope.cats.length == 0 ){ 622 $scope.updateSearch(); 623 } 624 }) 625}); 626 627 628$().ready(function(){ 629 $("#searchContainer .dropdown-menu").find("a, input, label").click(function(e){ 630 e.stopPropagation(); 631 }) 632}) 633 634 635app.filter('orderObjectBy', function() { 636 return function(items, field, reverse) { 637 var filtered = []; 638 angular.forEach(items, function(item) { 639 filtered.push(item); 640 }); 641 filtered.sort(function (a, b) { 642 return (a[field] > b[field] ? 1 : -1); 643 }); 644 if(reverse) filtered.reverse(); 645 return filtered; 646 }; 647});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.