From f6be59c1b9253ad8438ec65ab959e294686c65ab Mon Sep 17 00:00:00 2001 From: Matias Niemelä Date: Tue, 18 Jun 2013 09:37:29 -0400 Subject: chore(ngdocs): provide test code for lunr search in docs --- docs/src/templates/js/docs.js | 154 ++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 87 deletions(-) (limited to 'docs/src/templates/js/docs.js') diff --git a/docs/src/templates/js/docs.js b/docs/src/templates/js/docs.js index ae0f1e58..88a4a9d1 100644 --- a/docs/src/templates/js/docs.js +++ b/docs/src/templates/js/docs.js @@ -62,29 +62,31 @@ docsApp.controller.DocsVersionsCtrl = ['$scope', '$window', 'NG_VERSIONS', funct }; }]; -docsApp.controller.DocsNavigationCtrl = ['$scope', 'fullTextSearch', '$location', function($scope, fullTextSearch, $location) { - fullTextSearch.init(); +docsApp.controller.DocsNavigationCtrl = ['$scope', '$location', 'docsSearch', function($scope, $location, docsSearch) { + function clearResults() { + $scope.results = []; + $scope.colClassName = null; + $scope.hasResults = false; + } + $scope.search = function(q) { - fullTextSearch.search(q, function(results) { - if(q && q.length >= 4) { - $scope.results = results; - var totalSections = 0; - for(var i in results) { - ++totalSections; - } - if(totalSections > 0) { - $scope.colClassName = 'cols-' + totalSections; - $scope.hasResults = true; - } - else { - $scope.hasResults = false; - } + var MIN_SEARCH_LENGTH = 4; + if(q.length >= MIN_SEARCH_LENGTH) { + var results = docsSearch(q); + var totalSections = 0; + for(var i in results) { + ++totalSections; } - else { - $scope.hasResults = false; + if(totalSections > 0) { + $scope.colClassName = 'cols-' + totalSections; + $scope.hasResults = true; } - if(!$scope.$$phase) $scope.$apply(); - }); + $scope.results = results; + } + else { + clearResults(); + } + if(!$scope.$$phase) $scope.$apply(); }; $scope.submit = function() { var result; @@ -100,82 +102,60 @@ docsApp.controller.DocsNavigationCtrl = ['$scope', 'fullTextSearch', '$location' } }; $scope.hideResults = function() { - $scope.hasResults = false; + clearResults(); $scope.q = ''; }; }]; -docsApp.serviceFactory.fullTextSearch = ['$q', '$rootScope', 'NG_PAGES', function($q, $rootScope, NG_PAGES) { - return { - dbName : 'docs', - indexName : 'docsindex', +docsApp.serviceFactory.lunrSearch = function() { + return function(properties) { + var engine = lunr(properties); + return { + store : function(values) { + engine.add(values); + }, + search : function(q) { + return engine.search(q); + } + }; + }; +}; - init : function(onReady) { - this.init = function() {}; +docsApp.serviceFactory.docsSearch = ['$rootScope','lunrSearch', 'NG_PAGES', + function($rootScope, lunrSearch, NG_PAGES) { - var self = this; - this.deferReady = $q.defer(); - this.readyPromise = this.deferReady.promise; + var index = lunrSearch(function() { + this.ref('id'); + this.field('title', {boost: 50}); + this.field('description', { boost : 20 }); + }); - this.engine = lunr(function () { - this.ref('id'); - this.field('title', {boost: 50}); - this.field('description', { boost : 20 }); - }); - this.prepare(); - this.onReady(); - }, - onReady : function() { - this.ready = true; - var self = this; - self.deferReady.resolve(); - if(!$rootScope.$$phase) { - $rootScope.$apply(); - } - }, - whenReady : function(fn) { - if(this.ready) { - fn(); - } - else { - this.init(); - this.readyPromise.then(fn); + angular.forEach(NG_PAGES, function(page, i) { + var title = page.shortName; + if(title.charAt(0) == 'n' && title.charAt(1) == 'g') { + title = title + ' ' + title.charAt(2).toLowerCase() + title.substr(3); + } + index.store({ + id: i, + title: title, + description: page.keywords + }); + }); + + return function(q) { + var results = {}; + angular.forEach(index.search(q), function(result) { + var item = NG_PAGES[result.ref]; + var section = item.section; + if(section == 'cookbook') { + section = 'tutorial'; } - }, - prepare : function(injector, callback) { - for(var i=0;i