1 | /* |
---|
2 | |
---|
3 | _/ _/_/ _/_/_/_/_/ _/ |
---|
4 | _/ _/ _/ _/_/ _/ _/ _/_/_/ _/_/_/ |
---|
5 | _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/ |
---|
6 | _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ |
---|
7 | _/ _/_/ _/ _/ _/_/ _/_/_/ _/_/_/ _/ _/ |
---|
8 | _/ |
---|
9 | _/ |
---|
10 | |
---|
11 | Created by David Kaneda <http://www.davidkaneda.com> |
---|
12 | Documentation and issue tracking on Google Code <http://code.google.com/p/jqtouch/> |
---|
13 | |
---|
14 | Special thanks to Jonathan Stark <http://jonathanstark.com/> |
---|
15 | |
---|
16 | Lots of this code is specifically derived from Jonathan's book, |
---|
17 | "Building iPhone Apps with HTML, CSS, and JavaScript" |
---|
18 | |
---|
19 | (c) 2009 by jQTouch project members. |
---|
20 | See LICENSE.txt for license. |
---|
21 | |
---|
22 | */ |
---|
23 | |
---|
24 | (function($) { |
---|
25 | if ($.jQTouch) |
---|
26 | { |
---|
27 | $.jQTouch.addExtension(function Offline(){ |
---|
28 | |
---|
29 | // Convenience array of status values |
---|
30 | var cacheStatusValues = []; |
---|
31 | cacheStatusValues[0] = 'uncached'; |
---|
32 | cacheStatusValues[1] = 'idle'; |
---|
33 | cacheStatusValues[2] = 'checking'; |
---|
34 | cacheStatusValues[3] = 'downloading'; |
---|
35 | cacheStatusValues[4] = 'updateready'; |
---|
36 | cacheStatusValues[5] = 'obsolete'; |
---|
37 | |
---|
38 | // Listeners for all possible events |
---|
39 | var cache = window.applicationCache; |
---|
40 | cache.addEventListener('cached', logEvent, false); |
---|
41 | cache.addEventListener('checking', logEvent, false); |
---|
42 | cache.addEventListener('downloading', logEvent, false); |
---|
43 | cache.addEventListener('error', logEvent, false); |
---|
44 | cache.addEventListener('noupdate', logEvent, false); |
---|
45 | cache.addEventListener('obsolete', logEvent, false); |
---|
46 | cache.addEventListener('progress', logEvent, false); |
---|
47 | cache.addEventListener('updateready', logEvent, false); |
---|
48 | |
---|
49 | // Log every event to the console |
---|
50 | function logEvent(e) { |
---|
51 | var online, status, type, message; |
---|
52 | online = (isOnline()) ? 'yes' : 'no'; |
---|
53 | status = cacheStatusValues[cache.status]; |
---|
54 | type = e.type; |
---|
55 | message = 'online: ' + online; |
---|
56 | message+= ', event: ' + type; |
---|
57 | message+= ', status: ' + status; |
---|
58 | if (type == 'error' && navigator.onLine) { |
---|
59 | message+= ' There was an unknown error, check your Cache Manifest.'; |
---|
60 | } |
---|
61 | console.log(message); |
---|
62 | } |
---|
63 | |
---|
64 | function isOnline() { |
---|
65 | return navigator.onLine; |
---|
66 | } |
---|
67 | |
---|
68 | if (!$('html').attr('manifest')) { |
---|
69 | console.log('No Cache Manifest listed on the <html> tag.') |
---|
70 | } |
---|
71 | |
---|
72 | // Swap in newly download files when update is ready |
---|
73 | cache.addEventListener('updateready', function(e){ |
---|
74 | // Don't perform "swap" if this is the first cache |
---|
75 | if (cacheStatusValues[cache.status] != 'idle') { |
---|
76 | cache.swapCache(); |
---|
77 | console.log('Swapped/updated the Cache Manifest.'); |
---|
78 | } |
---|
79 | } |
---|
80 | , false); |
---|
81 | |
---|
82 | // These two functions check for updates to the manifest file |
---|
83 | function checkForUpdates(){ |
---|
84 | cache.update(); |
---|
85 | } |
---|
86 | function autoCheckForUpdates(){ |
---|
87 | setInterval(function(){cache.update()}, 10000); |
---|
88 | } |
---|
89 | |
---|
90 | return { |
---|
91 | isOnline: isOnline, |
---|
92 | checkForUpdates: checkForUpdates, |
---|
93 | autoCheckForUpdates: autoCheckForUpdates |
---|
94 | } |
---|
95 | }); |
---|
96 | } |
---|
97 | })(jQuery); |
---|