Projet

Général

Profil

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

calebasse / calebasse / static / js / calebasse.agenda.js @ 3e4631f4

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 delete_url = $(this).data('delete-url');
294
        var delete_button = {
295
            text: "Supprimer",
296
            id: "delete-btn",
297
            click: function () {
298
                var r = delete_prompt("Etes-vous sûr de vouloir supprimer ce rendez-vous récurrent ?");
299
                if (r == true)
300
                {
301
                  $.ajax({
302
                    url: delete_url,
303
                    type: 'DELETE',
304
                    success: function(data) {
305
                        window.location.reload(true);
306
                        return false;
307
                    }
308
                  });
309
                }
310
            }
311
        };
312
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
313
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
314
      });
315
    }
316

    
317
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
318
          add_periodic_events);
319

    
320
}
321

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

    
330
              $('#tabs > div > div').accordion({active: false,
331
                                                autoHeight: false,
332
                                                collapsible: true});
333
              enable_events('#tabs');
334
          },
335
          selected: -1,
336
          collapsible: true,
337
      });
338

    
339
      $('button#print-button').click(function() { window.print();});
340

    
341
      enable_new_event();
342
      enable_new_appointment();
343

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

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

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

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

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

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

    
387
      });
388

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

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

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