aboutsummaryrefslogtreecommitdiffstats
path: root/lib/caldav
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-09-19 11:11:16 -0700
committerJames Lal <james@lightsofapollo.com>2012-09-19 11:11:16 -0700
commit0cac01e4243b112f3c0820c89d9de2bf38005c79 (patch)
tree1fc042f4d9b26f21f606e4dff15c7c70bab2f70b /lib/caldav
parent459e5b14f1868960e60c8fc358103cd6fbbaed7f (diff)
downloadjsCalDAV-0cac01e4243b112f3c0820c89d9de2bf38005c79.tar.gz
decouple ical-js will resolve #5
Diffstat (limited to 'lib/caldav')
-rw-r--r--lib/caldav/caldav.js14
-rw-r--r--lib/caldav/ical.js139
-rw-r--r--lib/caldav/index.js1
-rw-r--r--lib/caldav/sax/calendar_data_handler.js39
-rw-r--r--lib/caldav/sax/dav_response.js23
-rw-r--r--lib/caldav/sax/index.js5
6 files changed, 55 insertions, 166 deletions
diff --git a/lib/caldav/caldav.js b/lib/caldav/caldav.js
index ee1b92d..f3c6cad 100644
--- a/lib/caldav/caldav.js
+++ b/lib/caldav/caldav.js
@@ -20,6 +20,16 @@
Exports.prototype = {
/**
+ * Associate and register module.
+ *
+ * @param {String} path uri associated with module.
+ * @param {Object} object module object.
+ */
+ register: function(path, object) {
+ paths[path] = object;
+ },
+
+ /**
* Unified require between browser/node.
* Path is relative to this file so you
* will want to use it like this from any depth.
@@ -46,7 +56,7 @@
* Maps exports to a file path.
*/
set exports(val) {
- return paths[this.path] = val;
+ this.register(this.path, val);
},
get exports() {
@@ -69,11 +79,11 @@
return new Exports(path);
}
+ Module.register = Exports.prototype.register;
Module.require = Exports.prototype.require;
Module.exports = Module;
Module._paths = paths;
-
/**
* Reference self as exports
* which also happens to be the constructor
diff --git a/lib/caldav/ical.js b/lib/caldav/ical.js
deleted file mode 100644
index a7da546..0000000
--- a/lib/caldav/ical.js
+++ /dev/null
@@ -1,139 +0,0 @@
-(function(module, ns) {
- // Credit: Andreas Gal - I removed the callback / xhr logic
-
- // Iterate over all entries if x is an array, otherwise just call fn on x.
-
- /* Pattern for an individual entry: name:value */
- var ENTRY = /^([A-Za-z0-9-]+)((?:;[A-Za-z0-9-]+=(?:"[^"]+"|[^";:,]+)(?:,(?:"[^"]+"|[^";:,]+))*)*):(.*)$/;
-
- /* Pattern for an individual parameter: name=value[,value] */
- var PARAM = /;([A-Za-z0-9-]+)=((?:"[^"]+"|[^";:,]+)(?:,(?:"[^"]+"|[^";:,]+))*)/g;
-
- /* Pattern for an individual parameter value: value | "value" */
- var PARAM_VALUE = /,?("[^"]+"|[^";:,]+)/g;
-
- // Parse a calendar in iCal format.
- function ParseICal(text) {
- // Parse the text into an object graph
- var lines = text.replace(/\r/g, '').split('\n');
- var tos = Object.create(null);
- var stack = [tos];
-
- // Parse parameters for an entry. Foramt: <param>=<pvalue>[;...]
- function parseParams(params) {
- var map = Object.create(null);
- var param = PARAM.exec(params);
- while (param) {
- var values = [];
- var value = PARAM_VALUE.exec(param[2]);
- while (value) {
- values.push(value[1].replace(/^"(.*)"$/, '$1'));
- value = PARAM_VALUE.exec(param[2]);
- }
- map[param[1].toLowerCase()] = (values.length > 1 ? values : values[0]);
- param = PARAM.exec(params);
- }
- return map;
- }
-
- // Add a property to the current object. If a property with the same name
- // already exists, turn it into an array.
- function add(prop, value, params) {
- if (params)
- value = { parameters: parseParams(params), value: value };
- if (prop in tos) {
- var previous = tos[prop];
- if (previous instanceof Array) {
- previous.push(value);
- return;
- }
- value = [previous, value];
- }
- tos[prop] = value;
- }
-
- for (var n = 0; n < lines.length; ++n) {
- var line = lines[n];
- // check whether the line continues (next line stats with space or tab)
- var nextLine;
- while ((nextLine = lines[n+1]) && (nextLine[0] === ' ' || nextLine[0] === '\t')) {
- line += nextLine.substr(1);
- ++n;
- continue;
- }
- // parse the entry, format is 'PROPERTY:VALUE'
- var matches = ENTRY.exec(line);
-
- if (!matches) {
- throw new Error('invalid format');
- }
-
- var prop = matches[1].toLowerCase();
- var params = matches[2];
- var value = matches[3];
- switch (prop) {
- case 'begin':
- var obj = Object.create(null);
- add(value.toLowerCase(), obj);
- stack.push(tos = obj);
- break;
- case 'end':
- stack.pop();
- tos = stack[stack.length - 1];
- if (stack.length == 1) {
- var cal = stack[0];
- if (typeof cal.vcalendar !== 'object' || cal.vcalendar instanceof Array) {
- throw new Error('single vcalendar object expected');
- }
-
- return cal.vcalendar;
- }
- break;
- default:
- add(prop, value, params);
- break;
- }
- }
- throw new Error('unexpected end of file');
- }
-
- function Value(v) {
- return (typeof v !== 'object') ? v : v.value;
- }
-
- function Parameter(v, name) {
- if (typeof v !== 'object')
- return undefined;
- return v.parameters[name];
- }
-
- // Parse a time specification.
- function ParseDateTime(v) {
- var dt = Value(v);
- if (Parameter(v, 'VALUE') === 'DATE') {
- // 20081202
- return new Date(dt.substr(0, 4), dt.substr(4, 2), dt.substr(6, 2));
- }
- v = Value(v);
- // 20120426T130000Z
- var year = dt.substr(0, 4);
- var month = dt.substr(4, 2) - 1;
- var day = dt.substr(6, 2);
- var hour = dt.substr(9, 2);
- var min = dt.substr(11, 2);
- var sec = dt.substr(13, 2);
- if (dt[15] == 'Z') {
- return new Date(Date.UTC(year, month, day, hour, min, sec));
- }
- return new Date(year, month, day, hour, min, sec);
- }
-
- module.exports = ParseICal;
-
-}.apply(
- this,
- (this.Caldav) ?
- [Caldav('ical'), Caldav] :
- [module, require('./caldav')]
-));
-
diff --git a/lib/caldav/index.js b/lib/caldav/index.js
index e079028..4dd6852 100644
--- a/lib/caldav/index.js
+++ b/lib/caldav/index.js
@@ -2,7 +2,6 @@
var exports = module.exports;
- exports.Ical = ns.require('ical');
exports.Responder = ns.require('responder');
exports.Sax = ns.require('sax');
exports.Template = ns.require('template');
diff --git a/lib/caldav/sax/calendar_data_handler.js b/lib/caldav/sax/calendar_data_handler.js
new file mode 100644
index 0000000..d24bfb0
--- /dev/null
+++ b/lib/caldav/sax/calendar_data_handler.js
@@ -0,0 +1,39 @@
+(function(module, ns) {
+
+ var Base = ns.require('sax/base');
+
+ var CalendarDataHandler = Base.create({
+ name: 'calendar data',
+
+ //don't add text only elements
+ //to the stack as objects
+ onopentag: null,
+ onclosetag: null,
+
+ //add the value to the parent
+ //value where key is local tag name
+ //and value is the text.
+ ontext: function(data) {
+ var handler = this.handler;
+ this.current[this.currentTag[handler.tagField]] =
+ CalendarDataHandler.parseICAL(data);
+ }
+ });
+
+ /**
+ * Default ical parser handler.
+ *
+ * XXX: Feels a little hacky but works...
+ */
+ CalendarDataHandler.parseICAL = function(input) {
+ return input;
+ };
+
+ module.exports = CalendarDataHandler;
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('sax/calendar_data_handler'), Caldav] :
+ [module, require('../caldav')]
+));
diff --git a/lib/caldav/sax/dav_response.js b/lib/caldav/sax/dav_response.js
index 0957376..e257027 100644
--- a/lib/caldav/sax/dav_response.js
+++ b/lib/caldav/sax/dav_response.js
@@ -1,12 +1,9 @@
(function(module, ns) {
- if (typeof(ICAL) === 'undefined') {
- require('../../ical.js');
- }
-
var HTTP_STATUS = /([0-9]{3,3})/;
var Base = ns.require('sax/base');
+ var CalendarDataHandler = ns.require('sax/calendar_data_handler');
var TextHandler = Base.create({
name: 'text',
@@ -25,24 +22,6 @@
}
});
- var CalendarDataHandler = Base.create({
- name: 'calendar data',
-
- //don't add text only elements
- //to the stack as objects
- onopentag: null,
- onclosetag: null,
-
- //add the value to the parent
- //value where key is local tag name
- //and value is the text.
- ontext: function(data) {
- var handler = this.handler;
- this.current[this.currentTag[handler.tagField]] = ICAL.parse(data);
- }
- });
-
-
var HrefHandler = Base.create({
name: 'href',
diff --git a/lib/caldav/sax/index.js b/lib/caldav/sax/index.js
index 2ba2f16..3e5ea99 100644
--- a/lib/caldav/sax/index.js
+++ b/lib/caldav/sax/index.js
@@ -1,8 +1,9 @@
(function(module, ns) {
module.exports = {
- Abstract: ns.require('sax/abstract'),
- CalendarQuery: ns.require('sax/dav_response')
+ Base: ns.require('sax/base'),
+ CalendarDataHandler: ns.require('sax/calendar_data_handler'),
+ DavResponse: ns.require('sax/dav_response')
};
}.apply(