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 cb2f39f1 Serghei MIHAI
var path = location.pathname.split('/');
2
var service = path[1];
3 ef52dd46 Jérôme Schneider
var app_name = path[2];
4 cb2f39f1 Serghei MIHAI
var current_date = path[3];
5 ff6a41ac Serghei MIHAI
COOKIE_PATH = '/' + service + '/agenda';
6 7d589b3e Serghei MIHAI
7 ad2b7483 Serghei MIHAI
function get_initial_fields(button, base) {
8 1610c193 Serghei MIHAI
    var participants = new Array();
9 b0cd758c Serghei MIHAI
    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 1610c193 Serghei MIHAI
    }
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 dd986559 Serghei MIHAI
                    ressource: ressource,
31 ad2b7483 Serghei MIHAI
                    time: $(button).data('hour'),
32
                    duration: $(button).data('duration')}, true);
33 1610c193 Serghei MIHAI
}
34
35 b2351503 Serghei MIHAI
function enable_new_appointment(base) {
36
    var base = base || 'body';
37
    $(base).find('.newrdv').click(function() {
38 5d2bef30 Serghei MIHAI
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouveau rendez-vous', '880px', 'Ajouter');
39 b2351503 Serghei MIHAI
    });
40
}
41
42
function enable_new_event(base) {
43
    var base = base || 'body';
44
    $(base).find('.newevent').click(function() {
45 5d2bef30 Serghei MIHAI
        add_dialog('#ajax-dlg', $(this).data('url') + "?" + get_initial_fields(this, base), 'Nouvel événement', '850px', 'Ajouter');
46 b2351503 Serghei MIHAI
    });
47
}
48
49 b306fcbb Frédéric Péters
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 3e29d8ec Serghei MIHAI
          var span = textarea.prev();
56
          var btn = $(this);
57 74d4a9e2 Serghei MIHAI
          var comment = {description: textarea.val()};
58 66d9afe6 Serghei MIHAI
          var data = JSON.stringify(comment);
59 72f229a9 Jérôme Schneider
            $.ajax({
60 915ecce9 Benjamin Dauvergne
              url: '/api/v1/event/' + $(this).data("event-id") + '/?format=json&date=' + $(this).data('date'),
61 b306fcbb Frédéric Péters
              type: 'PATCH',
62
              contentType: 'application/json',
63 19eda2d2 Jérôme Schneider
              data: data,
64 7b8d8bd5 Serghei MIHAI
              success: function(response) {
65 72f229a9 Jérôme Schneider
                btn.attr('disabled', 'disabled');
66 74d4a9e2 Serghei MIHAI
                if (comment['description'])
67 7b8d8bd5 Serghei MIHAI
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeIn();
68
                else
69
                    $('h3#' + btn.data("event-id") + ' span.icon-comment').fadeOut();
70 72f229a9 Jérôme Schneider
                span.html('Commentaire modifié avec succès');
71 b306fcbb Frédéric Péters
              }
72 72f229a9 Jérôme Schneider
            });
73 b306fcbb Frédéric Péters
      });
74 9123ecac Jérôme Schneider
      /* TODO: put this in a generic function */
75
      $('.input_is_billable').click(function() {
76 90e0b55f Jérôme Schneider
          if ($(this).data("switch-billable") == "True") {
77 9123ecac Jérôme Schneider
              var value = "false";
78 90e0b55f Jérôme Schneider
          } else {
79
              var value = "true";
80 9123ecac Jérôme Schneider
          }
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 b2351503 Serghei MIHAI
    enable_new_appointment(base);
107
    enable_new_event(base);
108
109 b306fcbb Frédéric Péters
      $(base).find('.appointment').on('click', function() {
110
          $('.textedit span', this).html('');
111
      });
112
113
      $(base).find('.remove-appointment').on('click', function() {
114 b5f052b7 Benjamin Dauvergne
          var r = delete_prompt("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
115 b306fcbb Frédéric Péters
          if (r == true)
116
          {
117
            $.ajax({
118 1f9e881e Benjamin Dauvergne
              url: $(this).data('url'),
119 b306fcbb Frédéric Péters
              type: 'DELETE',
120
              success: function(data) {
121
                  window.location.reload(true);
122
                  return false;
123
              }
124
            });
125
           }
126
        return false;
127
      });
128 b2351503 Serghei MIHAI
129 b306fcbb Frédéric Péters
      $(base).find('.edit-appointment').click(function() {
130 5feec0ab Jérôme Schneider
        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 b306fcbb Frédéric Péters
      });
143
      $(base).find('.edit-event').click(function() {
144 5d6a177e Serghei MIHAI
          event_dialog("/" + service + "/agenda/" + current_date + "/update-event/" + $(this).data('event-id') , 'Modifier un événement', '850px', 'Modifier');
145 b306fcbb Frédéric Péters
          return false;
146
      });
147 23937808 Frédéric Péters
148
      $('.generate-mail-btn', base).click(function() {
149 c2246b82 Benjamin Dauvergne
        var url = '../../dossiers/' + $(this).data('dossier-id') + '/generate?event-id=' + $(this).data('event-id') + '&date=' + $(this).data('date');
150 23937808 Frédéric Péters
        $('#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 6a5a3da2 Mikaël Ates
               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 599e3c4b Jérôme Schneider
               if ($(this).data('address-recipient')){address += $(this).data('address-recipient') + '\n';}
163 6a5a3da2 Mikaël Ates
               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 23937808 Frédéric Péters
               $('#id_address').val(address);
169 b4310641 Mikaël Ates
               $('#id_phone_address').val($(this).attr('data-address-phone'));
170 23937808 Frédéric Péters
             });
171
             $('.addresses input[type=radio]').first().click();
172
          });
173
        return false;
174
      });
175 b306fcbb Frédéric Péters
}
176
177 34933291 Serghei MIHAI
function toggle_ressource(ressource) {
178 0d23d74f Benjamin Dauvergne
179 34933291 Serghei MIHAI
    var ressource_id = $(ressource).attr('id');
180
181
    var ressource_target = $(ressource).data('target');
182
     if (!ressource_target) {
183 1e0be261 Frédéric Péters
        return;
184
    }
185
186 34933291 Serghei MIHAI
    $(ressource).toggleClass('active');
187
    if (!($.cookie('agenda-tabs'))) {
188
        $.cookie('agenda-tabs', new Array(), { path: COOKIE_PATH });
189 1e0be261 Frédéric Péters
    }
190 34933291 Serghei MIHAI
    if ($(ressource).hasClass('active')) {
191
        var tabs = $.cookie('agenda-tabs');
192
        if ($.inArray(ressource_id, tabs) == -1)
193 1e0be261 Frédéric Péters
        {
194 34933291 Serghei MIHAI
            tabs.push(ressource_id);
195
            $.cookie('agenda-tabs', tabs, { path: COOKIE_PATH });
196 1e0be261 Frédéric Péters
        }
197
    }
198
    else {
199 34933291 Serghei MIHAI
        var agendatabs = $.cookie('agenda-tabs');
200 58b4847d Serghei MIHAI
        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 1e0be261 Frédéric Péters
        $.each(agendatabs, function (i, value) {
206 34933291 Serghei MIHAI
            if (value == ressource_id) {
207 1e0be261 Frédéric Péters
                agendatabs.splice(i, 1);
208
            }
209
        });
210 34933291 Serghei MIHAI
        $.cookie('agenda-tabs', agendatabs, { path: COOKIE_PATH });
211 1e0be261 Frédéric Péters
    }
212 58b4847d Serghei MIHAI
    $(ressource_target).toggle();
213 1fc93cd3 Serghei MIHAI
    $('#close-all-agendas').toggle($('#users li.active').length != 0);
214
    if (! $('#users li.active').length) {
215
        $('#agendas #tabs div').hide();
216
    }
217 1e0be261 Frédéric Péters
218 34933291 Serghei MIHAI
    var tab = $(ressource_target);
219 1e0be261 Frédéric Péters
    var tab_list = $(tab).parent().get(0);
220
    $(tab).detach().appendTo(tab_list);
221 5acde67b Serghei MIHAI
222 6dea6ad0 Serghei MIHAI
    var url = $("#date-selector").data('url');
223 34933291 Serghei MIHAI
    var tab_selector = '#' + ressource_id + '.active';
224 7ee2f91a Serghei MIHAI
225
    if ($(tab_selector).length) {
226 5acde67b Serghei MIHAI
        /* load disponibility column */
227 ab7f3dc9 Serghei MIHAI
        $.ajaxSetup({async:false});
228 34933291 Serghei MIHAI
        $.get(url + 'disponibility/' + ressource_id,
229 1e0be261 Frédéric Péters
            function(data) {
230 e4e0b5fc Jérôme Schneider
                if ($(tab_selector).hasClass('active')) {
231 488e8418 Serghei MIHAI
                    var availability_block = $('ul#availability');
232
                    availability_block.append($(data));
233 e4e0b5fc Jérôme Schneider
                }
234 1e0be261 Frédéric Péters
            }
235
        );
236 ab7f3dc9 Serghei MIHAI
       $.ajaxSetup({async:true});
237 1e0be261 Frédéric Péters
    } else {
238 7ee2f91a Serghei MIHAI
        // remove hidden ressource availability
239 34933291 Serghei MIHAI
        $('ul#availability li.' + ressource_id).remove();
240 1e0be261 Frédéric Péters
    }
241 34933291 Serghei MIHAI
    return $(ressource_target).find('a.tab');
242 1e0be261 Frédéric Péters
}
243
244 b0e22743 Jérôme Schneider
function event_dialog(url, title, width, btn_text) {
245 cf65455c Jérôme Schneider
    function add_periodic_events(base) {
246 610f195f Serghei MIHAI
      init_datepickers(base);
247 cf65455c Jérôme Schneider
      $(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 31541882 Serghei MIHAI
        var delete_button = {
256
            text: "Supprimer",
257
            id: "delete-btn",
258
            click: function () {
259 cf65455c Jérôme Schneider
                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 d8088940 Serghei MIHAI
            };
273 31541882 Serghei MIHAI
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-event/' + id,
274 b472e22b Serghei MIHAI
          'Modifier un évènement périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
275 cf65455c Jérôme Schneider
      });
276
      $(base).on('click', '.update-periodic-rdv', function () {
277
        $('.ui-icon-closethick').click();
278
        var id = $(this).data('id');
279 b8c44af8 Mikaël Ates
        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 cf65455c Jérôme Schneider
                    }
299
                }
300 b8c44af8 Mikaël Ates
            };
301
        }
302 31541882 Serghei MIHAI
        generic_ajaxform_dialog('/' + service + '/' + app_name + '/' + current_date + '/update-periodic-rdv/' + id,
303 b472e22b Serghei MIHAI
          'Modifier un rendez-vous périodique', '#ajax-dlg', '900px', 'Modifier', null, init_datepickers, null, delete_button);
304 cf65455c Jérôme Schneider
      });
305
    }
306
307
    generic_ajaxform_dialog(url, title, '#ajax-dlg', width, btn_text, null,
308
          add_periodic_events);
309
310 b0e22743 Jérôme Schneider
}
311
312 a5a20700 Benjamin Dauvergne
(function($) {
313
  $(function() {
314 5acde67b Serghei MIHAI
      $('#tabs').tabs({
315
          load: function(event, ui) {
316 2136241a Serghei MIHAI
              var tab = $(ui.tab).attr('id').split('-');
317
              if(tab[0] == 'ressource')
318
                  $.cookie('last-ressource', tab[1], { path: COOKIE_PATH });
319
320 c8933b52 Serghei MIHAI
              $('#tabs > div > div').accordion({active: false,
321
                                                autoHeight: false,
322
                                                collapsible: true});
323 b2351503 Serghei MIHAI
              enable_events('#tabs');
324 5acde67b Serghei MIHAI
          },
325
          selected: -1,
326 c8933b52 Serghei MIHAI
          collapsible: true,
327 5acde67b Serghei MIHAI
      });
328 02fff58c Jérôme Schneider
329 67c76d0b Serghei MIHAI
      $('button#print-button').click(function() { window.print();});
330 d8088940 Serghei MIHAI
331 b2351503 Serghei MIHAI
      enable_new_event();
332
      enable_new_appointment();
333
334 34933291 Serghei MIHAI
      if ($('#users .item').length) {
335
          $('#users .item').on('click', function() {
336
              var target = toggle_ressource(this);
337 5acde67b Serghei MIHAI
338 1e0be261 Frédéric Péters
              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 e1551fb2 Jérôme Schneider
             if (! ($('li.agenda:visible').hasClass('ui-state-active'))) {
347
                $('li.agenda:visible:last a.tab').click();
348
              }
349 1e0be261 Frédéric Péters
          });
350
351
          $('a.tab').click(function() {
352 34933291 Serghei MIHAI
              $.cookie('active-agenda', $(this).attr('id'), { path: COOKIE_PATH });
353 1e0be261 Frédéric Péters
          });
354 1df3d699 Serghei MIHAI
355 34933291 Serghei MIHAI
          if ($.cookie('agenda-tabs')) {
356
              $.each($.cookie('agenda-tabs'), function (i, selector) {
357
                  toggle_ressource($('#' + selector));
358 1df3d699 Serghei MIHAI
              });
359 296f1bd9 Jérôme Schneider
360 34933291 Serghei MIHAI
              if ($.cookie('active-agenda'))
361 1e0be261 Frédéric Péters
              {
362 34933291 Serghei MIHAI
                  var target = $("#" + $.cookie('active-agenda')).data('target');
363
                  if (!$('#tabs ' + target).hasClass('ui-state-active')) {
364
                      $("#tabs " + target + ' a.tab').click();
365 1e0be261 Frédéric Péters
                  }
366 296f1bd9 Jérôme Schneider
              }
367
          }
368
      }
369
370 1e0be261 Frédéric Péters
      $('a.close-tab').click(function() {
371 86870402 Serghei MIHAI
          var target = '#' + $(this).data('target');
372
          $(target).click();
373 34933291 Serghei MIHAI
          if ($.cookie('active-agenda') == $(target).attr('id')) {
374
              $.cookie('active-agenda', '', { path: COOKIE_PATH });
375 86870402 Serghei MIHAI
          }
376
377 1e0be261 Frédéric Péters
      });
378
379 a5a20700 Benjamin Dauvergne
      /* Gestion du filtre sur les utilisateurs */
380
      $('#filtre input').keyup(function() {
381
          var filtre = $(this).val();
382 e1551fb2 Jérôme Schneider
          if ($('#show-everybody').length) {
383 1e0be261 Frédéric Péters
              var everybody = $('#show-everybody').is(':checked');
384 e1551fb2 Jérôme Schneider
          } else {
385 1e0be261 Frédéric Péters
              var everybody = true;
386
          }
387 a5a20700 Benjamin Dauvergne
          if (filtre) {
388 3efd63d9 Frédéric Péters
              $('#show-everybody').attr('checked', true);
389 a5a20700 Benjamin Dauvergne
              $('#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 3efd63d9 Frédéric Péters
              if (! everybody) {
399 34933291 Serghei MIHAI
                  $('.item.worker:not(.in_service)').hide();
400
                  $('.item.worker:not(.intervenant)').hide();
401 3a2d3dc6 Frédéric Péters
              }
402 a5a20700 Benjamin Dauvergne
          }
403 3a2d3dc6 Frédéric Péters
          /* 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 a5a20700 Benjamin Dauvergne
      });
407 3a2d3dc6 Frédéric Péters
408 a5a20700 Benjamin Dauvergne
      $('.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 0485309f Frédéric Péters
      $('#show-everybody').change(function() {
416 3efd63d9 Frédéric Péters
      if (! $(this).is(':checked')) {
417 3a2d3dc6 Frédéric Péters
        $('#filtre input').val('');
418 24221a0a Mikaël Ates
      }
419 3a2d3dc6 Frédéric Péters
      $('#filtre input').keyup();
420
      return;
421 24221a0a Mikaël Ates
    });
422 16836835 Mikaël Ates
    $('select[name^="act_state"]').on('change', function () {
423
    $(this).next('button').prop('disabled',
424
      ($(this).data('previous') == $(this).val()));
425
    })
426 3a2d3dc6 Frédéric Péters
    $('#filtre input').keyup();
427 edda5421 Serghei MIHAI
428 34933291 Serghei MIHAI
    $.each({'persons': 'worker',
429 dd986559 Serghei MIHAI
            'ressources': 'ressource'},
430 edda5421 Serghei MIHAI
         function(key, value) {
431 488e8418 Serghei MIHAI
             $('#close-all-agendas').click(function() {
432 34933291 Serghei MIHAI
                 $.cookie('active-agenda', '', {path: COOKIE_PATH});
433
                 $('#users .item.active').each(function (i, v) {
434
                     toggle_ressource(v, value);
435 edda5421 Serghei MIHAI
                 });
436
             });
437
         });
438 b0e22743 Jérôme Schneider
  });
439 a5a20700 Benjamin Dauvergne
})(window.jQuery)