From 073d1d1b61328bea3a238fdd90a9cb6501732030 Mon Sep 17 00:00:00 2001 From: Ole Petter Bang Date: Mon, 17 Sep 2012 18:41:27 +0200 Subject: [PATCH] #25: Add ready event. --- src/remark.js | 2 ++ src/remark/dom.js | 34 ++++++++++++++++++++++++++++------ test/remark/api_test.js | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 test/remark/api_test.js diff --git a/src/remark.js b/src/remark.js index bcdbc8d9..cbb98f3d 100644 --- a/src/remark.js +++ b/src/remark.js @@ -24,6 +24,8 @@ dom.on('load', function () { styleDocument(); setupSlideshow(sourceElement, slideshowElement); + + api.emit('ready'); }); function assureElementsExist (sourceElement, slideshowElement) { diff --git a/src/remark/dom.js b/src/remark/dom.js index 2d740f40..4f8c563e 100644 --- a/src/remark/dom.js +++ b/src/remark/dom.js @@ -24,13 +24,19 @@ proxyEvent(dom.document, 'mousewheel'); dom.on('load', updateDimensions); dom.on('resize', updateDimensions); -function proxyObject (object) { - if (typeof this[object] !== 'undefined') { - object = this[object]; +function proxyObject (objectName) { + var object; + + if (typeof this[objectName] !== 'undefined') { + object = this[objectName]; } else { object = new EventEmitter(); object.addEventListener = object.on; + + if (objectName === 'window') { + object.location = {}; + } } return object; @@ -41,17 +47,33 @@ function proxyFunction (element, func) { dom[func] = function () { return element[func].apply(element, arguments); }; } else { - if (func === 'createElement') { + if (['createElement', 'getElementById'].indexOf(func) !== -1) { dom[func] = function () { return { style: {} , appendChild: function () { } - , getElementsByTagName: function () { return []; } + , getElementsByTagName: function () { + return [{ + parentNode: { + nodeName: '' + } + , childNodes: [] + , className: '' + }]; + } + , innerHTML: '' }; }; } + else if (func === 'getElementsByTagName') { + dom[func] = function () { + return [{ + insertBefore: function () {} + }]; + }; + } else { - dom[func] = function () { return {}; }; + dom[func] = function () { return {}; }; } } } diff --git a/test/remark/api_test.js b/test/remark/api_test.js new file mode 100644 index 00000000..eb0c39e2 --- /dev/null +++ b/test/remark/api_test.js @@ -0,0 +1,23 @@ +var api = require('../../src/remark/api') + , dom = require('../../src/remark/dom') + ; + +describe('API', function () { + describe('when loading remark', function () { + before(function () { + var remark = require('../../src/remark.js'); + }); + + it('should be exposed', function () { + dom.exports.should.have.property('remark', api); + }); + + it('should trigger ready event', function (done) { + api.on('ready', function () { + done(); + }); + + dom.emit('load'); + }); + }); +});