123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- describe('options - orientation', function () {
- const getIntSize = px => Math.round(parseFloat(px));
- const getTopPos = (el, wrap) => el.offsetTop + (wrap ? wrap.offsetTop : 0) + window.scrollY;
- const getLeftPos = (el, wrap) => el.offsetLeft + (wrap ? wrap.offsetLeft : 0) + window.scrollX;
- const getBottomPos = (el, wrap) => getTopPos(el, wrap) + el.offsetHeight;
- const getRightPos = (el, wrap) => getLeftPos(el, wrap) + el.offsetWidth;
- let wrapper;
- let input;
- beforeEach(function () {
- wrapper = document.createElement('div');
- Object.assign(wrapper.style, {
- boxSizing: 'border-box',
- position: 'fixed',
- top: '50px',
- left: '50px',
- width: '300px',
- paddingTop: '300px',
- });
- input = document.createElement('input');
- wrapper.appendChild(input);
- testContainer.appendChild(wrapper);
- });
- afterEach(function () {
- domUtils.emptyChildNodes(testContainer);
- });
- it('"auto" makes the picker show on top left of the input by default', function () {
- const {dp, picker} = createDP(input);
- dp.show();
- expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
- expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"auto" makes the picker show on top right of the input if computed style of the input has direction: rrl', function () {
- const {dp, picker} = createDP(input);
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
- expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"auto" makes the picker show on bottom of the input if visible space above the input < picker height', function () {
- const {dp, picker} = createDP(input);
- wrapper.style.paddingTop = '0';
- dp.show();
- expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
- expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
- expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"auto" makes the picker move to 10px from document\'s left if picker\'s left < document\'s', function () {
- const {dp, picker} = createDP(input);
- wrapper.style.left = '-40px';
- dp.show();
- expect(getIntSize(picker.style.left), 'to be', 10);
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"auto" makes the picker show on right if picker\'s right edge > document\'s', function () {
- const {dp, picker} = createDP(input);
- Object.assign(wrapper.style, {left: 'auto', right: 0, width: '150px'});
- dp.show();
- expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"top" makes the picker show on top of the input regardless of the size of the space above', function () {
- const {dp, picker} = createDP(input, {orientation: 'top'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.style.paddingTop = '0';
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"bottom" makes the picker show on bottom of the input regardless of the size of the space below', function () {
- wrapper.style.paddingTop = '0';
- const {dp, picker} = createDP(input, {orientation: 'bottom'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"left" makes the picker show on left of the input regardless of the direction of the input', function () {
- const {dp, picker} = createDP(input, {orientation: 'left'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"right" makes the picker show on right of the input regardless of the direction of the input', function () {
- const {dp, picker} = createDP(input, {orientation: 'right'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"top left" makes the picker always show on top left of the input', function () {
- wrapper.style.paddingTop = '0';
- const {dp, picker} = createDP(input, {orientation: 'top left'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: '0', bottom: ''});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"top right" makes the picker always show on top right of the input', function () {
- wrapper.style.paddingTop = '0';
- const {dp, picker} = createDP(input, {orientation: 'top right'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: '0', bottom: ''});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('"bottom left" makes the picker always show on bottom left of the input', function () {
- wrapper.style.paddingTop = '0';
- const {dp, picker} = createDP(input, {orientation: 'bottom left'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: '0', bottom: ''});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"bottom right" makes the picker always show on bottom right of the input', function () {
- wrapper.style.paddingTop = '0';
- const {dp, picker} = createDP(input, {orientation: 'bottom right'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- wrapper.setAttribute('dir', 'rtl');
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- Object.assign(wrapper.style, {top: '0', bottom: ''});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- it('can be updated with setOptions()', function () {
- const {dp, picker} = createDP(input);
- dp.setOptions({orientation: 'right bottom'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- dp.setOptions({orientation: 'bottom auto'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- dp.setOptions({orientation: 'auto right'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.hide();
- dp.setOptions({orientation: 'auto'});
- dp.show();
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- describe('with custom container', function () {
- let foo;
- beforeEach(function () {
- foo = parseHTML('<div id="foo"></div>').firstChild;
- Object.assign(foo.style, {
- boxSizing: 'border-box',
- position: 'fixed',
- top: '20px',
- left: '20px',
- height: '360px',
- overflow: 'auto',
- });
- Object.assign(wrapper.style, {position: '', top: '', left: ''});
- testContainer.replaceChild(foo, wrapper);
- foo.appendChild(wrapper);
- });
- it('makes the picker\'s position relative to the container', function () {
- const {dp, picker} = createDP(input, {container: '#foo'});
- dp.show();
- expect(getIntSize(picker.style.top), 'to be', input.offsetTop - picker.offsetHeight);
- expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
- expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.hide();
- wrapper.style.paddingBottom = '200px';
- foo.scrollTop = 100;
- dp.show();
- expect(getIntSize(picker.style.top), 'to be', input.offsetTop + input.offsetHeight);
- expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
- expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- foo.scrollTop = 0;
- wrapper.style.paddingBottom = '';
- dp.destroy();
- });
- it('"auto" makes the picker move to 10px from container\'s left if picker\'s left < container\'s', function () {
- const {dp, picker} = createDP(input, {container: '#foo'});
- wrapper.style.marginLeft = '-40px';
- dp.show();
- expect(getIntSize(picker.style.left), 'to be', 10);
- expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
- dp.destroy();
- });
- it('"auto" makes the picker show on right if picker\'s right edge < container\'s', function () {
- const {dp, picker} = createDP(input, {container: '#foo'});
- wrapper.style.width = '150px';
- dp.show();
- expect(getIntSize(picker.style.left), 'to be', input.offsetLeft + input.offsetWidth - picker.offsetWidth);
- expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
- dp.destroy();
- });
- });
- });
|