PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/index.htm

https://bitbucket.org/dnteam/eu.ebabel.xcats
HTML | 297 lines | 279 code | 18 blank | 0 comment | 0 complexity | ff3e92b1b1610b171af93b46963d29f8 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>X-Cats App</title>
  7. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
  8. <style>
  9. table {
  10. border-spacing: 0;
  11. margin: 0 auto;
  12. }
  13. table th, table td {
  14. padding: 0.2em 0.5em;
  15. }
  16. table th {
  17. text-align: left;
  18. }
  19. .centre {
  20. text-align: center;
  21. }
  22. #Message {
  23. padding: 0.7em 1.5em;
  24. }
  25. </style>
  26. <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
  27. <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.js"></script>
  28. <script>
  29. // X-Cats library.
  30. (function ($) {
  31. "use strict";
  32. var settings, xcatGui, xcatData;
  33. xcatData = {
  34. addCat: function () {
  35. var catToAdd, cats;
  36. catToAdd = {
  37. catGuid: xcatData.createGuid(),
  38. name: $( settings.catName ).val(),
  39. dateOfBirth: new Date( $( settings.catDob ).val() ),
  40. mutantPower: $( settings.mutantPower ).val()
  41. };
  42. cats = xcatData.readLocalStorage( "cats" );
  43. cats.push( catToAdd );
  44. xcatData.saveLocalStorage( { key: "cats", value: cats } );
  45. return catToAdd;
  46. },
  47. readLocalStorage: function ( key ) {
  48. return ( localStorage[ key ] === undefined ) ?
  49. JSON.parse( "[]" ) :
  50. JSON.parse( localStorage[ key ] );
  51. },
  52. saveLocalStorage: function ( keyValuePair ) {
  53. localStorage[ keyValuePair.key ] = JSON.stringify(keyValuePair.value);
  54. },
  55. clearLocalStorage: function () {
  56. localStorage.clear();
  57. },
  58. addSurvey: function () {
  59. var surveyToAdd, surveys;
  60. surveyToAdd = xcatData.populateSurveyToAdd();
  61. surveys = xcatData.readLocalStorage( "surveys" );
  62. surveys.push(surveyToAdd);
  63. xcatData.saveLocalStorage( { key: "surveys", value: surveys } );
  64. localStorage.removeItem( "cats" );
  65. },
  66. populateSurveyToAdd: function () {
  67. return {
  68. surveyGuid: xcatData.createGuid(),
  69. timestamp: new Date().getTime(),
  70. surveyor: $( "#Surveyor" ).val(),
  71. cats: xcatData.readLocalStorage( "cats" )
  72. };
  73. },
  74. syncCats: function () {
  75. var json;
  76. $.ajax(
  77. {
  78. type: 'PUT',
  79. url: '/cats.php',
  80. contentType: 'application/json; charset=utf-8',
  81. dataType: 'json',
  82. data: {
  83. 'cats': JSON.stringify ( xcatData.readLocalStorage( 'cats' ) )
  84. }
  85. } )
  86. .done(function ( data )
  87. {
  88. if ( data.saved <= 0 )
  89. $( '#Message' ).html( 'You failed to save any mutant cats. This is disappointing.' );
  90. if ( data.saved === 1 )
  91. $( '#Message' ).html( 'You saved your one special mutant cat.' );
  92. if ( data.saved > 1 )
  93. $( '#Message' ).html( [ 'You\'re a hero! ', data.saved, ' mutant cats have been saved because of you!' ].join( '' ) );
  94. if ( data.saved >= 1 )
  95. {
  96. xcatData.clearLocalStorage();
  97. $( '#CatsTable tbody' ).xcatGui( 'clearTable' );
  98. }
  99. $( '#Popup' ).popup( 'open' );
  100. } )
  101. .fail(function ()
  102. {
  103. $( '#Message' ).html( 'Oops, sorry, your mutant cats haven\'t been saved!' );
  104. $( '#Popup' ).popup( 'open' );
  105. } );
  106. },
  107. createGuid: function () {
  108. var S4 = function () {
  109. return Math.floor(
  110. Math.random() * 0x10000 /* 65536 */
  111. ).toString(16);
  112. };
  113. return [ S4(), S4(), "-",
  114. S4(), "-",
  115. S4(), "-",
  116. S4(), "-",
  117. S4(), S4(), S4() ].join( "" );
  118. }
  119. };
  120. xcatGui = {
  121. init: function ( options ) {
  122. var defaults = {
  123. output: $( this.selector + " tbody" ).selector,
  124. addCat: "#AddCat",
  125. addSurvey: "#AddSurvey",
  126. catName: "#CatName",
  127. catDob: "#CatDob",
  128. mutantPower: "#MutantPower"
  129. };
  130. settings = $.extend( defaults, options );
  131. // Handle adding a survey submit.
  132. $(settings.addSurvey).on( "submit", function ( event ) {
  133. xcatData.addSurvey();
  134. $( settings.output ).xcatGui( "clearTable" );
  135. event.preventDefault();
  136. });
  137. },
  138. populateCatsTable: function ( cats ) {
  139. var index;
  140. for ( index = 0; index < cats.length; index += 1 ) {
  141. $( this ).append( "<tr><td>" + cats[ index ].name + "</td><td>" + new Date(cats[ index ].dateOfBirth ).toDateString() + "</td><td>" + cats[ index ].mutantPower + "</td></tr>" );
  142. }
  143. },
  144. appendCatToTable: function () {
  145. xcatData.addCat();
  146. },
  147. populateCatsTableFromLocalStorage: function () {
  148. $( this ).xcatGui( "populateCatsTable", xcatData.readLocalStorage( "cats" ) );
  149. },
  150. clearTable: function () {
  151. $( this ).html( "" );
  152. },
  153. syncCatsToServer: function () {
  154. xcatData.syncCats();
  155. }
  156. };
  157. // Method calling logic
  158. $.fn.xcatGui = function ( method ) {
  159. if ( xcatGui[ method ] ) {
  160. return xcatGui[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
  161. }
  162. if ( typeof method === "object" || !method ) {
  163. return xcatGui.init.apply( this, arguments );
  164. }
  165. $.error( "Method " + method + " does not exist on jQuery.xcatGui" );
  166. };
  167. } ) ( jQuery );
  168. </script>
  169. </head>
  170. <body>
  171. <div data-role="page" id="ListXCats">
  172. <div data-role="header">
  173. <a href="#ListXCats" data-icon="home" data-iconpos="notext" class="ui-btn-active"></a>
  174. <h1>X-Cats App</h1>
  175. </div>
  176. <div data-role="content">
  177. <table data-role="table" class="ui-body-d" id="CatsTable">
  178. <thead>
  179. <tr class="ui-bar-d">
  180. <th>Name</th>
  181. <th data-priority="2">DOB</th>
  182. <th data-priority="1">Power</th>
  183. </tr>
  184. </thead>
  185. <tbody></tbody>
  186. </table>
  187. </div>
  188. <div data-role="footer" data-position="fixed" class="ui-bar centre">
  189. <div data-role="controlgroup" data-type="horizontal">
  190. <a href="#AddXCat" data-role="button" data-icon="plus" data-transition="slide">Add</a>
  191. <a href="#CompleteSurvey" data-role="button" data-icon="refresh" data-transition="slide">Sync</a>
  192. </div>
  193. </div>
  194. </div>
  195. <div data-role="page" id="AddXCat">
  196. <div data-role="header">
  197. <a href="#ListXCats" data-icon="home" data-iconpos="notext" data-direction="reverse" data-transition="slide"></a>
  198. <h1>Add a new X-Cat</h1>
  199. </div>
  200. <div data-role="content">
  201. <form action="#AddXCat" method="post" id="AddCat">
  202. <p data-role="fieldcontain">
  203. <label for="CatName">Name: </label>
  204. <input type="text" id="CatName" />
  205. </p>
  206. <p data-role="fieldcontain">
  207. <label for="CatDob">Date of birth: </label>
  208. <input type="date" id="CatDob" value="" />
  209. </p>
  210. <p data-role="fieldcontain">
  211. <label for="MutantPower">Mutant power</label>
  212. <input type="text" id="MutantPower" />
  213. </p>
  214. <p class="submitButton">
  215. <input type="button" value="Add new cat" data-theme="b" id="AddCatButton" />
  216. </p>
  217. </form>
  218. </div>
  219. <div data-role="footer" data-position="fixed" class="ui-bar centre">
  220. <div data-role="controlgroup" data-type="horizontal">
  221. <a href="#AddXCat" data-role="button" data-icon="plus" class="ui-btn-active">Add</a>
  222. <a href="#CompleteSurvey" data-role="button" data-icon="refresh" data-transition="slide">Sync</a>
  223. </div>
  224. </div>
  225. </div>
  226. <div data-role="page" id="CompleteSurvey">
  227. <div data-role="header">
  228. <a href="#ListXCats" data-icon="home" data-iconpos="notext" data-direction="reverse" data-transition="slide"></a>
  229. <h1>Sync X-Cats</h1>
  230. </div>
  231. <div data-role="content">
  232. <form action="#AddXCat" method="post" id="AddCat">
  233. <p class="centre">
  234. Please confirm you want to sync your XCats with our mutant server.
  235. </p>
  236. <p class="submitButton">
  237. <input type="button" value="X-Cats GO!" data-theme="b" id="SyncButton" />
  238. </p>
  239. </form>
  240. <div data-role="popup" id="Popup">
  241. <a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
  242. <p id="Message"><p>
  243. </div>
  244. </div>
  245. <div data-role="footer" data-position="fixed" class="ui-bar centre">
  246. <div data-role="controlgroup" data-type="horizontal">
  247. <a href="#AddXCat" data-role="button" data-icon="plus" data-transition="slide">Add</a>
  248. <a href="#CompleteSurvey" data-role="button" data-icon="refresh" class="ui-btn-active">Sync</a>
  249. </div>
  250. </div>
  251. </div>
  252. <script>
  253. // todo: 1. Syncing via ajax with a php/mysql web API.
  254. $( document ).bind( 'pageinit', function ( e ) {
  255. // Initialise the plugin.
  256. $( "#CatsTable" ).xcatGui();
  257. } );
  258. $( "#ListXCats" ).bind ( "pagebeforecreate", function ( e ) {
  259. // Display the cats.
  260. $( "#CatsTable tbody" ).xcatGui( "populateCatsTableFromLocalStorage" );
  261. } );
  262. $( "#AddXCat" ).bind ( "pagebeforecreate", function ( e ) {
  263. // Handle adding an X-Cat.
  264. $( "#AddCatButton" ).on ( "click", function ( event ) {
  265. $( "#CatsTable tbody" ).xcatGui( "appendCatToTable" );
  266. $.mobile.changePage( "#ListXCats", { transition: "slide", reverse: true } );
  267. $( "#CatsTable tbody" ).xcatGui( "clearTable" );
  268. $( "#CatsTable tbody" ).xcatGui( "populateCatsTableFromLocalStorage" );
  269. } );
  270. } );
  271. $( "#CompleteSurvey" ).bind ( "pagebeforecreate", function ( e ) {
  272. // Handle syncing all the X-Cats to the mutant server.
  273. $( "#SyncButton" ).on ( "click", function ( event ) {
  274. $( "#CatsTable tbody" ).xcatGui( "syncCatsToServer" );
  275. } );
  276. } );
  277. </script>
  278. </body>
  279. </html>