canvas.utils.nearestInDocumentOrder
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 4 Aug 2013 20:14:40 +0000 (22:14 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 4 Aug 2013 20:14:40 +0000 (22:14 +0200)
modules/documentCanvas/canvas/utils.js [new file with mode: 0644]
modules/documentCanvas/canvas/utils.test3.js [new file with mode: 0644]

diff --git a/modules/documentCanvas/canvas/utils.js b/modules/documentCanvas/canvas/utils.js
new file mode 100644 (file)
index 0000000..829b466
--- /dev/null
@@ -0,0 +1,26 @@
+define([
+'libs/jquery-1.9.1.min',
+], function($) {
+    
+'use strict';
+
+
+var nearestInDocumentOrder = function(selector, direction, element) {
+    var parents = $(element).parents(),
+        parent = parents.length ? $(parents[parents.length-1]) : element;
+
+    var adj = parent.find(selector).filter(function() {
+        return this.compareDocumentPosition(element) & (direction === 'above' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING);
+    });
+
+    if(adj.length) {
+        return adj[direction === 'above' ? adj.length-1 : 0];
+    }
+    return null;
+}
+
+return {
+    nearestInDocumentOrder: nearestInDocumentOrder
+};
+
+});
diff --git a/modules/documentCanvas/canvas/utils.test3.js b/modules/documentCanvas/canvas/utils.test3.js
new file mode 100644 (file)
index 0000000..c93e74b
--- /dev/null
@@ -0,0 +1,70 @@
+define([
+'libs/chai',
+'modules/documentCanvas/canvas/utils'
+
+], function(chai, utils) {
+
+'use strict';
+
+var expect = chai.expect;
+
+
+describe('utils.nearestInDocumentOrder', function() {
+
+
+    var tests = [
+        ['return null if no match found',
+            '<span>\
+                <span></span>\
+                <div id="b">\
+                <span></span>\
+            </span>'
+        ],
+        ['returns nearest sibling if applicable',
+            '<div n1>\
+                <div n2></div>\
+                <div n3>\
+                    <div id="a"></div>\
+                    <div id="b"></div>\
+                    <div id="c"></div>\
+                </div>\
+                <div n4></div>\
+            </div>'
+        ],
+        ['looks inside siblings children',
+            '<div>\
+                <div></div>\
+                <div>\
+                    <div></div>\
+                    <span>\
+                        <div id="a"></div>\
+                    </span>\
+                    <div id="b"></div>\
+                    <span>\
+                        <div id="c"></div>\
+                    </span>\
+                    <div></div>\
+                </div>\
+                <div></div>\
+            </div>'
+        ]
+
+
+    ];
+
+    tests.forEach(function(test) {
+        var description = test[0],
+            html = test[1];
+        it(description, function() {
+            var dom = $(html),
+                a = dom.find('#a').length ? dom.find('#a')[0] : null,
+                b = dom.find('#b')[0],
+                c = dom.find('#c').length ? dom.find('#c')[0] : null;
+            expect(utils.nearestInDocumentOrder('div', 'above', b)).to.equal(a, 'above');
+            expect(utils.nearestInDocumentOrder('div', 'below', b)).to.equal(c, 'below');
+        });
+    });
+
+});
+
+});