notes.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Returns a function, that, as long as it continues to be invoked, will not
  2. // be triggered. The function will be called after it stops being called for
  3. // N milliseconds. If `immediate` is passed, trigger the function on the
  4. // leading edge, instead of the trailing.
  5. function debounce(func, wait, immediate) {
  6. var timeout;
  7. return function() {
  8. var context = this, args = arguments;
  9. var later = function() {
  10. timeout = null;
  11. if (!immediate) func.apply(context, args);
  12. };
  13. var callNow = immediate && !timeout;
  14. clearTimeout(timeout);
  15. timeout = setTimeout(later, wait);
  16. if (callNow) func.apply(context, args);
  17. };
  18. };
  19. // we can do this as a node script or in the browser.
  20. // as a node script we don't need to request but we
  21. // need local access to the files. webdavfs
  22. // could fill the gap, since we'd have the
  23. // credentials to read notes remotely anyway.
  24. function indexNotes () {
  25. // for each path, get the note content
  26. // return makeIndex(notes);
  27. }
  28. function makeIndex (notes) {
  29. var idx = lunr(function () {
  30. this.ref('path');
  31. this.field('body');
  32. this.metadataWhitelist = ['position'];
  33. var builder = this;
  34. var i, note;
  35. for (i = 0, note = notes[i]; i < notes.length; i++) {
  36. builder.add({
  37. "path": note.path,
  38. "body": note.body
  39. });
  40. }
  41. });
  42. return idx;
  43. }
  44. function addDocument(builder) {
  45. };
  46. }
  47. function WebDavFileSystem(baseUrl) {
  48. this.baseUrl = baseUrl || "/storage";
  49. }
  50. WebDavFileSystem.prototype.list = function list(path) {
  51. var baseUrl = this.baseUrl;
  52. var url = baseUrl + path;
  53. return fetch(url, {method: "PROPFIND", credentials: 'include', mode: 'cors'})
  54. .then(function (r) { return r.text(); })
  55. .then(function (str) { return new window.DOMParser().parseFromString(str, "text/xml"); })
  56. .then(function (r) {
  57. console.log(r);
  58. var items = r.querySelectorAll("multistatus response");
  59. items = Array.prototype.slice.call(items);
  60. items = items.map(function (i) {
  61. var i = {
  62. path: i.querySelector("href").textContent,
  63. created_at: i.querySelector("prop creationdate").textContent,
  64. modified_at: i.querySelector("prop getlastmodified").textContent,
  65. length_bytes: i.querySelector("prop getcontentlength").textContent,
  66. display_name: i.querySelector("prop displayname").textContent,
  67. status: i.querySelector("status").textContent
  68. };
  69. if (i.path) {
  70. var baseUrlParts = new URL(baseUrl, document.baseURI);
  71. i.path = i.path.substr(baseUrlParts.pathname.length);
  72. }
  73. return i;
  74. });
  75. return items;
  76. });
  77. }
  78. WebDavFileSystem.prototype.upload = function upload(path, contentType, content) {
  79. var url = this.baseUrl + path;
  80. return fetch(url, {method: 'PUT', body: content, credentials: 'include', mode: 'cors'});
  81. }
  82. WebDavFileSystem.prototype.download = function download(path) {
  83. var url = this.baseUrl + path;
  84. return fetch(url, {credentials: 'include', mode: 'cors'})
  85. .then(function (r) { return r.text(); })
  86. }
  87. var fs = new WebDavFileSystem();
  88. var notesStore;
  89. function saveNote() {
  90. // post note to /upload
  91. // post new .csv to /upload (logic should be on server)
  92. }
  93. function syncNotesFromDir() {
  94. fs.list("/").then(function (files) {
  95. var IDs = files.filter(function (f) {
  96. return f.path.endsWith(".txt");
  97. });
  98. var data = IDs.map(function (id) {
  99. return [id.path, id.created_at, id.modified_at];
  100. })
  101. console.log(data);
  102. //notesStore.clear();
  103. notesStore.loadData(data, true);
  104. });
  105. }
  106. var notesStore = new Ext.data.SimpleStore({
  107. fields: ["id", "created", "modified", "title", "body", "attachment_keys"],
  108. id: 0,
  109. data: []
  110. });
  111. notesStore.on("add", function (store, records, idx) {
  112. console.log("Notes added: ");
  113. console.log(records);
  114. var noteNodes = records.map(noteToNode);
  115. var notesFolders = Ext.ComponentMgr.get("notesFolders");
  116. var notesNode = notesFolders.root;
  117. notesNode.appendChild(noteNodes);
  118. });
  119. function noteToNode(record) {
  120. var title = record.data.id;
  121. var node = new Ext.tree.TreeNode({
  122. text: title,
  123. leaf: true,
  124. noteId: record.data.id
  125. });
  126. return node;
  127. }
  128. function runNotesApp () {
  129. Ext.QuickTips.init();
  130. var viewport = Ext.getCmp('viewport');
  131. var win;
  132. var selectedNoteId = null;
  133. if (!win) {
  134. var noteNodes = notesStore.getRange().map(noteToNode);
  135. var notesNode = new Ext.tree.TreeNode({
  136. text: "Notes",
  137. expanded: true
  138. });
  139. notesNode.appendChild(noteNodes);
  140. function addHandler() {
  141. var newId = "/note-" + Math.random() + ".txt";
  142. var date = new Date().toString();
  143. newId = prompt("ID for new note:", newId);
  144. // this should be the effect of a dispatch.
  145. fs.upload(newId, "text/plain", "").then(function () {
  146. notesStore.loadData([
  147. [newId, date, date]
  148. ], true);
  149. var note = notesStore.getById(newId);
  150. //notesNode.appendChild(noteToNode(note));
  151. });
  152. }
  153. var foldersComp = {
  154. xtype: "treepanel",
  155. id: "notesFolders",
  156. tbar: [{
  157. text: 'Add',
  158. handler: addHandler
  159. }],
  160. root: notesNode,
  161. listeners: {
  162. click: function (node, evt) {
  163. var id = node.attributes.noteId;
  164. var editor = Ext.ComponentMgr.get("noteEditor");
  165. selectedNoteId = id; // includes deselect, eg. folder.
  166. if (!id) {
  167. editor.setValue("");
  168. return;
  169. }
  170. console.log("note click: " + id);
  171. var note = notesStore.getById(id);
  172. //var noteHtml = note.data.body;
  173. fs.download( note.data.id ).catch(function (err) {
  174. console.log("error: " + err.httpStatus);
  175. }).then(function (noteHtml) {
  176. var editor = Ext.ComponentMgr.get("noteEditor");
  177. editor.setValue(noteHtml);
  178. });
  179. }
  180. }
  181. };
  182. var foldersConfig = {
  183. region: "west",
  184. split: true,
  185. width: 160,
  186. layout: "fit",
  187. border: false,
  188. items: [foldersComp]
  189. };
  190. var noteConfig = {
  191. region: "center",
  192. split: true,
  193. border: false,
  194. layout: "border",
  195. items: [{
  196. region: "center",
  197. xtype: "htmleditor",
  198. html: "Note.",
  199. id: "noteEditor",
  200. border: false
  201. }, {
  202. region: "south",
  203. border: false,
  204. html: "Note stats."
  205. }]
  206. };
  207. var searchConfig = {
  208. xtype: "textfield",
  209. width: 150,
  210. emptyText: "Search..."
  211. };
  212. win = new Ext.Panel({
  213. tbar: [{
  214. xtype: "tbspacer"
  215. }, searchConfig],
  216. id: "notes-win",
  217. header: false,
  218. title: "Notes",
  219. layout: "border",
  220. bodyBorder: false,
  221. autoShow: true,
  222. //border: false,
  223. //height: 280,
  224. //width: 460,
  225. items: [foldersConfig, noteConfig],
  226. //bbar: []
  227. });
  228. }
  229. viewport.add(win);
  230. viewport.doLayout();
  231. //win.show();
  232. //win.maximize();
  233. syncNotesFromDir();
  234. var editor = Ext.ComponentMgr.get("noteEditor");
  235. editor.on("sync", debounce(function (e, html) {
  236. console.log("Editor sync. Selected = " + selectedNoteId);
  237. if (!selectedNoteId) {
  238. return;
  239. }
  240. var note = notesStore.getById(selectedNoteId);
  241. fs.upload(note.data.id, "text/plain", html);
  242. //note.beginEdit();
  243. //note.set("body", html);
  244. //note.endEdit();
  245. }, 750));
  246. }