index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* jslint browser:true */
  2. /* global angular */
  3. /* global $ */
  4. 'use strict';
  5. var app = angular.module('app', [
  6. 'hc.marked'
  7. ]);
  8. app.config(['markedProvider', function (markedProvider) {
  9. markedProvider.setOptions({gfm: true});
  10. }]);
  11. app.controller('MainController', function ($scope, $http, $timeout, $document) {
  12. $scope.things = [];
  13. $scope.tags = [];
  14. $scope.filter = '';
  15. $scope.thingsBusy = true;
  16. $scope.tagsBusy = true;
  17. $scope.addFormData = {
  18. busy: true,
  19. content: ''
  20. };
  21. $scope.editFormData = {
  22. busy: true,
  23. id: '',
  24. content: ''
  25. };
  26. $scope.delFormData = {
  27. busy: true,
  28. id: ''
  29. };
  30. $scope.settings = {
  31. title: 'Holy Guacamoly',
  32. backgroundUrl: ''
  33. };
  34. $scope.addFilterTag = function (tag) {
  35. var hashed = '#' + tag.name;
  36. if ($scope.filter.indexOf(hashed) === -1) {
  37. $scope.filter = $scope.filter + ' ' + hashed;
  38. } else {
  39. $scope.filter = $scope.filter.replace(hashed, '');
  40. }
  41. $scope.fetchThings();
  42. };
  43. $scope.showEditThing = function (id, content) {
  44. $scope.editFormData.busy = false;
  45. $scope.editFormData.id = id;
  46. $scope.editFormData.content = content;
  47. $('#modalEdit').modal('show');
  48. };
  49. $scope.showDelThing = function (id) {
  50. $scope.delFormData.busy = false;
  51. $scope.delFormData.id = id;
  52. $('#modalDel').modal('show');
  53. };
  54. $scope.addThing = function () {
  55. $scope.addFormData.busy = true;
  56. $http.post('/api/things', { content: $scope.addFormData.content }, {}).then(function (result) {
  57. $scope.addFormData.busy = false;
  58. $scope.addFormData.content = '';
  59. $('#modalAdd').modal('hide');
  60. $scope.fetchThings();
  61. $scope.fetchTags();
  62. }, function (result) {
  63. console.error('error:', result);
  64. $scope.addFormData.busy = false;
  65. });
  66. };
  67. $scope.editThing = function () {
  68. $scope.editFormData.busy = true;
  69. $http.put('/api/things/' + $scope.editFormData.id, { content: $scope.editFormData.content }, {}).then(function (result) {
  70. $scope.editFormData.busy = false;
  71. $scope.editFormData.id = '';
  72. $scope.editFormData.content = '';
  73. $('#modalEdit').modal('hide');
  74. $scope.fetchThings();
  75. $scope.fetchTags();
  76. }, function (result) {
  77. console.error('error:', result);
  78. $scope.editFormData.busy = false;
  79. });
  80. };
  81. $scope.delThing = function (id) {
  82. $scope.delFormData.busy = true;
  83. $http.delete('/api/things/' + $scope.delFormData.id, {}).then(function (result) {
  84. $scope.delFormData.busy = false;
  85. $scope.delFormData.id = '';
  86. $('#modalDel').modal('hide');
  87. $scope.fetchThings();
  88. }, function (result) {
  89. console.error('error:', result);
  90. $scope.delFormData.busy = false;
  91. });
  92. };
  93. $scope.fetchThings = function () {
  94. $scope.thingsBusy = true;
  95. var url = '/api/things';
  96. if ($scope.filter) {
  97. url += '?filter=' + encodeURIComponent($scope.filter);
  98. }
  99. $http.get(url, {}).then(function (result) {
  100. angular.copy(result.data.things, $scope.things);
  101. $scope.thingsBusy = false;
  102. }, function (result) {
  103. console.error('error:', result);
  104. $scope.thingsBusy = false;
  105. });
  106. };
  107. $scope.fetchTags = function () {
  108. $scope.tagsBusy = true;
  109. $http.get('/api/tags', {}).then(function (result) {
  110. angular.copy(result.data.tags, $scope.tags);
  111. $scope.tagsBusy = false;
  112. }, function (result) {
  113. console.error('error:', result);
  114. $scope.tagsBusy = false;
  115. });
  116. };
  117. $('#modalSettings').on('hidden.bs.modal', function () {
  118. $scope.fetchSettings();
  119. });
  120. $scope.saveSettings = function () {
  121. $http.post('/api/settings', { settings: $scope.settings }, {}).then(function (result) {
  122. $('#modalSettings').modal('hide');
  123. }, function (result) {
  124. console.error('error:', result);
  125. });
  126. };
  127. $scope.fetchSettings = function () {
  128. $http.get('/api/settings', {}).then(function (result) {
  129. angular.extend($scope.settings, result.data.settings);
  130. }, function (result) {
  131. console.error('error:', result);
  132. });
  133. };
  134. $scope.fetchThings();
  135. $scope.fetchTags();
  136. $scope.fetchSettings();
  137. $timeout(function () {
  138. $.material.init();
  139. $('#modalAdd').on('shown.bs.modal', function () {
  140. $('#inputContent').focus();
  141. });
  142. });
  143. });
  144. app.filter('prettyDateOffset', function () {
  145. // http://ejohn.org/files/pretty.js
  146. return function prettyDateOffset(time){
  147. var date = new Date(time),
  148. diff = (((new Date()).getTime() - date.getTime()) / 1000),
  149. day_diff = Math.floor(diff / 86400);
  150. if (isNaN(day_diff) || day_diff < 0)
  151. return;
  152. return day_diff === 0 && (
  153. diff < 60 && 'just now' ||
  154. diff < 120 && '1 minute ago' ||
  155. diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
  156. diff < 7200 && '1 hour ago' ||
  157. diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
  158. day_diff === 1 && 'Yesterday' ||
  159. day_diff < 7 && day_diff + ' days ago' ||
  160. day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
  161. day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' ||
  162. Math.round( day_diff / 365 ) + ' years ago';
  163. };
  164. });
  165. app.filter('prettyDate', function () {
  166. var months = new Array(12);
  167. months[0] = 'January';
  168. months[1] = 'February';
  169. months[2] = 'March';
  170. months[3] = 'April';
  171. months[4] = 'May';
  172. months[5] = 'June';
  173. months[6] = 'July';
  174. months[7] = 'August';
  175. months[8] = 'September';
  176. months[9] = 'October';
  177. months[10] = 'November';
  178. months[11] = 'December';
  179. return function prettyDateOffset(time) {
  180. var date = new Date(time);
  181. return date.getUTCMonth() + 1 + ' ' + months[date.getDate()] + ' ' + date.getUTCFullYear();
  182. };
  183. });
  184. app.directive('ngEnter', function () {
  185. return function (scope, element, attrs) {
  186. element.bind('keydown keypress', function (event) {
  187. if(event.which === 13) {
  188. scope.$apply(function (){
  189. scope.$eval(attrs.ngEnter);
  190. });
  191. event.preventDefault();
  192. }
  193. });
  194. };
  195. });