Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9dea9de

Browse files
Di PengIgorMinar
Di Peng
authored andcommitted
feat(docs): add full offline support
1 parent bee78a8 commit 9dea9de

File tree

7 files changed

+163
-68
lines changed

7 files changed

+163
-68
lines changed

Rakefile

+16-1
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,14 @@ end
221221

222222

223223
desc 'Generate docs'
224-
task :docs do
224+
task :docs => [:init] do
225225
`node docs/src/gen-docs.js`
226+
File.open(path_to('docs/.htaccess'), File::RDWR) do |f|
227+
text = f.read
228+
f.truncate 0
229+
f.rewind
230+
f.write text.sub('"NG_VERSION_FULL"', NG_VERSION.full)
231+
end
226232
end
227233

228234

@@ -250,19 +256,28 @@ task :package => [:clean, :compile, :docs] do
250256

251257
File.open("#{pkg_dir}/docs-#{NG_VERSION.full}/index.html", File::RDWR) do |f|
252258
text = f.read
259+
f.truncate 0
253260
f.rewind
254261
f.write text.sub('angular.min.js', "angular-#{NG_VERSION.full}.min.js")
255262
end
256263

257264
File.open("#{pkg_dir}/docs-#{NG_VERSION.full}/docs-scenario.html", File::RDWR) do |f|
258265
text = f.read
266+
f.truncate 0
259267
f.rewind
260268
f.write text.sub('angular-scenario.js', "angular-scenario-#{NG_VERSION.full}.js")
261269
end
262270

263271
File.open("#{pkg_dir}/docs-#{NG_VERSION.full}/appcache.manifest", File::RDWR) do |f|
272+
text = f.read
273+
f.truncate 0
274+
f.rewind
275+
f.write text.sub('angular.min.js', "angular-#{NG_VERSION.full}.min.js")
276+
end
264277

278+
File.open("#{pkg_dir}/docs-#{NG_VERSION.full}/appcache-offline.manifest", File::RDWR) do |f|
265279
text = f.read
280+
f.truncate 0
266281
f.rewind
267282
f.write text.sub('angular.min.js', "angular-#{NG_VERSION.full}.min.js")
268283
end

docs/src/appCache.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ function appCache(path) {
1111
if(!path) {
1212
return appCacheTemplate();
1313
}
14-
var blackList = ["offline.html",
15-
"sitemap.xml",
16-
"robots.txt",
17-
"docs-scenario.html",
18-
"docs-scenario.js",
19-
"appcache.manifest"
14+
var blackList = ["build/docs/offline.html",
15+
"build/docs/sitemap.xml",
16+
"build/docs/robots.txt",
17+
"build/docs/docs-scenario.html",
18+
"build/docs/docs-scenario.js",
19+
"build/docs/appcache.manifest",
20+
"build/docs/.htaccess"
2021
];
2122

2223
var result = ["CACHE MANIFEST",

docs/src/gen-docs.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function writeTheRest(writesFuture) {
5757
writesFuture.push(writer.output('docs-scenario.js', ngdoc.scenarios(docs)));
5858
writesFuture.push(writer.output('robots.txt', 'Sitemap: http://docs.angularjs.org/sitemap.xml\n'));
5959
writesFuture.push(writer.output('appcache.manifest',appCache()));
60+
writesFuture.push(writer.copyTpl('.htaccess'));
6061

6162
writesFuture.push(writer.merge(['docs.js',
6263
'doc_widgets.js'],

docs/src/templates/.htaccess

+9-55
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
1-
Options +Indexes
2-
IndexIgnore favicon.ico
3-
RewriteEngine on
4-
5-
# Enable Crawling of AJAX apps.
6-
# See: http://code.google.com/web/ajaxcrawling/docs/getting-started.html
7-
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=\/(.*)$
8-
RewriteRule ^(.*)$ /$1/%1.html?
9-
10-
# Map versiond angular files to root folder.
11-
RewriteRule ^angular([\-\w]+)(\d+\.\d+\.\d+)(.*)$ /$2/angular$1$2$3
12-
# Map 1.2.3/docs to 1.2.3/docs-1.2.3/ so that we don't need the version in the URL twice
13-
RewriteRule ^(\d+\.\d+\.\d+)/docs/(.*)$ /$1/docs-$1/$2
14-
15-
# If the user has forgot the trailing slash than add it through redirect
16-
RewriteRule ^latest$ /latest/ [R]
17-
RewriteRule ^latest/docs$ /latest/docs/ [R]
18-
19-
# To change the latest version just change this line.
20-
# IMPORTANT: the order of lines matters, do not move to top!
21-
RewriteRule ^latest/(.*)$ /0.9.17/$1
22-
23-
# If the request comes on http://docs.angularjs.org then point it to latest/docs
24-
RewriteCond %{HTTP_HOST} ^docs\.angularjs\.org$
25-
RewriteCond %{REQUEST_URI} !^/\d+\.\d+\.\d+
26-
RewriteCond %{REQUEST_URI} !^/latest
27-
RewriteCond %{REQUEST_URI} !^/angular-
28-
RewriteCond %{REQUEST_URI} !^/favicon.ico
29-
RewriteRule ^(.*)$ /latest/docs/$1
30-
31-
## PERFORMANCE ##
32-
33-
ExpiresActive On
1+
## OFFLINE SUPPORT ##
342

35-
# cache js files for one year
36-
<FilesMatch "^angular-(ie-compat-)?[\d\.]+(\.min)?\.js">
37-
ExpiresDefault "access plus 1 year"
38-
</FilesMatch>
39-
40-
# don't cache appcache manifests
41-
<FilesMatch "\.manifest$">
42-
ExpiresDefault "access"
43-
</FilesMatch>
44-
45-
# cache everything else for 1h
46-
ExpiresDefault "access plus 60 minutes"
47-
48-
49-
# compression
50-
SetOutputFilter DEFLATE
51-
Header set Vary "Accept-Encoding"
52-
53-
54-
# content types
55-
AddType application/javascript js
56-
AddType text/cache-manifest .manifest
3+
# These rules tell apache to check if there is a cookie called "offline", with value set to the
4+
# current angular version. If this rule matches the appcache-offline.manifest will be served for
5+
# requests to appcache.manifest
6+
#
7+
# This file must be processed by Rake in order to replace %ANGULAR_VERSION% with the actual version.
578

9+
RewriteEngine on
10+
RewriteCond %{HTTP_COOKIE} ng-offline="NG_VERSION_FULL"
11+
RewriteRule appcache.manifest appcache-offline.manifest

docs/src/templates/docs.css

+81
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ li {
8383
font-size: 12px;
8484
}
8585

86+
#offline{
87+
text-decoration:underline;
88+
cursor:pointer;
89+
color:blue;
90+
}
91+
8692
#copyright {
8793
float:right;
8894
text-align: right;
@@ -317,3 +323,78 @@ li {
317323
display: inline;
318324
padding-left: 0;
319325
}
326+
327+
/* subpages */
328+
329+
#fader {
330+
display: none;
331+
position: fixed;
332+
top: 0px;
333+
left: 0px;
334+
width: 100%;
335+
height: 100%;
336+
background-color: black;
337+
opacity: 0.8;
338+
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
339+
filter: alpha(opacity=80);
340+
z-index: 3;
341+
}
342+
343+
#subpage {
344+
display: none;
345+
}
346+
347+
#subpage > div {
348+
position: fixed;
349+
top: 50%;
350+
left: 50%;
351+
width: 729px;
352+
margin-top: -140px;
353+
margin-left: -365px;
354+
z-index: 3;
355+
background-color: #7989D6;
356+
border-radius: 15px;
357+
-moz-border-radius: 15px;
358+
-webkit-border-radius: 15px;
359+
box-shadow: 4px 4px 6px #48577D;
360+
-webkit-box-shadow: 4px 4px 6px #48577D;
361+
-moz-box-shadow: 4px 4px 6px #48577D;
362+
}
363+
364+
#subpage h2 {
365+
height: 1.8em;
366+
-moz-border-radius-topright: 15px;
367+
-moz-border-radius-topleft: 15px;
368+
border-radius-topright: 15px;
369+
border-radius-topleft: 15px;
370+
-webkit-border-top-right-radius: 15px;
371+
-webkit-border-top-left-radius: 15px;
372+
border-top-right-radius: 15px;
373+
border-top-left-radius: 15px;
374+
padding: 0.6em 0 0 1em;
375+
margin: 0;
376+
color: white;
377+
}
378+
379+
#subpage > div > a {
380+
color: black;
381+
float: right;
382+
margin: -40px 10px;
383+
font-size: 1em;
384+
}
385+
386+
#subpage > div > a:hover {
387+
text-decoration: none;
388+
color: white;
389+
}
390+
391+
#subpage > div > p {
392+
background-color: white;
393+
padding: 0.5em 1em 0.5em 1em;
394+
margin-bottom: 0.5em;
395+
}
396+
397+
#cacheButton {
398+
margin: 0em 2em 1em 0em;
399+
float:right;
400+
}

docs/src/templates/docs.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var HAS_HASH = /#/;
2-
DocsController.$inject = ['$location', '$browser', '$window'];
3-
function DocsController($location, $browser, $window) {
1+
DocsController.$inject = ['$location', '$browser', '$window', '$cookies'];
2+
function DocsController($location, $browser, $window, $cookies) {
43
window.$root = this.$root;
5-
var self = this;
4+
5+
var self = this,
6+
OFFLINE_COOKIE_NAME = 'ng-offline',
7+
HAS_HASH = /#/;
8+
69
this.$location = $location;
710

811
self.versionNumber = angular.version.full;
912
self.version = angular.version.full + " " + angular.version.codeName;
10-
self.cookieName = "angularPref";
1113
self.subpage = false;
12-
self.offlineEnabled = (document.cookie.indexOf(self.cookieName) !== -1);
14+
self.offlineEnabled = ($cookies[OFFLINE_COOKIE_NAME] == angular.version.full);
1315

1416
if (!HAS_HASH.test($location.href)) {
1517
$location.hashPath = '!/api';
@@ -73,6 +75,27 @@ function DocsController($location, $browser, $window) {
7375
"subject=" + escape("Feedback on " + $location.href) + "&" +
7476
"body=" + escape("Hi there,\n\nI read " + $location.href + " and wanted to ask ....");
7577
};
78+
79+
/** stores a cookie that is used by apache to decide which manifest ot send */
80+
this.enableOffline = function() {
81+
//The cookie will be good for one year!
82+
var date = new Date();
83+
date.setTime(date.getTime()+(365*24*60*60*1000));
84+
var expires = "; expires="+date.toGMTString();
85+
var value = angular.version.full;
86+
document.cookie = OFFLINE_COOKIE_NAME + "="+value+expires+"; path=" + $location.path;
87+
88+
//force the page to reload so server can serve new manifest file
89+
window.location.reload(true);
90+
};
91+
92+
// bind escape to hash reset callback
93+
angular.element(window).bind('keydown', function(e) {
94+
if (e.keyCode === 27) {
95+
self.subpage = false;
96+
self.$eval();
97+
}
98+
});
7699
}
77100

78101
// prevent compilation of code

docs/src/templates/index.html

+20
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,30 @@ <h2 ng:bind="partialTitle"></h2>
7777
ng:href="https://github.com/angular/angular.js/blob/master/CHANGELOG.md#{{versionNumber}}"
7878
ng:bind-template="v{{version}}">
7979
</a>
80+
<a ng:hide="offlineEnabled" ng:click ="subpage = true">(enable offline support)</a>
81+
<span ng:show="offlineEnabled">(offline support enabled)</span>
8082
<span id="copyright">© 2010-2011 AngularJS</span>
8183
</div>
8284
</div>
8385

86+
<div id="fader" ng:show="subpage"></div>
87+
<div id="subpage" ng:show="subpage">
88+
<div>
89+
<h2>Would you like full offline support for this AngularJS Docs App?</h2>
90+
<a ng:click="subpage=false">&#10005;</a>
91+
<p>
92+
If you want to be able to access the entire AngularJS documentation offline, click the
93+
button below. This will reload the current page and trigger background downloads of all the
94+
necessary files (approximately 3.5MB). The next time you load the docs, the browser will
95+
use these cached files.
96+
<br><br>
97+
This feature is supported on all modern browsers, except for IE9 which lacks application
98+
cache support.
99+
</p>
100+
<button id="cacheButton" ng:click="enableOffline()">Let me have them all!</button>
101+
</div>
102+
</div>
103+
84104
<script src="syntaxhighlighter/syntaxhighlighter-combined.js"></script>
85105
<!-- jquery place holder -->
86106
<script src="../angular.min.js" ng:autobind></script>

0 commit comments

Comments
 (0)