index.js 5.0 KB

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