egroupware/api/js/jsapi/egw_calendar.js

303 lines
6.7 KiB
JavaScript
Raw Normal View History

/**
* EGroupware clientside API object
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package etemplate
* @subpackage api
* @link http://www.egroupware.org
* @author Andreas Stöckel (as AT stylite.de)
* @author Ralf Becker <RalfBecker@outdoor-training.de>
* @version $Id$
*/
/*egw:uses
2012-03-12 22:20:46 +01:00
jquery.jquery-ui;
jquery.jquery-ui-timepicker-addon;
egw_core;
egw_preferences;
// egw_jquery;
2012-03-09 19:46:29 +01:00
egw_css;
*/
/**
* Date and timepicker
*
* @augments Class
* @param {string} _app application name object is instanciated for
* @param {object} _wnd window object is instanciated for
*/
egw.extend('calendar', egw.MODULE_WND_LOCAL, function(_app, _wnd)
{
"use strict";
// translate only once
var calendar_translated = false,timepicker_translated = false;
function calendarPreferences(_egw)
{
// Date format in jQuery UI date format
var dateformat = dateTimeFormat(_egw.preference("dateformat"));
// First day of the week
var first_day = {"Monday": 1, "Sunday": 0, "Saturday": 6};
var first_day_pref = _egw.preference("weekdaystarts","calendar");
return {
2012-03-09 19:46:29 +01:00
'dateFormat': dateformat,
'firstDay': first_day_pref ? first_day[first_day_pref] : 0
2012-03-09 19:46:29 +01:00
};
};
/**
* transform PHP date/time-format to jQuery date/time-format
*
* @param {string} _php_format
* @returns {string}
*/
function dateTimeFormat(_php_format)
{
return _php_format
.replace("Y","yy")
.replace("d","dd")
.replace("m","mm")
.replace("M", "M")
.replace('H', 'hh')
.replace('i', 'mm') // datepicker uses mm for month and minutes, depending where in format it's written!
.replace('s', 'ss');
}
2012-03-12 22:20:46 +01:00
function timePreferences(_egw)
{
return {
// timepicker does NOT work with spaces in timeformat
"timeFormat": egw.preference("timeformat") == 12 ? "h:mmtt" : "HH:mm",
2012-03-12 22:20:46 +01:00
"ampm": (egw.preference("timeformat") == "12"),
"hourGrid": 4,
"minuteGrid": 10
};
2012-03-12 22:20:46 +01:00
};
function setupCalendar(_egw, _input, _time, _callback, _context)
{
if (!calendar_translated)
{
// Set template's icon for date popup - could probably use jquery-ui icons
_egw.css(".et2_date input.hasDatepicker:enabled:hover", "background-image: url(" + egw().image('datepopup') + ")");
translateCalendar();
calendar_translated = true;
}
if (!timepicker_translated)
{
translateTimepicker();
timepicker_translated = true;
}
var prefs = calendarPreferences(_egw);
var params = {
2012-03-09 19:46:29 +01:00
dateFormat: prefs.dateFormat,
firstDay: prefs.firstDay,
autoSize: true,
showButtonPanel: true, // Today, Done buttons
showOtherMonths: true,
selectOtherMonths: true,
showWeek: true, // Week numbers
changeMonth: true, // Month selectbox
2012-03-09 19:46:29 +01:00
changeYear: true // Year selectbox
};
// Get the preferences
2012-03-12 22:20:46 +01:00
if(_time)
{
params = jQuery.extend(params, timePreferences(_egw));
_wnd.jQuery(_input).datetimepicker(params);
}
else
{
2014-02-25 18:16:42 +01:00
_wnd.jQuery(_input).datepicker(params);
2012-03-12 22:20:46 +01:00
}
/*
onClose: function(date_text, picker) {
// Only update if there's a change - "" if no date selected
if(date_text != "") self.set_value(new Date(
picker.selectedYear,
picker.selectedMonth,
picker.selectedDay,
self.input_hours ? self.input_hours.val() : 0,
self.input_minutes ? self.input_minutes.val() : 0,
0,0
));
},
});
2012-03-09 19:46:29 +01:00
*/
};
2012-03-12 22:20:46 +01:00
/**
* Set up an input to have a time selection popup
*
* @param {egw} _egw egw object to use
* @param {(node|string)} _input input field to use
2012-03-12 22:20:46 +01:00
*/
function setupTime(_egw, _input)
2012-03-12 22:20:46 +01:00
{
if (!timepicker_translated)
{
translateTimepicker();
timepicker_translated = true;
}
2012-03-12 22:20:46 +01:00
_wnd.jQuery(_input).timepicker(timePreferences(_egw));
}
2012-03-09 19:46:29 +01:00
/**
* Translate, and set as default values
*
*/
function translateCalendar()
{
2012-03-09 19:46:29 +01:00
var translate_fields = {
// These ones are simple strings
"nextText": false,
"currentText": false,
"prevText": false,
"closeText": false,
2016-04-28 00:31:05 +02:00
"weekHeader": false,
// These ones are arrays.
2012-03-09 19:46:29 +01:00
// Integers are length. If lang() has no short translation, just trim full
"dayNames": false,
2012-03-09 19:46:29 +01:00
"dayNamesShort":3,
"dayNamesMin": 2,
"monthNames": false,
"monthNamesShort": 3
};
2012-03-09 19:46:29 +01:00
var regional = {};
var full = [];
for(var i in translate_fields)
{
2012-03-12 18:26:33 +01:00
var trans = _wnd.jQuery.datepicker._defaults[i];
2012-03-09 19:46:29 +01:00
if(typeof trans === 'string')
{
trans = egw().lang(trans);
}
2012-03-09 19:46:29 +01:00
else
{
for(var key in trans) {
if(translate_fields[i] === false)
{
2012-03-09 19:46:29 +01:00
trans[key] = egw().lang(trans[key]);
}
else
{
if (trans.hasOwnProperty(key)) trans[key] = full[key].substr(0,translate_fields[i]);
}
}
2012-03-09 19:46:29 +01:00
// Keep the full one for missing short ones
if(translate_fields[i] === false) full = trans;
2012-03-09 19:46:29 +01:00
}
regional[i] = trans;
}
// Set some non-lang defaults too
/*
var prefs = calendarPreferences(egw());
for(var i in prefs)
{
regional[i] = prefs[i];
}
*/
2012-03-12 18:26:33 +01:00
_wnd.jQuery.datepicker.setDefaults(regional);
2012-03-09 19:46:29 +01:00
};
2012-03-12 22:20:46 +01:00
function translateTimepicker()
{
var translate_fields = {
// These ones are simple strings
"timeOnlyTitle": false,
"timeText": false,
"hourText": false,
"minuteText": false,
"currentText": false,
"closeText": false
};
var regional = {};
for(var i in translate_fields)
{
var trans = _wnd.jQuery.timepicker._defaults[i];
if(typeof trans === 'string')
{
trans = egw().lang(trans);
}
regional[i] = trans;
}
_wnd.jQuery.timepicker.setDefaults(regional);
};
return {
/**
* setup a calendar / date-selection
*
* @member of egw
* @param _input
* @param _time
* @param _callback
* @param _context
* @returns
*/
calendar: function(_input, _time, _callback, _context)
{
setupCalendar(this, _input, _time, _callback, _context);
2012-03-12 22:20:46 +01:00
},
/**
* setup a time-selection
*
* @param _input
* @param _callback
* @param _context
* @returns
*/
time: function(_input, _callback, _context)
{
2012-03-12 22:20:46 +01:00
setupTime(this, _input, _callback, _context);
},
/**
* transform PHP date/time-format to jQuery date/time-format
*
* @param {string} _php_format
* @returns {string}
*/
dateTimeFormat: function(_php_format)
{
return dateTimeFormat(_php_format);
},
/**
* Calculate the start of the week, according to user's preference
*
* @param {string} date
*/
week_start: function(date) {
var d = new Date(date);
var day = d.getUTCDay();
var diff = 0;
switch(egw.preference('weekdaystarts','calendar'))
{
case 'Saturday':
diff = day === 6 ? 0 : day === 0 ? -1 : -(day + 1);
break;
case 'Monday':
diff = day === 0 ? -6 : 1-day;
break;
case 'Sunday':
default:
diff = -day;
}
d.setUTCDate(d.getUTCDate() + diff);
return d;
}
};
});