core.js 6.8 KB

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