📄 icalendar.js
字号:
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.cal.iCalendar");
dojo.require("dojo.lang.common");
dojo.require("dojo.cal.textDirectory");
dojo.require("dojo.date.common");
dojo.require("dojo.date.serialize");
dojo.cal.iCalendar.fromText = function (text) {
var properties = dojo.cal.textDirectory.tokenise(text);
var calendars = [];
for (var i = 0, begun = false; i < properties.length; i++) {
var prop = properties[i];
if (!begun) {
if (prop.name == "BEGIN" && prop.value == "VCALENDAR") {
begun = true;
var calbody = [];
}
} else {
if (prop.name == "END" && prop.value == "VCALENDAR") {
calendars.push(new dojo.cal.iCalendar.VCalendar(calbody));
begun = false;
} else {
calbody.push(prop);
}
}
}
return calendars;
};
dojo.cal.iCalendar.Component = function (body) {
if (!this.name) {
this.name = "COMPONENT";
}
this.properties = [];
this.components = [];
if (body) {
for (var i = 0, context = ""; i < body.length; i++) {
if (context == "") {
if (body[i].name == "BEGIN") {
context = body[i].value;
var childprops = [];
} else {
this.addProperty(new dojo.cal.iCalendar.Property(body[i]));
}
} else {
if (body[i].name == "END" && body[i].value == context) {
if (context == "VEVENT") {
this.addComponent(new dojo.cal.iCalendar.VEvent(childprops));
} else {
if (context == "VTIMEZONE") {
this.addComponent(new dojo.cal.iCalendar.VTimeZone(childprops));
} else {
if (context == "VTODO") {
this.addComponent(new dojo.cal.iCalendar.VTodo(childprops));
} else {
if (context == "VJOURNAL") {
this.addComponent(new dojo.cal.iCalendar.VJournal(childprops));
} else {
if (context == "VFREEBUSY") {
this.addComponent(new dojo.cal.iCalendar.VFreeBusy(childprops));
} else {
if (context == "STANDARD") {
this.addComponent(new dojo.cal.iCalendar.Standard(childprops));
} else {
if (context == "DAYLIGHT") {
this.addComponent(new dojo.cal.iCalendar.Daylight(childprops));
} else {
if (context == "VALARM") {
this.addComponent(new dojo.cal.iCalendar.VAlarm(childprops));
} else {
dojo.unimplemented("dojo.cal.iCalendar." + context);
}
}
}
}
}
}
}
}
context = "";
} else {
childprops.push(body[i]);
}
}
}
if (this._ValidProperties) {
this.postCreate();
}
}
};
dojo.extend(dojo.cal.iCalendar.Component, {addProperty:function (prop) {
this.properties.push(prop);
this[prop.name.toLowerCase()] = prop;
}, addComponent:function (prop) {
this.components.push(prop);
}, postCreate:function () {
for (var x = 0; x < this._ValidProperties.length; x++) {
var evtProperty = this._ValidProperties[x];
var found = false;
for (var y = 0; y < this.properties.length; y++) {
var prop = this.properties[y];
var propName = prop.name.toLowerCase();
if (dojo.lang.isArray(evtProperty)) {
var alreadySet = false;
for (var z = 0; z < evtProperty.length; z++) {
var evtPropertyName = evtProperty[z].name.toLowerCase();
if ((this[evtPropertyName]) && (evtPropertyName != propName)) {
alreadySet = true;
}
}
if (!alreadySet) {
this[propName] = prop;
}
} else {
if (propName == evtProperty.name.toLowerCase()) {
found = true;
if (evtProperty.occurance == 1) {
this[propName] = prop;
} else {
found = true;
if (!dojo.lang.isArray(this[propName])) {
this[propName] = [];
}
this[propName].push(prop);
}
}
}
}
if (evtProperty.required && !found) {
dojo.debug("iCalendar - " + this.name + ": Required Property not found: " + evtProperty.name);
}
}
if (dojo.lang.isArray(this.rrule)) {
for (var x = 0; x < this.rrule.length; x++) {
var rule = this.rrule[x].value;
this.rrule[x].cache = function () {
};
var temp = rule.split(";");
for (var y = 0; y < temp.length; y++) {
var pair = temp[y].split("=");
var key = pair[0].toLowerCase();
var val = pair[1];
if ((key == "freq") || (key == "interval") || (key == "until")) {
this.rrule[x][key] = val;
} else {
var valArray = val.split(",");
this.rrule[x][key] = valArray;
}
}
}
this.recurring = true;
}
}, toString:function () {
return "[iCalendar.Component; " + this.name + ", " + this.properties.length + " properties, " + this.components.length + " components]";
}});
dojo.cal.iCalendar.Property = function (prop) {
this.name = prop.name;
this.group = prop.group;
this.params = prop.params;
this.value = prop.value;
};
dojo.extend(dojo.cal.iCalendar.Property, {toString:function () {
return "[iCalenday.Property; " + this.name + ": " + this.value + "]";
}});
var _P = function (n, oc, req) {
return {name:n, required:(req) ? true : false, occurance:(oc == "*" || !oc) ? -1 : oc};
};
dojo.cal.iCalendar.VCalendar = function (calbody) {
this.name = "VCALENDAR";
this.recurring = [];
this.nonRecurringEvents = function () {
};
dojo.cal.iCalendar.Component.call(this, calbody);
};
dojo.inherits(dojo.cal.iCalendar.VCalendar, dojo.cal.iCalendar.Component);
dojo.extend(dojo.cal.iCalendar.VCalendar, {addComponent:function (prop) {
this.components.push(prop);
if (prop.name.toLowerCase() == "vevent") {
if (prop.rrule) {
this.recurring.push(prop);
} else {
var startDate = prop.getDate();
var month = startDate.getMonth() + 1;
var dateString = month + "-" + startDate.getDate() + "-" + startDate.getFullYear();
if (!dojo.lang.isArray(this[dateString])) {
this.nonRecurringEvents[dateString] = [];
}
this.nonRecurringEvents[dateString].push(prop);
}
}
}, preComputeRecurringEvents:function (until) {
var calculatedEvents = function () {
};
for (var x = 0; x < this.recurring.length; x++) {
var dates = this.recurring[x].getDates(until);
for (var y = 0; y < dates.length; y++) {
var month = dates[y].getMonth() + 1;
var dateStr = month + "-" + dates[y].getDate() + "-" + dates[y].getFullYear();
if (!dojo.lang.isArray(calculatedEvents[dateStr])) {
calculatedEvents[dateStr] = [];
}
if (!dojo.lang.inArray(calculatedEvents[dateStr], this.recurring[x])) {
calculatedEvents[dateStr].push(this.recurring[x]);
}
}
}
this.recurringEvents = calculatedEvents;
}, getEvents:function (date) {
var events = [];
var recur = [];
var nonRecur = [];
var month = date.getMonth() + 1;
var dateStr = month + "-" + date.getDate() + "-" + date.getFullYear();
if (dojo.lang.isArray(this.nonRecurringEvents[dateStr])) {
nonRecur = this.nonRecurringEvents[dateStr];
dojo.debug("Number of nonRecurring Events: " + nonRecur.length);
}
if (dojo.lang.isArray(this.recurringEvents[dateStr])) {
recur = this.recurringEvents[dateStr];
}
events = recur.concat(nonRecur);
if (events.length > 0) {
return events;
}
return null;
}});
var StandardProperties = [_P("dtstart", 1, true), _P("tzoffsetto", 1, true), _P("tzoffsetfrom", 1, true), _P("comment"), _P("rdate"), _P("rrule"), _P("tzname")];
dojo.cal.iCalendar.Standard = function (body) {
this.name = "STANDARD";
this._ValidProperties = StandardProperties;
dojo.cal.iCalendar.Component.call(this, body);
};
dojo.inherits(dojo.cal.iCalendar.Standard, dojo.cal.iCalendar.Component);
var DaylightProperties = [_P("dtstart", 1, true), _P("tzoffsetto", 1, true), _P("tzoffsetfrom", 1, true), _P("comment"), _P("rdate"), _P("rrule"), _P("tzname")];
dojo.cal.iCalendar.Daylight = function (body) {
this.name = "DAYLIGHT";
this._ValidProperties = DaylightProperties;
dojo.cal.iCalendar.Component.call(this, body);
};
dojo.inherits(dojo.cal.iCalendar.Daylight, dojo.cal.iCalendar.Component);
var VEventProperties = [_P("class", 1), _P("created", 1), _P("description", 1), _P("dtstart", 1), _P("geo", 1), _P("last-mod", 1), _P("location", 1), _P("organizer", 1), _P("priority", 1), _P("dtstamp", 1), _P("seq", 1), _P("status", 1), _P("summary", 1), _P("transp", 1), _P("uid", 1), _P("url", 1), _P("recurid", 1), [_P("dtend", 1), _P("duration", 1)], _P("attach"), _P("attendee"), _P("categories"), _P("comment"), _P("contact"), _P("exdate"), _P("exrule"), _P("rstatus"), _P("related"), _P("resources"), _P("rdate"), _P("rrule")];
dojo.cal.iCalendar.VEvent = function (body) {
this._ValidProperties = VEventProperties;
this.name = "VEVENT";
dojo.cal.iCalendar.Component.call(this, body);
this.recurring = false;
this.startDate = dojo.date.fromIso8601(this.dtstart.value);
};
dojo.inherits(dojo.cal.iCalendar.VEvent, dojo.cal.iCalendar.Component);
dojo.extend(dojo.cal.iCalendar.VEvent, {getDates:function (until) {
var dtstart = this.getDate();
var recurranceSet = [];
var weekdays = ["su", "mo", "tu", "we", "th", "fr", "sa"];
var order = {"daily":1, "weekly":2, "monthly":3, "yearly":4, "byday":1, "bymonthday":1, "byweekno":2, "bymonth":3, "byyearday":4};
for (var x = 0; x < this.rrule.length; x++) {
var rrule = this.rrule[x];
var freq = rrule.freq.toLowerCase();
var interval = 1;
if (rrule.interval > interval) {
interval = rrule.interval;
}
var set = [];
var freqInt = order[freq];
if (rrule.until) {
var tmpUntil = dojo.date.fromIso8601(rrule.until);
} else {
var tmpUntil = until;
}
if (tmpUntil > until) {
tmpUntil = until;
}
if (dtstart < tmpUntil) {
var expandingRules = function () {
};
var cullingRules = function () {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -