PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/html5_experiments/db/index.html

https://github.com/paolochiodi/paolochiodi.github.com
HTML | 284 lines | 251 code | 33 blank | 0 comment | 0 complexity | 12d89d0105512be4778d25de8b4945ea MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Db | Paolo Chiodi html5 experiments</title>
  5. <meta name="description" content="Html5 Experiment Db: persistent client database in your browser with javascript (safari)" />
  6. <link rel="stylesheet" href="../css/layout.css" type="text/css" charset="utf-8" />
  7. <style type="text/css" media="screen">
  8. #msgs {
  9. list-style-image:none;
  10. list-style-position:outside;
  11. list-style-type:none;
  12. }
  13. #msgs li {
  14. margin: 10px 0;
  15. }
  16. textarea {
  17. width:500px;
  18. height:100px;
  19. color: #333;
  20. font-size: 20px;
  21. }
  22. </style>
  23. <script type="text/javascript" charset="utf-8">
  24. var offline_db = {
  25. db:null,
  26. row_count:0,
  27. init: function(){
  28. try{
  29. if(!this.db){
  30. this.db = window.openDatabase('Dbdb', '1.0', 'HTML5 Database API example', 200000);
  31. if(this.db){
  32. this.create();
  33. return true;
  34. }
  35. else{
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. catch(ex){
  42. return false
  43. }
  44. },
  45. create:function(created_callback){
  46. if(this.init()){
  47. this.db.transaction(function(tx) {
  48. tx.executeSql("SELECT COUNT(*) FROM Messages", [], function(result) {
  49. if(created_callback){
  50. created_callback();
  51. }
  52. },
  53. function(tx, error) {
  54. tx.executeSql("CREATE TABLE Messages (id REAL UNIQUE, msg TEXT, timestamp REAL)", [], function(result) {
  55. if(created_callback){
  56. created_callback();
  57. }
  58. });
  59. });
  60. });
  61. }
  62. },
  63. load_messages:function(loaded_callback){
  64. if(this.init()){
  65. this.db.transaction(function(tx){
  66. tx.executeSql("SELECT id, msg, timestamp FROM Messages", [], function(tx, result) {
  67. var ret = [];
  68. for (var i = 0, len = result.rows.length; i < len; ++i) {
  69. var row = result.rows.item(i);
  70. ret[ret.length] = {
  71. msg: row['msg'],
  72. timestamp: row['timestamp']
  73. }
  74. }
  75. this.row_count = ret.length;
  76. loaded_callback(ret);
  77. });
  78. });
  79. }
  80. },
  81. save_message: function(msg, saved_callback, error_callback){
  82. if(this.init()){
  83. this.db.transaction(function(tx){
  84. this.row_count++;
  85. var ts = (new Date()).getTime();
  86. tx.executeSql('INSERT INTO Messages (id, msg, timestamp) VALUES (?, ?, ?)',
  87. [this.row_count, msg, ts],
  88. function(tx, result){
  89. saved_callback(msg, ts);
  90. },
  91. function(tx, error){
  92. if(error_callback){
  93. error_callback();
  94. }
  95. });
  96. });
  97. }
  98. },
  99. clear_messages: function(cleared_callback, error_callback){
  100. if(this.init()){
  101. var self = this;
  102. this.db.transaction(function(tx){
  103. tx.executeSql('DELETE FROM Messages', [], function(){self.row_count=0;cleared_callback()}, function(){error_callback()});
  104. });
  105. }
  106. },
  107. }
  108. var local_storage = {
  109. db_name:'html5_experiments',
  110. init:function(){
  111. if(typeof localStorage !== 'undefined'){
  112. var db_name = this.db_name,
  113. ls = localStorage[db_name];
  114. try{
  115. if(ls == null){
  116. localStorage[db_name] = JSON.stringify([]);
  117. }
  118. }
  119. catch(ex){
  120. localStorage[db_name] = JSON.stringify([]);
  121. }
  122. return true;
  123. }
  124. return false;
  125. },
  126. load_messages:function(loaded_callback){
  127. loaded_callback(JSON.parse(localStorage[this.db_name]));
  128. },
  129. save_message:function(msg, saved_callback, error_callback){
  130. try{
  131. var db_name = this.db_name,
  132. ts = (new Date()).getTime(),
  133. el = {
  134. msg:msg,
  135. timestamp:ts
  136. },
  137. storage = JSON.parse(localStorage[db_name]);
  138. storage.push(el);
  139. localStorage[db_name] = JSON.stringify(storage);
  140. saved_callback(msg, ts);
  141. }
  142. catch(ex){
  143. error_callback();
  144. }
  145. },
  146. clear_messages:function(cleared_callback, error_callback){
  147. try{
  148. localStorage[this.db_name] = JSON.stringify([]);
  149. cleared_callback();
  150. }
  151. catch(ex){
  152. error_callback();
  153. }
  154. }
  155. }
  156. var db = null;
  157. window.onload = function(){
  158. if(offline_db.init()){
  159. db = offline_db;
  160. }
  161. else{
  162. if(local_storage.init()){
  163. db = local_storage;
  164. }
  165. }
  166. document.getElementById('store').addEventListener('click', store_click, true);
  167. document.getElementById('clear').addEventListener('click', clear_click, true);
  168. db.init();
  169. db.load_messages(messages_loaded);
  170. }
  171. function store_click(){
  172. db.save_message(document.getElementById('msg').value, message_saved, error_thrown);
  173. }
  174. function clear_click(){
  175. db.clear_messages(message_cleared, error_thrown);
  176. }
  177. function messages_loaded(m){
  178. var frag = document.createDocumentFragment();
  179. for(var i=0, len = m.length; i < len; i++){
  180. var el = m[i];
  181. append_message(el.msg, el.timestamp, frag);
  182. }
  183. document.getElementById('msgs').appendChild(frag);
  184. }
  185. function message_saved(msg, ts){
  186. append_message(msg, ts, document.getElementById('msgs'));
  187. document.getElementById('msg').value = '';
  188. }
  189. function message_cleared(){
  190. var f = document.getElementById('msgs');
  191. while(f.hasChildNodes()){
  192. f.removeChild(f.firstChild);
  193. }
  194. document.getElementById('msg').value = '';
  195. }
  196. function append_message(msg, ts, father){
  197. var l = document.createElement('li');
  198. var t = document.createTextNode(new Date(ts) + ' >> ' + msg)
  199. l.appendChild(t);
  200. if(father.hasChildNodes()){
  201. father.insertBefore(l, father.firstChild);
  202. }
  203. else{
  204. father.appendChild(l);
  205. }
  206. }
  207. function error_thrown(){
  208. alert('error');
  209. }
  210. </script>
  211. <script type="text/javascript">
  212. var _gaq = _gaq || [];
  213. _gaq.push(['_setAccount', 'UA-10675353-1']);
  214. _gaq.push(['_trackPageview']);
  215. (function() {
  216. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  217. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  218. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  219. })();
  220. </script>
  221. </head>
  222. <body>
  223. <header>
  224. <h1>HTML5 <span>demo</span></h1>
  225. </header>
  226. <nav class="menu">
  227. <ul>
  228. <li><a href="http://linkedin.com/paolochiodi" title="about me">About Me</a></li>
  229. <li><a href="../show/index.xhtml" title="video">The HTML5 Show</a></li>
  230. <li><a href="../jsboard/jsboard.html" title="jsboard">JSBoard</a></li>
  231. <li><a href="../geo/index.html" title="Geo">Geo</a></li>
  232. <li class="active"><a href="../db/index.html" title="db">Db</a></li>
  233. <li><a href="../asciitube/index.html" title="video">AsciiTube</a></li>
  234. <li><a href="../video/video.xhtml" title="video">Video</a></li>
  235. <li><a href="../../index.html" title="home">Home</a></li>
  236. </ul>
  237. </nav>
  238. <section id="main">
  239. <h2>Storeable Messages</h2>
  240. <div>
  241. <textarea id="msg"></textarea>
  242. <nav class="footer">
  243. <input id="store" type="button" value="store" />
  244. <input class="last" id="clear" type="button" value="clear" />
  245. </nav>
  246. </div>
  247. <ul id="msgs">
  248. </ul>
  249. </section>
  250. </body>
  251. </html>