Advertisement
Guest User

Untitled

a guest
Aug 25th, 2011
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. var db = null;
  2. try {
  3. if (window.openDatabase) {
  4. db = openDatabase("NoteTest", "1.0", "HTML5 Database API example", 200000);
  5. if (!db) alert("Failed to open the database on disk. This is probably because the version was bad or there is not enough space left in this domain's quota");
  6. } else alert("Couldn't open the database. Please try with a WebKit nightly with this feature enabled");
  7. } catch (err) {
  8. db = null;
  9. alert("Couldn't open the database. Please try with a WebKit nightly with this feature enabled");
  10. }
  11. var captured = null;
  12. var highestZ = 0;
  13. var highestId = 0;
  14.  
  15. function Note() {
  16. var self = this;
  17. var note = document.createElement('div');
  18. note.className = 'note';
  19. note.addEventListener('mousedown', function (e) {
  20. return self.onMouseDown(e)
  21. }, false);
  22. note.addEventListener('click', function () {
  23. return self.onNoteClick()
  24. }, false);
  25. this.note = note;
  26. var close = document.createElement('div');
  27. close.className = 'closebutton';
  28. close.addEventListener('click', function (event) {
  29. return self.close(event)
  30. }, false);
  31. note.appendChild(close);
  32. var edit = document.createElement('div');
  33. edit.className = 'edit';
  34. edit.setAttribute('contenteditable', true);
  35. edit.addEventListener('keyup', function () {
  36. return self.onKeyUp()
  37. }, false);
  38. note.appendChild(edit);
  39. this.editField = edit;
  40. var ts = document.createElement('div');
  41. ts.className = 'timestamp';
  42. ts.addEventListener('mousedown', function (e) {
  43. return self.onMouseDown(e)
  44. }, false);
  45. note.appendChild(ts);
  46. this.lastModified = ts;
  47. document.body.appendChild(note);
  48. return this;
  49. }
  50. Note.prototype = {
  51. get id() {
  52. if (!("_id" in this)) this._id = 0;
  53. return this._id;
  54. }, set id(x) {
  55. this._id = x;
  56. }, get text() {
  57. return this.editField.innerHTML;
  58. }, set text(x) {
  59. this.editField.innerHTML = x;
  60. }, get timestamp() {
  61. if (!("_timestamp" in this)) this._timestamp = 0;
  62. return this._timestamp;
  63. }, set timestamp(x) {
  64. if (this._timestamp == x) return;
  65. this._timestamp = x;
  66. var date = new Date();
  67. date.setTime(parseFloat(x));
  68. this.lastModified.textContent = modifiedString(date);
  69. }, get left() {
  70. return this.note.style.left;
  71. }, set left(x) {
  72. this.note.style.left = x;
  73. }, get top() {
  74. return this.note.style.top;
  75. }, set top(x) {
  76. this.note.style.top = x;
  77. }, get zIndex() {
  78. return this.note.style.zIndex;
  79. }, set zIndex(x) {
  80. this.note.style.zIndex = x;
  81. }, close: function (event) {
  82. this.cancelPendingSave();
  83. var note = this;
  84. db.transaction(function (tx) {
  85. tx.executeSql("DELETE FROM WebKitStickyNotes WHERE id = ?", [note.id]);
  86. });
  87. var duration = event.shiftKey ? 2 : .25;
  88. this.note.style.webkitTransition = '-webkit-transform ' + duration + 's ease-in, opacity ' + duration + 's ease-in';
  89. this.note.offsetTop; // Force style recalc
  90. this.note.style.webkitTransformOrigin = "0 0";
  91. this.note.style.webkitTransform = 'skew(30deg, 0deg) scale(0)';
  92. this.note.style.opacity = '0';
  93. var self = this;
  94. setTimeout(function () {
  95. document.body.removeChild(self.note)
  96. }, duration * 1000);
  97. },
  98. saveSoon: function () {
  99. this.cancelPendingSave();
  100. var self = this;
  101. this._saveTimer = setTimeout(function () {
  102. self.save()
  103. }, 200);
  104. },
  105. cancelPendingSave: function () {
  106. if (!("_saveTimer" in this)) return;
  107. clearTimeout(this._saveTimer);
  108. delete this._saveTimer;
  109. },
  110. save: function () {
  111. this.cancelPendingSave();
  112. if ("dirty" in this) {
  113. this.timestamp = new Date().getTime();
  114. delete this.dirty;
  115. }
  116. var note = this;
  117. db.transaction(function (tx) {
  118. tx.executeSql("UPDATE WebKitStickyNotes SET note = ?, timestamp = ?, left = ?, top = ?, zindex = ? WHERE id = ?", [note.text, note.timestamp, note.left, note.top, note.zIndex, note.id]);
  119. });
  120. },
  121. saveAsNew: function () {
  122. this.timestamp = new Date().getTime();
  123. var note = this;
  124. db.transaction(function (tx) {
  125. tx.executeSql("INSERT INTO WebKitStickyNotes (id, note, timestamp, left, top, zindex) VALUES (?, ?, ?, ?, ?, ?)", [note.id, note.text, note.timestamp, note.left, note.top, note.zIndex]);
  126. });
  127. },
  128. onMouseDown: function (e) {
  129. captured = this;
  130. this.startX = e.clientX - this.note.offsetLeft;
  131. this.startY = e.clientY - this.note.offsetTop;
  132. this.zIndex = ++highestZ;
  133. var self = this;
  134. if (!("mouseMoveHandler" in this)) {
  135. this.mouseMoveHandler = function (e) {
  136. return self.onMouseMove(e)
  137. }
  138. this.mouseUpHandler = function (e) {
  139. return self.onMouseUp(e)
  140. }
  141. }
  142. document.addEventListener('mousemove', this.mouseMoveHandler, true);
  143. document.addEventListener('mouseup', this.mouseUpHandler, true);
  144. return false;
  145. },
  146. onMouseMove: function (e) {
  147. if (this != captured) return true;
  148. this.left = e.clientX - this.startX + 'px';
  149. this.top = e.clientY - this.startY + 'px';
  150. return false;
  151. },
  152. onMouseUp: function (e) {
  153. document.removeEventListener('mousemove', this.mouseMoveHandler, true);
  154. document.removeEventListener('mouseup', this.mouseUpHandler, true);
  155. this.save();
  156. return false;
  157. },
  158. onNoteClick: function (e) {
  159. this.editField.focus();
  160. getSelection().collapseToEnd();
  161. },
  162. onKeyUp: function () {
  163. this.dirty = true;
  164. this.saveSoon();
  165. },
  166. }
  167. function loaded() {
  168. db.transaction(function (tx) {
  169. tx.executeSql("SELECT COUNT(*) FROM WebkitStickyNotes", [], function (result) {
  170. loadNotes();
  171. }, function (tx, error) {
  172. tx.executeSql("CREATE TABLE WebKitStickyNotes (id REAL UNIQUE, note TEXT, timestamp REAL, left TEXT, top TEXT, zindex REAL)", [], function (result) {
  173. loadNotes();
  174. });
  175. });
  176. });
  177. }
  178. function loadNotes() {
  179. db.transaction(function (tx) {
  180. tx.executeSql("SELECT id, note, timestamp, left, top, zindex FROM WebKitStickyNotes", [], function (tx, result) {
  181. for (var i = 0; i < result.rows.length; ++i) {
  182. var row = result.rows.item(i);
  183. var note = new Note();
  184. note.id = row['id'];
  185. note.text = row['note'];
  186. note.timestamp = row['timestamp'];
  187. note.left = row['left'];
  188. note.top = row['top'];
  189. note.zIndex = row['zindex'];
  190. if (row['id'] > highestId) highestId = row['id'];
  191. if (row['zindex'] > highestZ) highestZ = row['zindex'];
  192. }
  193. if (!result.rows.length) newNote();
  194. }, function (tx, error) {
  195. alert('Failed to retrieve notes from database - ' + error.message);
  196. return;
  197. });
  198. });
  199. }
  200. function modifiedString(date) {
  201. return 'Last Modified: ' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
  202. }
  203. function newNote() {
  204. var note = new Note();
  205. note.id = ++highestId;
  206. note.timestamp = new Date().getTime();
  207. note.left = Math.round(Math.random() * 400) + 'px';
  208. note.top = Math.round(Math.random() * 500) + 'px';
  209. note.zIndex = ++highestZ;
  210. note.saveAsNew();
  211. }
  212. if (db != null) addEventListener('load', loaded, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement