PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/www/js/calendar/adminControllers.js

https://bitbucket.org/snakeyyy/srs
JavaScript | 251 lines | 208 code | 37 blank | 6 comment | 34 complexity | 7bd149e2f447486a7c62eacc5013b0f5 MD5 | raw file
  1. var REFRESH_INTERVAL = 10000;
  2. var api_path = basePath + '/api/program/';
  3. function AdminCalendarCtrl($scope, $http, $q, $timeout) {
  4. $scope.option = ''; // indexovane bloky - pro snadne vyhledavani a prirazovani
  5. $scope.event = null; // udalost se kterou prave pracuji
  6. $scope.config = null; // konfiguracni nastaveni pro kalendar
  7. $scope.blocks = []; // neindexovane bloky - v poli - pro filtrovani
  8. $scope.startup = function () {
  9. var promise, promisses = [];
  10. promise = $http.get(api_path + "getblocks", {})
  11. .success(function (data, status, headers, config) {
  12. $scope.options = data;
  13. }).error(function (data, status, headers, config) {
  14. $scope.status = status;
  15. });
  16. promisses.push(promise);
  17. promise = $http.get(api_path + "getprograms", {})
  18. .success(function (data, status, headers, config) {
  19. $scope.events = data;
  20. }).error(function (data, status, headers, config) {
  21. $scope.status = status;
  22. });
  23. promisses.push(promise);
  24. promise = $http.get(api_path + "getcalendarconfig", {})
  25. .success(function (data, status, headers, config) {
  26. $scope.config = data;
  27. }).error(function (data, status, headers, config) {
  28. $scope.status = status;
  29. });
  30. promisses.push(promise);
  31. //pote co jsou vsechny inicializacni ajax requesty splneny
  32. $q.all(promisses).then(function () {
  33. angular.forEach($scope.events, function (event, key) {
  34. event.block = $scope.options[event.block];
  35. setColor(event);
  36. });
  37. angular.forEach($scope.options, function (block, key) {
  38. $scope.blocks.push(block);
  39. });
  40. if (!$scope.config.is_allowed_modify_schedule) {
  41. $scope.warning = 'Úprava harmonogramu semináře je zakázána. Nemáte právo spravovat harmonogram nebo musíte povolit úpravu harmonogramu v modulu konfigurace.';
  42. }
  43. bindCalendar($scope);
  44. });
  45. }
  46. $scope.startup();
  47. $scope.saveEvent = function (event) {
  48. $scope.event = event;
  49. event.startJSON = fixDate(event.start);
  50. event.endJSON = fixDate(event.end);
  51. if (event.block) { //php si neumi poradit s html apod v jsondecode
  52. event.block.perex = '';
  53. event.block.description = '';
  54. }
  55. seen = [];
  56. var json = JSON.stringify(event, function (key, val) {
  57. if (typeof val == "object") {
  58. if ($.inArray(val, seen) >= 0)
  59. return undefined;
  60. seen.push(val);
  61. }
  62. if (key == 'source') { // tyto data nepotřebujeme
  63. return undefined;
  64. }
  65. return val
  66. });
  67. $http.post(api_path + "setprogram?data=" + json)
  68. .success(function (data, status, headers, config) {
  69. $scope.event.id = data['id'];
  70. });
  71. }
  72. $scope.update = function (event, option) {
  73. $('#blockModal').modal('hide');
  74. $scope.event.mandatory = event.mandatory;
  75. if (option) {
  76. $scope.event.title = option.name;
  77. $scope.event.attendees_count = 0;
  78. if ($scope.event.block) {
  79. var old_block = $scope.event.block;
  80. }
  81. else {
  82. var old_block = null;
  83. }
  84. $scope.event.block = $scope.options[option.id];
  85. if ($scope.event.block != old_block) {
  86. if (old_block != null) old_block.program_count--;
  87. $scope.event.block.program_count++;
  88. }
  89. $scope.event.duration = $scope.event.block.duration;
  90. var end = bindEndToBlockDuration($scope.event.start, $scope.event._end, $scope.event.block.duration, $scope.config.basic_block_duration);
  91. $scope.event.end = end;
  92. }
  93. else {
  94. $scope.event.title = '(Nepřiřazeno)';
  95. $scope.event.block = null;
  96. }
  97. setColor($scope.event);
  98. $scope.saveEvent($scope.event);
  99. $('#calendar').fullCalendar('updateEvent', [$scope.event]);
  100. }
  101. $scope.remove = function (event) {
  102. if (event.block != null || event.block != undefined) {
  103. event.block.program_count--;
  104. }
  105. $http.post(api_path + "deleteprogram/" + event.id);
  106. $('#blockModal').modal('hide');
  107. $('#calendar').fullCalendar('removeEvents', [event._id]);
  108. }
  109. $scope.refreshForm = function () {
  110. this.event = $scope.event;
  111. if ($scope.event.block != undefined && $scope.event.block != null) {
  112. var id = $scope.event.block.id
  113. this.option = $scope.options[id];
  114. }
  115. else {
  116. this.option = null;
  117. }
  118. $scope.$apply();
  119. }
  120. }
  121. function bindCalendar(scope) {
  122. var local_config = {
  123. aspectRatio:1.6,
  124. editable:scope.config.is_allowed_modify_schedule,
  125. droppable:scope.config.is_allowed_modify_schedule,
  126. events:scope.events,
  127. year:scope.config.year,
  128. month:scope.config.month,
  129. date:scope.config.date,
  130. selectable:scope.config.is_allowed_modify_schedule,
  131. selectHelper:scope.config.is_allowed_modify_schedule,
  132. seminarLength:scope.config.seminar_duration,
  133. firstDay:scope.config.seminar_start_day,
  134. select:function (start, end, allDay) {
  135. end = bindEndToBasicBlockDuration(start, end, scope.config.basic_block_duration);
  136. var title = '(Nepřiřazeno)';
  137. var event = {
  138. title:title,
  139. start:start,
  140. end:end,
  141. allDay:allDay,
  142. mandatory:false
  143. }
  144. scope.event = event;
  145. setColor(scope.event);
  146. scope.saveEvent(event);
  147. calendar.fullCalendar('renderEvent',
  148. scope.event,
  149. true // make the event "stick"
  150. );
  151. calendar.fullCalendar('unselect');
  152. },
  153. eventClick:function (event, element) {
  154. if (scope.config.is_allowed_modify_schedule) {
  155. scope.event = event;
  156. scope.refreshForm();
  157. $('#blockModal').modal('show');
  158. }
  159. },
  160. eventDrop:function (event, jsEvent, ui, view) {
  161. scope.event = event;
  162. scope.saveEvent(event);
  163. },
  164. eventResize:function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
  165. if (event.block == null || event.block == undefined) {
  166. var end = bindEndToBasicBlockDuration(event.start, event.end, scope.config.basic_block_duration);
  167. event.end = end;
  168. scope.event = event;
  169. scope.saveEvent(scope.event);
  170. $('#calendar').fullCalendar('updateEvent', event);
  171. }
  172. else {
  173. flashMessage('Položkám s přiřazeným programovým blokem nelze měnit délku', 'error');
  174. revertFunc();
  175. }
  176. },
  177. drop:function (date, allDay) {
  178. // retrieve the dropped element's stored Event Object
  179. var originalEventObject = $(this).data('eventObject');
  180. // we need to copy it, so that multiple events don't have a reference to the same object
  181. var event = $.extend({}, originalEventObject);
  182. // assign it the date that was reported
  183. event.start = date;
  184. event.attendees_count = 0;
  185. event.allDay = allDay;
  186. event.block.program_count++;
  187. event.end = bindEndToBlockDuration(date, null, event.block.duration, scope.config.basic_block_duration);
  188. scope.event = event;
  189. setColor(scope.event);
  190. scope.saveEvent(event);
  191. // render the event on the calendar
  192. // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
  193. $('#calendar').fullCalendar('renderEvent', event, true);
  194. },
  195. eventRender:function (event, element) {
  196. var options = {}
  197. options.html = true;
  198. options.trigger = 'hover';
  199. options.title = event.title;
  200. options.content = '';
  201. if (event.block != null && event.block != undefined) {
  202. options.content += "<ul class='no-margin block-properties'>";
  203. options.content += "<li><span>lektor:</span> " + event.block.lector + "</li>";
  204. options.content += "<li><span>Kapacita:</span>" + event.attendees_count + "/" + event.block.capacity + "</li>";
  205. options.content += "<li><span>Lokalita:</span> " + event.block.location + "</li>";
  206. options.content += "<li><span>Pomůcky:</span> " + event.block.tools + "</li>";
  207. options.content += "</ul>";
  208. options.content += "<p>" + event.block.perex + "</p>";
  209. }
  210. element.find('.fc-event-title').popover(options);
  211. }
  212. }
  213. var calendar = $('#calendar').fullCalendar(jQuery.extend(local_config, localization_config));
  214. }