|
| 1 | +DocsController.$inject = ['$scope', '$location', '$window', '$cookies', '$filter']; |
| 2 | +function DocsController(scope, $location, $window, $cookies, $filter) { |
| 3 | + window.$root = scope.$root; |
| 4 | + |
| 5 | + var OFFLINE_COOKIE_NAME = 'ng-offline', |
| 6 | + DOCS_PATH = /^\/(api)|(guide)|(cookbook)|(misc)|(tutorial)/, |
| 7 | + INDEX_PATH = /^(\/|\/index[^\.]*.html)$/, |
| 8 | + filter = $filter('filter'); |
| 9 | + |
| 10 | + scope.$location = $location; |
| 11 | + scope.versionNumber = angular.version.full; |
| 12 | + scope.version = angular.version.full + " " + angular.version.codeName; |
| 13 | + scope.subpage = false; |
| 14 | + scope.offlineEnabled = ($cookies[OFFLINE_COOKIE_NAME] == angular.version.full); |
| 15 | + scope.futurePartialTitle = null; |
| 16 | + scope.loading = 0; |
| 17 | + |
| 18 | + if (!$location.path() || INDEX_PATH.test($location.path())) { |
| 19 | + $location.path('/api').replace(); |
| 20 | + } |
| 21 | + |
| 22 | + scope.$watch('$location.path()', function(path) { |
| 23 | + // ignore non-doc links which are used in examples |
| 24 | + if (DOCS_PATH.test(path)) { |
| 25 | + var parts = path.split('/'); |
| 26 | + scope.sectionId = parts[1]; |
| 27 | + scope.partialId = parts[2] || 'index'; |
| 28 | + scope.pages = filter(NG_PAGES, {section: scope.sectionId}); |
| 29 | + |
| 30 | + var i = scope.pages.length; |
| 31 | + while (i--) { |
| 32 | + if (scope.pages[i].id == scope.partialId) break; |
| 33 | + } |
| 34 | + if (i<0) { |
| 35 | + scope.partialTitle = 'Error: Page Not Found!'; |
| 36 | + delete scope.partialId; |
| 37 | + } else { |
| 38 | + // TODO(i): this is not ideal but better than updating the title before a partial arrives, |
| 39 | + // which results in the old partial being displayed with the new title |
| 40 | + scope.futurePartialTitle = scope.pages[i].name; |
| 41 | + scope.loading++; |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + scope.getUrl = function(page) { |
| 47 | + return page.section + (page.id == 'index' ? '' : '/' + page.id); |
| 48 | + }; |
| 49 | + |
| 50 | + scope.getCurrentPartial = function() { |
| 51 | + return this.partialId |
| 52 | + ? ('./partials/' + this.sectionId + '/' + this.partialId.replace('angular.Module', 'angular.IModule') + '.html') |
| 53 | + : ''; |
| 54 | + }; |
| 55 | + |
| 56 | + scope.getClass = function(page) { |
| 57 | + var depth = page.depth, |
| 58 | + cssClass = 'level-' + depth + (page.name == this.partialId ? ' selected' : ''); |
| 59 | + |
| 60 | + if (page.section == 'api') |
| 61 | + cssClass += ' monospace'; |
| 62 | + |
| 63 | + return cssClass; |
| 64 | + }; |
| 65 | + |
| 66 | + scope.selectedSection = function(section) { |
| 67 | + return section == scope.sectionId ? 'current' : ''; |
| 68 | + }; |
| 69 | + |
| 70 | + scope.selectedPartial = function(partial) { |
| 71 | + return partial.id == scope.partialId ? 'current' : ''; |
| 72 | + }; |
| 73 | + |
| 74 | + scope.afterPartialLoaded = function() { |
| 75 | + var currentPageId = $location.path(); |
| 76 | + scope.loading--; |
| 77 | + scope.partialTitle = scope.futurePartialTitle; |
| 78 | + SyntaxHighlighter.highlight(); |
| 79 | + $window._gaq.push(['_trackPageview', currentPageId]); |
| 80 | + loadDisqus(currentPageId); |
| 81 | + }; |
| 82 | + |
| 83 | + /** stores a cookie that is used by apache to decide which manifest ot send */ |
| 84 | + scope.enableOffline = function() { |
| 85 | + //The cookie will be good for one year! |
| 86 | + var date = new Date(); |
| 87 | + date.setTime(date.getTime()+(365*24*60*60*1000)); |
| 88 | + var expires = "; expires="+date.toGMTString(); |
| 89 | + var value = angular.version.full; |
| 90 | + document.cookie = OFFLINE_COOKIE_NAME + "="+value+expires+"; path=" + $location.path; |
| 91 | + |
| 92 | + //force the page to reload so server can serve new manifest file |
| 93 | + window.location.reload(true); |
| 94 | + }; |
| 95 | + |
| 96 | + // bind escape to hash reset callback |
| 97 | + angular.element(window).bind('keydown', function(e) { |
| 98 | + if (e.keyCode === 27) { |
| 99 | + scope.$apply(function() { |
| 100 | + scope.subpage = false; |
| 101 | + }); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + function loadDisqus(currentPageId) { |
| 106 | + // http://docs.disqus.com/help/2/ |
| 107 | + window.disqus_shortname = 'angularjs'; |
| 108 | + window.disqus_identifier = currentPageId; |
| 109 | + window.disqus_url = 'http://docs-next.angularjs.org' + currentPageId; |
| 110 | + |
| 111 | + if ($location.host() == 'localhost') { |
| 112 | + return; // don't display disqus on localhost, comment this out if needed |
| 113 | + //window.disqus_developer = 1; |
| 114 | + } |
| 115 | + |
| 116 | + // http://docs.disqus.com/developers/universal/ |
| 117 | + (function() { |
| 118 | + var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; |
| 119 | + dsq.src = 'http://angularjs.disqus.com/embed.js'; |
| 120 | + (document.getElementsByTagName('head')[0] || |
| 121 | + document.getElementsByTagName('body')[0]).appendChild(dsq); |
| 122 | + })(); |
| 123 | + |
| 124 | + angular.element(document.getElementById('disqus_thread')).html(''); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +SyntaxHighlighter['defaults'].toolbar = false; |
| 129 | +SyntaxHighlighter['defaults'].gutter = true; |
| 130 | + |
| 131 | +/** |
| 132 | + * Controller for tutorial instructions |
| 133 | + * @param $cookieStore |
| 134 | + * @constructor |
| 135 | + */ |
| 136 | +function TutorialInstructionsCtrl($cookieStore) { |
| 137 | + this.selected = $cookieStore.get('selEnv') || 'git-mac'; |
| 138 | + |
| 139 | + this.currentCls = function(id, cls) { |
| 140 | + return this.selected == id ? cls || 'current' : ''; |
| 141 | + }; |
| 142 | + |
| 143 | + this.select = function(id) { |
| 144 | + this.selected = id; |
| 145 | + $cookieStore.put('selEnv', id); |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | +angular.module('ngdocs', ['ngdocs.directives'], function($locationProvider, $filterProvider, $compileProvider) { |
| 150 | + $locationProvider.html5Mode(true).hashPrefix('!'); |
| 151 | + |
| 152 | + $filterProvider.register('title', function(){ |
| 153 | + return function(text) { |
| 154 | + return text && text.replace(/^angular\.module\.([^\.]+)(\.(.*))?$/, function(_, module, _0, name){ |
| 155 | + name = name.replace('$compileProvider.directive.', 'directive - ') |
| 156 | + return 'Module ' + module + (name ? ' - ' + name : ''); |
| 157 | + }); |
| 158 | + }; |
| 159 | + }); |
| 160 | + |
| 161 | + $compileProvider.directive('code', function() { |
| 162 | + return { terminal: true }; |
| 163 | + }); |
| 164 | +}); |
0 commit comments