| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /* jslint browser:true */
- /* global angular */
- /* global $ */
- 'use strict';
- var app = angular.module('app', [
- 'hc.marked'
- ]);
- app.config(['markedProvider', function (markedProvider) {
- markedProvider.setOptions({gfm: true});
- }]);
- app.controller('MainController', function ($scope, $http, $timeout, $document) {
- $scope.things = [];
- $scope.tags = [];
- $scope.filter = '';
- $scope.addFormData = {
- busy: true,
- content: ''
- };
- $scope.editFormData = {
- busy: true,
- id: '',
- content: ''
- };
- $scope.settings = {
- title: 'Holy Guacamoly',
- backgroundUrl: ''
- };
- $scope.addFilterTag = function (tag) {
- var hashed = '#' + tag.name;
- if ($scope.filter.indexOf(hashed) === -1) {
- $scope.filter = $scope.filter + ' ' + hashed;
- } else {
- $scope.filter = $scope.filter.replace(hashed, '');
- }
- $scope.fetchThings();
- };
- $scope.showEditThing = function (id, content) {
- $scope.editFormData.busy = false;
- $scope.editFormData.id = id;
- $scope.editFormData.content = content;
- $('#modalEdit').modal('show');
- };
- $scope.addThing = function () {
- $scope.addFormData.busy = true;
- $http.post('/api/things', { content: $scope.addFormData.content }, {}).then(function (result) {
- $scope.addFormData.busy = false;
- $scope.addFormData.content = '';
- $('#modalAdd').modal('hide');
- $scope.fetchThings();
- $scope.fetchTags();
- }, function (result) {
- console.error('error:', result);
- $scope.addFormData.busy = false;
- });
- };
- $scope.editThing = function () {
- $scope.editFormData.busy = true;
- $http.put('/api/things/' + $scope.editFormData.id, { content: $scope.editFormData.content }, {}).then(function (result) {
- $scope.editFormData.busy = false;
- $scope.editFormData.id = '';
- $scope.editFormData.content = '';
- $('#modalEdit').modal('hide');
- $scope.fetchThings();
- $scope.fetchTags();
- }, function (result) {
- console.error('error:', result);
- $scope.editFormData.busy = false;
- });
- };
- $scope.deleteThing = function (id) {
- $http.delete('/api/things/' + id, {}).then(function (result) {
- $scope.fetchThings();
- }, function (result) {
- console.error('error:', result);
- });
- };
- $scope.fetchThings = function () {
- var url = '/api/things';
- if ($scope.filter) {
- url += '?filter=' + encodeURIComponent($scope.filter);
- }
- $http.get(url, {}).then(function (result) {
- angular.copy(result.data.things, $scope.things);
- }, function (result) {
- console.error('error:', result);
- });
- };
- $scope.fetchTags = function () {
- $http.get('/api/tags', {}).then(function (result) {
- angular.copy(result.data.tags, $scope.tags);
- }, function (result) {
- console.error('error:', result);
- });
- };
- $('#modalSettings').on('hidden.bs.modal', function () {
- $scope.fetchSettings();
- });
- $scope.saveSettings = function () {
- $http.post('/api/settings', { settings: $scope.settings }, {}).then(function (result) {
- $('#modalSettings').modal('hide');
- }, function (result) {
- console.error('error:', result);
- });
- };
- $scope.fetchSettings = function () {
- $http.get('/api/settings', {}).then(function (result) {
- angular.extend($scope.settings, result.data.settings);
- }, function (result) {
- console.error('error:', result);
- });
- };
- $scope.fetchThings();
- $scope.fetchTags();
- $scope.fetchSettings();
- $timeout(function () {
- $.material.init();
- $('#modalAdd').on('shown.bs.modal', function () {
- $('#inputContent').focus();
- });
- });
- });
- app.filter('prettyDateOffset', function () {
- // http://ejohn.org/files/pretty.js
- return function prettyDateOffset(time){
- var date = new Date(time),
- diff = (((new Date()).getTime() - date.getTime()) / 1000),
- day_diff = Math.floor(diff / 86400);
- if (isNaN(day_diff) || day_diff < 0)
- return;
- return day_diff === 0 && (
- diff < 60 && 'just now' ||
- diff < 120 && '1 minute ago' ||
- diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
- diff < 7200 && '1 hour ago' ||
- diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
- day_diff === 1 && 'Yesterday' ||
- day_diff < 7 && day_diff + ' days ago' ||
- day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
- day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' ||
- Math.round( day_diff / 365 ) + ' years ago';
- };
- });
- app.filter('prettyDate', function () {
- var months = new Array(12);
- months[0] = 'January';
- months[1] = 'February';
- months[2] = 'March';
- months[3] = 'April';
- months[4] = 'May';
- months[5] = 'June';
- months[6] = 'July';
- months[7] = 'August';
- months[8] = 'September';
- months[9] = 'October';
- months[10] = 'November';
- months[11] = 'December';
- return function prettyDateOffset(time) {
- var date = new Date(time);
- return date.getUTCMonth() + 1 + ' ' + months[date.getDate()] + ' ' + date.getUTCFullYear();
- };
- });
- app.directive('ngEnter', function () {
- return function (scope, element, attrs) {
- element.bind('keydown keypress', function (event) {
- if(event.which === 13) {
- scope.$apply(function (){
- scope.$eval(attrs.ngEnter);
- });
- event.preventDefault();
- }
- });
- };
- });
|