Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ 74d4a9e2

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_initial_fields(button, base) {
17
    var participants = new Array();
18
    var ressource = null;
19
    if ($.cookie('active-agenda')) {
20
        var active_agenda = $.cookie('active-agenda').split('-');
21
        if (active_agenda[0] == 'ressource') {
22
            ressource = active_agenda[1];
23
        } else {
24
            ressource = $.cookie('last-ressource');
25
        }
26
    }
27

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

    
44
function enable_new_appointment(base) {
45
    var base = base || 'body';
46
    $(base).find('.newrdv').click(function() {
47
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouveau rendez-vous', '880px', 'Ajouter');
48
    });
49
}
50

    
51
function enable_new_event(base) {
52
    var base = base || 'body';
53
    $(base).find('.newevent').click(function() {
54
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouvel événement', '850px', 'Ajouter');
55
    });
56
}
57

    
58
function enable_events(base) {
59
      $(base).find('.textedit').on('keydown', function() {
60
          $('button', this).removeAttr("disabled");
61
      });
62
      $(base).find('.textedit button').on('click', function() {
63
          var textarea = $(this).prev();
64
          var span = textarea.prev();
65
          var btn = $(this);
66
          var comment = {description: textarea.val()};
67
          var data = JSON.stringify(comment);
68
          if ($(this).data('act-id'))
69
          {
70

    
71
            $.ajax({
72
              url: '/api/v1/act/' + $(this).data("act-id") + '/?format=json&date=' + $(this).data('date'),
73
              type: 'PATCH',
74
              contentType: 'application/json',
75
              data: data,
76
              success: function(data) {
77
                btn.attr('disabled', 'disabled');
78
                if (comment['description']) {
79
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeIn();
80
                }
81
                else {
82
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeOut();
83
                }
84
                span.html('Commentaire modifié avec succès');
85
              }
86
            });
87
          }
88
          else
89
          {
90
            $.ajax({
91
              url: '/api/v1/event/' + $(this).data("event-id") + '/?format=json&date=' + $(this).data('date'),
92
              type: 'PATCH',
93
              contentType: 'application/json',
94
              data: data,
95
              success: function(response) {
96
                btn.attr('disabled', 'disabled');
97
                if (comment['description'])
98
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeIn();
99
                else
100
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeOut();
101
                span.html('Commentaire modifié avec succès');
102
              }
103
            });
104
          }
105
      });
106
      /* TODO: put this in a generic function */
107
      $('.input_is_billable').click(function() {
108
          if ($(this).data("switch-billable") == "True") {
109
              var value = "false";
110
          } else {
111
              var value = "true";
112
          }
113
          $.ajax({
114
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
115
              type: 'PATCH',
116
              contentType: 'application/json',
117
              data: '{"switch_billable": ' + value + '}',
118
              success: function(data) {
119
              }
120
          });
121
      });
122
      $('.input_is_lost').click(function() {
123
          if ((this.checked) == true) {
124
              var value = "true";
125
          } else {
126
              var value = "false";
127
          }
128
          $.ajax({
129
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
130
              type: 'PATCH',
131
              contentType: 'application/json',
132
              data: '{"is_lost": ' + value + '}',
133
              success: function(data) {
134
              }
135
          });
136
      });
137

    
138
    enable_new_appointment(base);
139
    enable_new_event(base);
140

    
141
      $(base).find('.appointment').on('click', function() {
142
          $('.textedit span', this).html('');
143
      });
144

    
145
      $(base).find('.remove-appointment').on('click', function() {
146
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
147
          if (r == true)
148
          {
149
            $.ajax({
150
              url: $(this).data('url'),
151
              type: 'DELETE',
152
              success: function(data) {
153
                  window.location.reload(true);
154
                  return false;
155
              }
156
            });
157
           }
158
        return false;
159
      });
160

    
161
      $(base).find('.edit-appointment').click(function() {
162
        id = $(this).data("event-id");
163
        $.getJSON("/api/v1/eventwithact/" + id + "/?format=json")
164
          .done(function () {
165
            event_dialog("/" + service + "/agenda/" + current_date + "/update-rdv/" + id,
166
              'Modifier rendez-vous', '850px', 'Modifier');
167
          })
168
         .fail(function() {
169
            window.location.reload(true);
170
            $('.messages').html("Le rendez-vous n'existe plus");
171
            return false;
172
         });
173
        return false;
174
      });
175
      $(base).find('.edit-event').click(function() {
176
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
177
          return false;
178
      });
179

    
180
      $('.generate-mail-btn', base).click(function() {
181
        var url = '../../dossiers/' + $(this).data('dossier-id') + '/generate?event-id=' + $(this).data('event-id') + '&date=' + $(this).data('date');
182
        $('#ajax-dlg').load(url,
183
          function () {
184
            $(this).dialog({title: 'Générer un courrier', width: '500px',
185
                      buttons: [ { text: "Fermer",
186
                          click: function() { $(this).dialog("close"); } },
187
                      { text: "Générer",
188
                          click: function() { $("#ajax-dlg form").submit(); $(this).dialog("close"); } }]});
189
             $(this).find('.addresses input[type=radio]').change(function() {
190
               var address = '';
191
               if ($(this).data('contact-gender')){address += $(this).data('contact-gender') + ' ';}
192
               if ($(this).data('contact-first-name')){address += $(this).data('contact-first-name') + ' ';}
193
               if ($(this).data('contact-last-name')){address += $(this).data('contact-last-name') + '\n';}
194
               if ($(this).data('address-recipient')){address += $(this).data('address-recipient') + '\n';}
195
               if ($(this).data('address-number')){address += $(this).data('address-number') + ' ';}
196
               if ($(this).data('address-street')){address += $(this).data('address-street') + '\n';}
197
               if ($(this).data('address-address-complement')){address += $(this).data('address-address-complement') + '\n';}
198
               if ($(this).data('address-zip-code')){address += $(this).data('address-zip-code') + ' ';}
199
               if ($(this).data('address-city')){address += $(this).data('address-city') + '\n';}
200
               $('#id_address').val(address);
201
               $('#id_phone_address').val($(this).attr('data-address-phone'));
202
             });
203
             $('.addresses input[type=radio]').first().click();
204
          });
205
        return false;
206
      });
207
}
208

    
209
function toggle_ressource(ressource) {
210

    
211
    var ressource_id = $(ressource).attr('id');
212

    
213
    var ressource_target = $(ressource).data('target');
214
     if (!ressource_target) {
215
        return;
216
    }
217

    
218
    $(ressource).toggleClass('active');
219
    if (!($.cookie('agenda-tabs'))) {
220
        $.cookie('agenda-tabs', new Array(), { path: COOKIE_PATH });
221
    }
222
    if ($(ressource).hasClass('active')) {
223
        var tabs = $.cookie('agenda-tabs');
224
        if ($.inArray(ressource_id, tabs) == -1)
225
        {
226
            tabs.push(ressource_id);
227
            $.cookie('agenda-tabs', tabs, { path: COOKIE_PATH });
228
        }
229
    }
230
    else {
231
        var agendatabs = $.cookie('agenda-tabs');
232
        if ($('#users li.item.ressource.active:last').attr('id'))
233
            $.cookie('last-ressource', $('#users li.item.ressource.active:last').attr('id').split('-')[1], { path: COOKIE_PATH });
234
        else
235
            $.cookie('last-ressource', '', {path: COOKIE_PATH});
236

    
237
        $.each(agendatabs, function (i, value) {
238
            if (value == ressource_id) {
239
                agendatabs.splice(i, 1);
240
            }
241
        });
242
        $.cookie('agenda-tabs', agendatabs, { path: COOKIE_PATH });
243
    }
244
    $(ressource_target).toggle();
245
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
246
    if (! $('#users li.active').length) {
247
        $('#agendas #tabs div').hide();
248
    }
249

    
250
    var tab = $(ressource_target);
251
    var tab_list = $(tab).parent().get(0);
252
    $(tab).detach().appendTo(tab_list);
253

    
254
    var url = $("#date-selector").data('url');
255
    var tab_selector = '#' + ressource_id + '.active';
256

    
257
    if ($(tab_selector).length) {
258
        /* load disponibility column */
259
        $.ajaxSetup({async:false});
260
        $.get(url + 'disponibility/' + ressource_id,
261
            function(data) {
262
                if ($(tab_selector).hasClass('active')) {
263
                    var availability_block = $('ul#availability');
264
                    availability_block.append($(data));
265
                }
266
            }
267
        );
268
       $.ajaxSetup({async:true});
269
    } else {
270
        // remove hidden ressource availability
271
        $('ul#availability li.' + ressource_id).remove();
272
    }
273
    return $(ressource_target).find('a.tab');
274
}
275

    
276
function init_datepickers(dialog) {
277
    $('.datepicker-date', dialog).datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
278
    $('.datepicker input', dialog).datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
279
}
280

    
281
function event_dialog(url, title, width, btn_text) {
282
    function add_periodic_events(base) {
283
      init_datepickers(base);
284
      $(base).on('click', '.update-periodic-event', function () {
285
        $('.ui-icon-closethick').click();
286
        // remove the form from previous hidden layer in order to prevent two
287
        // elements with 'id_date' id on the page
288
        $(this).parent().remove();
289

    
290
        var id = $(this).data('id');
291
        var delete_url = $(this).data('delete-url');
292
        var delete_button = {
293
            text: "Supprimer",
294
            id: "delete-btn",
295
            click: function () {
296
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer cet évènement récurrent ?");
297
                if (r == true)
298
                {
299
                  $.ajax({
300
                    url: delete_url,
301
                    type: 'DELETE',
302
                    success: function(data) {
303
                        window.location.reload(true);
304
                        return false;
305
                    }
306
                  });
307
                }
308
              }
309
            };
310
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
311
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
312
      });
313
      $(base).on('click', '.update-periodic-rdv', function () {
314
        $('.ui-icon-closethick').click();
315
        var id = $(this).data('id');
316
        var delete_url = $(this).data('delete-url');
317
        var delete_button = {
318
            text: "Supprimer",
319
            id: "delete-btn",
320
            click: function () {
321
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer ce rendez-vous récurrent ?");
322
                if (r == true)
323
                {
324
                  $.ajax({
325
                    url: delete_url,
326
                    type: 'DELETE',
327
                    success: function(data) {
328
                        window.location.reload(true);
329
                        return false;
330
                    }
331
                  });
332
                }
333
            }
334
        };
335
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
336
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
337
      });
338
    }
339

    
340
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
341
          add_periodic_events);
342

    
343
}
344

    
345
(function($) {
346
  $(function() {
347
      $('#tabs').tabs({
348
          load: function(event, ui) {
349
              var tab = $(ui.tab).attr('id').split('-');
350
              if(tab[0] == 'ressource')
351
                  $.cookie('last-ressource', tab[1], { path: COOKIE_PATH });
352

    
353
              $('#tabs > div > div').accordion({active: false,
354
                                                autoHeight: false,
355
                                                collapsible: true});
356
              enable_events('#tabs');
357
          },
358
          selected: -1,
359
          collapsible: true,
360
      });
361

    
362
      $('button#print-button').click(function() { window.print();});
363

    
364
      enable_new_event();
365
      enable_new_appointment();
366

    
367
      if ($('#users .item').length) {
368
          $('#users .item').on('click', function() {
369
              var target = toggle_ressource(this);
370

    
371
              if ($(target).is(':visible')) {
372
                  $(target).click();
373
              }
374
              if ($('#filtre input').val()) {
375
                  $('#filtre input').val('');
376
                  $('#filtre input').keyup();
377
                  $('#filtre input').focus();
378
              }
379
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
380
                $('li.agenda:visible:last a.tab').click();
381
              }
382
          });
383

    
384
          $('a.tab').click(function() {
385
              $.cookie('active-agenda', $(this).attr('id'), { path: COOKIE_PATH });
386
          });
387

    
388
          if ($.cookie('agenda-tabs')) {
389
              $.each($.cookie('agenda-tabs'), function (i, selector) {
390
                  toggle_ressource($('#' + selector));
391
              });
392

    
393
              if ($.cookie('active-agenda'))
394
              {
395
                  var target = $("#" + $.cookie('active-agenda')).data('target');
396
                  if (!$('#tabs ' + target).hasClass('ui-state-active')) {
397
                      $("#tabs " + target + ' a.tab').click();
398
                  }
399
              }
400
          }
401
      }
402

    
403
      $('a.close-tab').click(function() {
404
          var target = '#' + $(this).data('target');
405
          $(target).click();
406
          if ($.cookie('active-agenda') == $(target).attr('id')) {
407
              $.cookie('active-agenda', '', { path: COOKIE_PATH });
408
          }
409

    
410
      });
411

    
412
      /* Gestion du filtre sur les utilisateurs */
413
      $('#filtre input').keyup(function() {
414
          var filtre = $(this).val();
415
          if ($('#show-everybody').length) {
416
              var everybody = $('#show-everybody').is(':checked');
417
          } else {
418
              var everybody = true;
419
          }
420
          if (filtre) {
421
              $('#show-everybody').attr('checked', true);
422
              $('#users li').each(function() {
423
                  if ($(this).text().match(new RegExp(filtre, "i"))) {
424
                      $(this).show();
425
                  } else {
426
                      $(this).hide();
427
                  }
428
              });
429
          } else {
430
              $('#users li').show();
431
              if (! everybody) {
432
                  $('.item.worker:not(.in_service)').hide();
433
                  $('.item.worker:not(.intervenant)').hide();
434
              }
435
          }
436
          /* hide worker type titles that do not have a single visible person */
437
          $("#users ul:has(*):has(:visible)").parent().prev().show();
438
          $("#users ul:has(*):not(:has(:visible))").parent().prev().hide();
439
      });
440

    
441
      $('.date').datepicker({showOn: 'button'});
442
      $('#add-intervenant-btn').click(function() {
443
          var text = $(this).prev().val();
444
          $('#intervenants ul').append('<li><input type="checkbox" value="' + text + '" checked="checked">' + text + '</input></li>');
445
          $(this).prev().val('').focus();
446
          return false;
447
      });
448
      $('#show-everybody').change(function() {
449
      if (! $(this).is(':checked')) {
450
        $('#filtre input').val('');
451
      }
452
      $('#filtre input').keyup();
453
      return;
454
    });
455
    $('select[name^="act_state"]').on('change', function () {
456
    $(this).next('button').prop('disabled',
457
      ($(this).data('previous') == $(this).val()));
458
    })
459
    $('#filtre input').keyup();
460

    
461
    $.each({'persons': 'worker',
462
            'ressources': 'ressource'},
463
         function(key, value) {
464
             $('#close-all-agendas').click(function() {
465
                 $.cookie('active-agenda', '', {path: COOKIE_PATH});
466
                 $('#users .item.active').each(function (i, v) {
467
                     toggle_ressource(v, value);
468
                 });
469
             });
470
         });
471
  });
472
})(window.jQuery)
(2-2/21)