Projet

Général

Profil

Télécharger (16,8 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / static / js / calebasse.agenda.js @ dd986559

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

    
16
function get_participants() {
17
    var participants = new Array();
18
    var active_agenda = $.cookie('active-agenda').split('-');
19
    var ressource = '';
20
    if (active_agenda[0] == 'ressource') {
21
        ressource = active_agenda[1];
22
    } else {
23
        ressource = $.cookie('last-ressource');
24
    }
25

    
26
    if ($.cookie('agenda-tabs')) {
27
        participants = $.cookie('agenda-tabs').filter(function(v) {
28
            var data = v.split('-');
29
            if(data[0]=='worker')
30
                return true;
31
        });
32
        participants = participants.map(function(v) {
33
            var data = v.split('-'); return data[1]
34
        });
35
    }
36
    return $.param({participants: $.makeArray(participants),
37
                    ressource: ressource,
38
                    time: $(this).data('hour') }, true);
39
}
40

    
41
function enable_new_appointment(base) {
42
    var base = base || 'body';
43
    $(base).find('.newrdv').click(function() {
44
        event_dialog($(this).data('url') + "?" + get_participants(), 'Nouveau rendez-vous', '850px', 'Ajouter');
45
    });
46
}
47

    
48
function enable_new_event(base) {
49
    var base = base || 'body';
50
    $(base).find('.newevent').click(function() {
51
        event_dialog($(this).data('url') + "?" + get_participants(), 'Nouvel événement', '850px', 'Ajouter');
52
    });
53
}
54

    
55
function enable_events(base) {
56
      $(base).find('.textedit').on('keydown', function() {
57
          $('button', this).removeAttr("disabled");
58
      });
59
      $(base).find('.textedit button').on('click', function() {
60
          var textarea = $(this).prev();
61
          var span = textarea.prev()
62
          var btn = $(this)
63
          if ($(this).data('act-id'))
64
          {
65
            var data = {comment: textarea.val() };
66
            var data = JSON.stringify(data);
67
            $.ajax({
68
              url: '/api/v1/act/' + $(this).data("act-id") + '/?format=json&date=' + $(this).data('date'),
69
              type: 'PATCH',
70
              contentType: 'application/json',
71
              data: data,
72
              success: function(data) {
73
                btn.attr('disabled', 'disabled');
74
                span.html('Commentaire modifié avec succès');
75
              }
76
            });
77
          }
78
          else
79
          {
80
            var data = {description: textarea.val() };
81
            var data = JSON.stringify(data);
82
            $.ajax({
83
              url: '/api/v1/event/' + $(this).data("event-id") + '/?format=json&date=' + $(this).data('date'),
84
              type: 'PATCH',
85
              contentType: 'application/json',
86
              data: data,
87
              success: function(data) {
88
                btn.attr('disabled', 'disabled');
89
                span.html('Commentaire modifié avec succès');
90
              }
91
            });
92
          }
93
      });
94
      /* TODO: put this in a generic function */
95
      $('.input_is_billable').click(function() {
96
          if ($(this).data("switch-billable") == "True") {
97
              var value = "false";
98
          } else {
99
              var value = "true";
100
          }
101
          $.ajax({
102
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
103
              type: 'PATCH',
104
              contentType: 'application/json',
105
              data: '{"switch_billable": ' + value + '}',
106
              success: function(data) {
107
              }
108
          });
109
      });
110
      $('.input_is_lost').click(function() {
111
          if ((this.checked) == true) {
112
              var value = "true";
113
          } else {
114
              var value = "false";
115
          }
116
          $.ajax({
117
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
118
              type: 'PATCH',
119
              contentType: 'application/json',
120
              data: '{"is_lost": ' + value + '}',
121
              success: function(data) {
122
              }
123
          });
124
      });
125

    
126
    enable_new_appointment(base);
127
    enable_new_event(base);
128

    
129
      $(base).find('.appointment').on('click', function() {
130
          $('.textedit span', this).html('');
131
      });
132

    
133
      $(base).find('.remove-appointment').on('click', function() {
134
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
135
          if (r == true)
136
          {
137
            $.ajax({
138
              url: $(this).data('url'),
139
              type: 'DELETE',
140
              success: function(data) {
141
                  window.location.reload(true);
142
                  return false;
143
              }
144
            });
145
           }
146
        return false;
147
      });
148

    
149
      $(base).find('.edit-appointment').click(function() {
150
        id = $(this).data("event-id");
151
        $.getJSON("/api/v1/eventwithact/" + id + "/?format=json")
152
          .done(function () {
153
            event_dialog("/" + service + "/agenda/" + current_date + "/update-rdv/" + id,
154
              'Modifier rendez-vous', '850px', 'Modifier');
155
          })
156
         .fail(function() {
157
            window.location.reload(true);
158
            $('.messages').html("Le rendez-vous n'existe plus");
159
            return false;
160
         });
161
        return false;
162
      });
163
      $(base).find('.edit-event').click(function() {
164
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
165
          return false;
166
      });
167
      $(base).find('#print-button').click(function() { window.print(); });
168

    
169
      $('.generate-mail-btn', base).click(function() {
170
        var url = '../../dossiers/' + $(this).data('dossier-id') + '/generate?event-id=' + $(this).data('event-id') + '&date=' + $(this).data('date');
171
        $('#ajax-dlg').load(url,
172
          function () {
173
            $(this).dialog({title: 'Générer un courrier', width: '500px',
174
                      buttons: [ { text: "Fermer",
175
                          click: function() { $(this).dialog("close"); } },
176
                      { text: "Générer",
177
                          click: function() { $("#ajax-dlg form").submit(); $(this).dialog("close"); } }]});
178
             $(this).find('.addresses input[type=radio]').change(function() {
179
               var address = '';
180
               if ($(this).data('contact-gender')){address += $(this).data('contact-gender') + ' ';}
181
               if ($(this).data('contact-first-name')){address += $(this).data('contact-first-name') + ' ';}
182
               if ($(this).data('contact-last-name')){address += $(this).data('contact-last-name') + '\n';}
183
               if ($(this).data('address-number')){address += $(this).data('address-number') + ' ';}
184
               if ($(this).data('address-street')){address += $(this).data('address-street') + '\n';}
185
               if ($(this).data('address-address-complement')){address += $(this).data('address-address-complement') + '\n';}
186
               if ($(this).data('address-zip-code')){address += $(this).data('address-zip-code') + ' ';}
187
               if ($(this).data('address-city')){address += $(this).data('address-city') + '\n';}
188
               $('#id_address').val(address);
189
               $('#id_phone_address').val($(this).attr('data-address-phone'));
190
             });
191
             $('.addresses input[type=radio]').first().click();
192
          });
193
        return false;
194
      });
195
      $(base).on('click', '.update-periodic-event', function () {
196
        $('.ui-icon-closethick').click();
197
        // remove the form from previous hidden layer in order to prevent two
198
        // elements with 'id_date' id on the page
199
        $(this).parent().remove();
200

    
201
        var id = $(this).data('id');
202
        var delete_url = $(this).data('delete-url');
203
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
204
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null,
205
          function (dialog) {
206
            $('#ajax-dlg .datepicker-date').datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
207
            var buttons = $(dialog).dialog('option', 'buttons');
208
            buttons.push({
209
              text: "Supprimer",
210
              id: "delete-btn",
211
              click: function () {
212
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer cet évènement récurrent ?");
213
                if (r == true)
214
                {
215
                  $.ajax({
216
                    url: delete_url,
217
                    type: 'DELETE',
218
                    success: function(data) {
219
                        window.location.reload(true);
220
                        return false;
221
                    }
222
                  });
223
                }
224
              }
225
            });
226
            $(dialog).dialog('option', 'buttons', buttons);
227
          }
228
        );
229
      });
230
      $(base).on('click', '.update-periodic-rdv', function () {
231
        $('.ui-icon-closethick').click();
232
        var id = $(this).data('id');
233
        var delete_url = $(this).data('delete-url');
234
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
235
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null,
236
          function (dialog) {
237
            $('#ajax-dlg .datepicker-date').datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
238
            var buttons = $(dialog).dialog('option', 'buttons');
239
            buttons.push({
240
              text: "Supprimer",
241
             id: "delete-btn",
242
              click: function () {
243
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer ce rendez-vous récurrent ?");
244
                if (r == true)
245
                {
246
                  $.ajax({
247
                    url: delete_url,
248
                    type: 'DELETE',
249
                    success: function(data) {
250
                        window.location.reload(true);
251
                        return false;
252
                    }
253
                  });
254
                }
255
              }
256
            });
257
            $(dialog).dialog('option', 'buttons', buttons);
258
          }
259
        );
260
      });
261
}
262

    
263
function toggle_ressource(ressource) {
264

    
265
    var ressource_id = $(ressource).attr('id');
266

    
267
    var ressource_target = $(ressource).data('target');
268
     if (!ressource_target) {
269
        return;
270
    }
271

    
272
    $(ressource).toggleClass('active');
273
    if (!($.cookie('agenda-tabs'))) {
274
        $.cookie('agenda-tabs', new Array(), { path: COOKIE_PATH });
275
    }
276
    if ($(ressource).hasClass('active')) {
277
        var tabs = $.cookie('agenda-tabs');
278
        if ($.inArray(ressource_id, tabs) == -1)
279
        {
280
            tabs.push(ressource_id);
281
            $.cookie('agenda-tabs', tabs, { path: COOKIE_PATH });
282
        }
283
    }
284
    else {
285
        var agendatabs = $.cookie('agenda-tabs');
286
        if ($('#users li.item.ressource.active:last').attr('id'))
287
            $.cookie('last-ressource', $('#users li.item.ressource.active:last').attr('id').split('-')[1], { path: COOKIE_PATH });
288
        else
289
            $.cookie('last-ressource', '', {path: COOKIE_PATH});
290

    
291
        $.each(agendatabs, function (i, value) {
292
            if (value == ressource_id) {
293
                agendatabs.splice(i, 1);
294
            }
295
        });
296
        $.cookie('agenda-tabs', agendatabs, { path: COOKIE_PATH });
297
    }
298
    $(ressource_target).toggle();
299
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
300
    if (! $('#users li.active').length) {
301
        $('#agendas #tabs div').hide();
302
    }
303

    
304
    var tab = $(ressource_target);
305
    var tab_list = $(tab).parent().get(0);
306
    $(tab).detach().appendTo(tab_list);
307

    
308
    var url = $("#date-selector").data('url');
309
    var tab_selector = '#' + ressource_id + '.active';
310

    
311
    if ($(tab_selector).length) {
312
        /* load disponibility column */
313
        $.ajaxSetup({async:false});
314
        $.get(url + 'disponibility/' + ressource_id,
315
            function(data) {
316
                if ($(tab_selector).hasClass('active')) {
317
                    var availability_block = $('ul#availability');
318
                    availability_block.append($(data));
319
                }
320
            }
321
        );
322
       $.ajaxSetup({async:true});
323
    } else {
324
        // remove hidden ressource availability
325
        $('ul#availability li.' + ressource_id).remove();
326
    }
327
    return $(ressource_target).find('a.tab');
328
}
329

    
330
function event_dialog(url, title, width, btn_text) {
331
    add_dialog('#ajax-dlg', url, title, width, btn_text);
332
}
333

    
334
(function($) {
335
  $(function() {
336
      $('#tabs').tabs({
337
          load: function(event, ui) {
338
              var tab = $(ui.tab).attr('id').split('-');
339
              if(tab[0] == 'ressource')
340
                  $.cookie('last-ressource', tab[1], { path: COOKIE_PATH });
341

    
342
              $('#tabs > div > div').accordion({active: false,
343
                                                autoHeight: false,
344
                                                collapsible: true});
345
              enable_events('#tabs');
346
          },
347
          selected: -1,
348
          collapsible: true,
349
      });
350

    
351
      enable_new_event();
352
      enable_new_appointment();
353

    
354
      if ($('#users .item').length) {
355
          $('#users .item').on('click', function() {
356
              var target = toggle_ressource(this);
357

    
358
              if ($(target).is(':visible')) {
359
                  $(target).click();
360
              }
361
              if ($('#filtre input').val()) {
362
                  $('#filtre input').val('');
363
                  $('#filtre input').keyup();
364
                  $('#filtre input').focus();
365
              }
366
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
367
                $('li.agenda:visible:last a.tab').click();
368
              }
369
          });
370

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

    
375
          if ($.cookie('agenda-tabs')) {
376
              $.each($.cookie('agenda-tabs'), function (i, selector) {
377
                  toggle_ressource($('#' + selector));
378
              });
379

    
380
              if ($.cookie('active-agenda'))
381
              {
382
                  var target = $("#" + $.cookie('active-agenda')).data('target');
383
                  if (!$('#tabs ' + target).hasClass('ui-state-active')) {
384
                      $("#tabs " + target + ' a.tab').click();
385
                  }
386
              }
387
          }
388
      }
389

    
390
      $('a.close-tab').click(function() {
391
          var target = '#' + $(this).data('target');
392
          $(target).click();
393
          if ($.cookie('active-agenda') == $(target).attr('id')) {
394
              $.cookie('active-agenda', '', { path: COOKIE_PATH });
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
                  $('.item.worker:not(.in_service)').hide();
420
                  $('.item.worker: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': 'worker',
449
            'ressources': 'ressource'},
450
         function(key, value) {
451
             $('#close-all-agendas').click(function() {
452
                 $.cookie('active-agenda', '', {path: COOKIE_PATH});
453
                 $('#users .item.active').each(function (i, v) {
454
                     toggle_ressource(v, value);
455
                 });
456
             });
457
         });
458
  });
459
})(window.jQuery)
(2-2/21)