core.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.richContent = richContent;
  26. }
  27. function ThingsApi() {
  28. this._addCallbacks = [];
  29. this._editCallbacks = [];
  30. this._delCallbacks = [];
  31. this._operation = '';
  32. }
  33. ThingsApi.prototype.get = function (filter, callback) {
  34. var that = this;
  35. var u = url('/api/things');
  36. var operation = guid();
  37. this._operation = operation;
  38. if (filter) u += '&filter=' + encodeURIComponent(filter);
  39. superagent.get(u).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. var tmp = 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, tmp);
  51. }));
  52. };
  53. ThingsApi.prototype.add = function (content, callback) {
  54. var that = this;
  55. superagent.post(url('/api/things')).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. ThingsApi.prototype.edit = function (thing, callback) {
  65. var that = this;
  66. superagent.put(url('/api/things/' + thing.id)).send({ content: thing.content }).end(errorWrapper(function (error, result) {
  67. if (error) return callback(error);
  68. if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  69. that._editCallbacks.forEach(function (callback) {
  70. setTimeout(callback, 0);
  71. });
  72. callback(null);
  73. }));
  74. };
  75. ThingsApi.prototype.del = function (thing, callback) {
  76. var that = this;
  77. superagent.del(url('/api/things/' + thing.id)).end(errorWrapper(function (error, result) {
  78. if (error) return callback(error);
  79. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  80. that._delCallbacks.forEach(function (callback) {
  81. setTimeout(callback, 0);
  82. });
  83. callback(null);
  84. }));
  85. };
  86. ThingsApi.prototype.import = function (formData, callback) {
  87. superagent.post(url('/api/import')).send(formData).end(function (error, result) {
  88. if (error) return callback(error);
  89. callback(null);
  90. });
  91. };
  92. ThingsApi.prototype.onAdded = function (callback) {
  93. this._addCallbacks.push(callback);
  94. };
  95. ThingsApi.prototype.onEdited = function (callback) {
  96. this._editCallbacks.push(callback);
  97. };
  98. ThingsApi.prototype.onDeleted = function (callback) {
  99. this._delCallbacks.push(callback);
  100. };
  101. ThingsApi.prototype.export = function () {
  102. window.location.href = url('/api/export');
  103. };
  104. function SettingsApi() {
  105. this._changeCallbacks = [];
  106. this.data = {};
  107. this.reset();
  108. }
  109. SettingsApi.prototype.reset = function () {
  110. this.data.title = 'Guacamoly';
  111. this.data.backgroundUrl = '';
  112. };
  113. SettingsApi.prototype.save = function (callback) {
  114. superagent.post(url('/api/settings')).send({ settings: this.data }).end(errorWrapper(function (error, result) {
  115. if (error) return callback(error);
  116. if (result.status !== 202) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  117. callback(null);
  118. }));
  119. };
  120. SettingsApi.prototype.get = function (callback) {
  121. var that = this;
  122. superagent.get(url('/api/settings')).end(errorWrapper(function (error, result) {
  123. if (error) return callback(error);
  124. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  125. that.set(result.body.settings);
  126. callback(null, result.body.settings);
  127. }));
  128. };
  129. SettingsApi.prototype.set = function (data) {
  130. var that = this;
  131. this.data.title = data.title;
  132. this.data.backgroundUrl = data.backgroundUrl;
  133. this._changeCallbacks.forEach(function (callback) {
  134. setTimeout(callback.bind(null, that.data), 0);
  135. });
  136. };
  137. SettingsApi.prototype.onChanged = function (callback) {
  138. this._changeCallbacks.push(callback);
  139. };
  140. function TagsApi() {}
  141. TagsApi.prototype.get = function (callback) {
  142. superagent.get(url('/api/tags')).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 SessionApi() {}
  149. SessionApi.prototype.login = function (username, password, callback) {
  150. superagent.post(g_server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
  151. if (error) return callback(error);
  152. if (result.status !== 201) return callback(new Error('Login failed. ' + result.status + '. ' + result.text));
  153. g_token = result.body.token;
  154. localStorage.token = g_token;
  155. callback(null);
  156. });
  157. };
  158. SessionApi.prototype.logout = function () {
  159. superagent.get(url('/api/logout')).end(function (error, result) {
  160. if (error) console.error(error);
  161. if (result.status !== 200) console.error('Logout failed.', result.status, result.text);
  162. g_token = '';
  163. delete localStorage.token;
  164. module.exports.onLogout();
  165. });
  166. };
  167. module.exports = {
  168. loginFailed: function () {},
  169. onLogout: function () {},
  170. Thing: Thing,
  171. session: new SessionApi(),
  172. settings: new SettingsApi(),
  173. things: new ThingsApi(),
  174. tags: new TagsApi()
  175. };