orientation.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. describe('options - orientation', function () {
  2. const getIntSize = px => Math.round(parseFloat(px));
  3. const getTopPos = (el, wrap) => el.offsetTop + (wrap ? wrap.offsetTop : 0) + window.scrollY;
  4. const getLeftPos = (el, wrap) => el.offsetLeft + (wrap ? wrap.offsetLeft : 0) + window.scrollX;
  5. const getBottomPos = (el, wrap) => getTopPos(el, wrap) + el.offsetHeight;
  6. const getRightPos = (el, wrap) => getLeftPos(el, wrap) + el.offsetWidth;
  7. let wrapper;
  8. let input;
  9. beforeEach(function () {
  10. wrapper = document.createElement('div');
  11. Object.assign(wrapper.style, {
  12. boxSizing: 'border-box',
  13. position: 'fixed',
  14. top: '50px',
  15. left: '50px',
  16. width: '300px',
  17. paddingTop: '300px',
  18. });
  19. input = document.createElement('input');
  20. wrapper.appendChild(input);
  21. testContainer.appendChild(wrapper);
  22. });
  23. afterEach(function () {
  24. domUtils.emptyChildNodes(testContainer);
  25. });
  26. it('"auto" makes the picker show on top left of the input by default', function () {
  27. const {dp, picker} = createDP(input);
  28. dp.show();
  29. expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
  30. expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
  31. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  32. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  33. dp.destroy();
  34. });
  35. it('"auto" makes the picker show on top right of the input if computed style of the input has direction: rrl', function () {
  36. const {dp, picker} = createDP(input);
  37. wrapper.setAttribute('dir', 'rtl');
  38. dp.show();
  39. expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
  40. expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
  41. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  42. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  43. dp.destroy();
  44. });
  45. it('"auto" makes the picker show on bottom of the input if visible space above the input < picker height', function () {
  46. const {dp, picker} = createDP(input);
  47. wrapper.style.paddingTop = '0';
  48. dp.show();
  49. expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
  50. expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
  51. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  52. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  53. dp.hide();
  54. wrapper.setAttribute('dir', 'rtl');
  55. dp.show();
  56. expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
  57. expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
  58. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  59. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  60. dp.destroy();
  61. });
  62. it('"auto" makes the picker move to 10px from document\'s left if picker\'s left < document\'s', function () {
  63. const {dp, picker} = createDP(input);
  64. wrapper.style.left = '-40px';
  65. dp.show();
  66. expect(getIntSize(picker.style.left), 'to be', 10);
  67. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  68. dp.destroy();
  69. });
  70. it('"auto" makes the picker show on right if picker\'s right edge > document\'s', function () {
  71. const {dp, picker} = createDP(input);
  72. Object.assign(wrapper.style, {left: 'auto', right: 0, width: '150px'});
  73. dp.show();
  74. expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
  75. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  76. dp.destroy();
  77. });
  78. it('"top" makes the picker show on top of the input regardless of the size of the space above', function () {
  79. const {dp, picker} = createDP(input, {orientation: 'top'});
  80. dp.show();
  81. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  82. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  83. dp.hide();
  84. wrapper.style.paddingTop = '0';
  85. dp.show();
  86. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  87. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  88. dp.hide();
  89. wrapper.setAttribute('dir', 'rtl');
  90. dp.show();
  91. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  92. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  93. dp.destroy();
  94. });
  95. it('"bottom" makes the picker show on bottom of the input regardless of the size of the space below', function () {
  96. wrapper.style.paddingTop = '0';
  97. const {dp, picker} = createDP(input, {orientation: 'bottom'});
  98. dp.show();
  99. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  100. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  101. dp.hide();
  102. Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
  103. dp.show();
  104. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  105. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  106. dp.hide();
  107. wrapper.setAttribute('dir', 'rtl');
  108. dp.show();
  109. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  110. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  111. dp.destroy();
  112. });
  113. it('"left" makes the picker show on left of the input regardless of the direction of the input', function () {
  114. const {dp, picker} = createDP(input, {orientation: 'left'});
  115. dp.show();
  116. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  117. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  118. dp.hide();
  119. wrapper.setAttribute('dir', 'rtl');
  120. dp.show();
  121. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  122. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  123. dp.destroy();
  124. });
  125. it('"right" makes the picker show on right of the input regardless of the direction of the input', function () {
  126. const {dp, picker} = createDP(input, {orientation: 'right'});
  127. dp.show();
  128. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  129. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  130. dp.hide();
  131. wrapper.setAttribute('dir', 'rtl');
  132. dp.show();
  133. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  134. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  135. dp.destroy();
  136. });
  137. it('"top left" makes the picker always show on top left of the input', function () {
  138. wrapper.style.paddingTop = '0';
  139. const {dp, picker} = createDP(input, {orientation: 'top left'});
  140. dp.show();
  141. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  142. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  143. dp.hide();
  144. Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
  145. dp.show();
  146. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  147. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  148. dp.hide();
  149. wrapper.setAttribute('dir', 'rtl');
  150. dp.show();
  151. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  152. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  153. dp.hide();
  154. Object.assign(wrapper.style, {top: '0', bottom: ''});
  155. dp.show();
  156. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  157. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  158. dp.destroy();
  159. });
  160. it('"top right" makes the picker always show on top right of the input', function () {
  161. wrapper.style.paddingTop = '0';
  162. const {dp, picker} = createDP(input, {orientation: 'top right'});
  163. dp.show();
  164. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  165. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  166. dp.hide();
  167. Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
  168. dp.show();
  169. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  170. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  171. dp.hide();
  172. wrapper.setAttribute('dir', 'rtl');
  173. dp.show();
  174. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  175. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  176. dp.hide();
  177. Object.assign(wrapper.style, {top: '0', bottom: ''});
  178. dp.show();
  179. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  180. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  181. dp.destroy();
  182. });
  183. it('"bottom left" makes the picker always show on bottom left of the input', function () {
  184. wrapper.style.paddingTop = '0';
  185. const {dp, picker} = createDP(input, {orientation: 'bottom left'});
  186. dp.show();
  187. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  188. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  189. dp.hide();
  190. Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
  191. dp.show();
  192. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  193. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  194. dp.hide();
  195. wrapper.setAttribute('dir', 'rtl');
  196. dp.show();
  197. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  198. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  199. dp.hide();
  200. Object.assign(wrapper.style, {top: '0', bottom: ''});
  201. dp.show();
  202. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  203. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  204. dp.destroy();
  205. });
  206. it('"bottom right" makes the picker always show on bottom right of the input', function () {
  207. wrapper.style.paddingTop = '0';
  208. const {dp, picker} = createDP(input, {orientation: 'bottom right'});
  209. dp.show();
  210. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  211. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  212. dp.hide();
  213. Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
  214. dp.show();
  215. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  216. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  217. dp.hide();
  218. wrapper.setAttribute('dir', 'rtl');
  219. dp.show();
  220. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  221. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  222. dp.hide();
  223. Object.assign(wrapper.style, {top: '0', bottom: ''});
  224. dp.show();
  225. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  226. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  227. dp.destroy();
  228. });
  229. it('can be updated with setOptions()', function () {
  230. const {dp, picker} = createDP(input);
  231. dp.setOptions({orientation: 'right bottom'});
  232. dp.show();
  233. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  234. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  235. dp.hide();
  236. dp.setOptions({orientation: 'bottom auto'});
  237. dp.show();
  238. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  239. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  240. dp.hide();
  241. dp.setOptions({orientation: 'auto right'});
  242. dp.show();
  243. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  244. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  245. dp.hide();
  246. dp.setOptions({orientation: 'auto'});
  247. dp.show();
  248. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  249. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  250. dp.destroy();
  251. });
  252. describe('with custom container', function () {
  253. let foo;
  254. beforeEach(function () {
  255. foo = parseHTML('<div id="foo"></div>').firstChild;
  256. Object.assign(foo.style, {
  257. boxSizing: 'border-box',
  258. position: 'fixed',
  259. top: '20px',
  260. left: '20px',
  261. height: '360px',
  262. overflow: 'auto',
  263. });
  264. Object.assign(wrapper.style, {position: '', top: '', left: ''});
  265. testContainer.replaceChild(foo, wrapper);
  266. foo.appendChild(wrapper);
  267. });
  268. it('makes the picker\'s position relative to the container', function () {
  269. const {dp, picker} = createDP(input, {container: '#foo'});
  270. dp.show();
  271. expect(getIntSize(picker.style.top), 'to be', input.offsetTop - picker.offsetHeight);
  272. expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
  273. expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
  274. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  275. dp.hide();
  276. wrapper.style.paddingBottom = '200px';
  277. foo.scrollTop = 100;
  278. dp.show();
  279. expect(getIntSize(picker.style.top), 'to be', input.offsetTop + input.offsetHeight);
  280. expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
  281. expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
  282. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  283. foo.scrollTop = 0;
  284. wrapper.style.paddingBottom = '';
  285. dp.destroy();
  286. });
  287. it('"auto" makes the picker move to 10px from container\'s left if picker\'s left < container\'s', function () {
  288. const {dp, picker} = createDP(input, {container: '#foo'});
  289. wrapper.style.marginLeft = '-40px';
  290. dp.show();
  291. expect(getIntSize(picker.style.left), 'to be', 10);
  292. expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
  293. dp.destroy();
  294. });
  295. it('"auto" makes the picker show on right if picker\'s right edge < container\'s', function () {
  296. const {dp, picker} = createDP(input, {container: '#foo'});
  297. wrapper.style.width = '150px';
  298. dp.show();
  299. expect(getIntSize(picker.style.left), 'to be', input.offsetLeft + input.offsetWidth - picker.offsetWidth);
  300. expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
  301. dp.destroy();
  302. });
  303. });
  304. });