From 179ad93dc58a28f626d5668dcf69e8b8ebe31da6 Mon Sep 17 00:00:00 2001 From: Michael Sievers Date: Mon, 3 Feb 2014 10:38:29 +0100 Subject: [PATCH 1/4] Make ElementsParser a constructor function and create instances in fabric.parseElements --- src/elements_parser.js | 27 +++++++++++++-------------- src/parser.js | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/elements_parser.js b/src/elements_parser.js index 142ac7da..4ad7c800 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -1,19 +1,18 @@ -fabric.ElementsParser = { +fabric.ElementsParser = function(elements, callback, options, reviver) { + this.elements = elements; + this.callback = callback; + this.options = options; + this.reviver = reviver; - parse: function(elements, callback, options, reviver) { + this.parse = function() { - this.elements = elements; - this.callback = callback; - this.options = options; - this.reviver = reviver; - - this.instances = new Array(elements.length); - this.numElements = elements.length; + this.instances = new Array(this.elements.length); + this.numElements = this.elements.length; this.createObjects(); }, - createObjects: function() { + this.createObjects = function() { for (var i = 0, len = this.elements.length; i < len; i++) { (function(_this, i) { setTimeout(function() { @@ -23,7 +22,7 @@ fabric.ElementsParser = { } }, - createObject: function(el, index) { + this.createObject = function(el, index) { var klass = fabric[fabric.util.string.capitalize(el.tagName)]; if (klass && klass.fromElement) { try { @@ -38,7 +37,7 @@ fabric.ElementsParser = { } }, - _createObject: function(klass, el, index) { + this._createObject = function(klass, el, index) { if (klass.async) { klass.fromElement(el, this.createCallback(index, el), this.options); } @@ -50,7 +49,7 @@ fabric.ElementsParser = { } }, - createCallback: function(index, el) { + this.createCallback = function(index, el) { var _this = this; return function(obj) { _this.reviver && _this.reviver(el, obj); @@ -59,7 +58,7 @@ fabric.ElementsParser = { }; }, - checkIfDone: function() { + this.checkIfDone = function() { if (--this.numElements === 0) { this.instances = this.instances.filter(function(el) { return el != null; diff --git a/src/parser.js b/src/parser.js index 9516894c..0f177013 100644 --- a/src/parser.js +++ b/src/parser.js @@ -614,7 +614,7 @@ * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created. */ parseElements: function(elements, callback, options, reviver) { - fabric.ElementsParser.parse(elements, callback, options, reviver); + new fabric.ElementsParser(elements, callback, options, reviver).parse(); }, /** From d44bde46050867ed5905a2c6ec2dc842cff40188 Mon Sep 17 00:00:00 2001 From: Michael Sievers Date: Tue, 4 Feb 2014 13:15:53 +0100 Subject: [PATCH 2/4] Added semicolon to satisfy jshint --- src/elements_parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elements_parser.js b/src/elements_parser.js index 4ad7c800..6994b52a 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -66,5 +66,5 @@ fabric.ElementsParser = function(elements, callback, options, reviver) { fabric.resolveGradients(this.instances); this.callback(this.instances); } - } + }; }; From bd2a235b12cb664fbd83a45633d35e8ef2ba278a Mon Sep 17 00:00:00 2001 From: Michael Sievers Date: Wed, 12 Feb 2014 17:21:22 +0100 Subject: [PATCH 3/4] Move ElementsParser instance methods to prototype --- src/elements_parser.js | 111 ++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/src/elements_parser.js b/src/elements_parser.js index 6994b52a..4ae5891d 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -3,68 +3,67 @@ fabric.ElementsParser = function(elements, callback, options, reviver) { this.callback = callback; this.options = options; this.reviver = reviver; +}; - this.parse = function() { +fabric.ElementsParser.prototype.parse = function() { + this.instances = new Array(this.elements.length); + this.numElements = this.elements.length; - this.instances = new Array(this.elements.length); - this.numElements = this.elements.length; + this.createObjects(); +}; - this.createObjects(); - }, +fabric.ElementsParser.prototype.createObjects = function() { + for (var i = 0, len = this.elements.length; i < len; i++) { + (function(_this, i) { + setTimeout(function() { + _this.createObject(_this.elements[i], i); + }, 0); + })(this, i); + } +}, - this.createObjects = function() { - for (var i = 0, len = this.elements.length; i < len; i++) { - (function(_this, i) { - setTimeout(function() { - _this.createObject(_this.elements[i], i); - }, 0); - })(this, i); +fabric.ElementsParser.prototype.createObject = function(el, index) { + var klass = fabric[fabric.util.string.capitalize(el.tagName)]; + if (klass && klass.fromElement) { + try { + this._createObject(klass, el, index); } - }, + catch (err) { + fabric.log(err); + } + } + else { + this.checkIfDone(); + } +}; - this.createObject = function(el, index) { - var klass = fabric[fabric.util.string.capitalize(el.tagName)]; - if (klass && klass.fromElement) { - try { - this._createObject(klass, el, index); - } - catch (err) { - fabric.log(err); - } - } - else { - this.checkIfDone(); - } - }, +fabric.ElementsParser.prototype._createObject = function(klass, el, index) { + if (klass.async) { + klass.fromElement(el, this.createCallback(index, el), this.options); + } + else { + var obj = klass.fromElement(el, this.options); + this.reviver && this.reviver(el, obj); + this.instances.splice(index, 0, obj); + this.checkIfDone(); + } +}; - this._createObject = function(klass, el, index) { - if (klass.async) { - klass.fromElement(el, this.createCallback(index, el), this.options); - } - else { - var obj = klass.fromElement(el, this.options); - this.reviver && this.reviver(el, obj); - this.instances.splice(index, 0, obj); - this.checkIfDone(); - } - }, - - this.createCallback = function(index, el) { - var _this = this; - return function(obj) { - _this.reviver && _this.reviver(el, obj); - _this.instances.splice(index, 0, obj); - _this.checkIfDone(); - }; - }, - - this.checkIfDone = function() { - if (--this.numElements === 0) { - this.instances = this.instances.filter(function(el) { - return el != null; - }); - fabric.resolveGradients(this.instances); - this.callback(this.instances); - } +fabric.ElementsParser.prototype.createCallback = function(index, el) { + var _this = this; + return function(obj) { + _this.reviver && _this.reviver(el, obj); + _this.instances.splice(index, 0, obj); + _this.checkIfDone(); }; }; + +fabric.ElementsParser.prototype.checkIfDone = function() { + if (--this.numElements === 0) { + this.instances = this.instances.filter(function(el) { + return el != null; + }); + fabric.resolveGradients(this.instances); + this.callback(this.instances); + } +}; From 3faf5f0bae9a1c9fdc3397eccf002a1e26b83db2 Mon Sep 17 00:00:00 2001 From: Michael Sievers Date: Thu, 13 Feb 2014 09:10:35 +0100 Subject: [PATCH 4/4] Added missing semicolon --- src/elements_parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elements_parser.js b/src/elements_parser.js index 4ae5891d..46f67580 100644 --- a/src/elements_parser.js +++ b/src/elements_parser.js @@ -20,7 +20,7 @@ fabric.ElementsParser.prototype.createObjects = function() { }, 0); })(this, i); } -}, +}; fabric.ElementsParser.prototype.createObject = function(el, index) { var klass = fabric[fabric.util.string.capitalize(el.tagName)];