index.js 5.9 KB

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