index.js 4.3 KB

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