core.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. var superagent = require('superagent');
  3. var g_server = '';
  4. var g_token = localStorage.token || '';
  5. function guid() {
  6. function s4() {
  7. return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  8. }
  9. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  10. }
  11. function errorWrapper(callback) {
  12. return function (error, result) {
  13. if (error && error.status === 401) return module.exports.loginFailed();
  14. callback(error, result);
  15. };
  16. }
  17. function url(path) {
  18. return g_server + path + '?token=' + g_token;
  19. }
  20. function Thing(id, createdAt, tags, content, richContent) {
  21. this.id = id;
  22. this.createdAt = createdAt || 0;
  23. this.tags = tags || [];
  24. this.content = content;
  25. this.edit = false;
  26. this.richContent = richContent;
  27. }
  28. function ThingsApi() {
  29. this._addCallbacks = [];
  30. this._editCallbacks = [];
  31. this._delCallbacks = [];
  32. this._operation = '';
  33. }
  34. ThingsApi.prototype.get = function (filter, callback) {
  35. var that = this;
  36. var u = url('/api/things');
  37. var operation = guid();
  38. this._operation = operation;
  39. if (filter) u += '&filter=' + encodeURIComponent(filter);
  40. superagent.get(u).end(errorWrapper(function (error, result) {
  41. // ignore this if we moved on
  42. if (that._operation !== operation) {
  43. console.log('ignore this call');
  44. return;
  45. }
  46. if (error) return callback(error);
  47. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  48. var tmp = result.body.things.map(function (thing) {
  49. return new Thing(thing._id, new Date(thing.createdAt).getTime(), thing.tags, thing.content, thing.richContent);
  50. });
  51. callback(null, tmp);
  52. }));
  53. };
  54. ThingsApi.prototype.add = function (content, callback) {
  55. var that = this;
  56. superagent.post(url('/api/things')).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. ThingsApi.prototype.edit = function (thing, callback) {
  66. var that = this;
  67. superagent.put(url('/api/things/' + thing.id)).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. ThingsApi.prototype.del = function (thing, callback) {
  77. var that = this;
  78. superagent.del(url('/api/things/' + thing.id)).end(errorWrapper(function (error, result) {
  79. if (error) return callback(error);
  80. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  81. that._delCallbacks.forEach(function (callback) {
  82. setTimeout(callback, 0);
  83. });
  84. callback(null);
  85. }));
  86. };
  87. ThingsApi.prototype.import = function (formData, callback) {
  88. superagent.post(url('/api/import')).send(formData).end(function (error, result) {
  89. if (error) return callback(error);
  90. callback(null);
  91. });
  92. };
  93. ThingsApi.prototype.onAdded = function (callback) {
  94. this._addCallbacks.push(callback);
  95. };
  96. ThingsApi.prototype.onEdited = function (callback) {
  97. this._editCallbacks.push(callback);
  98. };
  99. ThingsApi.prototype.onDeleted = function (callback) {
  100. this._delCallbacks.push(callback);
  101. };
  102. ThingsApi.prototype.export = function () {
  103. window.location.href = url('/api/export');
  104. };
  105. function SettingsApi() {
  106. this._changeCallbacks = [];
  107. this.data = {};
  108. this.reset();
  109. }
  110. SettingsApi.prototype.reset = function () {
  111. var that = this;
  112. this.data.title = 'Guacamoly';
  113. this.data.backgroundUrl = '';
  114. this._changeCallbacks.forEach(function (callback) {
  115. setTimeout(callback.bind(null, that.data), 0);
  116. });
  117. };
  118. SettingsApi.prototype.save = function (callback) {
  119. superagent.post(url('/api/settings')).send({ settings: this.data }).end(errorWrapper(function (error, result) {
  120. if (error) return callback(error);
  121. if (result.status !== 202) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  122. callback(null);
  123. }));
  124. };
  125. SettingsApi.prototype.get = function (callback) {
  126. var that = this;
  127. superagent.get(url('/api/settings')).end(errorWrapper(function (error, result) {
  128. if (error) return callback(error);
  129. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  130. that.set(result.body.settings);
  131. callback(null, result.body.settings);
  132. }));
  133. };
  134. SettingsApi.prototype.set = function (data) {
  135. var that = this;
  136. this.data.title = data.title;
  137. this.data.backgroundUrl = data.backgroundUrl;
  138. this._changeCallbacks.forEach(function (callback) {
  139. setTimeout(callback.bind(null, that.data), 0);
  140. });
  141. };
  142. SettingsApi.prototype.onChanged = function (callback) {
  143. this._changeCallbacks.push(callback);
  144. };
  145. function TagsApi() {}
  146. TagsApi.prototype.get = function (callback) {
  147. superagent.get(url('/api/tags')).end(errorWrapper(function (error, result) {
  148. if (error) return callback(error);
  149. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  150. callback(null, result.body.tags);
  151. }));
  152. };
  153. function SessionApi() {}
  154. SessionApi.prototype.login = function (username, password, callback) {
  155. superagent.post(g_server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
  156. if (error) return callback(error);
  157. if (result.status !== 201) return callback(new Error('Login failed. ' + result.status + '. ' + result.text));
  158. g_token = result.body.token;
  159. localStorage.token = g_token;
  160. callback(null);
  161. });
  162. };
  163. SessionApi.prototype.logout = function () {
  164. superagent.get(url('/api/logout')).end(function (error, result) {
  165. if (error) console.error(error);
  166. if (result.status !== 200) console.error('Logout failed.', result.status, result.text);
  167. g_token = '';
  168. delete localStorage.token;
  169. module.exports.onLogout();
  170. });
  171. };
  172. module.exports = {
  173. loginFailed: function () {},
  174. onLogout: function () {},
  175. Thing: Thing,
  176. session: new SessionApi(),
  177. settings: new SettingsApi(),
  178. things: new ThingsApi(),
  179. tags: new TagsApi()
  180. };