index.js 3.4 KB

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