aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/calendar_base.js
blob: 41ae8e5978c8ac2adf4a2edabeabc22a3dc41f05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Base Javascript class for the Calendar plugin
 *
 * @author Lazlo Westerhof <hello@lazlo.me>
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * @licstart  The following is the entire license notice for the
 * JavaScript code in this page.
 *
 * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
 * Copyright (C) 2013-2015, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page.
 */

// Basic setup for Roundcube calendar client class
function rcube_calendar(settings)
{
    // extend base class
    rcube_libcalendaring.call(this, settings);

    // member vars
    this.ui;
    this.ui_loaded = false;
    this.selected_attachment = null;

    // private vars
    var me = this;

    // create new event from current mail message
    this.create_from_mail = function(uid)
    {
      if (uid || (uid = rcmail.get_single_uid())) {
        // load calendar UI (scripts and edit dialog template)
        if (!this.ui_loaded) {
          $.when(
            $.getScript(rcmail.assets_path('plugins/calendar/calendar_ui.js')),
            $.getScript(rcmail.assets_path('plugins/calendar/lib/js/fullcalendar.js')),
            $.get(rcmail.url('calendar/inlineui'), function(html){ $(document.body).append(html); }, 'html')
          ).then(function() {
            // disable attendees feature (autocompletion and stuff is not initialized)
            for (var c in rcmail.env.calendars)
              rcmail.env.calendars[c].attendees = rcmail.env.calendars[c].resources = false;
            
            me.ui_loaded = true;
            me.ui = new rcube_calendar_ui(me.settings);
            me.create_from_mail(uid);  // start over
          });
          return;
        }
        else {
          // get message contents for event dialog
          var lock = rcmail.set_busy(true, 'loading');
          rcmail.http_post('calendar/mailtoevent', {
              '_mbox': rcmail.env.mailbox,
              '_uid': uid
            }, lock);
        }
      }
    };
    
    // callback function triggered from server with contents for the new event
    this.mail2event_dialog = function(event)
    {
      if (event.title) {
        this.ui.add_event(event);
        if (rcmail.message_list)
          rcmail.message_list.blur();
      }
    };

    // handler for attachment-save-calendar commands
    this.save_to_calendar = function(p)
    {
      // TODO: show dialog to select the calendar for importing
      if (this.selected_attachment && window.rcube_libcalendaring) {
        rcmail.http_post('calendar/mailimportattach', {
            _uid: rcmail.env.uid,
            _mbox: rcmail.env.mailbox,
            _part: this.selected_attachment,
            // _calendar: $('#calendar-attachment-saveto').val(),
          }, rcmail.set_busy(true, 'itip.savingdata'));
      }
    }
}


/* calendar plugin initialization (for non-calendar tasks) */
window.rcmail && rcmail.addEventListener('init', function(evt) {
  if (rcmail.task != 'calendar') {
    var cal = new rcube_calendar($.extend(rcmail.env.calendar_settings, rcmail.env.libcal_settings));

    // register create-from-mail command to message_commands array
    if (rcmail.env.task == 'mail') {
      rcmail.register_command('calendar-create-from-mail', function() { cal.create_from_mail() });
      rcmail.register_command('attachment-save-calendar', function() { cal.save_to_calendar() });
      rcmail.addEventListener('plugin.mail2event_dialog', function(p){ cal.mail2event_dialog(p) });
      rcmail.addEventListener('plugin.unlock_saving', function(p){ cal.ui && cal.ui.unlock_saving(); });
      
      if (rcmail.env.action != 'show') {
        rcmail.env.message_commands.push('calendar-create-from-mail');
        rcmail.add_element($('<a>'));
      }
      else {
        rcmail.enable_command('calendar-create-from-mail', true);
      }

      rcmail.addEventListener('beforemenu-open', function(p) {
        if (p.menu == 'attachmentmenu') {
          cal.selected_attachment = p.id;
          var mimetype = rcmail.env.attachments[p.id];
          rcmail.enable_command('attachment-save-calendar', mimetype == 'text/calendar' || mimetype == 'text/x-vcalendar' || mimetype == 'application/ics');
        }
      });
    }
  }

  rcmail.register_command('plugin.calendar', function() { rcmail.switch_task('calendar'); }, true);
  
  rcmail.addEventListener('plugin.ping_url', function(p){
    var action = p.action;
    p.action = p.event = null;
    new Image().src = rcmail.url(action, p);
  });
});

© 2014-2024 Faster IT GmbH | imprint | privacy policy