ui_utils_spec.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _ui_utils = require("../../web/ui_utils");
  24. var _util = require("../../shared/util");
  25. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  26. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  27. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  28. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  29. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  30. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  31. describe('ui_utils', function () {
  32. describe('binary search', function () {
  33. function isTrue(_boolean) {
  34. return _boolean;
  35. }
  36. function isGreater3(number) {
  37. return number > 3;
  38. }
  39. it('empty array', function () {
  40. expect((0, _ui_utils.binarySearchFirstItem)([], isTrue)).toEqual(0);
  41. });
  42. it('single boolean entry', function () {
  43. expect((0, _ui_utils.binarySearchFirstItem)([false], isTrue)).toEqual(1);
  44. expect((0, _ui_utils.binarySearchFirstItem)([true], isTrue)).toEqual(0);
  45. });
  46. it('three boolean entries', function () {
  47. expect((0, _ui_utils.binarySearchFirstItem)([true, true, true], isTrue)).toEqual(0);
  48. expect((0, _ui_utils.binarySearchFirstItem)([false, true, true], isTrue)).toEqual(1);
  49. expect((0, _ui_utils.binarySearchFirstItem)([false, false, true], isTrue)).toEqual(2);
  50. expect((0, _ui_utils.binarySearchFirstItem)([false, false, false], isTrue)).toEqual(3);
  51. });
  52. it('three numeric entries', function () {
  53. expect((0, _ui_utils.binarySearchFirstItem)([0, 1, 2], isGreater3)).toEqual(3);
  54. expect((0, _ui_utils.binarySearchFirstItem)([2, 3, 4], isGreater3)).toEqual(2);
  55. expect((0, _ui_utils.binarySearchFirstItem)([4, 5, 6], isGreater3)).toEqual(0);
  56. });
  57. });
  58. describe('getPDFFileNameFromURL', function () {
  59. it('gets PDF filename', function () {
  60. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/file1.pdf')).toEqual('file1.pdf');
  61. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/file2.pdf')).toEqual('file2.pdf');
  62. });
  63. it('gets fallback filename', function () {
  64. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/file1.txt')).toEqual('document.pdf');
  65. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/file2.txt')).toEqual('document.pdf');
  66. });
  67. it('gets custom fallback filename', function () {
  68. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/file1.txt', 'qwerty1.pdf')).toEqual('qwerty1.pdf');
  69. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/file2.txt', 'qwerty2.pdf')).toEqual('qwerty2.pdf');
  70. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/file3.txt', '')).toEqual('');
  71. });
  72. it('gets fallback filename when url is not a string', function () {
  73. expect((0, _ui_utils.getPDFFileNameFromURL)(null)).toEqual('document.pdf');
  74. expect((0, _ui_utils.getPDFFileNameFromURL)(null, 'file.pdf')).toEqual('file.pdf');
  75. });
  76. it('gets PDF filename from URL containing leading/trailing whitespace', function () {
  77. expect((0, _ui_utils.getPDFFileNameFromURL)(' /pdfs/file1.pdf ')).toEqual('file1.pdf');
  78. expect((0, _ui_utils.getPDFFileNameFromURL)(' http://www.example.com/pdfs/file2.pdf ')).toEqual('file2.pdf');
  79. });
  80. it('gets PDF filename from query string', function () {
  81. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/pdfs.html?name=file1.pdf')).toEqual('file1.pdf');
  82. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/pdf.html?file2.pdf')).toEqual('file2.pdf');
  83. });
  84. it('gets PDF filename from hash string', function () {
  85. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/pdfs.html#name=file1.pdf')).toEqual('file1.pdf');
  86. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/pdf.html#file2.pdf')).toEqual('file2.pdf');
  87. });
  88. it('gets correct PDF filename when multiple ones are present', function () {
  89. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/file1.pdf?name=file.pdf')).toEqual('file1.pdf');
  90. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/pdfs/file2.pdf#file.pdf')).toEqual('file2.pdf');
  91. });
  92. it('gets PDF filename from URI-encoded data', function () {
  93. var encodedUrl = encodeURIComponent('http://www.example.com/pdfs/file1.pdf');
  94. expect((0, _ui_utils.getPDFFileNameFromURL)(encodedUrl)).toEqual('file1.pdf');
  95. var encodedUrlWithQuery = encodeURIComponent('http://www.example.com/pdfs/file.txt?file2.pdf');
  96. expect((0, _ui_utils.getPDFFileNameFromURL)(encodedUrlWithQuery)).toEqual('file2.pdf');
  97. });
  98. it('gets PDF filename from data mistaken for URI-encoded', function () {
  99. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/%AA.pdf')).toEqual('%AA.pdf');
  100. expect((0, _ui_utils.getPDFFileNameFromURL)('/pdfs/%2F.pdf')).toEqual('%2F.pdf');
  101. });
  102. it('gets PDF filename from (some) standard protocols', function () {
  103. expect((0, _ui_utils.getPDFFileNameFromURL)('http://www.example.com/file1.pdf')).toEqual('file1.pdf');
  104. expect((0, _ui_utils.getPDFFileNameFromURL)('https://www.example.com/file2.pdf')).toEqual('file2.pdf');
  105. expect((0, _ui_utils.getPDFFileNameFromURL)('file:///path/to/files/file3.pdf')).toEqual('file3.pdf');
  106. expect((0, _ui_utils.getPDFFileNameFromURL)('ftp://www.example.com/file4.pdf')).toEqual('file4.pdf');
  107. });
  108. it('gets PDF filename from query string appended to "blob:" URL', function () {
  109. if ((0, _is_node["default"])()) {
  110. pending('Blob in not supported in Node.js.');
  111. }
  112. var typedArray = new Uint8Array([1, 2, 3, 4, 5]);
  113. var blobUrl = (0, _util.createObjectURL)(typedArray, 'application/pdf');
  114. expect(blobUrl.startsWith('blob:')).toEqual(true);
  115. expect((0, _ui_utils.getPDFFileNameFromURL)(blobUrl + '?file.pdf')).toEqual('file.pdf');
  116. });
  117. it('gets fallback filename from query string appended to "data:" URL', function () {
  118. var typedArray = new Uint8Array([1, 2, 3, 4, 5]);
  119. var dataUrl = (0, _util.createObjectURL)(typedArray, 'application/pdf', true);
  120. expect(dataUrl.startsWith('data:')).toEqual(true);
  121. expect((0, _ui_utils.getPDFFileNameFromURL)(dataUrl + '?file1.pdf')).toEqual('document.pdf');
  122. expect((0, _ui_utils.getPDFFileNameFromURL)(' ' + dataUrl + '?file2.pdf')).toEqual('document.pdf');
  123. });
  124. });
  125. describe('EventBus', function () {
  126. it('dispatch event', function () {
  127. var eventBus = new _ui_utils.EventBus();
  128. var count = 0;
  129. eventBus.on('test', function (evt) {
  130. expect(evt).toEqual(undefined);
  131. count++;
  132. });
  133. eventBus.dispatch('test');
  134. expect(count).toEqual(1);
  135. });
  136. it('dispatch event with arguments', function () {
  137. var eventBus = new _ui_utils.EventBus();
  138. var count = 0;
  139. eventBus.on('test', function (evt) {
  140. expect(evt).toEqual({
  141. abc: 123
  142. });
  143. count++;
  144. });
  145. eventBus.dispatch('test', {
  146. abc: 123
  147. });
  148. expect(count).toEqual(1);
  149. });
  150. it('dispatch different event', function () {
  151. var eventBus = new _ui_utils.EventBus();
  152. var count = 0;
  153. eventBus.on('test', function () {
  154. count++;
  155. });
  156. eventBus.dispatch('nottest');
  157. expect(count).toEqual(0);
  158. });
  159. it('dispatch event multiple times', function () {
  160. var eventBus = new _ui_utils.EventBus();
  161. var count = 0;
  162. eventBus.dispatch('test');
  163. eventBus.on('test', function () {
  164. count++;
  165. });
  166. eventBus.dispatch('test');
  167. eventBus.dispatch('test');
  168. expect(count).toEqual(2);
  169. });
  170. it('dispatch event to multiple handlers', function () {
  171. var eventBus = new _ui_utils.EventBus();
  172. var count = 0;
  173. eventBus.on('test', function () {
  174. count++;
  175. });
  176. eventBus.on('test', function () {
  177. count++;
  178. });
  179. eventBus.dispatch('test');
  180. expect(count).toEqual(2);
  181. });
  182. it('dispatch to detached', function () {
  183. var eventBus = new _ui_utils.EventBus();
  184. var count = 0;
  185. var listener = function listener() {
  186. count++;
  187. };
  188. eventBus.on('test', listener);
  189. eventBus.dispatch('test');
  190. eventBus.off('test', listener);
  191. eventBus.dispatch('test');
  192. expect(count).toEqual(1);
  193. });
  194. it('dispatch to wrong detached', function () {
  195. var eventBus = new _ui_utils.EventBus();
  196. var count = 0;
  197. eventBus.on('test', function () {
  198. count++;
  199. });
  200. eventBus.dispatch('test');
  201. eventBus.off('test', function () {
  202. count++;
  203. });
  204. eventBus.dispatch('test');
  205. expect(count).toEqual(2);
  206. });
  207. it('dispatch to detached during handling', function () {
  208. var eventBus = new _ui_utils.EventBus();
  209. var count = 0;
  210. var listener1 = function listener1() {
  211. eventBus.off('test', listener2);
  212. count++;
  213. };
  214. var listener2 = function listener2() {
  215. eventBus.off('test', listener1);
  216. count++;
  217. };
  218. eventBus.on('test', listener1);
  219. eventBus.on('test', listener2);
  220. eventBus.dispatch('test');
  221. eventBus.dispatch('test');
  222. expect(count).toEqual(2);
  223. });
  224. it('should not, by default, re-dispatch to DOM', function (done) {
  225. if ((0, _is_node["default"])()) {
  226. pending('Document in not supported in Node.js.');
  227. }
  228. var eventBus = new _ui_utils.EventBus();
  229. var count = 0;
  230. eventBus.on('test', function (evt) {
  231. expect(evt).toEqual(undefined);
  232. count++;
  233. });
  234. function domEventListener() {
  235. done.fail('shall not dispatch DOM event.');
  236. }
  237. document.addEventListener('test', domEventListener);
  238. eventBus.dispatch('test');
  239. Promise.resolve().then(function () {
  240. expect(count).toEqual(1);
  241. document.removeEventListener('test', domEventListener);
  242. done();
  243. });
  244. });
  245. it('should re-dispatch to DOM', function (done) {
  246. if ((0, _is_node["default"])()) {
  247. pending('Document in not supported in Node.js.');
  248. }
  249. var eventBus = new _ui_utils.EventBus({
  250. dispatchToDOM: true
  251. });
  252. var count = 0;
  253. eventBus.on('test', function (evt) {
  254. expect(evt).toEqual(undefined);
  255. count++;
  256. });
  257. function domEventListener(evt) {
  258. expect(evt.detail).toEqual({});
  259. count++;
  260. }
  261. document.addEventListener('test', domEventListener);
  262. eventBus.dispatch('test');
  263. Promise.resolve().then(function () {
  264. expect(count).toEqual(2);
  265. document.removeEventListener('test', domEventListener);
  266. done();
  267. });
  268. });
  269. it('should re-dispatch to DOM, with arguments (without internal listeners)', function (done) {
  270. if ((0, _is_node["default"])()) {
  271. pending('Document in not supported in Node.js.');
  272. }
  273. var eventBus = new _ui_utils.EventBus({
  274. dispatchToDOM: true
  275. });
  276. var count = 0;
  277. function domEventListener(evt) {
  278. expect(evt.detail).toEqual({
  279. abc: 123
  280. });
  281. count++;
  282. }
  283. document.addEventListener('test', domEventListener);
  284. eventBus.dispatch('test', {
  285. abc: 123
  286. });
  287. Promise.resolve().then(function () {
  288. expect(count).toEqual(1);
  289. document.removeEventListener('test', domEventListener);
  290. done();
  291. });
  292. });
  293. });
  294. describe('isValidRotation', function () {
  295. it('should reject non-integer angles', function () {
  296. expect((0, _ui_utils.isValidRotation)()).toEqual(false);
  297. expect((0, _ui_utils.isValidRotation)(null)).toEqual(false);
  298. expect((0, _ui_utils.isValidRotation)(NaN)).toEqual(false);
  299. expect((0, _ui_utils.isValidRotation)([90])).toEqual(false);
  300. expect((0, _ui_utils.isValidRotation)('90')).toEqual(false);
  301. expect((0, _ui_utils.isValidRotation)(90.5)).toEqual(false);
  302. });
  303. it('should reject non-multiple of 90 degree angles', function () {
  304. expect((0, _ui_utils.isValidRotation)(45)).toEqual(false);
  305. expect((0, _ui_utils.isValidRotation)(-123)).toEqual(false);
  306. });
  307. it('should accept valid angles', function () {
  308. expect((0, _ui_utils.isValidRotation)(0)).toEqual(true);
  309. expect((0, _ui_utils.isValidRotation)(90)).toEqual(true);
  310. expect((0, _ui_utils.isValidRotation)(-270)).toEqual(true);
  311. expect((0, _ui_utils.isValidRotation)(540)).toEqual(true);
  312. });
  313. });
  314. describe('isPortraitOrientation', function () {
  315. it('should be portrait orientation', function () {
  316. expect((0, _ui_utils.isPortraitOrientation)({
  317. width: 200,
  318. height: 400
  319. })).toEqual(true);
  320. expect((0, _ui_utils.isPortraitOrientation)({
  321. width: 500,
  322. height: 500
  323. })).toEqual(true);
  324. });
  325. it('should be landscape orientation', function () {
  326. expect((0, _ui_utils.isPortraitOrientation)({
  327. width: 600,
  328. height: 300
  329. })).toEqual(false);
  330. });
  331. });
  332. describe('waitOnEventOrTimeout', function () {
  333. var eventBus;
  334. beforeAll(function (done) {
  335. eventBus = new _ui_utils.EventBus();
  336. done();
  337. });
  338. afterAll(function () {
  339. eventBus = null;
  340. });
  341. it('should reject invalid parameters', function (done) {
  342. var invalidTarget = (0, _ui_utils.waitOnEventOrTimeout)({
  343. target: 'window',
  344. name: 'DOMContentLoaded'
  345. }).then(function () {
  346. throw new Error('Should reject invalid parameters.');
  347. }, function (reason) {
  348. expect(reason instanceof Error).toEqual(true);
  349. });
  350. var invalidName = (0, _ui_utils.waitOnEventOrTimeout)({
  351. target: eventBus,
  352. name: ''
  353. }).then(function () {
  354. throw new Error('Should reject invalid parameters.');
  355. }, function (reason) {
  356. expect(reason instanceof Error).toEqual(true);
  357. });
  358. var invalidDelay = (0, _ui_utils.waitOnEventOrTimeout)({
  359. target: eventBus,
  360. name: 'pagerendered',
  361. delay: -1000
  362. }).then(function () {
  363. throw new Error('Should reject invalid parameters.');
  364. }, function (reason) {
  365. expect(reason instanceof Error).toEqual(true);
  366. });
  367. Promise.all([invalidTarget, invalidName, invalidDelay]).then(done, done.fail);
  368. });
  369. it('should resolve on event, using the DOM', function (done) {
  370. if ((0, _is_node["default"])()) {
  371. pending('Document in not supported in Node.js.');
  372. }
  373. var button = document.createElement('button');
  374. var buttonClicked = (0, _ui_utils.waitOnEventOrTimeout)({
  375. target: button,
  376. name: 'click',
  377. delay: 10000
  378. });
  379. button.click();
  380. buttonClicked.then(function (type) {
  381. expect(type).toEqual(_ui_utils.WaitOnType.EVENT);
  382. done();
  383. }, done.fail);
  384. });
  385. it('should resolve on timeout, using the DOM', function (done) {
  386. if ((0, _is_node["default"])()) {
  387. pending('Document in not supported in Node.js.');
  388. }
  389. var button = document.createElement('button');
  390. var buttonClicked = (0, _ui_utils.waitOnEventOrTimeout)({
  391. target: button,
  392. name: 'click',
  393. delay: 10
  394. });
  395. buttonClicked.then(function (type) {
  396. expect(type).toEqual(_ui_utils.WaitOnType.TIMEOUT);
  397. done();
  398. }, done.fail);
  399. });
  400. it('should resolve on event, using the EventBus', function (done) {
  401. var pageRendered = (0, _ui_utils.waitOnEventOrTimeout)({
  402. target: eventBus,
  403. name: 'pagerendered',
  404. delay: 10000
  405. });
  406. eventBus.dispatch('pagerendered');
  407. pageRendered.then(function (type) {
  408. expect(type).toEqual(_ui_utils.WaitOnType.EVENT);
  409. done();
  410. }, done.fail);
  411. });
  412. it('should resolve on timeout, using the EventBus', function (done) {
  413. var pageRendered = (0, _ui_utils.waitOnEventOrTimeout)({
  414. target: eventBus,
  415. name: 'pagerendered',
  416. delay: 10
  417. });
  418. pageRendered.then(function (type) {
  419. expect(type).toEqual(_ui_utils.WaitOnType.TIMEOUT);
  420. done();
  421. }, done.fail);
  422. });
  423. });
  424. describe('getPageSizeInches', function () {
  425. it('gets page size (in inches)', function () {
  426. var page = {
  427. view: [0, 0, 595.28, 841.89],
  428. userUnit: 1.0,
  429. rotate: 0
  430. };
  431. var _getPageSizeInches = (0, _ui_utils.getPageSizeInches)(page),
  432. width = _getPageSizeInches.width,
  433. height = _getPageSizeInches.height;
  434. expect(+width.toPrecision(3)).toEqual(8.27);
  435. expect(+height.toPrecision(4)).toEqual(11.69);
  436. });
  437. it('gets page size (in inches), for non-default /Rotate entry', function () {
  438. var pdfPage1 = {
  439. view: [0, 0, 612, 792],
  440. userUnit: 1,
  441. rotate: 0
  442. };
  443. var _getPageSizeInches2 = (0, _ui_utils.getPageSizeInches)(pdfPage1),
  444. width1 = _getPageSizeInches2.width,
  445. height1 = _getPageSizeInches2.height;
  446. expect(width1).toEqual(8.5);
  447. expect(height1).toEqual(11);
  448. var pdfPage2 = {
  449. view: [0, 0, 612, 792],
  450. userUnit: 1,
  451. rotate: 90
  452. };
  453. var _getPageSizeInches3 = (0, _ui_utils.getPageSizeInches)(pdfPage2),
  454. width2 = _getPageSizeInches3.width,
  455. height2 = _getPageSizeInches3.height;
  456. expect(width2).toEqual(11);
  457. expect(height2).toEqual(8.5);
  458. });
  459. });
  460. describe('getVisibleElements', function () {
  461. var BORDER_WIDTH = 9;
  462. var SPACING = 2 * BORDER_WIDTH - 7;
  463. function makePages(lines) {
  464. var result = [];
  465. var lineTop = 0,
  466. id = 0;
  467. var _iteratorNormalCompletion = true;
  468. var _didIteratorError = false;
  469. var _iteratorError = undefined;
  470. try {
  471. for (var _iterator = lines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  472. var line = _step.value;
  473. var lineHeight = line.reduce(function (maxHeight, pair) {
  474. return Math.max(maxHeight, pair[1]);
  475. }, 0);
  476. var offsetLeft = -BORDER_WIDTH;
  477. var _iteratorNormalCompletion2 = true;
  478. var _didIteratorError2 = false;
  479. var _iteratorError2 = undefined;
  480. try {
  481. for (var _iterator2 = line[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  482. var _step2$value = _slicedToArray(_step2.value, 2),
  483. clientWidth = _step2$value[0],
  484. clientHeight = _step2$value[1];
  485. var offsetTop = lineTop + (lineHeight - clientHeight) / 2 - BORDER_WIDTH;
  486. var div = {
  487. offsetLeft: offsetLeft,
  488. offsetTop: offsetTop,
  489. clientWidth: clientWidth,
  490. clientHeight: clientHeight,
  491. clientLeft: BORDER_WIDTH,
  492. clientTop: BORDER_WIDTH
  493. };
  494. result.push({
  495. id: id,
  496. div: div
  497. });
  498. ++id;
  499. offsetLeft += clientWidth + SPACING;
  500. }
  501. } catch (err) {
  502. _didIteratorError2 = true;
  503. _iteratorError2 = err;
  504. } finally {
  505. try {
  506. if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
  507. _iterator2["return"]();
  508. }
  509. } finally {
  510. if (_didIteratorError2) {
  511. throw _iteratorError2;
  512. }
  513. }
  514. }
  515. lineTop += lineHeight + SPACING;
  516. }
  517. } catch (err) {
  518. _didIteratorError = true;
  519. _iteratorError = err;
  520. } finally {
  521. try {
  522. if (!_iteratorNormalCompletion && _iterator["return"] != null) {
  523. _iterator["return"]();
  524. }
  525. } finally {
  526. if (_didIteratorError) {
  527. throw _iteratorError;
  528. }
  529. }
  530. }
  531. return result;
  532. }
  533. function slowGetVisibleElements(scroll, pages) {
  534. var views = [];
  535. var scrollLeft = scroll.scrollLeft,
  536. scrollTop = scroll.scrollTop;
  537. var scrollRight = scrollLeft + scroll.clientWidth;
  538. var scrollBottom = scrollTop + scroll.clientHeight;
  539. var _iteratorNormalCompletion3 = true;
  540. var _didIteratorError3 = false;
  541. var _iteratorError3 = undefined;
  542. try {
  543. for (var _iterator3 = pages[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  544. var view = _step3.value;
  545. var div = view.div;
  546. var viewLeft = div.offsetLeft + div.clientLeft;
  547. var viewRight = viewLeft + div.clientWidth;
  548. var viewTop = div.offsetTop + div.clientTop;
  549. var viewBottom = viewTop + div.clientHeight;
  550. if (viewLeft < scrollRight && viewRight > scrollLeft && viewTop < scrollBottom && viewBottom > scrollTop) {
  551. var hiddenHeight = Math.max(0, scrollTop - viewTop) + Math.max(0, viewBottom - scrollBottom);
  552. var hiddenWidth = Math.max(0, scrollLeft - viewLeft) + Math.max(0, viewRight - scrollRight);
  553. var visibleArea = (div.clientHeight - hiddenHeight) * (div.clientWidth - hiddenWidth);
  554. var percent = visibleArea * 100 / div.clientHeight / div.clientWidth | 0;
  555. views.push({
  556. id: view.id,
  557. x: viewLeft,
  558. y: viewTop,
  559. view: view,
  560. percent: percent
  561. });
  562. }
  563. }
  564. } catch (err) {
  565. _didIteratorError3 = true;
  566. _iteratorError3 = err;
  567. } finally {
  568. try {
  569. if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
  570. _iterator3["return"]();
  571. }
  572. } finally {
  573. if (_didIteratorError3) {
  574. throw _iteratorError3;
  575. }
  576. }
  577. }
  578. return {
  579. first: views[0],
  580. last: views[views.length - 1],
  581. views: views
  582. };
  583. }
  584. function scrollOverDocument(pages) {
  585. var horizontally = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  586. var size = pages.reduce(function (max, _ref) {
  587. var div = _ref.div;
  588. return Math.max(max, horizontally ? div.offsetLeft + div.clientLeft + div.clientWidth : div.offsetTop + div.clientTop + div.clientHeight);
  589. }, 0);
  590. for (var i = 0; i < size; i += 7) {
  591. for (var j = i + 5; j < size; j += j - i) {
  592. var scroll = horizontally ? {
  593. scrollTop: 0,
  594. scrollLeft: i,
  595. clientHeight: 10000,
  596. clientWidth: j - i
  597. } : {
  598. scrollTop: i,
  599. scrollLeft: 0,
  600. clientHeight: j - i,
  601. clientWidth: 10000
  602. };
  603. expect((0, _ui_utils.getVisibleElements)(scroll, pages, false, horizontally)).toEqual(slowGetVisibleElements(scroll, pages));
  604. }
  605. }
  606. }
  607. it('with pages of varying height', function () {
  608. var pages = makePages([[[50, 20], [20, 50]], [[30, 12], [12, 30]], [[20, 50], [50, 20]], [[50, 20], [20, 50]]]);
  609. scrollOverDocument(pages);
  610. });
  611. it('widescreen challenge', function () {
  612. var pages = makePages([[[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]], [[10, 90], [10, 80], [10, 70], [10, 60], [10, 50]], [[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]]]);
  613. scrollOverDocument(pages);
  614. });
  615. it('works with horizontal scrolling', function () {
  616. var pages = makePages([[[10, 50], [20, 20], [30, 10]]]);
  617. scrollOverDocument(pages, true);
  618. });
  619. it('handles `sortByVisibility` correctly', function () {
  620. var scrollEl = {
  621. scrollTop: 75,
  622. scrollLeft: 0,
  623. clientHeight: 750,
  624. clientWidth: 1500
  625. };
  626. var views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  627. var visible = (0, _ui_utils.getVisibleElements)(scrollEl, views);
  628. var visibleSorted = (0, _ui_utils.getVisibleElements)(scrollEl, views, true);
  629. var viewsOrder = [],
  630. viewsSortedOrder = [];
  631. var _iteratorNormalCompletion4 = true;
  632. var _didIteratorError4 = false;
  633. var _iteratorError4 = undefined;
  634. try {
  635. for (var _iterator4 = visible.views[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  636. var view = _step4.value;
  637. viewsOrder.push(view.id);
  638. }
  639. } catch (err) {
  640. _didIteratorError4 = true;
  641. _iteratorError4 = err;
  642. } finally {
  643. try {
  644. if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
  645. _iterator4["return"]();
  646. }
  647. } finally {
  648. if (_didIteratorError4) {
  649. throw _iteratorError4;
  650. }
  651. }
  652. }
  653. var _iteratorNormalCompletion5 = true;
  654. var _didIteratorError5 = false;
  655. var _iteratorError5 = undefined;
  656. try {
  657. for (var _iterator5 = visibleSorted.views[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
  658. var _view = _step5.value;
  659. viewsSortedOrder.push(_view.id);
  660. }
  661. } catch (err) {
  662. _didIteratorError5 = true;
  663. _iteratorError5 = err;
  664. } finally {
  665. try {
  666. if (!_iteratorNormalCompletion5 && _iterator5["return"] != null) {
  667. _iterator5["return"]();
  668. }
  669. } finally {
  670. if (_didIteratorError5) {
  671. throw _iteratorError5;
  672. }
  673. }
  674. }
  675. expect(viewsOrder).toEqual([0, 1, 2]);
  676. expect(viewsSortedOrder).toEqual([1, 2, 0]);
  677. });
  678. it('handles views being empty', function () {
  679. var scrollEl = {
  680. scrollTop: 10,
  681. scrollLeft: 0,
  682. clientHeight: 750,
  683. clientWidth: 1500
  684. };
  685. var views = [];
  686. expect((0, _ui_utils.getVisibleElements)(scrollEl, views)).toEqual({
  687. first: undefined,
  688. last: undefined,
  689. views: []
  690. });
  691. });
  692. it('handles all views being hidden (without errors)', function () {
  693. var scrollEl = {
  694. scrollTop: 100000,
  695. scrollLeft: 0,
  696. clientHeight: 750,
  697. clientWidth: 1500
  698. };
  699. var views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  700. expect((0, _ui_utils.getVisibleElements)(scrollEl, views)).toEqual({
  701. first: undefined,
  702. last: undefined,
  703. views: []
  704. });
  705. });
  706. describe('backtrackBeforeAllVisibleElements', function () {
  707. var tallPage = [10, 50];
  708. var shortPage = [10, 10];
  709. var top1 = 20 + SPACING + 40;
  710. var top2 = 20 + SPACING + 10;
  711. it('handles case 1', function () {
  712. var pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]], [[10, 20]]]);
  713. var bsResult = 4;
  714. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  715. });
  716. it('handles case 2', function () {
  717. var pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, tallPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  718. var bsResult = 6;
  719. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  720. });
  721. it('handles case 3', function () {
  722. var pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  723. var bsResult = 8;
  724. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  725. });
  726. it('handles case 4', function () {
  727. var pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  728. var bsResult = 4;
  729. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top2)).toEqual(4);
  730. });
  731. });
  732. });
  733. describe('moveToEndOfArray', function () {
  734. it('works on empty arrays', function () {
  735. var data = [];
  736. (0, _ui_utils.moveToEndOfArray)(data, function () {});
  737. expect(data).toEqual([]);
  738. });
  739. it('works when moving everything', function () {
  740. var data = [1, 2, 3, 4, 5];
  741. (0, _ui_utils.moveToEndOfArray)(data, function () {
  742. return true;
  743. });
  744. expect(data).toEqual([1, 2, 3, 4, 5]);
  745. });
  746. it('works when moving some things', function () {
  747. var data = [1, 2, 3, 4, 5];
  748. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  749. return x % 2 === 0;
  750. });
  751. expect(data).toEqual([1, 3, 5, 2, 4]);
  752. });
  753. it('works when moving one thing', function () {
  754. var data = [1, 2, 3, 4, 5];
  755. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  756. return x === 1;
  757. });
  758. expect(data).toEqual([2, 3, 4, 5, 1]);
  759. });
  760. it('works when moving nothing', function () {
  761. var data = [1, 2, 3, 4, 5];
  762. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  763. return x === 0;
  764. });
  765. expect(data).toEqual([1, 2, 3, 4, 5]);
  766. });
  767. });
  768. });