Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ 7b8d8bd5

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

    
133
    enable_new_appointment(base);
134
    enable_new_event(base);
135

    
136
      $(base).find('.appointment').on('click', function() {
137
          $('.textedit span', this).html('');
138
      });
139

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

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

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

    
204
function toggle_ressource(ressource) {
205

    
206
    var ressource_id = $(ressource).attr('id');
207

    
208
    var ressource_target = $(ressource).data('target');
209
     if (!ressource_target) {
210
        return;
211
    }
212

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

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

    
245
    var tab = $(ressource_target);
246
    var tab_list = $(tab).parent().get(0);
247
    $(tab).detach().appendTo(tab_list);
248

    
249
    var url = $("#date-selector").data('url');
250
    var tab_selector = '#' + ressource_id + '.active';
251

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

    
271
function event_dialog(url, title, width, btn_text) {
272
    function add_periodic_events(base) {
273
      $(base).on('click', '.update-periodic-event', function () {
274
        $('.ui-icon-closethick').click();
275
        // remove the form from previous hidden layer in order to prevent two
276
        // elements with 'id_date' id on the page
277
        $(this).parent().remove();
278

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

    
337
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
338
          add_periodic_events);
339

    
340
}
341

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

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

    
359
      $('button#print-button').click(function() { window.print();});
360

    
361
      enable_new_event();
362
      enable_new_appointment();
363

    
364
      if ($('#users .item').length) {
365
          $('#users .item').on('click', function() {
366
              var target = toggle_ressource(this);
367

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

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

    
385
          if ($.cookie('agenda-tabs')) {
386
              $.each($.cookie('agenda-tabs'), function (i, selector) {
387
                  toggle_ressource($('#' + selector));
388
              });
389

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

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

    
407
      });
408

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

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

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