Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ b8c44af8

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

    
115
    enable_new_appointment(base);
116
    enable_new_event(base);
117

    
118
      $(base).find('.appointment').on('click', function() {
119
          $('.textedit span', this).html('');
120
      });
121

    
122
      $(base).find('.remove-appointment').on('click', function() {
123
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
124
          if (r == true)
125
          {
126
            $.ajax({
127
              url: $(this).data('url'),
128
              type: 'DELETE',
129
              success: function(data) {
130
                  window.location.reload(true);
131
                  return false;
132
              }
133
            });
134
           }
135
        return false;
136
      });
137

    
138
      $(base).find('.edit-appointment').click(function() {
139
        id = $(this).data("event-id");
140
        $.getJSON("/api/v1/eventwithact/" + id + "/?format=json")
141
          .done(function () {
142
            event_dialog("/" + service + "/agenda/" + current_date + "/update-rdv/" + id,
143
              'Modifier rendez-vous', '850px', 'Modifier');
144
          })
145
         .fail(function() {
146
            window.location.reload(true);
147
            $('.messages').html("Le rendez-vous n'existe plus");
148
            return false;
149
         });
150
        return false;
151
      });
152
      $(base).find('.edit-event').click(function() {
153
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
154
          return false;
155
      });
156

    
157
      $('.generate-mail-btn', base).click(function() {
158
        var url = '../../dossiers/' + $(this).data('dossier-id') + '/generate?event-id=' + $(this).data('event-id') + '&date=' + $(this).data('date');
159
        $('#ajax-dlg').load(url,
160
          function () {
161
            $(this).dialog({title: 'Générer un courrier', width: '500px',
162
                      buttons: [ { text: "Fermer",
163
                          click: function() { $(this).dialog("close"); } },
164
                      { text: "Générer",
165
                          click: function() { $("#ajax-dlg form").submit(); $(this).dialog("close"); } }]});
166
             $(this).find('.addresses input[type=radio]').change(function() {
167
               var address = '';
168
               if ($(this).data('contact-gender')){address += $(this).data('contact-gender') + ' ';}
169
               if ($(this).data('contact-first-name')){address += $(this).data('contact-first-name') + ' ';}
170
               if ($(this).data('contact-last-name')){address += $(this).data('contact-last-name') + '\n';}
171
               if ($(this).data('address-recipient')){address += $(this).data('address-recipient') + '\n';}
172
               if ($(this).data('address-number')){address += $(this).data('address-number') + ' ';}
173
               if ($(this).data('address-street')){address += $(this).data('address-street') + '\n';}
174
               if ($(this).data('address-address-complement')){address += $(this).data('address-address-complement') + '\n';}
175
               if ($(this).data('address-zip-code')){address += $(this).data('address-zip-code') + ' ';}
176
               if ($(this).data('address-city')){address += $(this).data('address-city') + '\n';}
177
               $('#id_address').val(address);
178
               $('#id_phone_address').val($(this).attr('data-address-phone'));
179
             });
180
             $('.addresses input[type=radio]').first().click();
181
          });
182
        return false;
183
      });
184
}
185

    
186
function toggle_ressource(ressource) {
187

    
188
    var ressource_id = $(ressource).attr('id');
189

    
190
    var ressource_target = $(ressource).data('target');
191
     if (!ressource_target) {
192
        return;
193
    }
194

    
195
    $(ressource).toggleClass('active');
196
    if (!($.cookie('agenda-tabs'))) {
197
        $.cookie('agenda-tabs', new Array(), { path: COOKIE_PATH });
198
    }
199
    if ($(ressource).hasClass('active')) {
200
        var tabs = $.cookie('agenda-tabs');
201
        if ($.inArray(ressource_id, tabs) == -1)
202
        {
203
            tabs.push(ressource_id);
204
            $.cookie('agenda-tabs', tabs, { path: COOKIE_PATH });
205
        }
206
    }
207
    else {
208
        var agendatabs = $.cookie('agenda-tabs');
209
        if ($('#users li.item.ressource.active:last').attr('id'))
210
            $.cookie('last-ressource', $('#users li.item.ressource.active:last').attr('id').split('-')[1], { path: COOKIE_PATH });
211
        else
212
            $.cookie('last-ressource', '', {path: COOKIE_PATH});
213

    
214
        $.each(agendatabs, function (i, value) {
215
            if (value == ressource_id) {
216
                agendatabs.splice(i, 1);
217
            }
218
        });
219
        $.cookie('agenda-tabs', agendatabs, { path: COOKIE_PATH });
220
    }
221
    $(ressource_target).toggle();
222
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
223
    if (! $('#users li.active').length) {
224
        $('#agendas #tabs div').hide();
225
    }
226

    
227
    var tab = $(ressource_target);
228
    var tab_list = $(tab).parent().get(0);
229
    $(tab).detach().appendTo(tab_list);
230

    
231
    var url = $("#date-selector").data('url');
232
    var tab_selector = '#' + ressource_id + '.active';
233

    
234
    if ($(tab_selector).length) {
235
        /* load disponibility column */
236
        $.ajaxSetup({async:false});
237
        $.get(url + 'disponibility/' + ressource_id,
238
            function(data) {
239
                if ($(tab_selector).hasClass('active')) {
240
                    var availability_block = $('ul#availability');
241
                    availability_block.append($(data));
242
                }
243
            }
244
        );
245
       $.ajaxSetup({async:true});
246
    } else {
247
        // remove hidden ressource availability
248
        $('ul#availability li.' + ressource_id).remove();
249
    }
250
    return $(ressource_target).find('a.tab');
251
}
252

    
253
function init_datepickers(dialog) {
254
    $('.datepicker-date', dialog).datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
255
    $('.datepicker input', dialog).datepicker({dateFormat: 'd/m/yy', showOn: 'button'});
256
}
257

    
258
function event_dialog(url, title, width, btn_text) {
259
    function add_periodic_events(base) {
260
      init_datepickers(base);
261
      $(base).on('click', '.update-periodic-event', function () {
262
        $('.ui-icon-closethick').click();
263
        // remove the form from previous hidden layer in order to prevent two
264
        // elements with 'id_date' id on the page
265
        $(this).parent().remove();
266

    
267
        var id = $(this).data('id');
268
        var delete_url = $(this).data('delete-url');
269
        var delete_button = {
270
            text: "Supprimer",
271
            id: "delete-btn",
272
            click: function () {
273
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer cet évènement récurrent ?");
274
                if (r == true)
275
                {
276
                  $.ajax({
277
                    url: delete_url,
278
                    type: 'DELETE',
279
                    success: function(data) {
280
                        window.location.reload(true);
281
                        return false;
282
                    }
283
                  });
284
                }
285
              }
286
            };
287
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
288
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
289
      });
290
      $(base).on('click', '.update-periodic-rdv', function () {
291
        $('.ui-icon-closethick').click();
292
        var id = $(this).data('id');
293
        var one_act_already_billed = $(this).data('one_act_already_billed');
294
        var delete_button = null
295
        if (one_act_already_billed == 'False') {
296
            var delete_url = $(this).data('delete-url');
297
            var delete_button = {
298
                text: "Supprimer",
299
                id: "delete-btn",
300
                click: function () {
301
                    var r = delete_prompt("Etes-vous sûr de vouloir supprimer ce rendez-vous récurrent ?");
302
                    if (r == true)
303
                    {
304
                      $.ajax({
305
                        url: delete_url,
306
                        type: 'DELETE',
307
                        success: function(data) {
308
                            window.location.reload(true);
309
                            return false;
310
                        }
311
                      });
312
                    }
313
                }
314
            };
315
        }
316
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
317
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
318
      });
319
    }
320

    
321
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
322
          add_periodic_events);
323

    
324
}
325

    
326
(function($) {
327
  $(function() {
328
      $('#tabs').tabs({
329
          load: function(event, ui) {
330
              var tab = $(ui.tab).attr('id').split('-');
331
              if(tab[0] == 'ressource')
332
                  $.cookie('last-ressource', tab[1], { path: COOKIE_PATH });
333

    
334
              $('#tabs > div > div').accordion({active: false,
335
                                                autoHeight: false,
336
                                                collapsible: true});
337
              enable_events('#tabs');
338
          },
339
          selected: -1,
340
          collapsible: true,
341
      });
342

    
343
      $('button#print-button').click(function() { window.print();});
344

    
345
      enable_new_event();
346
      enable_new_appointment();
347

    
348
      if ($('#users .item').length) {
349
          $('#users .item').on('click', function() {
350
              var target = toggle_ressource(this);
351

    
352
              if ($(target).is(':visible')) {
353
                  $(target).click();
354
              }
355
              if ($('#filtre input').val()) {
356
                  $('#filtre input').val('');
357
                  $('#filtre input').keyup();
358
                  $('#filtre input').focus();
359
              }
360
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
361
                $('li.agenda:visible:last a.tab').click();
362
              }
363
          });
364

    
365
          $('a.tab').click(function() {
366
              $.cookie('active-agenda', $(this).attr('id'), { path: COOKIE_PATH });
367
          });
368

    
369
          if ($.cookie('agenda-tabs')) {
370
              $.each($.cookie('agenda-tabs'), function (i, selector) {
371
                  toggle_ressource($('#' + selector));
372
              });
373

    
374
              if ($.cookie('active-agenda'))
375
              {
376
                  var target = $("#" + $.cookie('active-agenda')).data('target');
377
                  if (!$('#tabs ' + target).hasClass('ui-state-active')) {
378
                      $("#tabs " + target + ' a.tab').click();
379
                  }
380
              }
381
          }
382
      }
383

    
384
      $('a.close-tab').click(function() {
385
          var target = '#' + $(this).data('target');
386
          $(target).click();
387
          if ($.cookie('active-agenda') == $(target).attr('id')) {
388
              $.cookie('active-agenda', '', { path: COOKIE_PATH });
389
          }
390

    
391
      });
392

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

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

    
442
    $.each({'persons': 'worker',
443
            'ressources': 'ressource'},
444
         function(key, value) {
445
             $('#close-all-agendas').click(function() {
446
                 $.cookie('active-agenda', '', {path: COOKIE_PATH});
447
                 $('#users .item.active').each(function (i, v) {
448
                     toggle_ressource(v, value);
449
                 });
450
             });
451
         });
452
  });
453
})(window.jQuery)
(2-2/21)