index.js 3.5 KB

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