Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ b057fff0

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
        $(base).unbind('click');
274
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
275
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
276
      });
277

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

    
310
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
311
          add_periodic_events);
312

    
313
}
314

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

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

    
332
      $('button#print-button').click(function() { window.print();});
333

    
334
      enable_new_event();
335
      enable_new_appointment();
336

    
337
      if ($('#users .item').length) {
338
          $('#users .item').on('click', function() {
339
              var target = toggle_ressource(this);
340

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

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

    
358
          if ($.cookie('agenda-tabs')) {
359
              $.each($.cookie('agenda-tabs'), function (i, selector) {
360
                  toggle_ressource($('#' + selector));
361
              });
362

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

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

    
380
      });
381

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

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

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