PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/org.dojotoolkit.dojo.1.3/samples/offline/helloworld/helloworld.html

https://github.com/victor-homyakov/frameworks
HTML | 263 lines | 213 code | 46 blank | 4 comment | 0 complexity | 1fb4af18bfd89c33d9e4b71826f3ca20 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="../lib/dojo/dojo.js" djConfig="isDebug: true"></script>
  4. <script type="text/javascript" src="../lib/dojox/off/offline.js"></script>
  5. <style type="text/css">
  6. @import "../lib/dojo/resources/dojo.css";
  7. /* Bring in the CSS for the default
  8. Dojo Offline UI widget */
  9. @import "../lib/dojox/off/resources/offline-widget.css";
  10. </style>
  11. <script>
  12. // configure how we should work offline
  13. // set our application name
  14. dojox.off.ui.appName = "Hello World";
  15. // automatically "slurp" the page and
  16. // capture the resources we need offline
  17. dojox.off.files.slurp();
  18. var helloWorld = {
  19. /* hello world messages to be sent */
  20. _messages: [],
  21. initialize: function(){
  22. console.debug("helloWorld.initialize");
  23. // Dojo Offline and Dojo Storage are ready to be used;
  24. // the page is also finished loading
  25. // listen for syncing events; there are several different
  26. // syncing event types returned, but we only care about
  27. // two: "download" and "finished"
  28. var self = this;
  29. dojo.connect(dojox.off.sync, "onSync", this, function(type){
  30. if(type == "download"){
  31. // the syncing process is at the download stage --
  32. // download any data that is necessary to work
  33. // offline
  34. // call some network service -- in our case
  35. // we just add some fake data.
  36. self._messages.push("Hi! This is fake downloaded data!");
  37. // persist this fake data into Dojo Storage
  38. dojox.storage.put("messages", self._messages);
  39. // now tell Dojo Sync to continue its syncing
  40. // process; a real network service would be
  41. // asychronous, so we would have to tell the
  42. // syncing system to continue at a later point
  43. // when we got our network results back
  44. dojox.off.sync.finishedDownloading();
  45. }else if(type == "finished"){
  46. // the syncing process is finished
  47. this._messages = [];
  48. dojox.storage.remove("messages");
  49. }
  50. });
  51. // While offline, as a user works, we create action objects
  52. // to represent what they have done while offline, adding
  53. // them to an action log that will automatically be persisted.
  54. // When we go back online, Dojo Sync will replay each of
  55. // these action, handing them to our onReplay handler
  56. // for us to handle and act upon now that we have a network
  57. dojo.connect(dojox.off.sync.actions, "onReplay", this,
  58. function(action, actionLog){
  59. if(action.name == "new hello world message"){
  60. // this was a hello world message created while offline
  61. // In a real application, we would probably call some network
  62. // service to replay this action, such as sending an email,
  63. // creating a task, etc. Instead, we just do an alert box.
  64. var message = action.data;
  65. self.sendMessage(message);
  66. // In a real application, the network call would be
  67. // asynchronous so we don't lock up the UI. For this reason,
  68. // we have to tell Dojo Sync when to continue replaying the
  69. // Action Log
  70. actionLog.continueReplay();
  71. }
  72. });
  73. // did the page load with us already offline? if so,
  74. // we will have to load our data from persistent storage for
  75. // the user to be able to work with since we can't
  76. // load it from the network
  77. if(!dojox.off.isOnline){
  78. this.loadOfflineData();
  79. }
  80. // print out what messages we have
  81. this.printMessages();
  82. },
  83. loadOfflineData: function(){
  84. console.debug("We are already offline -- loading Hello World",
  85. "messages from persistent storage");
  86. dojo.debug("We are already offline -- loading Hello World messages from Dojo Storage");
  87. // get our Hello World messages from Dojo Storage
  88. this._messages = dojox.storage.get("messages");
  89. if(!dojo.exists("_messages", this)){
  90. this._messages = [];
  91. }
  92. },
  93. printMessages: function(){
  94. if(!this._messages.length){
  95. console.debug("No Hello World messages available");
  96. return;
  97. }
  98. console.debug("The following Hello World messages are persistently stored:");
  99. dojo.forEach(this._messages, console.debug);
  100. },
  101. send: function(){
  102. // called when the send button is pressed
  103. // get the text to send
  104. var inputElem = dojo.byId("helloInput");
  105. var message = inputElem.value;
  106. if(!message){
  107. alert("Please enter a Hello World message to send");
  108. return;
  109. }
  110. // if we are online, immediately 'send' this
  111. // message to a network service
  112. if(dojox.off.isOnline){
  113. this.sendMessage(message);
  114. inputElem.value = "";
  115. return;
  116. }else{
  117. // we are offline
  118. this.saveOfflineSend(message);
  119. inputElem.value = "";
  120. }
  121. },
  122. sendMessage: function(message){
  123. // fake 'sending' of Hello World message to a network service -- we
  124. // just do an alert box
  125. alert("Sent the following Hello World message to server: "
  126. + message);
  127. },
  128. saveOfflineSend: function(message){
  129. // first, add this to our list of messages
  130. this._messages.push(message);
  131. // now, persist this into Dojo Storage so it is available
  132. // in the future if we don't reach a network while the
  133. // browser is open
  134. dojox.storage.put("messages", this._messages);
  135. // Dojo Sync has us represent offline actions as action
  136. // objects -- these can have anything we want inside of them.
  137. // When we create them, they will be added to an offline
  138. // Action Log, and when we go back online this log will
  139. // simply be replayed. Each of the action objects that we
  140. // added while offline will be handed to our onReplay handler
  141. // for us to deal with later, so we have to place enough info
  142. // in here to be useful later on
  143. var action = {name: "new hello world message", data: message};
  144. dojox.off.sync.actions.add(action);
  145. alert("This message has been saved for sending when we go back online");
  146. }
  147. }
  148. // Wait until Dojo Offline is ready
  149. // before we initialize ourselves. When this gets called the page
  150. // is also finished loading.
  151. dojo.connect(dojox.off.ui, "onLoad", helloWorld, "initialize");
  152. // tell Dojo Offline we are ready for it to initialize itself now
  153. // that we have finished configuring it for our application
  154. dojox.off.initialize();
  155. </script>
  156. </head>
  157. <body style="padding: 2em;">
  158. <h1>Hello World</h1>
  159. <h2>This is a simple Hello World program using
  160. Dojo Offline. It doesn't really do much --
  161. it gives a code sample of how to work with
  162. Dojo Offline, Dojo Sync, and Dojo SQL.</h2>
  163. <p>In our HTML, we create a DIV with the special id "dot-widget", which
  164. Dojo Offline will automagically fill with a default
  165. Dojo Offline widget that tells the user when they are online
  166. or offline; gives them sync messages; and provides instructions
  167. on how to download and install the small Dojo Offline client
  168. runtime. It basically handles all of the UI tedium and hard
  169. stuff of providing good offline feedback to a user.
  170. Here is that widget inserted into the page:</p>
  171. <!--
  172. Place the Dojo Offline widget here; it will be automagically
  173. filled into any element with ID "dot-widget".
  174. -->
  175. <div id="dot-widget"></div>
  176. <p>The default Dojo Offline UI will fill in your application
  177. name automatically into the UI based on what you set
  178. dojox.off.ui.appName to in your JavaScript
  179. file; in our case we set it to "Hello World".</p>
  180. <p>It will also fill out the Learn How link with your
  181. app name; click that link now to see an autogenerated
  182. page that helps your users make sure they can work
  183. with your application offline. That page automatically
  184. figures out if Dojo Offline is installed or not, and
  185. provides instructions on downloading it if not and
  186. providing an easy link to running your application
  187. offline (which is just autoset to be the URL of this
  188. current page, btw)</p>
  189. <p>Do a View Source on this page to see the JavaScript
  190. necessary to create an offline web application.</p>
  191. <p>As you play with this example, after installing the
  192. Google Gears plugin, experiment with
  193. dropping the network and going off- and on-line.</p>
  194. <p>To be somewhat of a real example, we need to be able
  195. to have an action that can be performend either on or
  196. offline. The following input lets you 'send' Hello
  197. World messages to a server (it's fake -- we don't
  198. really send messages). When we are online, entering
  199. a message and sending it will cause the action to
  200. happen immediately (we fake send it to a server); when
  201. offline, we queue this up to be sent when we next go
  202. online:</p>
  203. <div id="helloMessage" style="margin: 1em;">
  204. <label for="helloInput"
  205. style="margin-right: 0.2em;">
  206. Enter Hello World Message:
  207. </label>
  208. <input name="helloInput"
  209. id="helloInput"
  210. style="margin-right: 0.2em;">
  211. <button id="sendMessage"
  212. onclick="helloWorld.send()"
  213. style="margin-right: 0.2em;">Send</button>
  214. </div>
  215. <p>Debug output:</p>
  216. </body>
  217. </html>