From: Aleksander Ɓukasz Date: Sun, 4 Aug 2013 20:14:40 +0000 (+0200) Subject: canvas.utils.nearestInDocumentOrder X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/2f7b77263681f9b09078a958b9bca816b7086094 canvas.utils.nearestInDocumentOrder --- 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'); + }); + }); + +}); + +});