Little code re-arrangements: make rcube_calendar a global class
This commit is contained in:
parent
7e42d3207d
commit
fe47f049cd
1 changed files with 156 additions and 152 deletions
|
@ -22,35 +22,59 @@
|
||||||
+-------------------------------------------------------------------------+
|
+-------------------------------------------------------------------------+
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* calendar initialization */
|
// Roundcube calendar client class
|
||||||
window.rcmail && rcmail.addEventListener('init', function(evt) {
|
function rcube_calendar(settings)
|
||||||
|
{
|
||||||
// quote html entities
|
/*** member vars ***/
|
||||||
function Q(str)
|
|
||||||
{
|
|
||||||
return String(str).replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
||||||
}
|
|
||||||
// php equivalent
|
|
||||||
function nl2br(str)
|
|
||||||
{
|
|
||||||
return String(str).replace(/\n/g, "<br/>");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Roundcube calendar client class
|
|
||||||
function rcube_calendar(settings)
|
|
||||||
{
|
|
||||||
// member vars
|
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.alarm_ids = [];
|
this.alarm_ids = [];
|
||||||
this.alarm_dialog = null;
|
this.alarm_dialog = null;
|
||||||
this.snooze_popup = null;
|
this.snooze_popup = null;
|
||||||
this.dismiss_link = null
|
this.dismiss_link = null
|
||||||
|
|
||||||
// private vars
|
|
||||||
|
/*** private vars ***/
|
||||||
var me = this;
|
var me = this;
|
||||||
var day_clicked = day_clicked_ts = 0;
|
var day_clicked = day_clicked_ts = 0;
|
||||||
var ignore_click = false;
|
var ignore_click = false;
|
||||||
|
|
||||||
|
// general datepicker settings
|
||||||
|
var datepicker_settings = {
|
||||||
|
// translate from fullcalendar format to datepicker format
|
||||||
|
dateFormat: settings['date_format'].replace(/M/g, 'm').replace(/mmmmm/, 'MM').replace(/mmm/, 'M').replace(/dddd/, 'DD').replace(/ddd/, 'D').replace(/yy/g, 'y'),
|
||||||
|
firstDay : settings['first_day'],
|
||||||
|
dayNamesMin: settings['days_short'],
|
||||||
|
monthNames: settings['months'],
|
||||||
|
monthNamesShort: settings['months'],
|
||||||
|
changeMonth: false,
|
||||||
|
showOtherMonths: true,
|
||||||
|
selectOtherMonths: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*** private methods ***/
|
||||||
|
|
||||||
|
// quote html entities
|
||||||
|
var Q = function(str)
|
||||||
|
{
|
||||||
|
return String(str).replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
|
};
|
||||||
|
// php equivalent
|
||||||
|
var nl2br = function(str)
|
||||||
|
{
|
||||||
|
return String(str).replace(/\n/g, "<br/>");
|
||||||
|
};
|
||||||
|
|
||||||
|
// from time and date strings to a real date object
|
||||||
|
var parse_datetime = function(time, date) {
|
||||||
|
// we use the utility function from datepicker to parse dates
|
||||||
|
var date = $.datepicker.parseDate(datepicker_settings.dateFormat, date, datepicker_settings);
|
||||||
|
var time_arr = time.split(/[:.]/);
|
||||||
|
if (!isNaN(time_arr[0])) date.setHours(time_arr[0]);
|
||||||
|
if (!isNaN(time_arr[1])) date.setMinutes(time_arr[1]);
|
||||||
|
return date;
|
||||||
|
};
|
||||||
|
|
||||||
// create a nice human-readable string for the date/time range
|
// create a nice human-readable string for the date/time range
|
||||||
var event_date_text = function(event)
|
var event_date_text = function(event)
|
||||||
{
|
{
|
||||||
|
@ -262,8 +286,8 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
var buttons = {};
|
var buttons = {};
|
||||||
|
|
||||||
buttons[rcmail.gettext('save', 'calendar')] = function() {
|
buttons[rcmail.gettext('save', 'calendar')] = function() {
|
||||||
var start = me.parse_datetime(starttime.val(), startdate.val());
|
var start = parse_datetime(starttime.val(), startdate.val());
|
||||||
var end = me.parse_datetime(endtime.val(), enddate.val());
|
var end = parse_datetime(endtime.val(), enddate.val());
|
||||||
|
|
||||||
// basic input validatetion
|
// basic input validatetion
|
||||||
if (start.getTime() > end.getTime()) {
|
if (start.getTime() > end.getTime()) {
|
||||||
|
@ -293,7 +317,7 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
if (alarm) {
|
if (alarm) {
|
||||||
var val, offset = $('select.edit-alarm-offset').val();
|
var val, offset = $('select.edit-alarm-offset').val();
|
||||||
if (offset == '@')
|
if (offset == '@')
|
||||||
data.alarms = '@' + (me.parse_datetime($('input.edit-alarm-time').val(), $('input.edit-alarm-date').val()).getTime()/1000) + ':' + alarm;
|
data.alarms = '@' + (parse_datetime($('input.edit-alarm-time').val(), $('input.edit-alarm-date').val()).getTime()/1000) + ':' + alarm;
|
||||||
else if ((val = parseInt($('input.edit-alarm-value').val())) && !isNaN(val) && val >= 0)
|
else if ((val = parseInt($('input.edit-alarm-value').val())) && !isNaN(val) && val >= 0)
|
||||||
data.alarms = offset[0] + val + offset[1] + ':' + alarm;
|
data.alarms = offset[0] + val + offset[1] + ':' + alarm;
|
||||||
}
|
}
|
||||||
|
@ -310,7 +334,7 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
if (until == 'count')
|
if (until == 'count')
|
||||||
data.recurrence.COUNT = rrtimes.val();
|
data.recurrence.COUNT = rrtimes.val();
|
||||||
else if (until == 'until')
|
else if (until == 'until')
|
||||||
data.recurrence.UNTIL = me.parse_datetime(endtime.val(), rrenddate.val()).getTime()/1000;
|
data.recurrence.UNTIL = parse_datetime(endtime.val(), rrenddate.val()).getTime()/1000;
|
||||||
|
|
||||||
if (freq == 'WEEKLY') {
|
if (freq == 'WEEKLY') {
|
||||||
var byday = [];
|
var byday = [];
|
||||||
|
@ -442,30 +466,8 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// general datepicker settings
|
|
||||||
this.datepicker_settings = {
|
|
||||||
// translate from fullcalendar format to datepicker format
|
|
||||||
dateFormat: settings['date_format'].replace(/M/g, 'm').replace(/mmmmm/, 'MM').replace(/mmm/, 'M').replace(/dddd/, 'DD').replace(/ddd/, 'D').replace(/yy/g, 'y'),
|
|
||||||
firstDay : settings['first_day'],
|
|
||||||
dayNamesMin: settings['days_short'],
|
|
||||||
monthNames: settings['months'],
|
|
||||||
monthNamesShort: settings['months'],
|
|
||||||
changeMonth: false,
|
|
||||||
showOtherMonths: true,
|
|
||||||
selectOtherMonths: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// from time and date strings to a real date object
|
|
||||||
this.parse_datetime = function(time, date) {
|
|
||||||
// we use the utility function from datepicker to parse dates
|
|
||||||
var date = $.datepicker.parseDate(me.datepicker_settings.dateFormat, date, me.datepicker_settings);
|
|
||||||
var time_arr = time.split(/[:.]/);
|
|
||||||
if (!isNaN(time_arr[0])) date.setHours(time_arr[0]);
|
|
||||||
if (!isNaN(time_arr[1])) date.setMinutes(time_arr[1]);
|
|
||||||
return date;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/*** public methods ***/
|
||||||
|
|
||||||
// public method to bring up the new event dialog
|
// public method to bring up the new event dialog
|
||||||
this.add_event = function() {
|
this.add_event = function() {
|
||||||
|
@ -765,7 +767,7 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// initialize small calendar widget using jQuery UI datepicker
|
// initialize small calendar widget using jQuery UI datepicker
|
||||||
$('#datepicker').datepicker($.extend(this.datepicker_settings, {
|
$('#datepicker').datepicker($.extend(datepicker_settings, {
|
||||||
inline: true,
|
inline: true,
|
||||||
showWeek: true,
|
showWeek: true,
|
||||||
changeMonth: false, // maybe enable?
|
changeMonth: false, // maybe enable?
|
||||||
|
@ -797,52 +799,22 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
$("#calendar .fc-button-next").click(fullcalendar_update);
|
$("#calendar .fc-button-next").click(fullcalendar_update);
|
||||||
$("#calendar .fc-button-today").click(fullcalendar_update);
|
$("#calendar .fc-button-today").click(fullcalendar_update);
|
||||||
|
|
||||||
// hide event dialog when clicking somewhere into document
|
// format time string
|
||||||
$(document).bind('mousedown', dialog_check);
|
|
||||||
|
|
||||||
} // end rcube_calendar class
|
|
||||||
|
|
||||||
|
|
||||||
// configure toobar buttons
|
|
||||||
rcmail.register_command('plugin.addevent', function(){ cal.add_event(); }, true);
|
|
||||||
|
|
||||||
// export events
|
|
||||||
rcmail.register_command('plugin.export', function(){ rcmail.goto_url('plugin.export_events', { source:cal.selected_calendar }); }, true);
|
|
||||||
rcmail.enable_command('plugin.export', true);
|
|
||||||
|
|
||||||
// register callback commands
|
|
||||||
rcmail.addEventListener('plugin.display_alarms', function(alarms){ cal.display_alarms(alarms); });
|
|
||||||
|
|
||||||
// reload calendar
|
|
||||||
rcmail.addEventListener('plugin.reload_calendar', function() { $('#calendar').fullCalendar('refetchEvents'); });
|
|
||||||
|
|
||||||
|
|
||||||
var formattime = function(hour, minutes) {
|
var formattime = function(hour, minutes) {
|
||||||
return ((hour < 10) ? "0" : "") + hour + ((minutes < 10) ? ":0" : ":") + minutes;
|
return ((hour < 10) ? "0" : "") + hour + ((minutes < 10) ? ":0" : ":") + minutes;
|
||||||
};
|
};
|
||||||
|
|
||||||
// if start date is changed, shift end date according to initial duration
|
// if start date is changed, shift end date according to initial duration
|
||||||
var shift_enddate = function(dateText) {
|
var shift_enddate = function(dateText) {
|
||||||
var newstart = cal.parse_datetime('0', dateText);
|
var newstart = parse_datetime('0', dateText);
|
||||||
var newend = new Date(newstart.getTime() + $('#edit-startdate').data('duration') * 1000);
|
var newend = new Date(newstart.getTime() + $('#edit-startdate').data('duration') * 1000);
|
||||||
$('#edit-enddate').val($.fullCalendar.formatDate(newend, cal.settings['date_format']));
|
$('#edit-enddate').val($.fullCalendar.formatDate(newend, cal.settings['date_format']));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// let's go
|
|
||||||
var cal = new rcube_calendar(rcmail.env.calendar_settings);
|
|
||||||
|
|
||||||
$(window).resize(function() {
|
|
||||||
$('#calendar').fullCalendar('option', 'height', $(window).height() - 95);
|
|
||||||
}).resize();
|
|
||||||
|
|
||||||
// show toolbar
|
|
||||||
$('#toolbar').show();
|
|
||||||
|
|
||||||
// init event dialog
|
// init event dialog
|
||||||
$('#eventtabs').tabs();
|
$('#eventtabs').tabs();
|
||||||
$('#edit-enddate, input.edit-alarm-date').datepicker(cal.datepicker_settings);
|
$('#edit-enddate, input.edit-alarm-date').datepicker(datepicker_settings);
|
||||||
$('#edit-startdate').datepicker(cal.datepicker_settings).datepicker('option', 'onSelect', shift_enddate).change(function(){ shift_enddate(this.value); });
|
$('#edit-startdate').datepicker(datepicker_settings).datepicker('option', 'onSelect', shift_enddate).change(function(){ shift_enddate(this.value); });
|
||||||
$('#edit-allday').click(function(){ $('#edit-starttime, #edit-endtime')[(this.checked?'hide':'show')](); });
|
$('#edit-allday').click(function(){ $('#edit-starttime, #edit-endtime')[(this.checked?'hide':'show')](); });
|
||||||
|
|
||||||
// configure drop-down menu on time input fields based on jquery UI autocomplete
|
// configure drop-down menu on time input fields based on jquery UI autocomplete
|
||||||
|
@ -915,6 +887,38 @@ window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
if (freq)
|
if (freq)
|
||||||
$('#recurrence-form-'+freq+', #recurrence-form-until').show();
|
$('#recurrence-form-'+freq+', #recurrence-form-until').show();
|
||||||
});
|
});
|
||||||
$('#edit-recurrence-enddate').datepicker(cal.datepicker_settings).click(function(){ $("#edit-recurrence-repeat-until").prop('checked', true) });
|
$('#edit-recurrence-enddate').datepicker(datepicker_settings).click(function(){ $("#edit-recurrence-repeat-until").prop('checked', true) });
|
||||||
|
|
||||||
|
// hide event dialog when clicking somewhere into document
|
||||||
|
$(document).bind('mousedown', dialog_check);
|
||||||
|
|
||||||
|
} // end rcube_calendar class
|
||||||
|
|
||||||
|
|
||||||
|
/* calendar plugin initialization */
|
||||||
|
window.rcmail && rcmail.addEventListener('init', function(evt) {
|
||||||
|
|
||||||
|
// configure toobar buttons
|
||||||
|
rcmail.register_command('plugin.addevent', function(){ cal.add_event(); }, true);
|
||||||
|
|
||||||
|
// export events
|
||||||
|
rcmail.register_command('plugin.export', function(){ rcmail.goto_url('plugin.export_events', { source:cal.selected_calendar }); }, true);
|
||||||
|
rcmail.enable_command('plugin.export', true);
|
||||||
|
|
||||||
|
// register callback commands
|
||||||
|
rcmail.addEventListener('plugin.display_alarms', function(alarms){ cal.display_alarms(alarms); });
|
||||||
|
rcmail.addEventListener('plugin.reload_calendar', function(){ $('#calendar').fullCalendar('refetchEvents'); });
|
||||||
|
|
||||||
|
|
||||||
|
// let's go
|
||||||
|
var cal = new rcube_calendar(rcmail.env.calendar_settings);
|
||||||
|
cal.init_ui();
|
||||||
|
|
||||||
|
$(window).resize(function() {
|
||||||
|
$('#calendar').fullCalendar('option', 'height', $(window).height() - 95);
|
||||||
|
}).resize();
|
||||||
|
|
||||||
|
// show toolbar
|
||||||
|
$('#toolbar').show();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue