From 2f7b77263681f9b09078a958b9bca816b7086094 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Sun, 4 Aug 2013 22:14:40 +0200 Subject: [PATCH 1/1] canvas.utils.nearestInDocumentOrder --- modules/documentCanvas/canvas/utils.js | 26 ++++++++ modules/documentCanvas/canvas/utils.test3.js | 70 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 modules/documentCanvas/canvas/utils.js create mode 100644 modules/documentCanvas/canvas/utils.test3.js diff --git a/modules/documentCanvas/canvas/utils.js b/modules/documentCanvas/canvas/utils.js new file mode 100644 index 0000000..829b466 --- /dev/null +++ b/modules/documentCanvas/canvas/utils.js @@ -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 index 0000000..c93e74b --- /dev/null +++ b/modules/documentCanvas/canvas/utils.test3.js @@ -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', + '\ + \ +
\ + \ + ' + ], + ['returns nearest sibling if applicable', + '
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
' + ], + ['looks inside siblings children', + '
\ +
\ +
\ +
\ + \ +
\ +
\ +
\ + \ +
\ +
\ +
\ +
\ +
\ +
' + ] + + + ]; + + 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'); + }); + }); + +}); + +}); -- 2.20.1