Projet

Général

Profil

Télécharger (17,9 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / static / js / calebasse.agenda.js @ 1fc93cd3

1
var path = location.pathname.split('/');
2
var service = path[1];
3
var app_name = path[2];
4
var current_date = path[3];
5
COOKIE_PATH = '/' + service + '/agenda';
6

    
7
function delete_prompt(text) {
8
  var r = prompt(text + '\n Pour cela veuillez entrer DEL');
9
  if (r.toLowerCase().replace(/^\s+|\s+$/g, '') == 'del') {
10
    return true;
11
  } else {
12
    return false;
13
  }
14
}
15
function enable_events(base) {
16
      $(base).find('.textedit').on('keydown', function() {
17
          $('button', this).removeAttr("disabled");
18
      });
19
      $(base).find('.textedit button').on('click', function() {
20
          var textarea = $(this).prev();
21
          var span = textarea.prev()
22
          var btn = $(this)
23
          if ($(this).data('act-id'))
24
          {
25
            var data = {comment: textarea.val() };
26
            var data = JSON.stringify(data);
27
            $.ajax({
28
              url: '/api/v1/act/' + $(this).data("act-id") + '/?format=json&date=' + $(this).data('date'),
29
              type: 'PATCH',
30
              contentType: 'application/json',
31
              data: data,
32
              success: function(data) {
33
                btn.attr('disabled', 'disabled');
34
                span.html('Commentaire modifié avec succès');
35
              }
36
            });
37
          }
38
          else
39
          {
40
            var data = {description: textarea.val() };
41
            var data = JSON.stringify(data);
42
            $.ajax({
43
              url: '/api/v1/event/' + $(this).data("event-id") + '/?format=json&date=' + $(this).data('date'),
44
              type: 'PATCH',
45
              contentType: 'application/json',
46
              data: data,
47
              success: function(data) {
48
                btn.attr('disabled', 'disabled');
49
                span.html('Commentaire modifié avec succès');
50
              }
51
            });
52
          }
53
      });
54
      /* TODO: put this in a generic function */
55
      $('.input_is_billable').click(function() {
56
          if ($(this).data("switch-billable") == "True") {
57
              var value = "false";
58
          } else {
59
              var value = "true";
60
          }
61
          $.ajax({
62
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
63
              type: 'PATCH',
64
              contentType: 'application/json',
65
              data: '{"switch_billable": ' + value + '}',
66
              success: function(data) {
67
              }
68
          });
69
      });
70
      $('.input_is_lost').click(function() {
71
          if ((this.checked) == true) {
72
              var value = "true";
73
          } else {
74
              var value = "false";
75
          }
76
          $.ajax({
77
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
78
              type: 'PATCH',
79
              contentType: 'application/json',
80
              data: '{"is_lost": ' + value + '}',
81
              success: function(data) {
82
              }
83
          });
84
      });
85

    
86
      $(base).find('.appointment').on('click', function() {
87
          $('.textedit span', this).html('');
88
      });
89

    
90
      $(base).find('.remove-appointment').on('click', function() {
91
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
92
          if (r == true)
93
          {
94
            $.ajax({
95
              url: $(this).data('url'),
96
              type: 'DELETE',
97
              success: function(data) {
98
                  window.location.reload(true);
99
                  return false;
100
              }
101
            });
102
           }
103
        return false;
104
      });
105
      $(base).find('.newrdv').click(function() {
106
          var participants = new Array();
107
          if ($.cookie('agenda-worker-tabs')) {
108
              participants = $.cookie('agenda-worker-tabs').map(function(i, v) { var data = i.split('-'); return data[2]});
109
          }
110
          var qs = $.param({participants: $.makeArray(participants),
111
                            room: $.cookie('active-ressource-agenda'),
112
                            time: $(this).data('hour') }, true);
113
          var new_appointment_url = $(this).data('url') + "?" + qs;
114
          event_dialog(new_appointment_url, 'Nouveau rendez-vous', '850px', 'Ajouter');
115
      });
116
      $(base).find('.edit-appointment').click(function() {
117
        id = $(this).data("event-id");
118
        $.getJSON("/api/v1/eventwithact/" + id + "/?format=json")
119
          .done(function () {
120
            event_dialog("/" + service + "/agenda/" + current_date + "/update-rdv/" + id,
121
              'Modifier rendez-vous', '850px', 'Modifier');
122
          })
123
         .fail(function() {
124
            window.location.reload(true);
125
            $('.messages').html("Le rendez-vous n'existe plus");
126
            return false;
127
         });
128
        return false;
129
      });
130
      $(base).find('.newevent').click(function() {
131
          var participants = new Array();
132
          if ($.cookie('agenda-worker-tabs')) {
133
              var participants = $.cookie('agenda-worker-tabs').map(function(i, v) { var data = i.split('-'); return data[2]});
134
          }
135
          var qs = $.param({participants: $.makeArray(participants),
136
                            room: $.cookie('active-ressource-agenda'),
137
                            time: $(this).data('hour') }, true);
138
          event_dialog($(this).data('url') + "?" + qs, 'Nouvel événement', '850px', 'Ajouter');
139
      });
140
      $(base).find('.edit-event').click(function() {
141
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
142
          return false;
143
      });
144
      $(base).find('#print-button').click(function() { window.print(); });
145

    
146
      $('.generate-mail-btn', base).click(function() {
147
        var url = '../../dossiers/' + $(this).data('dossier-id') + '/generate?event-id=' + $(this).data('event-id') + '&date=' + $(this).data('date');
148
        $('#ajax-dlg').load(url,
149
          function () {
150
            $(this).dialog({title: 'Générer un courrier', width: '500px',
151
                      buttons: [ { text: "Fermer",
152
                          click: function() { $(this).dialog("close"); } },
153
                      { text: "Générer",
154
                          click: function() { $("#ajax-dlg form").submit(); $(this).dialog("close"); } }]});
155
             $(this).find('.addresses input[type=radio]').change(function() {
156
               var address = '';
157
               if ($(this).data('contact-gender')){address += $(this).data('contact-gender') + ' ';}
158
               if ($(this).data('contact-first-name')){address += $(this).data('contact-first-name') + ' ';}
159
               if ($(this).data('contact-last-name')){address += $(this).data('contact-last-name') + '\n';}
160
               if ($(this).data('address-number')){address += $(this).data('address-number') + ' ';}
161
               if ($(this).data('address-street')){address += $(this).data('address-street') + '\n';}
162
               if ($(this).data('address-address-complement')){address += $(this).data('address-address-complement') + '\n';}
163
               if ($(this).data('address-zip-code')){address += $(this).data('address-zip-code') + ' ';}
164
               if ($(this).data('address-city')){address += $(this).data('address-city') + '\n';}
165
               $('#id_address').val(address);
166
               $('#id_phone_address').val($(this).attr('data-address-phone'));
167
             });
168
             $('.addresses input[type=radio]').first().click();
169
          });
170
        return false;
171
      });
172
      $(base).on('click', '.update-periodic-event', function () {
173
        $('.ui-icon-closethick').click();
174
        // remove the form from previous hidden layer in order to prevent two
175
        // elements with 'id_date' id on the page
176
        $(this).parent().remove();
177

    
178
        var id = $(this).data('id');
179
        var delete_url = $(this).data('delete-url');
180
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
181
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null,
182
          function (dialog) {
183
            $('#ajax-dlg .datepicker-date').datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
184
            var buttons = $(dialog).dialog('option', 'buttons');
185
            buttons.push({
186
              text: "Supprimer",
187
              id: "delete-btn",
188
              click: function () {
189
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer cet évènement récurrent ?");
190
                if (r == true)
191
                {
192
                  $.ajax({
193
                    url: delete_url,
194
                    type: 'DELETE',
195
                    success: function(data) {
196
                        window.location.reload(true);
197
                        return false;
198
                    }
199
                  });
200
                }
201
              }
202
            });
203
            $(dialog).dialog('option', 'buttons', buttons);
204
          }
205
        );
206
      });
207
      $(base).on('click', '.update-periodic-rdv', function () {
208
        $('.ui-icon-closethick').click();
209
        var id = $(this).data('id');
210
        var delete_url = $(this).data('delete-url');
211
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
212
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null,
213
          function (dialog) {
214
            $('#ajax-dlg .datepicker-date').datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
215
            var buttons = $(dialog).dialog('option', 'buttons');
216
            buttons.push({
217
              text: "Supprimer",
218
             id: "delete-btn",
219
              click: function () {
220
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer ce rendez-vous récurrent ?");
221
                if (r == true)
222
                {
223
                  $.ajax({
224
                    url: delete_url,
225
                    type: 'DELETE',
226
                    success: function(data) {
227
                        window.location.reload(true);
228
                        return false;
229
                    }
230
                  });
231
                }
232
              }
233
            });
234
            $(dialog).dialog('option', 'buttons', buttons);
235
          }
236
        );
237
      });
238
}
239

    
240
function toggle_ressource(ressource_selector, ressource) {
241

    
242
    var ressource_id = $(ressource_selector).data(ressource + '-id');
243
     if (!ressource_id) {
244
        return;
245
    }
246

    
247
    $(ressource_selector).toggleClass('active');
248
    if (!($.cookie('agenda-' + ressource + '-tabs'))) {
249
        $.cookie('agenda-' + ressource + '-tabs', new Array(), { path: COOKIE_PATH });
250
    }
251
    if ($(ressource_selector).hasClass('active')) {
252
        var tabs = $.cookie('agenda-' + ressource + '-tabs');
253
        if ($.inArray($(ressource_selector).attr('id'), tabs) == -1)
254
        {
255
            tabs.push($(ressource_selector).attr('id'));
256
            $.cookie('agenda-' + ressource + '-tabs', tabs, { path: COOKIE_PATH });
257
        }
258
    }
259
    else {
260
        var agendatabs = $.cookie('agenda-' + ressource + '-tabs');
261
        $.each(agendatabs, function (i, value) {
262
            if (value == $(ressource_selector).attr('id')) {
263
                agendatabs.splice(i, 1);
264
            }
265
        });
266
        $.cookie('agenda-' + ressource + '-tabs', agendatabs, { path: COOKIE_PATH });
267
    }
268
    var target = $($(ressource_selector).data('target'));
269
    target.toggle()
270
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
271

    
272
    if (! $('#users li.active').length) {
273
        $('#agendas #tabs div').hide();
274
    }
275

    
276
    var tab = $('#link-tab-' + ressource + '-' + ressource_id).parent().get(0);
277
    var tab_list = $(tab).parent().get(0);
278
    $(tab).detach().appendTo(tab_list);
279

    
280
    var url = $("#date-selector").data('url');
281

    
282
    var tab_selector = '';
283
    if (ressource == 'worker') {
284
        tab_selector = '#selector-' + ressource + '-' + ressource_id + '.active';
285
    } else {
286
        tab_selector = '#selector-ressource-' + ressource_id + '.active';
287
    }
288

    
289
    if ($(tab_selector).length) {
290
        /* load disponibility column */
291
        $.get(url + 'ajax-' + ressource + '-disponibility-column/' + ressource_id,
292
            function(data) {
293
                if ($(tab_selector).hasClass('active')) {
294
                    var availability_block = $('ul#availability');
295
                    availability_block.append($(data));
296
                }
297
            }
298
        );
299
    } else {
300
        // remove hidden ressource availability
301
        $('ul#availability li.ressource-'+ressource_id).remove();
302
    }
303
    return target.find('a.tab');
304
}
305

    
306
function event_dialog(url, title, width, btn_text) {
307
    add_dialog('#ajax-dlg', url, title, width, btn_text);
308
}
309

    
310
(function($) {
311
  $(function() {
312
      $('#tabs').tabs({
313
          load: function(event, ui) {
314
              $('#tabs > div > div').accordion({active: false,
315
                                                autoHeight: false,
316
                                                collapsible: true});
317
              enable_events($('body'));
318
          },
319
          selected: -1,
320
          collapsible: true,
321
      });
322

    
323
      if ($('.worker-item').length) {
324
          $('.worker-item').on('click', function() {
325
              var target = toggle_ressource(this, 'worker');
326

    
327
              if ($(target).is(':visible')) {
328
                  $(target).click();
329
              }
330
              if ($('#filtre input').val()) {
331
                  $('#filtre input').val('');
332
                  $('#filtre input').keyup();
333
                  $('#filtre input').focus();
334
              }
335
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
336
                $('li.agenda:visible:last a.tab').click();
337
              }
338
          });
339

    
340
          $('a.tab').click(function() {
341
              $.cookie('active-worker-agenda', $(this).data('id'),  { path: COOKIE_PATH });
342
          });
343

    
344
          if ($.cookie('agenda-worker-tabs')) {
345
              $.each($.cookie('agenda-worker-tabs'), function (i, worker_selector) {
346
                  toggle_ressource('#' + worker_selector, 'worker');
347
              });
348
              if ($.cookie('active-worker-agenda'))
349
              {
350
                  var target = $('#link-tab-worker-' + $.cookie('active-worker-agenda'));
351
                  if (target.is(':visible')) {
352
                      target.click();
353
                  }
354
              }
355
          }
356
      }
357

    
358
      if ($('.ressource-item').length) {
359
          $('.ressource-item').on('click', function() {
360
              var target = toggle_ressource(this, 'ressource');
361
              if ($(target).is(':visible')) {
362
                  $(target).click();
363
              }
364
              if ($('#filtre input').val()) {
365
                  $('#filtre input').val('');
366
                  $('#filtre input').keyup();
367
                  $('#filtre input').focus();
368
              }
369
          });
370

    
371
          $('a.tab').click(function() {
372
              $.cookie('active-ressource-agenda', $(this).data('id'), { path: COOKIE_PATH });
373
          });
374

    
375
          if ($.cookie('agenda-ressource-tabs')) {
376
              $.each($.cookie('agenda-ressource-tabs'), function (i, ressource_selector) {
377
                  toggle_ressource('#' + ressource_selector, 'ressource');
378
              });
379
              if ($.cookie('active-ressource-agenda'))
380
              {
381
                  var target = $('#link-tab-ressource-' + $.cookie('active-ressource-agenda'));
382
                  if (target.is(':visible')) {
383
                      target.click();
384
                  }
385
              }
386
          }
387
      }
388

    
389
      $('a.close-tab').click(function() {
390
          var target = '#' + $(this).data('target');
391
          $(target).click();
392
          if ($.cookie('active-ressource-agenda') == $(target).data('ressource-id')) {
393
              $.cookie('active-ressource-agenda','', { path: COOKIE_PATH });
394
          }
395

    
396
      });
397

    
398

    
399
      /* Gestion du filtre sur les utilisateurs */
400
      $('#filtre input').keyup(function() {
401
          var filtre = $(this).val();
402
          if ($('#show-everybody').length) {
403
              var everybody = $('#show-everybody').is(':checked');
404
          } else {
405
              var everybody = true;
406
          }
407
          if (filtre) {
408
              $('#show-everybody').attr('checked', true);
409
              $('#users li').each(function() {
410
                  if ($(this).text().match(new RegExp(filtre, "i"))) {
411
                      $(this).show();
412
                  } else {
413
                      $(this).hide();
414
                  }
415
              });
416
          } else {
417
              $('#users li').show();
418
              if (! everybody) {
419
                  $('.worker-item:not(.in_service)').hide();
420
                  $('.worker-item:not(.intervenant)').hide();
421
              }
422
          }
423
          /* hide worker type titles that do not have a single visible person */
424
          $("#users ul:has(*):has(:visible)").parent().prev().show();
425
          $("#users ul:has(*):not(:has(:visible))").parent().prev().hide();
426
      });
427

    
428
      $('.date').datepicker({showOn: 'button'});
429
      $('#add-intervenant-btn').click(function() {
430
          var text = $(this).prev().val();
431
          $('#intervenants ul').append('<li><input type="checkbox" value="' + text + '" checked="checked">' + text + '</input></li>');
432
          $(this).prev().val('').focus();
433
          return false;
434
      });
435
      $('#show-everybody').change(function() {
436
      if (! $(this).is(':checked')) {
437
        $('#filtre input').val('');
438
      }
439
      $('#filtre input').keyup();
440
      return;
441
    });
442
    $('select[name^="act_state"]').on('change', function () {
443
    $(this).next('button').prop('disabled',
444
      ($(this).data('previous') == $(this).val()));
445
    })
446
    $('#filtre input').keyup();
447

    
448
    $.each({'persons': {'button': 'worker', 'element': 'worker'},
449
            'rooms': {'button': 'ressource', 'element': 'ressource'}
450
           },
451
         function(key, value) {
452
             $('#close-all-agendas').click(function() {
453
                 console.log(value);
454
                 $.cookie('active-' + value.element + '-agenda', '', {path: COOKIE_PATH});
455
                 $('.' + value.element + '-item.active').each(function (i, v) {
456
                     toggle_ressource(v, value.element);
457
                 });
458
             });
459
         });
460
  });
461
})(window.jQuery)
(2-2/21)