var docsApp = { controller: {}, directive: {}, serviceFactory: {} }; docsApp.controller.DocsVersionsCtrl = ['$scope', '$window', 'NG_VERSIONS', function($scope, $window, NG_VERSIONS) { $scope.versions = expandVersions(NG_VERSIONS); $scope.version = ($scope.version || angular.version.full).match(/^([\d\.]+\d+)/)[1]; //match only the number $scope.jumpToDocsVersion = function(value) { var isLastStable, version, versions = $scope.versions; for(var i=versions.length-1;i>=0;i--) { var v = versions[i]; if(v.version == value) { var next = versions[i - 1]; isLastStable = v.stable && (!next || next && !next.stable); version = v; break; } }; if(version && version.version >= '1.0.0') { //the older versions have a different path to the docs within their repo directory var docsPath = version.version < '1.0.2' ? 'docs-' + version.version : 'docs'; //the last stable version should redirect to docs.angularjs.org instead of code.angularjs.org var url = 'http://' + (isLastStable ? 'docs.angularjs.org' : 'code.angularjs.org/' + version.version + '/' + docsPath); $window.location = url; } }; function expandVersions(angularVersions) { var unstableVersionStart = 0; angularVersions.forEach(function(version) { var split = version.split('.'); unstableVersionStart = split[1] % 2 == 1 ? Math.max(unstableVersionStart, parseInt(split[0] + '' + split[1])) : unstableVersionStart; }); var versions = []; for(var i=angularVersions.length-1;i>=0;i--) { var version = angularVersions[i]; var split = version.split('.'); var stable = parseInt(split[0] + '' + split[1]) < unstableVersionStart; versions.push({ version : version, stable : stable, title : 'AngularJS - v' + version, group : (stable ? 'Stable' : 'Unstable') }); }; return versions; }; }]; docsApp.controller.DocsNavigationCtrl = ['$scope', '$location', 'docsSearch', function($scope, $location, docsSearch) { function clearResults() { $scope.results = []; $scope.colClassName = null; $scope.hasResults = false; } $scope.search = function(q) { var MIN_SEARCH_LENGTH = 3; if(q.length >= MIN_SEARCH_LENGTH) { var results = docsSearch(q); var totalSections = 0; for(var i in results) { ++totalSections; } if(totalSections > 0) { $scope.colClassName = 'cols-' + totalSections; $scope.hasResults = true; } $scope.results = results; } else { clearResults(); } if(!$scope.$$phase) $scope.$apply(); }; $scope.submit = function() { var result; for(var i in $scope.results) { result = $scope.results[i][0]; if(result) { break; } } if(result) { $location.path(result.url); $scope.hideResults(); } }; $scope.hideResults = function() { clearResults(); $scope.q = ''; }; }]; 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); } }; }; }; docsApp.serviceFactory.docsSearch = ['$rootScope','lunrSearch', 'NG_PAGES', function($rootScope, lunrSearch, NG_PAGES) { var index = lunrSearch(function() { this.ref('id'); this.field('title', {boost: 50}); this.field('description', { boost : 20 }); }); 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'; } results[section] = results[section] || []; if(results[section].length < 15) { results[section].push(item); } }); return results; }; }]; docsApp.directive.focused = function($timeout) { return function(scope, element, attrs) { element[0].focus(); element.on('focus', function() { scope.$apply(attrs.focused + '=true'); }); element.on('blur', function() { // have to use $timeout, so that we close the drop-down after the user clicks, // otherwise when the user clicks we process the closing before we process the click. $timeout(function() { scope.$eval(attrs.focused + '=false'); }); }); scope.$eval(attrs.focused + '=true'); }; }; docsApp.directive.docsSearchInput = function() { return function(scope, element, attrs) { var ESCAPE_KEY_KEYCODE = 27; element.bind('keydown', function(event) { if(event.keyCode == ESCAPE_KEY_KEYCODE) { event.stopPropagation(); event.preventDefault(); scope.$apply(function() { scope.hideResults(); }); } }); }; }; docsApp.directive.code = function() { return { restrict:'E', terminal: true }; }; docsApp.directive.sourceEdit = function(getEmbeddedTemplate) { return { template: '
Reset the workspace to step ' + step + '.
' + '' + command + '
Refresh your browser or check the app out on Angular\'s server.