main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. var majpushed = false;
  2. var clickedDate;
  3. $(window).resize(function() {
  4. $('#planning').fullCalendar('option', 'height', $(window).height()*0.83);
  5. });
  6. //CHARGEMENT DE LA PAGE
  7. function init_plugin_planning(){
  8. switch($.urlParam('page')){
  9. default:
  10. break;
  11. }
  12. if($('.planning-page').attr('data-event-editable')=='0')
  13. $('#planning-event-form').find('input,select,textarea').attr('readonly','readonly');
  14. $( "#planning-datepicker" ).datepicker({
  15. dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
  16. dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  17. dayNamesShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
  18. monthNames: ["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre"],
  19. monthNamesShort: ["Jan","Fév","Mars","Avr","Mai","Juin","Juil","Aout","Sept","Oct","Nov","Déc"],
  20. onSelect : function(date,element){
  21. $('#planning').fullCalendar( 'gotoDate', date );
  22. }
  23. });
  24. planning_search(function(){
  25. planning_calendar_init();
  26. });
  27. $(document).keydown(function(e){
  28. switch(e.keyCode){
  29. //Appuis sur maj
  30. case 16:
  31. majpushed = true;
  32. break;
  33. //Appuis sur suppr
  34. case 46:
  35. planning_event_delete($('.event-selected'));
  36. break;
  37. }
  38. });
  39. //relachage du maj
  40. $(document).keyup(function(e){
  41. if(e.keyCode!=16) return;
  42. majpushed = false;
  43. });
  44. $('#planning-event-type > .dropdown-menu > a').click(function(){
  45. var id = $(this).attr('data-id');
  46. var icon = $(this).find('i').attr('class');
  47. var color = $(this).attr('data-color');
  48. var text = $(this).text();
  49. $('#planning-event-type > button').css('background-color',color).attr('data-id',id).html('<i class="'+icon+'"></i> '+text);
  50. $('#label').focus();
  51. });
  52. }
  53. function init_setting_planning(){
  54. planning_event_type_search();
  55. $("#planning_event_types tbody").sortable({
  56. opacity: 0.75,
  57. cursor: "move",
  58. axis: "y",
  59. update: function(e, ui) {
  60. var data = { action : 'planning_event_type_sort' ,sort:[] };
  61. $("#planning_event_types tbody tr:visible").each(function(i,tr){
  62. data.sort.push($(tr).attr('data-id'));
  63. });;
  64. $.action(data);
  65. }
  66. }).disableSelection();
  67. }
  68. /** PLANNING **/
  69. //Récuperation d'une liste de planning dans le tableau #plannings
  70. function planning_search(callback){
  71. $.action({
  72. action:'planning_search'
  73. },function(r){
  74. var planning = $('#plannings');
  75. var sharedPlanning = $('#shared-plannings');
  76. planning.find('li:visible').remove();
  77. sharedPlanning.find('li:visible').remove();
  78. var tpl = $('li.hidden',planning).get(0).outerHTML;
  79. var options = '';
  80. for(var k in r.rows){
  81. var line = r.rows[k];
  82. options +='<option value="'+line.id+'">'+line.label+'</option>';
  83. var li = $(Mustache.render(tpl,line));
  84. if(line.selected) li.find('input').prop('checked',true);
  85. if(line.shared){
  86. sharedPlanning.append(li);
  87. }else{
  88. planning.append(li);
  89. }
  90. li.removeClass('hidden');
  91. }
  92. $('#event-planning').html(options);
  93. if(callback!=null) callback();
  94. });
  95. }
  96. //Ajout ou modification d'élément planning
  97. function planning_save(){
  98. var data = $('#planning-form').toJson();
  99. data.action = 'planning_save';
  100. $.action(data,function(r){
  101. $.message('success','Enregistré');
  102. $('#planningModal').modal('hide');
  103. planning_search();
  104. });
  105. }
  106. //Récuperation ou edition d'élément planning
  107. function planning_edit(element){
  108. var data = {};
  109. if(element)
  110. data.id = $(element).closest('li').attr('data-id');
  111. $('#planningModal .modal-content').load('action.php?action=planning_edit',data,function(){
  112. init_components('#planningModal .modal-content');
  113. planning_share_search();
  114. });
  115. $('#planningModal').modal('show');
  116. }
  117. //Suppression d'élement planning
  118. function planning_delete(element){
  119. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  120. var line = $(element).closest('li');
  121. $.action({
  122. action : 'planning_delete',
  123. id : line.attr('data-id')
  124. },function(r){
  125. line.remove();
  126. $('#planning').fullCalendar( 'refetchEvents' );
  127. $.message('info','Élement supprimé');
  128. });
  129. }
  130. function planning_select(){
  131. $('#planning').fullCalendar( 'refetchEvents' );
  132. }
  133. //Récuperation d'une liste de planning dans le tableau #plannings
  134. function planning_calendar_init(callback){
  135. var plannings = [];
  136. planning = $('#planning').fullCalendar({
  137. locale: 'fr',
  138. nextDayThreshold: '00:00:00',
  139. customButtons: {
  140. refreshButton: {
  141. text: 'Rafraichir',
  142. click: function() {
  143. $('#planning').fullCalendar( 'refetchEvents' );
  144. }
  145. }
  146. },
  147. header: {
  148. left: 'prev,next today refreshButton',
  149. center: 'title',
  150. right: 'month,agendaWeek,agendaDay,listWeek'
  151. },
  152. height: $(window).height()*0.83,
  153. navLinks: true, // can click day/week names to navigate views
  154. editable: false,
  155. eventLimit: true, // allow "more" link when too many events
  156. events: {
  157. url: 'action.php?action=planning_event_search',
  158. data: function () {
  159. parameters = { plannings : [] };
  160. $('.planning-list li:visible input:checked').each(function(i,input){
  161. parameters.plannings.push($(input).closest('li').attr('data-id'))
  162. });
  163. return parameters;
  164. },
  165. error: function() {
  166. $('#script-warning').removeClass('hidden');
  167. }
  168. },
  169. //weekNumbers :true,
  170. resources: [
  171. { id: 'a', title: 'Room A' },
  172. { id: 'b', title: 'Room B' },
  173. { id: 'c', title: 'Room C' },
  174. { id: 'd', title: 'Room D' }
  175. ],
  176. loading: function(isLoading){
  177. if(isLoading)
  178. $('.loaderStd').removeClass('hidden');
  179. else
  180. $('.loaderStd').addClass('hidden');
  181. },
  182. dayClick: function(date, jsEvent, view) {
  183. if($('.planning-page').attr('data-event-editable')=='0') return;
  184. clickedDate = date;
  185. $("#planning").on("mousemove", function(){
  186. clickedDate = null;
  187. $("#planning").off("mousemove");
  188. });
  189. },
  190. eventRender: function(event, element) {
  191. if(event.rendering=='background') return;
  192. element.find('.fc-content').append('<span class="fc-icon right"><i class="'+event.icon+'"></i></span>');
  193. element.attr('title','Planning : '+event.planningLabel);
  194. element.css('border-bottom','4px solid '+event.underlineColor) ;
  195. var event = planning_convert_event(event);
  196. if(event == null) return;
  197. element.attr('data-id',event.id).on("dblclick",function(){
  198. planning_event_edit(event);
  199. });
  200. },
  201. eventClick: function(event,jsEvent,view) {
  202. if(!majpushed) $('.event-selected').removeClass('event-selected');
  203. $(this).addClass('event-selected');
  204. jsEvent.stopPropagation();
  205. },
  206. eventMouseover: function(event, jsEvent, view){
  207. if(event.end == null || event.start == null) return;
  208. var startDay = event.start.format("DD/MM/YYYY");
  209. var endDay = event.end.format("DD/MM/YYYY");
  210. var startSchedule = event.start.format("HH:mm");
  211. var endSchedule = event.end.format("HH:mm");
  212. var location = event.location == null ? '' : event.location;
  213. var description = event.description == null ? '' : event.description;
  214. var info = "Du "+startDay+" au "+endDay+"\r"+event.title+"\r"+'De '+startSchedule+' à '+endSchedule+"\r"+description+"\r"+location;
  215. $("a[data-id='"+event.id+"']").attr("title", info);
  216. },
  217. eventDrop: function(event, delta, revertFunc) {
  218. if($('.planning-page').attr('data-event-editable')=='0') return;
  219. var event = planning_convert_event(event);
  220. if(event == null) return;
  221. delete event.source;
  222. delete event.start;
  223. delete event.end;
  224. planning_event_save(event,function(r){
  225. if(r.error)revertFunc();
  226. });
  227. },
  228. eventResize: function(event, delta, revertFunc, jsEvent, ui, view ) {
  229. if($('.planning-page').attr('data-event-editable')=='0') return;
  230. var event = planning_convert_event(event);
  231. if(event == null) return;
  232. planning_event_save(event,function(r){
  233. if(r.error )revertFunc();
  234. });
  235. },
  236. eventAfterAllRender : function(){
  237. if($.urlParam('event')=='') return;
  238. $('.fc-event[data-id="'+$.urlParam('event')+'"]').addClass('event-selected');
  239. }
  240. });
  241. if($.urlParam('start'))
  242. $('#planning').fullCalendar( 'gotoDate', $.urlParam('start') );
  243. //Ouvre la fenetre lors du double click sur une journée
  244. $('#planning').dblclick(function(clickEvent){
  245. if(!clickedDate) return;
  246. var view = $('#planning').fullCalendar( 'getView' );
  247. var calculatedEnd = parseInt(clickedDate.format("HH"))+1;
  248. var newEvent = {
  249. startDate : clickedDate.format("DD/MM/YYYY"),
  250. endDate : clickedDate.format("DD/MM/YYYY"),
  251. startHour : clickedDate.format("HH"),
  252. endHour : "00".substring(0, 2 - calculatedEnd.length) + ''+ calculatedEnd,
  253. startMinut : clickedDate.format("mm"),
  254. endMinut : clickedDate.format("mm")
  255. };
  256. if(view.name=='month'){
  257. newEvent.startHour = '08';
  258. newEvent.endHour = '12';
  259. newEvent.startMinut = '00';
  260. newEvent.endMinut = '00';
  261. var planningConf = $('#planning').data();
  262. var defaultStart = planningConf.startHour.split(':');
  263. var defaultEnd = planningConf.endHour.split(':');
  264. if(defaultStart.length==2){
  265. newEvent.startHour = defaultStart[0];
  266. newEvent.startMinut = defaultStart[1];
  267. }
  268. if(defaultEnd.length==2){
  269. newEvent.endHour = defaultEnd[0];
  270. newEvent.endMinut = defaultEnd[1];
  271. }
  272. }
  273. planning_event_edit(newEvent);
  274. clickEvent.stopPropagation();
  275. });
  276. $('#planning').click(function(){
  277. $('#planning-event-panel').addClass('fold');
  278. $('.event-selected').removeClass('event-selected');
  279. });
  280. }
  281. function planning_convert_event(event){
  282. if(event.end == null || event.start == null) return null;
  283. event.label = event.title;
  284. event.startDate = event.start.format("DD/MM/YYYY"),
  285. event.endDate = event.end.format("DD/MM/YYYY"),
  286. event.startHour = event.start.format("HH"),
  287. event.endHour = event.end.format("HH"),
  288. event.startMinut = event.start.format("mm"),
  289. event.endMinut = event.end.format("mm");
  290. return event;
  291. }
  292. /** PLANNING EVENT **/
  293. //Ajout ou modification d'élément planningevent
  294. function planning_event_save(data,callback){
  295. if($('.planning-page').attr('data-event-editable')=='0') return;
  296. if(!data){
  297. data = $('#planning-event-form').toJson();
  298. data.type = $('#planning-event-type > button').attr('data-id');
  299. data.id = $('#planning-event-form').attr('data-event');
  300. data.planning = $('#event-planning').val();
  301. data.dailyFrequency = $('[name="dailyRepeatFrequency"]:checked').attr('data-value');
  302. data.repeatEndType = $('[name="repeatEndType"]:checked').attr('data-value');
  303. data.weeklyDay = [];
  304. $('[data-type-form="weekly"] input[type="checkbox"]:checked').each(function(){
  305. data.weeklyDay.push($(this).attr('data-value'));
  306. });
  307. }
  308. data.action = 'planning_event_save';
  309. $.action(data,function(r){
  310. $('#planning-event-form').attr('data-event','');
  311. var eventPanel = $('#planning-event-panel');
  312. eventPanel.addClass('fold');
  313. $('#planning').fullCalendar('refetchEvents');
  314. if(callback) callback(r);
  315. },function(r){
  316. if(callback) callback(r);
  317. });
  318. }
  319. //Récuperation ou edition d'élément planningevent
  320. function planning_event_edit(event){
  321. var eventPanel = $('#planning-event-panel');
  322. eventPanel.removeClass('fold');
  323. eventPanel.find('#label').focus();
  324. $('#planning-event-form').find('input,textarea').val('');
  325. $.setForm('#planning-event-form',event);
  326. $('#event-repeat-type').val(event.repeatType).trigger('change');
  327. $('#event-repeat-occurences').val(event.repeatOccurence);
  328. $('#event-repeat-until').val(event.repeatUntil)
  329. $('#event-repeat-daily-number').val(event.repeatDailyNumber);
  330. $('#event-repeat-yearly-number').val(event.repeatYearlyNumber);
  331. $('#event-repeat-monthly-number').val(event.repeatMonthlyNumber);
  332. $('#event-repeat-yearly-month').val(event.repeatYearlyMonth);
  333. $('#event-repeat-monthly-day').val(event.repeatMonthlyDay);
  334. $('[data-type-form="weekly"] input[type="checkbox"]').each(function(){
  335. $(this).prop('checked',$.inArray($(this).attr('data-value'),event.repeatWeeklyDay) != -1);
  336. });
  337. $('[name="repeatEndType"][data-value="'+event.repeatEndType+'"]').prop('checked',true);
  338. $('#planning-event-type > .dropdown-menu > a[data-id="'+event.type+'"]').trigger('click');
  339. var planning = '';
  340. var checkedPlannings = $('.planning-list li:visible input:checked');
  341. //déduction du planning par défaut ou du planning correspondant a l'event
  342. if(event.planning){
  343. planning = event.planning ;
  344. }else{
  345. $('#planning-event-type .dropdown-menu > a:eq(0)').trigger('click');
  346. planning = $('.planning-list li[data-default="1"]').attr('data-id');
  347. if(checkedPlannings.length==1){
  348. planning = checkedPlannings.closest('li').attr('data-id');
  349. }
  350. }
  351. $('#event-planning').val(planning);
  352. $('#planning-event-form').attr('data-event',!event.id? '': event.id);
  353. }
  354. //Suppression d'élement planningevent
  355. function planning_event_delete (elements){
  356. if($('.planning-page').attr('data-event-editable')=='0') return;
  357. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  358. var events = [];
  359. elements.each(function(i,event){
  360. if($(event).attr('data-id') != null)
  361. events.push($(event).attr('data-id'));
  362. });
  363. $.action({
  364. action : 'planning_event_delete',
  365. events : events
  366. },function(r){
  367. $('#planning').fullCalendar( 'refetchEvents' );
  368. });
  369. }
  370. /** PLANNING EVENT TYPE **/
  371. //Récuperation d'une liste de planningeventtype dans le tableau #planningeventtypes
  372. function planning_event_type_search(callback){
  373. $('#planning_event_types').fill({
  374. action:'planning_event_type_search'
  375. },function(){
  376. if(callback!=null) callback();
  377. });
  378. }
  379. //Ajout ou modification d'élément event_type
  380. function planning_event_type_save(){
  381. var data = $('#event-type-form').toJson();
  382. $.action(data,function(r){
  383. $('#event-type-form').attr('data-id','');
  384. planning_event_type_search();
  385. $.message('success','Enregistré');
  386. });
  387. }
  388. //Récuperation ou edition d'élément event_type
  389. function planning_event_type_edit(element){
  390. var line = $(element).closest('tr');
  391. $.action({action:'planning_event_type_edit',id:line.attr('data-id')},function(r){
  392. $.setForm('#event-type-form',r);
  393. $('#event-type-form').attr('data-id',r.id);
  394. init_components('#event-type-form');
  395. });
  396. }
  397. //Suppression d'élement event_type
  398. function planning_event_type_delete(element){
  399. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  400. var line = $(element).closest('tr');
  401. $.action({
  402. action : 'planning_event_type_delete',
  403. id : line.attr('data-id')
  404. },function(r){
  405. line.remove();
  406. $.message('info','Élement supprimé');
  407. });
  408. }
  409. function planning_event_repeat_change(){
  410. var repeatEventType = $('#event-repeat-type').val();
  411. var eventOption = $('#event-repeat-options');
  412. if(repeatEventType=='never'){
  413. eventOption.addClass('hidden');
  414. }else{
  415. $('[data-type-form]',eventOption).addClass('hidden');
  416. $('[data-type-form="'+repeatEventType+'"]',eventOption).removeClass('hidden');
  417. eventOption.removeClass('hidden');
  418. }
  419. }
  420. /** PLANNINGSHARE **/
  421. //Récuperation d'une liste de planningshare dans le tableau #planningshares
  422. function planning_share_search(callback){
  423. var planning = $('#planning-form').attr('data-id');
  424. if(planning=='') return;
  425. $('#planning-shares').fill({
  426. action:'planning_share_search',
  427. planning : planning,
  428. },function(){
  429. if(callback!=null) callback();
  430. });
  431. }
  432. //Ajout ou modification d'élément planningshare
  433. function planning_share_save(){
  434. var data = $('#planning-share-form').toJson();
  435. data.action='planning_share_save';
  436. data.planning = $('#planning-form').attr('data-id');
  437. data.recipientEntity = $('#recipient').data('values')[0].entity;
  438. if(data.planning=='') return $.message('warning','Veuillez enregitrer une fois le planning avant de créer des partages');
  439. $.action(data,function(r){
  440. planning_share_search();
  441. });
  442. }
  443. //Suppression d'élement planningshare
  444. function planning_share_delete(element){
  445. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  446. var line = $(element).closest('tr');
  447. $.action({
  448. action : 'planning_share_delete',
  449. id : line.attr('data-id')
  450. },function(r){
  451. line.remove();
  452. $.message('info','Élement supprimé');
  453. });
  454. }
  455. //Enregistrement des configurations
  456. function planning_setting_save(){
  457. $.action({
  458. action : 'planning_setting_save',
  459. fields : $('#planning-setting-form').toJson()
  460. },function(){ $.message('info','Configuration enregistrée'); });
  461. }