Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ c2424b7f

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('active-agenda', '', {path: COOKIE_PATH})
204
            $.cookie('last-ressource', '', {path: COOKIE_PATH});
205
        }
206

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

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

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

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

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

    
255
        var id = $(this).data('id');
256
        var delete_url = $(this).data('delete-url');
257
        var delete_button = {
258
            text: "Supprimer",
259
            id: "delete-btn",
260
            click: function () {
261
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer cet évènement récurrent ?");
262
                if (r == true)
263
                {
264
                  $.ajax({
265
                    url: delete_url,
266
                    type: 'DELETE',
267
                    success: function(data) {
268
                        window.location.reload(true);
269
                        return false;
270
                    }
271
                  });
272
                }
273
              }
274
            };
275
        $(base).unbind('click');
276
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
277
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
278
      });
279

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

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

    
315
}
316

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

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

    
334
      $('button#print-button').click(function() { window.print();});
335

    
336
      $('#actes .frame select').on('change', function() {
337
          $('#actes .frame').removeClass('changed');
338
          if (window.location.hash)
339
              $(window.location.hash).css('box-shadow', 'none');
340
          $(this).closest('.frame').addClass('changed');
341
      });
342

    
343
      enable_new_event();
344
      enable_new_appointment();
345

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

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

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

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

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

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

    
389
      });
390

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

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

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