[Author: steveblock]
[google-gears.git] / gears / sdk / samples / hello_world_resourcestore.html
blob2827eac056ec17004ed504eb32f29c5acbd92fd9
1 <!--
2 Copyright 2007, Google Inc.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 3. Neither the name of Google Inc. nor the names of its contributors may be
13 used to endorse or promote products derived from this software without
14 specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 -->
28 <!DOCTYPE html>
30 <html>
31 <head>
32 <title>Hello World ResourceStore</title>
33 <link rel="stylesheet" type="text/css" href="sample.css">
34 </head>
35 <body>
37 <h1>Google Gears ResourceStore Demo</h1>
38 <div id="view-source">&nbsp;</div>
40 <form>
41 <input type="button" value="Create Store" onclick="createStore();">
42 <input type="button" value="Capture" onclick="capture();">
43 <input type="button" value="Uncapture" onclick="uncapture();">
44 <input type="button" value="Remove Store" onclick="removeStore();">
45 </form>
46 <p id="status">
47 <p id="status-1"></p>
48 <hr>
49 <p><b>This page demonstrates basic usage of ResourceStore.</b>
51 <p>Things you can do:
52 <ul>
53 <li>Press the 'CreateStore' button to create a resource store to contain captured resources.
54 <li>Press the 'Capture' button to capture this page.
55 <ul>
56 <li>Unplug the network cable and verify that you can still access this page.
57 <li>Delete your browser cache and verify that you can still access this page.
58 </ul>
59 <li>Press the 'Uncapture' button to remove the page from the ResourceStore. Verify
60 that you can no longer access the page offline.
61 <li>Press the 'RemoveStore' button to remove the resource store.
62 </ul>
64 <!-- ====================================== -->
65 <!-- End HTML code. Begin JavaScript code. -->
67 <script type="text/javascript" src="../gears_init.js"></script>
68 <script type="text/javascript" src="sample.js"></script>
69 <script>
70 var STORE_NAME = 'helloworld-store'
71 var localServer;
72 var store;
74 var filesToCapture = [
75 location.pathname,
76 'sample.js',
77 'sample.css',
78 '../gears_init.js'
81 init();
83 function init() {
84 if (!window.google || !google.gears) {
85 addStatus('Google Gears is not installed', 'error');
86 return;
89 try {
90 localServer =
91 google.gears.factory.create('beta.localserver');
92 } catch (ex) {
93 var buttons = document.forms[0].elements;
94 for (var i = 0, el; el = buttons[i]; i++) {
95 el.disabled = true;
98 setError('Could not create local server: ' + ex.message);
100 return;
102 store = localServer.openStore(STORE_NAME);
103 addStatus('Ready');
106 function createStore() {
107 if (!checkProtocol()) return;
109 // If the store already exists, it will be opened
110 store = localServer.createStore(STORE_NAME);
112 clearStatus();
113 addStatus('Created the store');
116 function capture() {
117 if (!store) {
118 setError('Please create a store for the captured resources');
119 return;
122 clearStatus();
123 addStatus('Capturing...');
125 // Capture this page and the js library we need to run offline.
126 store.capture(filesToCapture, captureCallback);
129 function captureCallback(url, success, captureId) {
130 addStatus(url + ' captured ' + (success ? 'succeeded' : 'failed'));
133 function uncapture() {
134 if (!store) {
135 setError('Please create a store for the captured resources');
136 return;
139 for (var i = 0; i < filesToCapture.length; i++) {
140 store.remove(filesToCapture[i]);
143 clearStatus();
144 addStatus('Removed files from the store');
147 function removeStore() {
148 // We call openStore() to test for it's existence prior to removing it
149 if (localServer.openStore(STORE_NAME)) {
150 localServer.removeStore(STORE_NAME);
151 store = null;
152 clearStatus();
153 addStatus('Removed the store');
154 } else {
155 clearStatus();
156 addStatus('The store does not exist', 'error');
160 </script>
161 </body>
162 </html>