diff options
author | James Lal <james@lightsofapollo.com> | 2012-07-06 08:13:15 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-07-06 08:13:15 -0700 |
commit | 20711f003e97a4a57d1e2838ba78a994c5faf6c0 (patch) | |
tree | b060e5de05a9ffefc2ec836469c087fffa99dcef /lib/caldav/request | |
parent | c8e85fe1e0a1d2e15df580068244324ff536a23a (diff) | |
download | jsCalDAV-20711f003e97a4a57d1e2838ba78a994c5faf6c0.tar.gz |
webcals -> caldav rename (yes, back again)
Diffstat (limited to 'lib/caldav/request')
-rw-r--r-- | lib/caldav/request/abstract.js | 92 | ||||
-rw-r--r-- | lib/caldav/request/calendar_query.js | 57 | ||||
-rw-r--r-- | lib/caldav/request/index.js | 14 | ||||
-rw-r--r-- | lib/caldav/request/propfind.js | 69 |
4 files changed, 232 insertions, 0 deletions
diff --git a/lib/caldav/request/abstract.js b/lib/caldav/request/abstract.js new file mode 100644 index 0000000..04d2128 --- /dev/null +++ b/lib/caldav/request/abstract.js @@ -0,0 +1,92 @@ +(function(module, ns) { + + var SAX = ns.require('sax'); + var XHR = ns.require('xhr'); + + + /** + * Creates an (Web/Cal)Dav request. + * + * @param {String} url location of resource. + * @param {Object} options additional options for request. + */ + function Abstract(url, options) { + if (typeof(options) === 'undefined') { + options = {}; + } + + var key; + var xhrOptions = {}; + + if (typeof(url) === 'undefined' || !url) { + throw new Error('request requires a url'); + } + + xhrOptions.url = url; + + if ('password' in options) { + xhrOptions.password = options.password; + delete options.password; + } + + if ('user' in options) { + xhrOptions.user = options.user; + delete options.user; + } + + this.sax = new SAX(); + + for (key in options) { + if (options.hasOwnProperty(key)) { + this[key] = options[key]; + } + } + + this.xhr = new XHR(xhrOptions); + } + + Abstract.prototype = { + + _createPayload: function() { + return ''; + }, + + _processResult: function(req, callback) { + callback.call(this, null, this.sax.root, req); + }, + + /** + * Sends request to server. + * + * @param {Function} callback node style callback. + * Receives three arguments + * error, parsedData, xhr. + */ + send: function(callback) { + var self = this; + var req = this.xhr; + req.data = this._createPayload(); + + // in the future we may stream data somehow + req.send(function xhrResult() { + var xhr = req.xhr; + if (xhr.status > 199 && xhr.status < 300) { + // success + self.sax.write(xhr.responseText).close(); + self._processResult(req, callback); + } else { + // fail + callback(new Error('http error code: ' + xhr.status)); + } + }); + } + }; + + module.exports = Abstract; + +}.apply( + this, + (this.Caldav) ? + [Caldav('request/abstract'), Caldav] : + [module, require('../caldav')] +)); diff --git a/lib/caldav/request/calendar_query.js b/lib/caldav/request/calendar_query.js new file mode 100644 index 0000000..7b64d3f --- /dev/null +++ b/lib/caldav/request/calendar_query.js @@ -0,0 +1,57 @@ +(function(module, ns) { + + var Propfind = ns.require('request/propfind'); + var CalendarData = ns.require('templates/calendar_data'); + var CalendarFilter = ns.require('templates/calendar_filter'); + + /** + * Creates a calendar query request. + * + * Defaults to Depth of 1. + * + * @param {String} url location to make request. + * @param {Object} options options for calendar query. + */ + function CalendarQuery(url, options) { + Propfind.apply(this, arguments); + + this.xhr.headers['Depth'] = this.depth || 1; + this.xhr.method = 'REPORT'; + this.fields = new CalendarData(); + this.filters = new CalendarFilter(); + + this.template.rootTag = ['caldav', 'calendar-query']; + } + + CalendarQuery.prototype = { + __proto__: Propfind.prototype, + + _createPayload: function() { + var content; + var props; + + props = this._props.join(''); + + if (this.fields) { + props += this.fields.render(this.template); + } + + content = this.template.tag('prop', props); + + if (this.filters) { + content += this.filters.render(this.template); + } + + return this.template.render(content); + } + + }; + + module.exports = CalendarQuery; + +}.apply( + this, + (this.Caldav) ? + [Caldav('request/calendar_query'), Caldav] : + [module, require('../caldav')] +)); diff --git a/lib/caldav/request/index.js b/lib/caldav/request/index.js new file mode 100644 index 0000000..fc49491 --- /dev/null +++ b/lib/caldav/request/index.js @@ -0,0 +1,14 @@ +(function(module, ns) { + + module.exports = { + Abstract: ns.require('request/abstract'), + CalendarQuery: ns.require('request/calendar_query'), + Propfind: ns.require('request/propfind') + }; + +}.apply( + this, + (this.Caldav) ? + [Caldav('request'), Caldav] : + [module, require('../caldav')] +)); diff --git a/lib/caldav/request/propfind.js b/lib/caldav/request/propfind.js new file mode 100644 index 0000000..7202ea5 --- /dev/null +++ b/lib/caldav/request/propfind.js @@ -0,0 +1,69 @@ +(function(module, ns) { + + var Abstract = ns.require('request/abstract'), + Template = ns.require('template'), + DavResponse = ns.require('sax/dav_response'); + + /** + * Creates a propfind request. + * + * @param {String} url location to make request. + * @param {Object} options options for propfind. + */ + function Propfind(url, options) { + Abstract.apply(this, arguments); + + this.template = new Template('propfind'); + this._props = []; + this.sax.registerHandler( + 'DAV:/response', + DavResponse + ); + + this.xhr.headers['Depth'] = this.depth; + this.xhr.method = 'PROPFIND'; + } + + Propfind.prototype = { + __proto__: Abstract.prototype, + + depth: 0, + + /** + * Adds property to request. + * + * @param {String|Array} tagDesc tag description. + * @param {Object} [attr] optional tag attrs. + * @param {Obj} [content] optional content. + */ + prop: function(tagDesc, attr, content) { + this._props.push(this.template.tag(tagDesc, attr, content)); + }, + + _createPayload: function() { + var content = this.template.tag('prop', this._props.join('')); + return this.template.render(content); + }, + + _processResult: function(req, callback) { + if ('multistatus' in this.sax.root) { + callback(null, this.sax.root.multistatus, req); + } else { + //XXX: Improve error handling + callback( + new Error('unexpected xml result'), + this.sax.root, req + ); + } + } + + }; + + module.exports = Propfind; + +}.apply( + this, + (this.Caldav) ? + [Caldav('request/propfind'), Caldav] : + [module, require('../caldav')] +)); |