ui_utils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.animationStarted = exports.VERTICAL_PADDING = exports.UNKNOWN_SCALE = exports.TextLayerMode = exports.SpreadMode = exports.SidebarView = exports.ScrollMode = exports.SCROLLBAR_PADDING = exports.RenderingStates = exports.RendererType = exports.ProgressBar = exports.PresentationModeState = exports.MIN_SCALE = exports.MAX_SCALE = exports.MAX_AUTO_SCALE = exports.DEFAULT_SCALE_VALUE = exports.DEFAULT_SCALE_DELTA = exports.DEFAULT_SCALE = exports.AutoPrintRegExp = void 0;
  27. exports.apiPageLayoutToViewerModes = apiPageLayoutToViewerModes;
  28. exports.apiPageModeToSidebarView = apiPageModeToSidebarView;
  29. exports.approximateFraction = approximateFraction;
  30. exports.backtrackBeforeAllVisibleElements = backtrackBeforeAllVisibleElements;
  31. exports.binarySearchFirstItem = binarySearchFirstItem;
  32. exports.getActiveOrFocusedElement = getActiveOrFocusedElement;
  33. exports.getOutputScale = getOutputScale;
  34. exports.getPageSizeInches = getPageSizeInches;
  35. exports.getVisibleElements = getVisibleElements;
  36. exports.isPortraitOrientation = isPortraitOrientation;
  37. exports.isValidRotation = isValidRotation;
  38. exports.isValidScrollMode = isValidScrollMode;
  39. exports.isValidSpreadMode = isValidSpreadMode;
  40. exports.noContextMenuHandler = noContextMenuHandler;
  41. exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
  42. exports.normalizeWheelEventDirection = normalizeWheelEventDirection;
  43. exports.parseQueryString = parseQueryString;
  44. exports.roundToDivide = roundToDivide;
  45. exports.scrollIntoView = scrollIntoView;
  46. exports.watchScroll = watchScroll;
  47. const DEFAULT_SCALE_VALUE = "auto";
  48. exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE;
  49. const DEFAULT_SCALE = 1.0;
  50. exports.DEFAULT_SCALE = DEFAULT_SCALE;
  51. const DEFAULT_SCALE_DELTA = 1.1;
  52. exports.DEFAULT_SCALE_DELTA = DEFAULT_SCALE_DELTA;
  53. const MIN_SCALE = 0.1;
  54. exports.MIN_SCALE = MIN_SCALE;
  55. const MAX_SCALE = 10.0;
  56. exports.MAX_SCALE = MAX_SCALE;
  57. const UNKNOWN_SCALE = 0;
  58. exports.UNKNOWN_SCALE = UNKNOWN_SCALE;
  59. const MAX_AUTO_SCALE = 1.25;
  60. exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE;
  61. const SCROLLBAR_PADDING = 40;
  62. exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING;
  63. const VERTICAL_PADDING = 5;
  64. exports.VERTICAL_PADDING = VERTICAL_PADDING;
  65. const LOADINGBAR_END_OFFSET_VAR = "--loadingBar-end-offset";
  66. const RenderingStates = {
  67. INITIAL: 0,
  68. RUNNING: 1,
  69. PAUSED: 2,
  70. FINISHED: 3
  71. };
  72. exports.RenderingStates = RenderingStates;
  73. const PresentationModeState = {
  74. UNKNOWN: 0,
  75. NORMAL: 1,
  76. CHANGING: 2,
  77. FULLSCREEN: 3
  78. };
  79. exports.PresentationModeState = PresentationModeState;
  80. const SidebarView = {
  81. UNKNOWN: -1,
  82. NONE: 0,
  83. THUMBS: 1,
  84. OUTLINE: 2,
  85. ATTACHMENTS: 3,
  86. LAYERS: 4
  87. };
  88. exports.SidebarView = SidebarView;
  89. const RendererType = {
  90. CANVAS: "canvas",
  91. SVG: "svg"
  92. };
  93. exports.RendererType = RendererType;
  94. const TextLayerMode = {
  95. DISABLE: 0,
  96. ENABLE: 1,
  97. ENABLE_ENHANCE: 2
  98. };
  99. exports.TextLayerMode = TextLayerMode;
  100. const ScrollMode = {
  101. UNKNOWN: -1,
  102. VERTICAL: 0,
  103. HORIZONTAL: 1,
  104. WRAPPED: 2,
  105. PAGE: 3
  106. };
  107. exports.ScrollMode = ScrollMode;
  108. const SpreadMode = {
  109. UNKNOWN: -1,
  110. NONE: 0,
  111. ODD: 1,
  112. EVEN: 2
  113. };
  114. exports.SpreadMode = SpreadMode;
  115. const AutoPrintRegExp = /\bprint\s*\(/;
  116. exports.AutoPrintRegExp = AutoPrintRegExp;
  117. function getOutputScale(ctx) {
  118. const devicePixelRatio = window.devicePixelRatio || 1;
  119. const backingStoreRatio = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  120. const pixelRatio = devicePixelRatio / backingStoreRatio;
  121. return {
  122. sx: pixelRatio,
  123. sy: pixelRatio,
  124. scaled: pixelRatio !== 1
  125. };
  126. }
  127. function scrollIntoView(element, spot, scrollMatches = false) {
  128. let parent = element.offsetParent;
  129. if (!parent) {
  130. console.error("offsetParent is not set -- cannot scroll");
  131. return;
  132. }
  133. let offsetY = element.offsetTop + element.clientTop;
  134. let offsetX = element.offsetLeft + element.clientLeft;
  135. while (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth || scrollMatches && (parent.classList.contains("markedContent") || getComputedStyle(parent).overflow === "hidden")) {
  136. offsetY += parent.offsetTop;
  137. offsetX += parent.offsetLeft;
  138. parent = parent.offsetParent;
  139. if (!parent) {
  140. return;
  141. }
  142. }
  143. if (spot) {
  144. if (spot.top !== undefined) {
  145. offsetY += spot.top;
  146. }
  147. if (spot.left !== undefined) {
  148. offsetX += spot.left;
  149. parent.scrollLeft = offsetX;
  150. }
  151. }
  152. parent.scrollTop = offsetY;
  153. }
  154. function watchScroll(viewAreaElement, callback) {
  155. const debounceScroll = function (evt) {
  156. if (rAF) {
  157. return;
  158. }
  159. rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
  160. rAF = null;
  161. const currentX = viewAreaElement.scrollLeft;
  162. const lastX = state.lastX;
  163. if (currentX !== lastX) {
  164. state.right = currentX > lastX;
  165. }
  166. state.lastX = currentX;
  167. const currentY = viewAreaElement.scrollTop;
  168. const lastY = state.lastY;
  169. if (currentY !== lastY) {
  170. state.down = currentY > lastY;
  171. }
  172. state.lastY = currentY;
  173. callback(state);
  174. });
  175. };
  176. const state = {
  177. right: true,
  178. down: true,
  179. lastX: viewAreaElement.scrollLeft,
  180. lastY: viewAreaElement.scrollTop,
  181. _eventHandler: debounceScroll
  182. };
  183. let rAF = null;
  184. viewAreaElement.addEventListener("scroll", debounceScroll, true);
  185. return state;
  186. }
  187. function parseQueryString(query) {
  188. const params = new Map();
  189. for (const [key, value] of new URLSearchParams(query)) {
  190. params.set(key.toLowerCase(), value);
  191. }
  192. return params;
  193. }
  194. function binarySearchFirstItem(items, condition) {
  195. let minIndex = 0;
  196. let maxIndex = items.length - 1;
  197. if (maxIndex < 0 || !condition(items[maxIndex])) {
  198. return items.length;
  199. }
  200. if (condition(items[minIndex])) {
  201. return minIndex;
  202. }
  203. while (minIndex < maxIndex) {
  204. const currentIndex = minIndex + maxIndex >> 1;
  205. const currentItem = items[currentIndex];
  206. if (condition(currentItem)) {
  207. maxIndex = currentIndex;
  208. } else {
  209. minIndex = currentIndex + 1;
  210. }
  211. }
  212. return minIndex;
  213. }
  214. function approximateFraction(x) {
  215. if (Math.floor(x) === x) {
  216. return [x, 1];
  217. }
  218. const xinv = 1 / x;
  219. const limit = 8;
  220. if (xinv > limit) {
  221. return [1, limit];
  222. } else if (Math.floor(xinv) === xinv) {
  223. return [1, xinv];
  224. }
  225. const x_ = x > 1 ? xinv : x;
  226. let a = 0,
  227. b = 1,
  228. c = 1,
  229. d = 1;
  230. while (true) {
  231. const p = a + c,
  232. q = b + d;
  233. if (q > limit) {
  234. break;
  235. }
  236. if (x_ <= p / q) {
  237. c = p;
  238. d = q;
  239. } else {
  240. a = p;
  241. b = q;
  242. }
  243. }
  244. let result;
  245. if (x_ - a / b < c / d - x_) {
  246. result = x_ === x ? [a, b] : [b, a];
  247. } else {
  248. result = x_ === x ? [c, d] : [d, c];
  249. }
  250. return result;
  251. }
  252. function roundToDivide(x, div) {
  253. const r = x % div;
  254. return r === 0 ? x : Math.round(x - r + div);
  255. }
  256. function getPageSizeInches({
  257. view,
  258. userUnit,
  259. rotate
  260. }) {
  261. const [x1, y1, x2, y2] = view;
  262. const changeOrientation = rotate % 180 !== 0;
  263. const width = (x2 - x1) / 72 * userUnit;
  264. const height = (y2 - y1) / 72 * userUnit;
  265. return {
  266. width: changeOrientation ? height : width,
  267. height: changeOrientation ? width : height
  268. };
  269. }
  270. function backtrackBeforeAllVisibleElements(index, views, top) {
  271. if (index < 2) {
  272. return index;
  273. }
  274. let elt = views[index].div;
  275. let pageTop = elt.offsetTop + elt.clientTop;
  276. if (pageTop >= top) {
  277. elt = views[index - 1].div;
  278. pageTop = elt.offsetTop + elt.clientTop;
  279. }
  280. for (let i = index - 2; i >= 0; --i) {
  281. elt = views[i].div;
  282. if (elt.offsetTop + elt.clientTop + elt.clientHeight <= pageTop) {
  283. break;
  284. }
  285. index = i;
  286. }
  287. return index;
  288. }
  289. function getVisibleElements({
  290. scrollEl,
  291. views,
  292. sortByVisibility = false,
  293. horizontal = false,
  294. rtl = false
  295. }) {
  296. const top = scrollEl.scrollTop,
  297. bottom = top + scrollEl.clientHeight;
  298. const left = scrollEl.scrollLeft,
  299. right = left + scrollEl.clientWidth;
  300. function isElementBottomAfterViewTop(view) {
  301. const element = view.div;
  302. const elementBottom = element.offsetTop + element.clientTop + element.clientHeight;
  303. return elementBottom > top;
  304. }
  305. function isElementNextAfterViewHorizontally(view) {
  306. const element = view.div;
  307. const elementLeft = element.offsetLeft + element.clientLeft;
  308. const elementRight = elementLeft + element.clientWidth;
  309. return rtl ? elementLeft < right : elementRight > left;
  310. }
  311. const visible = [],
  312. ids = new Set(),
  313. numViews = views.length;
  314. let firstVisibleElementInd = binarySearchFirstItem(views, horizontal ? isElementNextAfterViewHorizontally : isElementBottomAfterViewTop);
  315. if (firstVisibleElementInd > 0 && firstVisibleElementInd < numViews && !horizontal) {
  316. firstVisibleElementInd = backtrackBeforeAllVisibleElements(firstVisibleElementInd, views, top);
  317. }
  318. let lastEdge = horizontal ? right : -1;
  319. for (let i = firstVisibleElementInd; i < numViews; i++) {
  320. const view = views[i],
  321. element = view.div;
  322. const currentWidth = element.offsetLeft + element.clientLeft;
  323. const currentHeight = element.offsetTop + element.clientTop;
  324. const viewWidth = element.clientWidth,
  325. viewHeight = element.clientHeight;
  326. const viewRight = currentWidth + viewWidth;
  327. const viewBottom = currentHeight + viewHeight;
  328. if (lastEdge === -1) {
  329. if (viewBottom >= bottom) {
  330. lastEdge = viewBottom;
  331. }
  332. } else if ((horizontal ? currentWidth : currentHeight) > lastEdge) {
  333. break;
  334. }
  335. if (viewBottom <= top || currentHeight >= bottom || viewRight <= left || currentWidth >= right) {
  336. continue;
  337. }
  338. const hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, viewBottom - bottom);
  339. const hiddenWidth = Math.max(0, left - currentWidth) + Math.max(0, viewRight - right);
  340. const fractionHeight = (viewHeight - hiddenHeight) / viewHeight,
  341. fractionWidth = (viewWidth - hiddenWidth) / viewWidth;
  342. const percent = fractionHeight * fractionWidth * 100 | 0;
  343. visible.push({
  344. id: view.id,
  345. x: currentWidth,
  346. y: currentHeight,
  347. view,
  348. percent,
  349. widthPercent: fractionWidth * 100 | 0
  350. });
  351. ids.add(view.id);
  352. }
  353. const first = visible[0],
  354. last = visible[visible.length - 1];
  355. if (sortByVisibility) {
  356. visible.sort(function (a, b) {
  357. const pc = a.percent - b.percent;
  358. if (Math.abs(pc) > 0.001) {
  359. return -pc;
  360. }
  361. return a.id - b.id;
  362. });
  363. }
  364. return {
  365. first,
  366. last,
  367. views: visible,
  368. ids
  369. };
  370. }
  371. function noContextMenuHandler(evt) {
  372. evt.preventDefault();
  373. }
  374. function normalizeWheelEventDirection(evt) {
  375. let delta = Math.hypot(evt.deltaX, evt.deltaY);
  376. const angle = Math.atan2(evt.deltaY, evt.deltaX);
  377. if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
  378. delta = -delta;
  379. }
  380. return delta;
  381. }
  382. function normalizeWheelEventDelta(evt) {
  383. let delta = normalizeWheelEventDirection(evt);
  384. const MOUSE_DOM_DELTA_PIXEL_MODE = 0;
  385. const MOUSE_DOM_DELTA_LINE_MODE = 1;
  386. const MOUSE_PIXELS_PER_LINE = 30;
  387. const MOUSE_LINES_PER_PAGE = 30;
  388. if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
  389. delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
  390. } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
  391. delta /= MOUSE_LINES_PER_PAGE;
  392. }
  393. return delta;
  394. }
  395. function isValidRotation(angle) {
  396. return Number.isInteger(angle) && angle % 90 === 0;
  397. }
  398. function isValidScrollMode(mode) {
  399. return Number.isInteger(mode) && Object.values(ScrollMode).includes(mode) && mode !== ScrollMode.UNKNOWN;
  400. }
  401. function isValidSpreadMode(mode) {
  402. return Number.isInteger(mode) && Object.values(SpreadMode).includes(mode) && mode !== SpreadMode.UNKNOWN;
  403. }
  404. function isPortraitOrientation(size) {
  405. return size.width <= size.height;
  406. }
  407. const animationStarted = new Promise(function (resolve) {
  408. if (typeof window === "undefined") {
  409. setTimeout(resolve, 20);
  410. return;
  411. }
  412. window.requestAnimationFrame(resolve);
  413. });
  414. exports.animationStarted = animationStarted;
  415. function clamp(v, min, max) {
  416. return Math.min(Math.max(v, min), max);
  417. }
  418. class ProgressBar {
  419. constructor(id, {
  420. height,
  421. width,
  422. units
  423. } = {}) {
  424. this.visible = true;
  425. this.div = document.querySelector(id + " .progress");
  426. this.bar = this.div.parentNode;
  427. this.height = height || 100;
  428. this.width = width || 100;
  429. this.units = units || "%";
  430. this.div.style.height = this.height + this.units;
  431. this.percent = 0;
  432. }
  433. _updateBar() {
  434. if (this._indeterminate) {
  435. this.div.classList.add("indeterminate");
  436. this.div.style.width = this.width + this.units;
  437. return;
  438. }
  439. this.div.classList.remove("indeterminate");
  440. const progressSize = this.width * this._percent / 100;
  441. this.div.style.width = progressSize + this.units;
  442. }
  443. get percent() {
  444. return this._percent;
  445. }
  446. set percent(val) {
  447. this._indeterminate = isNaN(val);
  448. this._percent = clamp(val, 0, 100);
  449. this._updateBar();
  450. }
  451. setWidth(viewer) {
  452. if (!viewer) {
  453. return;
  454. }
  455. const container = viewer.parentNode;
  456. const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
  457. if (scrollbarWidth > 0) {
  458. const doc = document.documentElement;
  459. doc.style.setProperty(LOADINGBAR_END_OFFSET_VAR, `${scrollbarWidth}px`);
  460. }
  461. }
  462. hide() {
  463. if (!this.visible) {
  464. return;
  465. }
  466. this.visible = false;
  467. this.bar.classList.add("hidden");
  468. }
  469. show() {
  470. if (this.visible) {
  471. return;
  472. }
  473. this.visible = true;
  474. this.bar.classList.remove("hidden");
  475. }
  476. }
  477. exports.ProgressBar = ProgressBar;
  478. function getActiveOrFocusedElement() {
  479. let curRoot = document;
  480. let curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  481. while (curActiveOrFocused?.shadowRoot) {
  482. curRoot = curActiveOrFocused.shadowRoot;
  483. curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  484. }
  485. return curActiveOrFocused;
  486. }
  487. function apiPageLayoutToViewerModes(layout) {
  488. let scrollMode = ScrollMode.VERTICAL,
  489. spreadMode = SpreadMode.NONE;
  490. switch (layout) {
  491. case "SinglePage":
  492. scrollMode = ScrollMode.PAGE;
  493. break;
  494. case "OneColumn":
  495. break;
  496. case "TwoPageLeft":
  497. scrollMode = ScrollMode.PAGE;
  498. case "TwoColumnLeft":
  499. spreadMode = SpreadMode.ODD;
  500. break;
  501. case "TwoPageRight":
  502. scrollMode = ScrollMode.PAGE;
  503. case "TwoColumnRight":
  504. spreadMode = SpreadMode.EVEN;
  505. break;
  506. }
  507. return {
  508. scrollMode,
  509. spreadMode
  510. };
  511. }
  512. function apiPageModeToSidebarView(mode) {
  513. switch (mode) {
  514. case "UseNone":
  515. return SidebarView.NONE;
  516. case "UseThumbs":
  517. return SidebarView.THUMBS;
  518. case "UseOutlines":
  519. return SidebarView.OUTLINE;
  520. case "UseAttachments":
  521. return SidebarView.ATTACHMENTS;
  522. case "UseOC":
  523. return SidebarView.LAYERS;
  524. }
  525. return SidebarView.NONE;
  526. }