core.js 5.3 KB

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