core.js 5.6 KB

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