/public/javascripts/angularApp.js

https://gitlab.com/Pablo-Galbusera/airport · JavaScript · 330 lines · 234 code · 53 blank · 43 comment · 38 complexity · 5f8617561359731e27e412b0947c63f5 MD5 · raw file

  1. var app = angular.module('airport', ['ui.router']);
  2. app.factory('posts', ['$http', '$interval', '$location', function($http, $interval,$location){
  3. var o = {
  4. posts: [],
  5. counters: []
  6. };
  7. // if ($location.hash()=='superinternet') {
  8. //
  9. // $interval(loadPosts, 2000);
  10. //
  11. // function loadPosts () {
  12. // $http.get('/posts').success(function(data){
  13. // angular.copy(data, o.posts);
  14. // });
  15. // }
  16. //
  17. // o.posts.automaticCounter = 0;
  18. //
  19. // $interval(saveAutomaticPosts, 10000);
  20. //
  21. // function saveAutomaticPosts() {
  22. // o.posts.automaticCounter++;
  23. // var post = {};
  24. // post.title = 'testone '+o.posts.automaticCounter;
  25. // $http.post('/posts', post).success(function(data){
  26. // o.posts.push(data);
  27. // });
  28. // }
  29. //
  30. // }
  31. $interval(updateInfos, 2000);
  32. function updateInfos() {
  33. $http.get('/posts').success(function(data){
  34. angular.copy(data, o.posts);
  35. });
  36. $http.get('/counter').success(function(data){
  37. angular.copy(data, o.counters);
  38. });
  39. };
  40. o.getAll = function() {
  41. return $http.get('/posts').success(function(data){
  42. angular.copy(data, o.posts);
  43. $http.get('/counter').success(function(data){
  44. angular.copy(data, o.counters);
  45. });
  46. });
  47. };
  48. o.getCounters = function () {
  49. return $http.get('/counter').success(function (data) {
  50. // console.log(data);
  51. angular.copy(data, o.counters);
  52. });
  53. }
  54. o.create = function(post) {
  55. return $http.post('/posts', post).success(function(data){
  56. o.posts.push(data);
  57. });
  58. };
  59. o.upvote = function(post) {
  60. return $http.put('/posts/' + post._id + '/upvote')
  61. .success(function(data){
  62. post.upvotes += 1;
  63. });
  64. };
  65. o.upvoteCounter = function(counter) {
  66. console.log(counter);
  67. return $http.put('/counters/' + counter._id + '/upvote')
  68. .success(function(data){
  69. counter.upvotes += 1;
  70. });
  71. };
  72. o.update = function (post) {
  73. console.log(post);
  74. return $http.post('/posts/' + post._id + '/update', post).success(function(data){
  75. if (data.test == 'ok') {
  76. alert('Ho salvato zio');
  77. }
  78. // o.posts.push(data);
  79. });
  80. }
  81. o.get = function(id) {
  82. return $http.get('/posts/' + id).then(function(res){
  83. return res.data;
  84. });
  85. };
  86. o.addComment = function(id, comment) {
  87. return $http.post('/posts/' + id + '/comments', comment);
  88. };
  89. o.upvoteComment = function(post, comment) {
  90. return $http.put('/posts/' + post._id + '/comments/'+ comment._id + '/upvote')
  91. .success(function(data){
  92. comment.upvotes += 1;
  93. });
  94. };
  95. return o;
  96. }]);
  97. app.config([
  98. '$stateProvider',
  99. '$urlRouterProvider',
  100. '$locationProvider',
  101. function($stateProvider, $urlRouterProvider, $locationProvider) {
  102. $stateProvider
  103. .state('superinternetloveyou', {
  104. url: '/superinternetloveyou',
  105. templateUrl: '/home.html',
  106. controller: 'MainCtrl',
  107. resolve: {
  108. postPromise: ['posts', function(posts){
  109. return posts.getAll();
  110. }]
  111. }
  112. })
  113. .state('posts', {
  114. url: '/posts/{id}',
  115. templateUrl: '/posts.html',
  116. controller: 'PostsCtrl',
  117. resolve: {
  118. post: ['$stateParams', 'posts', function($stateParams, posts) {
  119. return posts.get($stateParams.id);
  120. }]
  121. }
  122. })
  123. .state('counter', {
  124. url: '/counter',
  125. templateUrl: '/counter.html',
  126. controller: 'CounterCtrl',
  127. resolve: {
  128. postPromise: ['posts', function (posts) {
  129. return posts.getCounters();
  130. }]
  131. }
  132. });
  133. $urlRouterProvider.otherwise('superinternetloveyou');
  134. // $locationProvider.html5Mode(true);
  135. }]);
  136. app.controller('MainCtrl', [
  137. '$scope', 'posts', '$interval', '$location',
  138. function($scope, posts, $interval, $location){
  139. if ($location.hash()=='superinternet') {
  140. $scope.admin = true;
  141. } else {
  142. $scope.admin = false;
  143. }
  144. if ($location.hash()=='add') {
  145. $scope.addArtist = true;
  146. } else {
  147. $scope.addArtist = false;
  148. }
  149. $scope.posts = posts.posts;
  150. $scope.counter = posts.counters;
  151. $scope.addPost = function(){
  152. var beginValue = new Date("June "+this.day+", 2016 "+this.hour+":"+this.minute+":00");
  153. // if(!$scope.name || $scope.name === '') { return; }
  154. posts.create({
  155. order: this.order,
  156. name: this.name,
  157. place: this.place,
  158. begin: beginValue.getTime(),
  159. status: this.status,
  160. delay: this.delay
  161. });
  162. $scope.name = '';
  163. $scope.place = '';
  164. $scope.order = '';
  165. $scope.posts = posts.posts;
  166. };
  167. // $scope.incrementUpvotes = function(post) {
  168. // posts.upvote(post);
  169. // };
  170. }]);
  171. app.controller('PostsCtrl', [
  172. '$scope',
  173. 'posts',
  174. 'post',
  175. function($scope, posts, post){
  176. $scope.post = post;
  177. $scope.editPost = function(){
  178. // default values
  179. if ($scope.order) {
  180. orderValue = this.order;
  181. } else {
  182. orderValue = post.order;
  183. }
  184. if ($scope.name) {
  185. nameValue = this.name;
  186. } else {
  187. nameValue = post.name;
  188. }
  189. if ($scope.place) {
  190. placeValue = this.place;
  191. } else {
  192. placeValue = post.place;
  193. }
  194. if ($scope.day) {
  195. beginValueDate = new Date("June "+this.day+", 2016 "+this.hour+":"+this.minute+":00");
  196. beginValue = beginValueDate.getTime();
  197. } else {
  198. beginValue = post.begin;
  199. }
  200. if ($scope.status) {
  201. statusValue = this.status;
  202. } else {
  203. statusValue = post.status;
  204. }
  205. if ($scope.delay) {
  206. delayValue = this.delay;
  207. } else {
  208. delayValue = post.delay;
  209. }
  210. postsEdited = {
  211. _id: post._id,
  212. order: orderValue,
  213. name: nameValue,
  214. place: placeValue,
  215. begin: beginValue,
  216. status: statusValue,
  217. delay: delayValue
  218. };
  219. posts.update(postsEdited);
  220. };
  221. }]);
  222. app.controller('CounterCtrl', ['$scope', 'posts', '$interval', '$location', function ($scope, posts, $interval, $location) {
  223. $scope.counter = posts.counters;
  224. $scope.addVisitor = function() {
  225. posts.upvoteCounter(this.c);
  226. };
  227. }]);
  228. app.controller('FlightController',['$scope', function ($scope) {
  229. // <option value="Boarding">Boarding</option>
  230. // <option value="In Flight">In Flight</option>
  231. // <option value="Landed">Landed</option>
  232. // <option value="Delayed">Delayed</option>
  233. // <option value="Cancelled">Cancelled</option>
  234. currentStatus = ' ';
  235. currentStatusDelay = ' ';
  236. $scope.flight = $scope.$parent.flight;
  237. if ($scope.flight) {
  238. var delayTime = 0;
  239. if ($scope.flight.delay) {
  240. var delayTime = $scope.flight.delay*60*1000;
  241. currentStatusDelay = $scope.flight.delay + "'";
  242. }
  243. var initialTime = $scope.flight.begin + delayTime;
  244. var boardingValue = 10*60*1000;
  245. // console.log($scope.flight.order);
  246. // console.log(initialTime);
  247. if ($scope.flight.status != "Automatic" && $scope.flight.status != "") {
  248. currentStatus = $scope.flight.status;
  249. }
  250. if ($scope.flight.status == "Automatic" || $scope.flight.status == "" || $scope.flight.status == "Delayed") {
  251. var currentTimeDate = new Date();
  252. var currentTime = currentTimeDate.getTime();
  253. // console.log("initialTime: "+initialTime);
  254. // console.log("boardingValue: "+boardingValue);
  255. var boardingTime = initialTime - boardingValue;
  256. // console.log("currentTime: "+currentTime);
  257. console.log("boardingTime: "+boardingTime);
  258. // console.log("initialTime: "+initialTime);
  259. if (currentTime > boardingTime && currentTime < initialTime) {
  260. currentStatus = "Boarding";
  261. } else if (currentTime >= initialTime) {
  262. currentStatus = "In Flight";
  263. currentStatusDelay = " ";
  264. }
  265. }
  266. if ($scope.flight.status == "Cancelled" || currentStatus == "Landed") {
  267. currentStatusDelay = " ";
  268. }
  269. }
  270. $scope.statusText = currentStatus;
  271. $scope.statusDelay = currentStatusDelay;
  272. }])
  273. app.filter('reverse', function() {
  274. return function(items) {
  275. return items.slice().reverse();
  276. };
  277. });