2009-09-30 10:02:08 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2009-10-01 15:18:20 -04:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2010-05-05 17:05:42 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
2009-09-30 10:02:08 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
2010-05-27 03:31:46 -04:00
|
|
|
const Pango = imports.gi.Pango;
|
2010-11-29 16:41:05 -05:00
|
|
|
const Gettext_gtk30 = imports.gettext.domain('gtk30');
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
const MSECS_IN_DAY = 24 * 60 * 60 * 1000;
|
2010-05-27 03:31:46 -04:00
|
|
|
const WEEKDATE_HEADER_WIDTH_DIGITS = 3;
|
2010-05-05 17:05:42 -04:00
|
|
|
const SHOW_WEEKDATE_KEY = 'show-weekdate';
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
function _sameDay(dateA, dateB) {
|
|
|
|
return (dateA.getDate() == dateB.getDate() &&
|
|
|
|
dateA.getMonth() == dateB.getMonth() &&
|
|
|
|
dateA.getYear() == dateB.getYear());
|
|
|
|
}
|
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
function _getCalendarWeekForDate(date) {
|
2010-05-30 17:17:40 -04:00
|
|
|
// Based on the algorithms found here:
|
|
|
|
// http://en.wikipedia.org/wiki/Talk:ISO_week_date
|
|
|
|
let midnightDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
|
|
// Need to get Monday to be 1 ... Sunday to be 7
|
|
|
|
let dayOfWeek = 1 + ((midnightDate.getDay() + 6) % 7);
|
|
|
|
let nearestThursday = new Date(midnightDate.getFullYear(), midnightDate.getMonth(),
|
|
|
|
midnightDate.getDate() + (4 - dayOfWeek));
|
|
|
|
|
|
|
|
let jan1st = new Date(nearestThursday.getFullYear(), 0, 1);
|
|
|
|
let diffDate = nearestThursday - jan1st;
|
|
|
|
let dayNumber = Math.floor(Math.abs(diffDate) / MSECS_IN_DAY);
|
|
|
|
let weekNumber = Math.floor(dayNumber / 7) + 1;
|
|
|
|
|
|
|
|
return weekNumber;
|
2010-05-27 03:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function _getDigitWidth(actor){
|
|
|
|
let context = actor.get_pango_context();
|
|
|
|
let themeNode = actor.get_theme_node();
|
|
|
|
let font = themeNode.get_font();
|
|
|
|
let metrics = context.get_metrics(font, context.get_language());
|
|
|
|
let width = metrics.get_approximate_digit_width();
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
function Calendar() {
|
|
|
|
this._init();
|
2010-03-15 09:50:05 -04:00
|
|
|
}
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
Calendar.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
// FIXME: This is actually the fallback method for GTK+ for the week start;
|
|
|
|
// GTK+ by preference uses nl_langinfo (NL_TIME_FIRST_WEEKDAY). We probably
|
|
|
|
// should add a C function so we can do the full handling.
|
|
|
|
this._weekStart = NaN;
|
2010-05-27 03:31:46 -04:00
|
|
|
this._weekdate = NaN;
|
|
|
|
this._digitWidth = NaN;
|
2010-05-05 17:05:42 -04:00
|
|
|
this._settings = new Gio.Settings({ schema: 'org.gnome.shell.calendar' });
|
2010-05-27 03:31:46 -04:00
|
|
|
|
2010-05-05 17:05:42 -04:00
|
|
|
this._settings.connect('changed::' + SHOW_WEEKDATE_KEY, Lang.bind(this, this._onSettingsChange));
|
|
|
|
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);
|
2010-05-27 03:31:46 -04:00
|
|
|
|
2010-11-29 16:41:05 -05:00
|
|
|
let weekStartString = Gettext_gtk30.gettext('calendar:week_start:0');
|
2010-05-13 15:46:04 -04:00
|
|
|
if (weekStartString.indexOf('calendar:week_start:') == 0) {
|
2009-09-30 10:02:08 -04:00
|
|
|
this._weekStart = parseInt(weekStartString.substring(20));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(this._weekStart) || this._weekStart < 0 || this._weekStart > 6) {
|
2010-05-13 15:46:04 -04:00
|
|
|
log('Translation of "calendar:week_start:0" in GTK+ is not correct');
|
2009-12-25 03:34:40 -05:00
|
|
|
this._weekStart = 0;
|
2009-09-30 10:02:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the ordering for month/year in the calendar heading
|
2010-11-29 16:41:05 -05:00
|
|
|
switch (Gettext_gtk30.gettext('calendar:MY')) {
|
2010-05-13 15:46:04 -04:00
|
|
|
case 'calendar:MY':
|
|
|
|
this._headerFormat = '%B %Y';
|
2009-09-30 10:02:08 -04:00
|
|
|
break;
|
2010-05-13 15:46:04 -04:00
|
|
|
case 'calendar:YM':
|
|
|
|
this._headerFormat = '%Y %B';
|
2009-09-30 10:02:08 -04:00
|
|
|
break;
|
|
|
|
default:
|
2010-05-13 15:46:04 -04:00
|
|
|
log('Translation of "calendar:MY" in GTK+ is not correct');
|
|
|
|
this._headerFormat = '%B %Y';
|
2009-09-30 10:02:08 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start off with the current date
|
|
|
|
this.date = new Date();
|
|
|
|
|
|
|
|
this.actor = new St.Table({ homogeneous: false,
|
2010-05-13 15:46:04 -04:00
|
|
|
style_class: 'calendar',
|
2009-10-01 15:18:20 -04:00
|
|
|
reactive: true });
|
|
|
|
|
|
|
|
this.actor.connect('scroll-event',
|
|
|
|
Lang.bind(this, this._onScroll));
|
2009-09-30 10:02:08 -04:00
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
this._buildHeader ();
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sets the calendar to show a specific date
|
|
|
|
setDate: function(date) {
|
|
|
|
if (!_sameDay(date, this.date)) {
|
|
|
|
this.date = date;
|
|
|
|
this._update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildHeader: function() {
|
|
|
|
let offsetCols = this._useWeekdate ? 1 : 0;
|
|
|
|
this.actor.destroy_children();
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
// Top line of the calendar '<| September 2009 |>'
|
|
|
|
this._topBox = new St.BoxLayout();
|
|
|
|
this.actor.add(this._topBox,
|
2010-05-27 03:31:46 -04:00
|
|
|
{ row: 0, col: 0, col_span: offsetCols + 7 });
|
2009-09-30 10:02:08 -04:00
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
this.actor.connect('style-changed', Lang.bind(this, this._onStyleChange));
|
2010-05-13 15:46:04 -04:00
|
|
|
let [backlabel, forwardlabel] = ['<', '>'];
|
2009-12-25 03:34:40 -05:00
|
|
|
if (St.Widget.get_default_direction () == St.TextDirection.RTL) {
|
|
|
|
[backlabel, forwardlabel] = [forwardlabel, backlabel];
|
|
|
|
}
|
|
|
|
|
|
|
|
let back = new St.Button({ label: backlabel, style_class: 'calendar-change-month' });
|
2009-09-30 10:02:08 -04:00
|
|
|
this._topBox.add(back);
|
2010-05-13 15:46:04 -04:00
|
|
|
back.connect('clicked', Lang.bind(this, this._prevMonth));
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
this._dateLabel = new St.Label();
|
|
|
|
this._topBox.add(this._dateLabel, { expand: true, x_fill: false, x_align: St.Align.MIDDLE });
|
|
|
|
|
2009-12-25 03:34:40 -05:00
|
|
|
let forward = new St.Button({ label: forwardlabel, style_class: 'calendar-change-month' });
|
2009-09-30 10:02:08 -04:00
|
|
|
this._topBox.add(forward);
|
2010-05-13 15:46:04 -04:00
|
|
|
forward.connect('clicked', Lang.bind(this, this._nextMonth));
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
// We need to figure out the abbreviated localized names for the days of the week;
|
|
|
|
// we do this by just getting the next 7 days starting from right now and then putting
|
|
|
|
// them in the right cell in the table. It doesn't matter if we add them in order
|
|
|
|
let iter = new Date(this.date);
|
|
|
|
iter.setSeconds(0); // Leap second protection. Hah!
|
2009-10-01 16:14:50 -04:00
|
|
|
iter.setHours(12);
|
2010-05-27 03:31:46 -04:00
|
|
|
|
|
|
|
if (this._useWeekdate) {
|
|
|
|
this._weekdateHeader = new St.Label();
|
|
|
|
this.actor.add(this._weekdateHeader,
|
|
|
|
{ row: 1,
|
|
|
|
col: 0,
|
|
|
|
x_fill: false, x_align: St.Align.MIDDLE });
|
|
|
|
this._setWeekdateHeaderWidth();
|
|
|
|
} else {
|
|
|
|
this._weekdateHeader = null;
|
|
|
|
}
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
for (let i = 0; i < 7; i++) {
|
2010-05-13 15:46:04 -04:00
|
|
|
this.actor.add(new St.Label({ text: iter.toLocaleFormat('%a') }),
|
2009-09-30 10:02:08 -04:00
|
|
|
{ row: 1,
|
2010-05-27 03:31:46 -04:00
|
|
|
col: offsetCols + (7 + iter.getDay() - this._weekStart) % 7,
|
2010-02-15 12:08:45 -05:00
|
|
|
x_fill: false, x_align: St.Align.END });
|
2009-09-30 10:02:08 -04:00
|
|
|
iter.setTime(iter.getTime() + MSECS_IN_DAY);
|
|
|
|
}
|
|
|
|
|
2009-10-01 16:14:50 -04:00
|
|
|
// All the children after this are days, and get removed when we update the calendar
|
|
|
|
this._firstDayIndex = this.actor.get_children().length;
|
2010-05-27 03:31:46 -04:00
|
|
|
},
|
2009-10-01 16:14:50 -04:00
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
_onStyleChange: function(actor, event) {
|
|
|
|
// width of a digit in pango units
|
|
|
|
this._digitWidth = _getDigitWidth(this.actor) / Pango.SCALE;
|
|
|
|
this._setWeekdateHeaderWidth();
|
2009-09-30 10:02:08 -04:00
|
|
|
},
|
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
_setWeekdateHeaderWidth: function() {
|
|
|
|
if (this.digitWidth != NaN && this._useWeekdate && this._weekdateHeader) {
|
|
|
|
this._weekdateHeader.set_width (this._digitWidth * WEEKDATE_HEADER_WIDTH_DIGITS);
|
2009-09-30 10:02:08 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-10-01 15:18:20 -04:00
|
|
|
_onScroll : function(actor, event) {
|
|
|
|
switch (event.get_scroll_direction()) {
|
|
|
|
case Clutter.ScrollDirection.UP:
|
|
|
|
case Clutter.ScrollDirection.LEFT:
|
|
|
|
this._prevMonth();
|
|
|
|
break;
|
|
|
|
case Clutter.ScrollDirection.DOWN:
|
|
|
|
case Clutter.ScrollDirection.RIGHT:
|
|
|
|
this._nextMonth();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
_prevMonth: function() {
|
|
|
|
if (this.date.getMonth() == 0) {
|
|
|
|
this.date.setMonth(11);
|
|
|
|
this.date.setFullYear(this.date.getFullYear() - 1);
|
|
|
|
} else {
|
|
|
|
this.date.setMonth(this.date.getMonth() - 1);
|
|
|
|
}
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
|
|
|
_nextMonth: function() {
|
|
|
|
if (this.date.getMonth() == 11) {
|
|
|
|
this.date.setMonth(0);
|
|
|
|
this.date.setFullYear(this.date.getFullYear() + 1);
|
|
|
|
} else {
|
|
|
|
this.date.setMonth(this.date.getMonth() + 1);
|
|
|
|
}
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
_onSettingsChange: function() {
|
2010-05-05 17:05:42 -04:00
|
|
|
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);
|
2010-05-27 03:31:46 -04:00
|
|
|
this._buildHeader();
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
_update: function() {
|
2009-10-01 16:14:50 -04:00
|
|
|
this._dateLabel.text = this.date.toLocaleFormat(this._headerFormat);
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
// Remove everything but the topBox and the weekday labels
|
|
|
|
let children = this.actor.get_children();
|
2009-10-01 16:14:50 -04:00
|
|
|
for (let i = this._firstDayIndex; i < children.length; i++)
|
2009-09-30 10:02:08 -04:00
|
|
|
children[i].destroy();
|
|
|
|
|
|
|
|
// Start at the beginning of the week before the start of the month
|
|
|
|
let iter = new Date(this.date);
|
|
|
|
iter.setDate(1);
|
|
|
|
iter.setSeconds(0);
|
2009-10-01 16:14:50 -04:00
|
|
|
iter.setHours(12);
|
2010-08-01 06:47:16 -04:00
|
|
|
let daysToWeekStart = (7 + iter.getDay() - this._weekStart) % 7;
|
|
|
|
iter.setTime(iter.getTime() - daysToWeekStart * MSECS_IN_DAY);
|
2009-09-30 10:02:08 -04:00
|
|
|
|
|
|
|
let now = new Date();
|
|
|
|
|
|
|
|
let row = 2;
|
|
|
|
while (true) {
|
|
|
|
let label = new St.Label({ text: iter.getDate().toString() });
|
|
|
|
if (_sameDay(now, iter))
|
2010-05-13 15:46:04 -04:00
|
|
|
label.style_class = 'calendar-day calendar-today';
|
2009-09-30 10:02:08 -04:00
|
|
|
else if (iter.getMonth() != this.date.getMonth())
|
2010-05-13 15:46:04 -04:00
|
|
|
label.style_class = 'calendar-day calendar-other-month-day';
|
2009-09-30 10:02:08 -04:00
|
|
|
else
|
2010-05-13 15:46:04 -04:00
|
|
|
label.style_class = 'calendar-day';
|
2010-05-27 03:31:46 -04:00
|
|
|
|
|
|
|
let offsetCols = this._useWeekdate ? 1 : 0;
|
2009-09-30 10:02:08 -04:00
|
|
|
this.actor.add(label,
|
2010-05-27 03:31:46 -04:00
|
|
|
{ row: row, col: offsetCols + (7 + iter.getDay() - this._weekStart) % 7,
|
2010-02-15 12:08:45 -05:00
|
|
|
x_fill: false, x_align: St.Align.END });
|
2009-09-30 10:02:08 -04:00
|
|
|
|
2010-05-27 03:31:46 -04:00
|
|
|
if (this._useWeekdate && iter.getDay() == 4) {
|
|
|
|
let label = new St.Label({ text: _getCalendarWeekForDate(iter).toString(),
|
|
|
|
style_class: 'calendar-day calendar-calendarweek'});
|
|
|
|
this.actor.add(label,
|
|
|
|
{ row: row, col: 0,
|
|
|
|
x_fill: false, x_align: St.Align.MIDDLE });
|
|
|
|
}
|
|
|
|
|
2009-09-30 10:02:08 -04:00
|
|
|
iter.setTime(iter.getTime() + MSECS_IN_DAY);
|
|
|
|
if (iter.getDay() == this._weekStart) {
|
|
|
|
// We stop on the first "first day of the week" after the month we are displaying
|
|
|
|
if (iter.getMonth() > this.date.getMonth() || iter.getYear() > this.date.getYear())
|
|
|
|
break;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|