index.js 5.0 KB

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