index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $http.get('/api/things', {}).then(function (result) {
  37. angular.copy(result.data.things, $scope.things);
  38. }, function (result) {
  39. console.error('error:', result);
  40. });
  41. };
  42. $scope.fetchThings();
  43. $timeout(function () {
  44. $.material.init();
  45. });
  46. });
  47. app.filter('prettyDateOffset', function () {
  48. // http://ejohn.org/files/pretty.js
  49. return function prettyDateOffset(time){
  50. var date = new Date(time),
  51. diff = (((new Date()).getTime() - date.getTime()) / 1000),
  52. day_diff = Math.floor(diff / 86400);
  53. if (isNaN(day_diff) || day_diff < 0)
  54. return;
  55. return day_diff === 0 && (
  56. diff < 60 && 'just now' ||
  57. diff < 120 && '1 minute ago' ||
  58. diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
  59. diff < 7200 && '1 hour ago' ||
  60. diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
  61. day_diff === 1 && 'Yesterday' ||
  62. day_diff < 7 && day_diff + ' days ago' ||
  63. day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
  64. day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' ||
  65. Math.round( day_diff / 365 ) + ' years ago';
  66. };
  67. });
  68. app.filter('prettyDate', function () {
  69. var months = new Array(12);
  70. months[0] = 'January';
  71. months[1] = 'February';
  72. months[2] = 'March';
  73. months[3] = 'April';
  74. months[4] = 'May';
  75. months[5] = 'June';
  76. months[6] = 'July';
  77. months[7] = 'August';
  78. months[8] = 'September';
  79. months[9] = 'October';
  80. months[10] = 'November';
  81. months[11] = 'December';
  82. return function prettyDateOffset(time) {
  83. var date = new Date(time);
  84. return date.getUTCMonth() + 1 + ' ' + months[date.getDate()] + ' ' + date.getUTCFullYear();
  85. };
  86. });