Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Jun 23, 2012
1 parent fe56b2a commit 17a9bdb
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 109 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function() {
'use strict';

module.exports = require('./lib/xmlhttprequest');
module.exports = require('./lib');
})();
14 changes: 14 additions & 0 deletions lib/anonxmlhttprequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function() {
'use strict';

var XMLHttpRequest = require('./xmlhttprequest');

function AnonXMLHttpRequest() {
if (!(this instanceof AnonXMLHttpRequest)) {
throw new TypeError('DOM object constructor cannot be called as a function');
}
XMLHttpRequest.call(this);
}
AnonXMLHttpRequest.prototype = Object.create(XMLHttpRequest.prototype);
module.exports = AnonXMLHttpRequest;
})();
30 changes: 30 additions & 0 deletions lib/eventtarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(function() {
'use strict';

function EventTarget() {
Object.defineProperty(this, '_eventListeners', {
value: {},
writable: true,
enumerable: false,
configurable: false
});
}
module.exports = EventTarget;

(function(proto) {
proto.addEventListener = function addEventListener(type, listener, useCapture) {
this['on' + type] = listener;
};

proto.dispatchEvent = function dispatchEvent(event) {
var _listener = this['on' + event.type];
if (typeof(_listener) === 'function') {
_listener.call(this, event);
}
};

proto.removeEventListener = function removeEventListener(type, listener, useCapture) {
// todo
};
})(EventTarget.prototype);
})();
16 changes: 16 additions & 0 deletions lib/formdata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function() {
'use strict';

function FormData() {
if (!(this instanceof FormData)) {
throw new TypeError('DOM object constructor cannot be called as a function.');
}
}
module.exports = FormData;

(function(proto) {
proto.append = function append(name, value, filename) {
// todo
};
})(FormData.prototype);
})();
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function() {
'use strict';

var XMLHttpRequestUpload = require('./xmlhttprequestupload');
var XMLHttpRequest = require('./xmlhttprequest');
var AnonXMLHttpRequest = require('./anonxmlhttprequest');
var FormData = require('./formdata');

module.exports = {
XMLHttpRequestUpload: XMLHttpRequestUpload,
XMLHttpRequest: XMLHttpRequest,
AnonXMLHttpRequest: AnonXMLHttpRequest,
FormData: FormData
};
})();
112 changes: 4 additions & 108 deletions lib/xmlhttprequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,9 @@
var http = require('http');
var https = require('https');
var urlparse = require('url').parse;

function EventTarget() {
Object.defineProperty(this, '_eventListeners', {
value: {},
writable: true,
enumerable: false,
configurable: false
});
}

(function(proto) {
proto.addEventListener = function addEventListener(type, listener, useCapture) {
this['on' + type] = listener;
};

proto.dispatchEvent = function dispatchEvent(event) {
var _listener = this['on' + event.type];
if (typeof(_listener) === 'function') {
_listener.call(this, event);
}
};

proto.removeEventListener = function removeEventListener(type, listener, useCapture) {
// todo
};
})(EventTarget.prototype);

function XMLHttpRequestEventTarget() {
EventTarget.call(this);
}
XMLHttpRequestEventTarget.prototype = Object.create(EventTarget.prototype);

(function(proto) {
// Event handler
(function(types) {
types.forEach(function(type) {
Object.defineProperty(proto, 'on' + type, {
get: function() {
var _listener = this._eventListeners[type];
if (typeof(_listener) !== 'function') {
return null;
}
return _listener;
},
set: function(listener) {
if (typeof(listener) !== 'function') {
delete this._eventListeners[type];
return listener;
}
return this._eventListeners[type] = listener;
},
enumerable: true,
configurable: false
});
});
})([
'loadstart',
'progress',
'abort',
'error', // todo
'load',
'timeout', // todo
'loadend'
]);
})(XMLHttpRequestEventTarget.prototype);

function XMLHttpRequestUpload() {
if (!(this instanceof XMLHttpRequestUpload)) {
throw new TypeError('DOM object constructor cannot be called as a function.');
}
XMLHttpRequestEventTarget.call(this);
}
XMLHttpRequestUpload.prototype = Object.create(XMLHttpRequestEventTarget.prototype);

var XMLHttpRequestResponseType = [
'',
'arraybuffer',
'blob',
'document',
'json',
'text'
];
var XMLHttpRequestEventTarget = require('./xmlhttprequesteventtarget');
var XMLHttpRequestUpload = require('./xmlhttprequestupload');
var XMLHttpRequestResponseType = require('./xmlhttprequestresponsetype');

function XMLHttpRequest() {
if (!(this instanceof XMLHttpRequest)) {
Expand Down Expand Up @@ -128,6 +49,7 @@
this.statusText = '';
}
XMLHttpRequest.prototype = Object.create(XMLHttpRequestEventTarget.prototype);
module.exports = XMLHttpRequest;

(function(proto) {
// Event handler
Expand Down Expand Up @@ -366,30 +288,4 @@
}.bind(this));
};
})(XMLHttpRequest.prototype);

function AnonXMLHttpRequest() {
if (!(this instanceof AnonXMLHttpRequest)) {
throw new TypeError('DOM object constructor cannot be called as a function');
}
XMLHttpRequest.call(this);
}
AnonXMLHttpRequest.prototype = Object.create(XMLHttpRequest.prototype);

function FormData() {
if (!(this instanceof FormData)) {
throw new TypeError('DOM object constructor cannot be called as a function.');
}
}

(function(proto) {
proto.append = function append(name, value, filename) {
// todo
};
})(FormData.prototype);

XMLHttpRequest.XMLHttpRequestUpload = XMLHttpRequestUpload;
XMLHttpRequest.XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest.AnonXMLHttpRequest = AnonXMLHttpRequest;
XMLHttpRequest.FormData = FormData;
module.exports = XMLHttpRequest;
})();
45 changes: 45 additions & 0 deletions lib/xmlhttprequesteventtarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(function() {
'use strict';

var EventTarget = require('./eventtarget');

function XMLHttpRequestEventTarget() {
EventTarget.call(this);
}
XMLHttpRequestEventTarget.prototype = Object.create(EventTarget.prototype);
module.exports = XMLHttpRequestEventTarget;

(function(proto) {
// Event handler
(function(types) {
types.forEach(function(type) {
Object.defineProperty(proto, 'on' + type, {
get: function() {
var _listener = this._eventListeners[type];
if (typeof(_listener) !== 'function') {
return null;
}
return _listener;
},
set: function(listener) {
if (typeof(listener) !== 'function') {
delete this._eventListeners[type];
return listener;
}
return this._eventListeners[type] = listener;
},
enumerable: true,
configurable: false
});
});
})([
'loadstart',
'progress',
'abort',
'error', // todo
'load',
'timeout', // todo
'loadend'
]);
})(XMLHttpRequestEventTarget.prototype);
})();
13 changes: 13 additions & 0 deletions lib/xmlhttprequestresponsetype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(function() {
'use strict';

var XMLHttpRequestResponseType = [
'',
'arraybuffer',
'blob',
'document',
'json',
'text'
];
module.exports = XMLHttpRequestResponseType;
})();
14 changes: 14 additions & 0 deletions lib/xmlhttprequestupload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function() {
'use strict';

var XMLHttpRequestEventTarget = require('./xmlhttprequesteventtarget');

function XMLHttpRequestUpload() {
if (!(this instanceof XMLHttpRequestUpload)) {
throw new TypeError('DOM object constructor cannot be called as a function.');
}
XMLHttpRequestEventTarget.call(this);
}
XMLHttpRequestUpload.prototype = Object.create(XMLHttpRequestEventTarget.prototype);
module.exports = XMLHttpRequestUpload;
})();

0 comments on commit 17a9bdb

Please sign in to comment.