index.js 4.2 KB

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