Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ 54318d2c

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 get_initial_fields(button, base) {
8
    var participants = new Array();
9
    var ressource = null;
10
    if ($.cookie('active-agenda')) {
11
        var active_agenda = $.cookie('active-agenda').split('-');
12
        if (active_agenda[0] == 'ressource') {
13
            ressource = active_agenda[1];
14
        } else {
15
            ressource = $.cookie('last-ressource');
16
        }
17
    }
18

    
19
    if ($.cookie('agenda-tabs')) {
20
        participants = $.cookie('agenda-tabs').filter(function(v) {
21
            var data = v.split('-');
22
            if(data[0]=='worker')
23
                return true;
24
        });
25
        participants = participants.map(function(v) {
26
            var data = v.split('-'); return data[1]
27
        });
28
    }
29
    return $.param({participants: $.makeArray(participants),
30
                    ressource: ressource,
31
                    time: $(button).data('hour'),
32
                    duration: $(button).data('duration')}, true);
33
}
34

    
35
function enable_new_appointment(base) {
36
    var base = base || 'body';
37
    $(base).find('.newrdv').click(function() {
38
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouveau rendez-vous', '880px', 'Ajouter');
39
    });
40
}
41

    
42
function enable_new_event(base) {
43
    var base = base || 'body';
44
    $(base).find('.newevent').click(function() {
45
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouvel événement', '850px', 'Ajouter');
46
    });
47
}
48

    
49
function enable_events(base) {
50
      $(base).find('.textedit').on('keydown', function() {
51
          $('button', this).removeAttr("disabled");
52
      });
53
      $(base).find('.textedit button').on('click', function() {
54
          var textarea = $(this).prev();
55
          var span = textarea.prev();
56
          var btn = $(this);
57
          var comment = {description: textarea.val()};
58
          var data = JSON.stringify(comment);
59
            $.ajax({
60
              url: '/api/v1/event/' + $(this).data("event-id") + '/?format=json&date=' + $(this).data('date'),
61
              type: 'PATCH',
62
              contentType: 'application/json',
63
              data: data,
64
              success: function(response) {
65
                btn.attr('disabled', 'disabled');
66
                if (comment['description'])
67
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeIn();
68
                else
69
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeOut();
70
                span.html('Commentaire modifié avec succès');
71
              }
72
            });
73
      });
74
      /* TODO: put this in a generic function */
75
      $('.input_is_billable').click(function() {
76
          if ($(this).data("switch-billable") == "True") {
77
              var value = "false";
78
          } else {
79
              var value = "true";
80
          }
81
          $.ajax({
82
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
83
              type: 'PATCH',
84
              contentType: 'application/json',
85
              data: '{"switch_billable": ' + value + '}',
86
              success: function(data) {
87
              }
88
          });
89
      });
90
      $('.input_is_lost').click(function() {
91
          if ((this.checked) == true) {
92
              var value = "true";
93
          } else {
94
              var value = "false";
95
          }
96
          $.ajax({
97
              url: '/api/v1/act/' + $(this).data("id") + '/?format=json',
98
              type: 'PATCH',
99
              contentType: 'application/json',
100
              data: '{"is_lost": ' + value + '}',
101
              success: function(data) {
102
              }
103
          });
104
      });
105

    
106
    enable_new_appointment(base);
107
    enable_new_event(base);
108

    
109
      $(base).find('.appointment').on('click', function() {
110
          $('.textedit span', this).html('');
111
      });
112

    
113
      $(base).find('.remove-appointment').on('click', function() {
114
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
115
          if (r == true)
116
          {
117
            $.ajax({
118
              url: $(this).data('url'),
119
              type: 'DELETE',
120
              success: function(data) {
121
                  window.location.reload(true);
122
                  return false;
123
              }
124
            });
125
           }
126
        return false;
127
      });
128

    
129
      $(base).find('.edit-appointment').click(function() {
130
        id = $(this).data("event-id");
131
        $.getJSON("/api/v1/eventwithact/" + id + "/?format=json")
132
          .done(function () {
133
            event_dialog("/" + service + "/agenda/" + current_date + "/update-rdv/" + id,
134
              'Modifier rendez-vous', '850px', 'Modifier');
135
          })
136
         .fail(function() {
137
            window.location.reload(true);
138
            $('.messages').html("Le rendez-vous n'existe plus");
139
            return false;
140
         });
141
        return false;
142
      });
143
      $(base).find('.edit-event').click(function() {
144
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
145
          return false;
146
      });
147

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

    
177
function toggle_ressource(ressource) {
178

    
179
    var ressource_id = $(ressource).attr('id');
180

    
181
    var ressource_target = $(ressource).data('target');
182
     if (!ressource_target) {
183
        return;
184
    }
185

    
186
    $(ressource).toggleClass('active');
187
    if (!($.cookie('agenda-tabs'))) {
188
        $.cookie('agenda-tabs', new Array(), { path: COOKIE_PATH });
189
    }
190
    if ($(ressource).hasClass('active')) {
191
        var tabs = $.cookie('agenda-tabs');
192
        if ($.inArray(ressource_id, tabs) == -1)
193
        {
194
            tabs.push(ressource_id);
195
            $.cookie('agenda-tabs', tabs, { path: COOKIE_PATH });
196
        }
197
    }
198
    else {
199
        var agendatabs = $.cookie('agenda-tabs');
200
        if ($('#users li.item.ressource.active:last').attr('id'))
201
            $.cookie('last-ressource', $('#users li.item.ressource.active:last').attr('id').split('-')[1], { path: COOKIE_PATH });
202
        else
203
            $.cookie('last-ressource', '', {path: COOKIE_PATH});
204

    
205
        $.each(agendatabs, function (i, value) {
206
            if (value == ressource_id) {
207
                agendatabs.splice(i, 1);
208
            }
209
        });
210
        $.cookie('agenda-tabs', agendatabs, { path: COOKIE_PATH });
211
    }
212
    $(ressource_target).toggle();
213
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
214
    if (! $('#users li.active').length) {
215
        $('#agendas #tabs div').hide();
216
    }
217

    
218
    var tab = $(ressource_target);
219
    var tab_list = $(tab).parent().get(0);
220
    $(tab).detach().appendTo(tab_list);
221

    
222
    var url = $("#date-selector").data('url');
223
    var tab_selector = '#' + ressource_id + '.active';
224

    
225
    if ($(tab_selector).length) {
226
        /* load disponibility column */
227
        $.ajaxSetup({async:false});
228
        $.get(url + 'disponibility/' + ressource_id,
229
            function(data) {
230
                if ($(tab_selector).hasClass('active')) {
231
                    var availability_block = $('ul#availability');
232
                    availability_block.append($(data));
233
                }
234
            }
235
        );
236
       $.ajaxSetup({async:true});
237
    } else {
238
        // remove hidden ressource availability
239
        $('ul#availability li.' + ressource_id).remove();
240
    }
241
    return $(ressource_target).find('a.tab');
242
}
243

    
244
function event_dialog(url, title, width, btn_text) {
245
    function add_periodic_events(base) {
246
      init_datepickers(base);
247
      $(base).on('click', '.update-periodic-event', function () {
248
        $('.ui-icon-closethick').click();
249
        // remove the form from previous hidden layer in order to prevent two
250
        // elements with 'id_date' id on the page
251
        $(this).parent().remove();
252

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

    
307
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
308
          add_periodic_events);
309

    
310
}
311

    
312
(function($) {
313
  $(function() {
314
      $('#tabs').tabs({
315
          load: function(event, ui) {
316
              var tab = $(ui.tab).attr('id').split('-');
317
              if(tab[0] == 'ressource')
318
                  $.cookie('last-ressource', tab[1], { path: COOKIE_PATH });
319

    
320
              $('#tabs > div > div').accordion({active: false,
321
                                                autoHeight: false,
322
                                                collapsible: true});
323
              enable_events('#tabs');
324
          },
325
          selected: -1,
326
          collapsible: true,
327
      });
328

    
329
      $('button#print-button').click(function() { window.print();});
330

    
331
      enable_new_event();
332
      enable_new_appointment();
333

    
334
      if ($('#users .item').length) {
335
          $('#users .item').on('click', function() {
336
              var target = toggle_ressource(this);
337

    
338
              if ($(target).is(':visible')) {
339
                  $(target).click();
340
              }
341
              if ($('#filtre input').val()) {
342
                  $('#filtre input').val('');
343
                  $('#filtre input').keyup();
344
                  $('#filtre input').focus();
345
              }
346
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
347
                $('li.agenda:visible:last a.tab').click();
348
              }
349
          });
350

    
351
          $('a.tab').click(function() {
352
              $.cookie('active-agenda', $(this).attr('id'), { path: COOKIE_PATH });
353
          });
354

    
355
          if ($.cookie('agenda-tabs')) {
356
              $.each($.cookie('agenda-tabs'), function (i, selector) {
357
                  toggle_ressource($('#' + selector));
358
              });
359

    
360
              if ($.cookie('active-agenda'))
361
              {
362
                  var target = $("#" + $.cookie('active-agenda')).data('target');
363
                  if (!$('#tabs ' + target).hasClass('ui-state-active')) {
364
                      $("#tabs " + target + ' a.tab').click();
365
                  }
366
              }
367
          }
368
      }
369

    
370
      $('a.close-tab').click(function() {
371
          var target = '#' + $(this).data('target');
372
          $(target).click();
373
          if ($.cookie('active-agenda') == $(target).attr('id')) {
374
              $.cookie('active-agenda', '', { path: COOKIE_PATH });
375
          }
376

    
377
      });
378

    
379
      /* Gestion du filtre sur les utilisateurs */
380
      $('#filtre input').keyup(function() {
381
          var filtre = $(this).val();
382
          if ($('#show-everybody').length) {
383
              var everybody = $('#show-everybody').is(':checked');
384
          } else {
385
              var everybody = true;
386
          }
387
          if (filtre) {
388
              $('#show-everybody').attr('checked', true);
389
              $('#users li').each(function() {
390
                  if ($(this).text().match(new RegExp(filtre, "i"))) {
391
                      $(this).show();
392
                  } else {
393
                      $(this).hide();
394
                  }
395
              });
396
          } else {
397
              $('#users li').show();
398
              if (! everybody) {
399
                  $('.item.worker:not(.in_service)').hide();
400
                  $('.item.worker:not(.intervenant)').hide();
401
              }
402
          }
403
          /* hide worker type titles that do not have a single visible person */
404
          $("#users ul:has(*):has(:visible)").parent().prev().show();
405
          $("#users ul:has(*):not(:has(:visible))").parent().prev().hide();
406
      });
407

    
408
      $('.date').datepicker({showOn: 'button'});
409
      $('#add-intervenant-btn').click(function() {
410
          var text = $(this).prev().val();
411
          $('#intervenants ul').append('<li><input type="checkbox" value="' + text + '" checked="checked">' + text + '</input></li>');
412
          $(this).prev().val('').focus();
413
          return false;
414
      });
415
      $('#show-everybody').change(function() {
416
      if (! $(this).is(':checked')) {
417
        $('#filtre input').val('');
418
      }
419
      $('#filtre input').keyup();
420
      return;
421
    });
422
    $('select[name^="act_state"]').on('change', function () {
423
    $(this).next('button').prop('disabled',
424
      ($(this).data('previous') == $(this).val()));
425
    })
426
    $('#filtre input').keyup();
427

    
428
    $.each({'persons': 'worker',
429
            'ressources': 'ressource'},
430
         function(key, value) {
431
             $('#close-all-agendas').click(function() {
432
                 $.cookie('active-agenda', '', {path: COOKIE_PATH});
433
                 $('#users .item.active').each(function (i, v) {
434
                     toggle_ressource(v, value);
435
                 });
436
             });
437
         });
438
  });
439
})(window.jQuery)
(2-2/22)