| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <script type="text/javascript" src="../../../../dojo/dojo.js" djConfig="isDebug: true"></script> |
|---|
| 4 | <script type="text/javascript" src="../../../../dojox/off/offline.js"></script> |
|---|
| 5 | |
|---|
| 6 | <style type="text/css"> |
|---|
| 7 | @import "../../../../dojo/resources/dojo.css"; |
|---|
| 8 | |
|---|
| 9 | /* Bring in the CSS for the default |
|---|
| 10 | Dojo Offline UI widget */ |
|---|
| 11 | @import "../../../../dojox/off/resources/offline-widget.css"; |
|---|
| 12 | </style> |
|---|
| 13 | |
|---|
| 14 | <script> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | dojox.off.ui.appName = "Hello World"; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | dojox.off.files.slurp(); |
|---|
| 23 | |
|---|
| 24 | var helloWorld = { |
|---|
| 25 | |
|---|
| 26 | _messages: [], |
|---|
| 27 | |
|---|
| 28 | initialize: function(){ |
|---|
| 29 | console.debug("helloWorld.initialize"); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | var self = this; |
|---|
| 38 | dojo.connect(dojox.off.sync, "onSync", this, function(type){ |
|---|
| 39 | if(type == "download"){ |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | self._messages.push("Hi! This is fake downloaded data!"); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | dojox.storage.put("messages", self._messages); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | dojox.off.sync.finishedDownloading(); |
|---|
| 57 | }else if(type == "finished"){ |
|---|
| 58 | |
|---|
| 59 | this._messages = []; |
|---|
| 60 | dojox.storage.remove("messages"); |
|---|
| 61 | } |
|---|
| 62 | }); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | dojo.connect(dojox.off.sync.actions, "onReplay", this, |
|---|
| 71 | function(action, actionLog){ |
|---|
| 72 | if(action.name == "new hello world message"){ |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | var message = action.data; |
|---|
| 79 | self.sendMessage(message); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | actionLog.continueReplay(); |
|---|
| 86 | } |
|---|
| 87 | }); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | if(!dojox.off.isOnline){ |
|---|
| 94 | this.loadOfflineData(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | this.printMessages(); |
|---|
| 99 | }, |
|---|
| 100 | |
|---|
| 101 | loadOfflineData: function(){ |
|---|
| 102 | console.debug("We are already offline -- loading Hello World", |
|---|
| 103 | "messages from persistent storage"); |
|---|
| 104 | |
|---|
| 105 | dojo.debug("We are already offline -- loading Hello World messages from Dojo Storage"); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | this._messages = dojox.storage.get("messages"); |
|---|
| 109 | if(!dojo.exists("_messages", this)){ |
|---|
| 110 | this._messages = []; |
|---|
| 111 | } |
|---|
| 112 | }, |
|---|
| 113 | |
|---|
| 114 | printMessages: function(){ |
|---|
| 115 | if(!this._messages.length){ |
|---|
| 116 | console.debug("No Hello World messages available"); |
|---|
| 117 | return; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | console.debug("The following Hello World messages are persistently stored:"); |
|---|
| 121 | dojo.forEach(this._messages, console.debug); |
|---|
| 122 | }, |
|---|
| 123 | |
|---|
| 124 | send: function(){ |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | var inputElem = dojo.byId("helloInput"); |
|---|
| 129 | var message = inputElem.value; |
|---|
| 130 | if(!message){ |
|---|
| 131 | alert("Please enter a Hello World message to send"); |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | if(dojox.off.isOnline){ |
|---|
| 138 | this.sendMessage(message); |
|---|
| 139 | inputElem.value = ""; |
|---|
| 140 | return; |
|---|
| 141 | }else{ |
|---|
| 142 | |
|---|
| 143 | this.saveOfflineSend(message); |
|---|
| 144 | inputElem.value = ""; |
|---|
| 145 | } |
|---|
| 146 | }, |
|---|
| 147 | |
|---|
| 148 | sendMessage: function(message){ |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | alert("Sent the following Hello World message to server: " |
|---|
| 152 | + message); |
|---|
| 153 | }, |
|---|
| 154 | |
|---|
| 155 | saveOfflineSend: function(message){ |
|---|
| 156 | |
|---|
| 157 | this._messages.push(message); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | dojox.storage.put("messages", this._messages); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | var action = {name: "new hello world message", data: message}; |
|---|
| 173 | dojox.off.sync.actions.add(action); |
|---|
| 174 | |
|---|
| 175 | alert("This message has been saved for sending when we go back online"); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | dojo.connect(dojox.off.ui, "onLoad", helloWorld, "initialize"); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | dojox.off.initialize(); |
|---|
| 187 | </script> |
|---|
| 188 | |
|---|
| 189 | </head> |
|---|
| 190 | |
|---|
| 191 | <body style="padding: 2em;"> |
|---|
| 192 | <h1>Hello World</h1> |
|---|
| 193 | |
|---|
| 194 | <h2>This is a simple Hello World program using |
|---|
| 195 | Dojo Offline. It doesn't really do much -- |
|---|
| 196 | it gives a code sample of how to work with |
|---|
| 197 | Dojo Offline, Dojo Sync, and Dojo SQL.</h2> |
|---|
| 198 | |
|---|
| 199 | <p>In our HTML, we create a DIV with the special id "dot-widget", which |
|---|
| 200 | Dojo Offline will automagically fill with a default |
|---|
| 201 | Dojo Offline widget that tells the user when they are online |
|---|
| 202 | or offline; gives them sync messages; and provides instructions |
|---|
| 203 | on how to download and install the small Dojo Offline client |
|---|
| 204 | runtime. It basically handles all of the UI tedium and hard |
|---|
| 205 | stuff of providing good offline feedback to a user. |
|---|
| 206 | Here is that widget inserted into the page:</p> |
|---|
| 207 | |
|---|
| 208 | <!-- |
|---|
| 209 | Place the Dojo Offline widget here; it will be automagically |
|---|
| 210 | filled into any element with ID "dot-widget". |
|---|
| 211 | --> |
|---|
| 212 | <div id="dot-widget"></div> |
|---|
| 213 | |
|---|
| 214 | <p>The default Dojo Offline UI will fill in your application |
|---|
| 215 | name automatically into the UI based on what you set |
|---|
| 216 | dojox.off.ui.appName to in your JavaScript |
|---|
| 217 | file; in our case we set it to "Hello World".</p> |
|---|
| 218 | |
|---|
| 219 | <p>It will also fill out the Learn How link with your |
|---|
| 220 | app name; click that link now to see an autogenerated |
|---|
| 221 | page that helps your users make sure they can work |
|---|
| 222 | with your application offline. That page automatically |
|---|
| 223 | figures out if Dojo Offline is installed or not, and |
|---|
| 224 | provides instructions on downloading it if not and |
|---|
| 225 | providing an easy link to running your application |
|---|
| 226 | offline (which is just autoset to be the URL of this |
|---|
| 227 | current page, btw)</p> |
|---|
| 228 | |
|---|
| 229 | <p>Do a View Source on this page to see the JavaScript |
|---|
| 230 | necessary to create an offline web application.</p> |
|---|
| 231 | |
|---|
| 232 | <p>As you play with this example, after installing the |
|---|
| 233 | Google Gears plugin, experiment with |
|---|
| 234 | dropping the network and going off- and on-line.</p> |
|---|
| 235 | |
|---|
| 236 | <p>To be somewhat of a real example, we need to be able |
|---|
| 237 | to have an action that can be performend either on or |
|---|
| 238 | offline. The following input lets you 'send' Hello |
|---|
| 239 | World messages to a server (it's fake -- we don't |
|---|
| 240 | really send messages). When we are online, entering |
|---|
| 241 | a message and sending it will cause the action to |
|---|
| 242 | happen immediately (we fake send it to a server); when |
|---|
| 243 | offline, we queue this up to be sent when we next go |
|---|
| 244 | online:</p> |
|---|
| 245 | |
|---|
| 246 | <div id="helloMessage" style="margin: 1em;"> |
|---|
| 247 | <label for="helloInput" |
|---|
| 248 | style="margin-right: 0.2em;"> |
|---|
| 249 | Enter Hello World Message: |
|---|
| 250 | </label> |
|---|
| 251 | |
|---|
| 252 | <input name="helloInput" |
|---|
| 253 | id="helloInput" |
|---|
| 254 | style="margin-right: 0.2em;"> |
|---|
| 255 | |
|---|
| 256 | <button id="sendMessage" |
|---|
| 257 | onclick="helloWorld.send()" |
|---|
| 258 | style="margin-right: 0.2em;">Send</button> |
|---|
| 259 | </div> |
|---|
| 260 | |
|---|
| 261 | <p>Debug output:</p> |
|---|
| 262 | </body> |
|---|
| 263 | </html> |
|---|