core.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.onAdded = function (callback) {
  87. this._addCallbacks.push(callback);
  88. };
  89. ThingsApi.prototype.onEdited = function (callback) {
  90. this._editCallbacks.push(callback);
  91. };
  92. ThingsApi.prototype.onDeleted = function (callback) {
  93. this._delCallbacks.push(callback);
  94. };
  95. ThingsApi.prototype.export = function () {
  96. window.location.href = this._server + '/api/export';
  97. };
  98. function SettingsApi() {
  99. this._changeCallbacks = [];
  100. this.data = {};
  101. this.reset();
  102. }
  103. SettingsApi.prototype.reset = function () {
  104. this.data.title = 'Guacamoly';
  105. this.data.backgroundUrl = '';
  106. };
  107. SettingsApi.prototype.save = function (callback) {
  108. superagent.post(url('/api/settings')).send({ settings: this.data }).end(errorWrapper(function (error, result) {
  109. if (error) return callback(error);
  110. if (result.status !== 202) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  111. callback(null);
  112. }));
  113. };
  114. SettingsApi.prototype.get = function (callback) {
  115. var that = this;
  116. superagent.get(url('/api/settings')).end(errorWrapper(function (error, result) {
  117. if (error) return callback(error);
  118. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  119. that.set(result.body.settings);
  120. callback(null, result.body.settings);
  121. }));
  122. };
  123. SettingsApi.prototype.set = function (data) {
  124. var that = this;
  125. this.data.title = data.title;
  126. this.data.backgroundUrl = data.backgroundUrl;
  127. this._changeCallbacks.forEach(function (callback) {
  128. setTimeout(callback.bind(null, that.data), 0);
  129. });
  130. };
  131. SettingsApi.prototype.onChanged = function (callback) {
  132. this._changeCallbacks.push(callback);
  133. };
  134. function TagsApi() {}
  135. TagsApi.prototype.get = function (callback) {
  136. superagent.get(url('/api/tags')).end(errorWrapper(function (error, result) {
  137. if (error) return callback(error);
  138. if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
  139. callback(null, result.body.tags);
  140. }));
  141. };
  142. function SessionApi() {}
  143. SessionApi.prototype.login = function (username, password, callback) {
  144. superagent.post(g_server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
  145. if (error) return callback(error);
  146. if (result.status !== 201) return callback(new Error('Login failed. ' + result.status + '. ' + result.text));
  147. g_token = result.body.token;
  148. localStorage.token = g_token;
  149. callback(null);
  150. });
  151. };
  152. SessionApi.prototype.logout = function () {
  153. superagent.get(url('/api/logout')).end(function (error, result) {
  154. if (error) console.error(error);
  155. if (result.status !== 200) console.error('Logout failed.', result.status, result.text);
  156. g_token = '';
  157. delete localStorage.token;
  158. module.exports.onLogout();
  159. });
  160. };
  161. module.exports = {
  162. loginFailed: function () {},
  163. onLogout: function () {},
  164. Thing: Thing,
  165. session: new SessionApi(),
  166. settings: new SettingsApi(),
  167. things: new ThingsApi(),
  168. tags: new TagsApi()
  169. };