core.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. var superagent = require('superagent');
  3. function guid() {
  4. function s4() {
  5. return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  6. }
  7. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  8. }
  9. function errorWrapper(callback) {
  10. return function (error, result) {
  11. if (error && error.status === 401) {
  12. window.location.href = '/auth/login';
  13. return;
  14. }
  15. callback(error, result);
  16. };
  17. }
  18. function Thing(id, createdAt, tags, content, richContent) {
  19. this.id = id;
  20. this.createdAt = createdAt || 0;
  21. this.tags = tags || [];
  22. this.content = content;
  23. this.richContent = richContent;
  24. }
  25. function Things(server) {
  26. this._server = server;
  27. this._addCallbacks = [];
  28. this._editCallbacks = [];
  29. this._delCallbacks = [];
  30. this.data = [];
  31. this._operation = '';
  32. }
  33. Things.prototype.get = function (filter, callback) {
  34. var that = this;
  35. var url = this._server + '/api/things';
  36. var operation = guid();
  37. this._operation = operation;
  38. if (filter) url += '?filter=' + encodeURIComponent(filter);
  39. superagent.get(url).end(errorWrapper(function (error, result) {
  40. // ignore this if we moved on
  41. if (that._operation !== operation) {
  42. console.log('ignore this call');
  43. return;
  44. }
  45. if (error) return callback(error);
  46. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  47. that.data = result.body.things.map(function (thing) {
  48. return new Thing(thing._id, new Date(thing.createdAt).getTime(), thing.tags, thing.content, thing.richContent);
  49. });
  50. callback(null, that.data);
  51. }));
  52. };
  53. Things.prototype.add = function (content, callback) {
  54. var that = this;
  55. var url = this._server + '/api/things';
  56. superagent.post(url).send({ content: content }).end(errorWrapper(function (error, result) {
  57. if (error) return callback(error);
  58. if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  59. that._addCallbacks.forEach(function (callback) {
  60. setTimeout(callback, 0);
  61. });
  62. callback(null);
  63. }));
  64. };
  65. Things.prototype.edit = function (thing, callback) {
  66. var that = this;
  67. var url = this._server + '/api/things/' + thing.id;
  68. superagent.put(url).send({ content: thing.content }).end(errorWrapper(function (error, result) {
  69. if (error) return callback(error);
  70. if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  71. that._editCallbacks.forEach(function (callback) {
  72. setTimeout(callback, 0);
  73. });
  74. callback(null);
  75. }));
  76. };
  77. Things.prototype.del = function (thing, callback) {
  78. var that = this;
  79. var url = this._server + '/api/things/' + thing.id;
  80. superagent.del(url).end(errorWrapper(function (error, result) {
  81. if (error) return callback(error);
  82. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  83. that._delCallbacks.forEach(function (callback) {
  84. setTimeout(callback, 0);
  85. });
  86. callback(null);
  87. }));
  88. };
  89. Things.prototype.onAdded = function (callback) {
  90. this._addCallbacks.push(callback);
  91. };
  92. Things.prototype.onEdited = function (callback) {
  93. this._editCallbacks.push(callback);
  94. };
  95. Things.prototype.onDeleted = function (callback) {
  96. this._delCallbacks.push(callback);
  97. };
  98. Things.prototype.export = function () {
  99. window.location.href = this._server + '/api/export';
  100. };
  101. function Settings(server) {
  102. this._server = server;
  103. this._changeCallbacks = [];
  104. this.data = {
  105. title: '',
  106. backgroundUrl: ''
  107. };
  108. }
  109. Settings.prototype.save = function (callback) {
  110. var url = this._server + '/api/settings';
  111. superagent.post(url).send({ settings: this.data }).end(errorWrapper(function (error, result) {
  112. if (error) return callback(error);
  113. if (result.status !== 202) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  114. callback(null);
  115. }));
  116. };
  117. Settings.prototype.get = function (callback) {
  118. var that = this;
  119. var url = this._server + '/api/settings';
  120. superagent.get(url).end(errorWrapper(function (error, result) {
  121. if (error) return callback(error);
  122. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  123. that.set(result.body.settings);
  124. callback(null, result.body.settings);
  125. }));
  126. };
  127. Settings.prototype.set = function (data) {
  128. var that = this;
  129. this.data.title = data.title;
  130. this.data.backgroundUrl = data.backgroundUrl;
  131. this._changeCallbacks.forEach(function (callback) {
  132. setTimeout(callback.bind(null, that.data), 0);
  133. });
  134. };
  135. Settings.prototype.onChanged = function (callback) {
  136. this._changeCallbacks.push(callback);
  137. };
  138. function Tags(server) {
  139. this._server = server;
  140. }
  141. Tags.prototype.get = function (callback) {
  142. var url = this._server + '/api/tags';
  143. superagent.get(url).end(errorWrapper(function (error, result) {
  144. if (error) return callback(error);
  145. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  146. callback(null, result.body.tags);
  147. }));
  148. };
  149. function Core() {
  150. this._server = '';
  151. this.settings = new Settings(this._server);
  152. this.things = new Things(this._server);
  153. this.tags = new Tags(this._server);
  154. }
  155. module.exports = {
  156. Thing: Thing,
  157. Core: new Core()
  158. };