ui_utils.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.approximateFraction = approximateFraction;
  27. exports.backtrackBeforeAllVisibleElements = backtrackBeforeAllVisibleElements;
  28. exports.binarySearchFirstItem = binarySearchFirstItem;
  29. exports.getActiveOrFocusedElement = getActiveOrFocusedElement;
  30. exports.getOutputScale = getOutputScale;
  31. exports.getPageSizeInches = getPageSizeInches;
  32. exports.getPDFFileNameFromURL = getPDFFileNameFromURL;
  33. exports.getVisibleElements = getVisibleElements;
  34. exports.isPortraitOrientation = isPortraitOrientation;
  35. exports.isValidRotation = isValidRotation;
  36. exports.isValidScrollMode = isValidScrollMode;
  37. exports.isValidSpreadMode = isValidSpreadMode;
  38. exports.moveToEndOfArray = moveToEndOfArray;
  39. exports.noContextMenuHandler = noContextMenuHandler;
  40. exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
  41. exports.normalizeWheelEventDirection = normalizeWheelEventDirection;
  42. exports.parseQueryString = parseQueryString;
  43. exports.roundToDivide = roundToDivide;
  44. exports.scrollIntoView = scrollIntoView;
  45. exports.waitOnEventOrTimeout = waitOnEventOrTimeout;
  46. exports.watchScroll = watchScroll;
  47. exports.WaitOnType = exports.VERTICAL_PADDING = exports.UNKNOWN_SCALE = exports.TextLayerMode = exports.SpreadMode = exports.SidebarView = exports.ScrollMode = exports.SCROLLBAR_PADDING = exports.RendererType = exports.ProgressBar = exports.PresentationModeState = exports.NullL10n = exports.MIN_SCALE = exports.MAX_SCALE = exports.MAX_AUTO_SCALE = exports.EventBus = exports.DEFAULT_SCALE_VALUE = exports.DEFAULT_SCALE = exports.CSS_UNITS = exports.AutoPrintRegExp = exports.animationStarted = void 0;
  48. const CSS_UNITS = 96.0 / 72.0;
  49. exports.CSS_UNITS = CSS_UNITS;
  50. const DEFAULT_SCALE_VALUE = "auto";
  51. exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE;
  52. const DEFAULT_SCALE = 1.0;
  53. exports.DEFAULT_SCALE = DEFAULT_SCALE;
  54. const MIN_SCALE = 0.1;
  55. exports.MIN_SCALE = MIN_SCALE;
  56. const MAX_SCALE = 10.0;
  57. exports.MAX_SCALE = MAX_SCALE;
  58. const UNKNOWN_SCALE = 0;
  59. exports.UNKNOWN_SCALE = UNKNOWN_SCALE;
  60. const MAX_AUTO_SCALE = 1.25;
  61. exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE;
  62. const SCROLLBAR_PADDING = 40;
  63. exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING;
  64. const VERTICAL_PADDING = 5;
  65. exports.VERTICAL_PADDING = VERTICAL_PADDING;
  66. const LOADINGBAR_END_OFFSET_VAR = "--loadingBar-end-offset";
  67. const PresentationModeState = {
  68. UNKNOWN: 0,
  69. NORMAL: 1,
  70. CHANGING: 2,
  71. FULLSCREEN: 3
  72. };
  73. exports.PresentationModeState = PresentationModeState;
  74. const SidebarView = {
  75. UNKNOWN: -1,
  76. NONE: 0,
  77. THUMBS: 1,
  78. OUTLINE: 2,
  79. ATTACHMENTS: 3,
  80. LAYERS: 4
  81. };
  82. exports.SidebarView = SidebarView;
  83. const RendererType = {
  84. CANVAS: "canvas",
  85. SVG: "svg"
  86. };
  87. exports.RendererType = RendererType;
  88. const TextLayerMode = {
  89. DISABLE: 0,
  90. ENABLE: 1,
  91. ENABLE_ENHANCE: 2
  92. };
  93. exports.TextLayerMode = TextLayerMode;
  94. const ScrollMode = {
  95. UNKNOWN: -1,
  96. VERTICAL: 0,
  97. HORIZONTAL: 1,
  98. WRAPPED: 2
  99. };
  100. exports.ScrollMode = ScrollMode;
  101. const SpreadMode = {
  102. UNKNOWN: -1,
  103. NONE: 0,
  104. ODD: 1,
  105. EVEN: 2
  106. };
  107. exports.SpreadMode = SpreadMode;
  108. const AutoPrintRegExp = /\bprint\s*\(/;
  109. exports.AutoPrintRegExp = AutoPrintRegExp;
  110. function formatL10nValue(text, args) {
  111. if (!args) {
  112. return text;
  113. }
  114. return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
  115. return name in args ? args[name] : "{{" + name + "}}";
  116. });
  117. }
  118. const NullL10n = {
  119. async getLanguage() {
  120. return "en-us";
  121. },
  122. async getDirection() {
  123. return "ltr";
  124. },
  125. async get(property, args, fallback) {
  126. return formatL10nValue(fallback, args);
  127. },
  128. async translate(element) {}
  129. };
  130. exports.NullL10n = NullL10n;
  131. function getOutputScale(ctx) {
  132. const devicePixelRatio = window.devicePixelRatio || 1;
  133. const backingStoreRatio = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  134. const pixelRatio = devicePixelRatio / backingStoreRatio;
  135. return {
  136. sx: pixelRatio,
  137. sy: pixelRatio,
  138. scaled: pixelRatio !== 1
  139. };
  140. }
  141. function scrollIntoView(element, spot, skipOverflowHiddenElements = false) {
  142. let parent = element.offsetParent;
  143. if (!parent) {
  144. console.error("offsetParent is not set -- cannot scroll");
  145. return;
  146. }
  147. let offsetY = element.offsetTop + element.clientTop;
  148. let offsetX = element.offsetLeft + element.clientLeft;
  149. while (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth || skipOverflowHiddenElements && getComputedStyle(parent).overflow === "hidden") {
  150. if (parent.dataset._scaleY) {
  151. offsetY /= parent.dataset._scaleY;
  152. offsetX /= parent.dataset._scaleX;
  153. }
  154. offsetY += parent.offsetTop;
  155. offsetX += parent.offsetLeft;
  156. parent = parent.offsetParent;
  157. if (!parent) {
  158. return;
  159. }
  160. }
  161. if (spot) {
  162. if (spot.top !== undefined) {
  163. offsetY += spot.top;
  164. }
  165. if (spot.left !== undefined) {
  166. offsetX += spot.left;
  167. parent.scrollLeft = offsetX;
  168. }
  169. }
  170. parent.scrollTop = offsetY;
  171. }
  172. function watchScroll(viewAreaElement, callback) {
  173. const debounceScroll = function (evt) {
  174. if (rAF) {
  175. return;
  176. }
  177. rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
  178. rAF = null;
  179. const currentX = viewAreaElement.scrollLeft;
  180. const lastX = state.lastX;
  181. if (currentX !== lastX) {
  182. state.right = currentX > lastX;
  183. }
  184. state.lastX = currentX;
  185. const currentY = viewAreaElement.scrollTop;
  186. const lastY = state.lastY;
  187. if (currentY !== lastY) {
  188. state.down = currentY > lastY;
  189. }
  190. state.lastY = currentY;
  191. callback(state);
  192. });
  193. };
  194. const state = {
  195. right: true,
  196. down: true,
  197. lastX: viewAreaElement.scrollLeft,
  198. lastY: viewAreaElement.scrollTop,
  199. _eventHandler: debounceScroll
  200. };
  201. let rAF = null;
  202. viewAreaElement.addEventListener("scroll", debounceScroll, true);
  203. return state;
  204. }
  205. function parseQueryString(query) {
  206. const parts = query.split("&");
  207. const params = Object.create(null);
  208. for (let i = 0, ii = parts.length; i < ii; ++i) {
  209. const param = parts[i].split("=");
  210. const key = param[0].toLowerCase();
  211. const value = param.length > 1 ? param[1] : null;
  212. params[decodeURIComponent(key)] = decodeURIComponent(value);
  213. }
  214. return params;
  215. }
  216. function binarySearchFirstItem(items, condition) {
  217. let minIndex = 0;
  218. let maxIndex = items.length - 1;
  219. if (maxIndex < 0 || !condition(items[maxIndex])) {
  220. return items.length;
  221. }
  222. if (condition(items[minIndex])) {
  223. return minIndex;
  224. }
  225. while (minIndex < maxIndex) {
  226. const currentIndex = minIndex + maxIndex >> 1;
  227. const currentItem = items[currentIndex];
  228. if (condition(currentItem)) {
  229. maxIndex = currentIndex;
  230. } else {
  231. minIndex = currentIndex + 1;
  232. }
  233. }
  234. return minIndex;
  235. }
  236. function approximateFraction(x) {
  237. if (Math.floor(x) === x) {
  238. return [x, 1];
  239. }
  240. const xinv = 1 / x;
  241. const limit = 8;
  242. if (xinv > limit) {
  243. return [1, limit];
  244. } else if (Math.floor(xinv) === xinv) {
  245. return [1, xinv];
  246. }
  247. const x_ = x > 1 ? xinv : x;
  248. let a = 0,
  249. b = 1,
  250. c = 1,
  251. d = 1;
  252. while (true) {
  253. const p = a + c,
  254. q = b + d;
  255. if (q > limit) {
  256. break;
  257. }
  258. if (x_ <= p / q) {
  259. c = p;
  260. d = q;
  261. } else {
  262. a = p;
  263. b = q;
  264. }
  265. }
  266. let result;
  267. if (x_ - a / b < c / d - x_) {
  268. result = x_ === x ? [a, b] : [b, a];
  269. } else {
  270. result = x_ === x ? [c, d] : [d, c];
  271. }
  272. return result;
  273. }
  274. function roundToDivide(x, div) {
  275. const r = x % div;
  276. return r === 0 ? x : Math.round(x - r + div);
  277. }
  278. function getPageSizeInches({
  279. view,
  280. userUnit,
  281. rotate
  282. }) {
  283. const [x1, y1, x2, y2] = view;
  284. const changeOrientation = rotate % 180 !== 0;
  285. const width = (x2 - x1) / 72 * userUnit;
  286. const height = (y2 - y1) / 72 * userUnit;
  287. return {
  288. width: changeOrientation ? height : width,
  289. height: changeOrientation ? width : height
  290. };
  291. }
  292. function backtrackBeforeAllVisibleElements(index, views, top) {
  293. if (index < 2) {
  294. return index;
  295. }
  296. let elt = views[index].div;
  297. let pageTop = elt.offsetTop + elt.clientTop;
  298. if (pageTop >= top) {
  299. elt = views[index - 1].div;
  300. pageTop = elt.offsetTop + elt.clientTop;
  301. }
  302. for (let i = index - 2; i >= 0; --i) {
  303. elt = views[i].div;
  304. if (elt.offsetTop + elt.clientTop + elt.clientHeight <= pageTop) {
  305. break;
  306. }
  307. index = i;
  308. }
  309. return index;
  310. }
  311. function getVisibleElements({
  312. scrollEl,
  313. views,
  314. sortByVisibility = false,
  315. horizontal = false,
  316. rtl = false
  317. }) {
  318. const top = scrollEl.scrollTop,
  319. bottom = top + scrollEl.clientHeight;
  320. const left = scrollEl.scrollLeft,
  321. right = left + scrollEl.clientWidth;
  322. function isElementBottomAfterViewTop(view) {
  323. const element = view.div;
  324. const elementBottom = element.offsetTop + element.clientTop + element.clientHeight;
  325. return elementBottom > top;
  326. }
  327. function isElementNextAfterViewHorizontally(view) {
  328. const element = view.div;
  329. const elementLeft = element.offsetLeft + element.clientLeft;
  330. const elementRight = elementLeft + element.clientWidth;
  331. return rtl ? elementLeft < right : elementRight > left;
  332. }
  333. const visible = [],
  334. numViews = views.length;
  335. let firstVisibleElementInd = binarySearchFirstItem(views, horizontal ? isElementNextAfterViewHorizontally : isElementBottomAfterViewTop);
  336. if (firstVisibleElementInd > 0 && firstVisibleElementInd < numViews && !horizontal) {
  337. firstVisibleElementInd = backtrackBeforeAllVisibleElements(firstVisibleElementInd, views, top);
  338. }
  339. let lastEdge = horizontal ? right : -1;
  340. for (let i = firstVisibleElementInd; i < numViews; i++) {
  341. const view = views[i],
  342. element = view.div;
  343. const currentWidth = element.offsetLeft + element.clientLeft;
  344. const currentHeight = element.offsetTop + element.clientTop;
  345. const viewWidth = element.clientWidth,
  346. viewHeight = element.clientHeight;
  347. const viewRight = currentWidth + viewWidth;
  348. const viewBottom = currentHeight + viewHeight;
  349. if (lastEdge === -1) {
  350. if (viewBottom >= bottom) {
  351. lastEdge = viewBottom;
  352. }
  353. } else if ((horizontal ? currentWidth : currentHeight) > lastEdge) {
  354. break;
  355. }
  356. if (viewBottom <= top || currentHeight >= bottom || viewRight <= left || currentWidth >= right) {
  357. continue;
  358. }
  359. const hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, viewBottom - bottom);
  360. const hiddenWidth = Math.max(0, left - currentWidth) + Math.max(0, viewRight - right);
  361. const fractionHeight = (viewHeight - hiddenHeight) / viewHeight,
  362. fractionWidth = (viewWidth - hiddenWidth) / viewWidth;
  363. const percent = fractionHeight * fractionWidth * 100 | 0;
  364. visible.push({
  365. id: view.id,
  366. x: currentWidth,
  367. y: currentHeight,
  368. view,
  369. percent,
  370. widthPercent: fractionWidth * 100 | 0
  371. });
  372. }
  373. const first = visible[0],
  374. last = visible[visible.length - 1];
  375. if (sortByVisibility) {
  376. visible.sort(function (a, b) {
  377. const pc = a.percent - b.percent;
  378. if (Math.abs(pc) > 0.001) {
  379. return -pc;
  380. }
  381. return a.id - b.id;
  382. });
  383. }
  384. return {
  385. first,
  386. last,
  387. views: visible
  388. };
  389. }
  390. function noContextMenuHandler(evt) {
  391. evt.preventDefault();
  392. }
  393. function isDataSchema(url) {
  394. let i = 0;
  395. const ii = url.length;
  396. while (i < ii && url[i].trim() === "") {
  397. i++;
  398. }
  399. return url.substring(i, i + 5).toLowerCase() === "data:";
  400. }
  401. function getPDFFileNameFromURL(url, defaultFilename = "document.pdf") {
  402. if (typeof url !== "string") {
  403. return defaultFilename;
  404. }
  405. if (isDataSchema(url)) {
  406. console.warn("getPDFFileNameFromURL: " + 'ignoring "data:" URL for performance reasons.');
  407. return defaultFilename;
  408. }
  409. const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  410. const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  411. const splitURI = reURI.exec(url);
  412. let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]);
  413. if (suggestedFilename) {
  414. suggestedFilename = suggestedFilename[0];
  415. if (suggestedFilename.includes("%")) {
  416. try {
  417. suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  418. } catch (ex) {}
  419. }
  420. }
  421. return suggestedFilename || defaultFilename;
  422. }
  423. function normalizeWheelEventDirection(evt) {
  424. let delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY);
  425. const angle = Math.atan2(evt.deltaY, evt.deltaX);
  426. if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
  427. delta = -delta;
  428. }
  429. return delta;
  430. }
  431. function normalizeWheelEventDelta(evt) {
  432. let delta = normalizeWheelEventDirection(evt);
  433. const MOUSE_DOM_DELTA_PIXEL_MODE = 0;
  434. const MOUSE_DOM_DELTA_LINE_MODE = 1;
  435. const MOUSE_PIXELS_PER_LINE = 30;
  436. const MOUSE_LINES_PER_PAGE = 30;
  437. if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
  438. delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
  439. } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
  440. delta /= MOUSE_LINES_PER_PAGE;
  441. }
  442. return delta;
  443. }
  444. function isValidRotation(angle) {
  445. return Number.isInteger(angle) && angle % 90 === 0;
  446. }
  447. function isValidScrollMode(mode) {
  448. return Number.isInteger(mode) && Object.values(ScrollMode).includes(mode) && mode !== ScrollMode.UNKNOWN;
  449. }
  450. function isValidSpreadMode(mode) {
  451. return Number.isInteger(mode) && Object.values(SpreadMode).includes(mode) && mode !== SpreadMode.UNKNOWN;
  452. }
  453. function isPortraitOrientation(size) {
  454. return size.width <= size.height;
  455. }
  456. const WaitOnType = {
  457. EVENT: "event",
  458. TIMEOUT: "timeout"
  459. };
  460. exports.WaitOnType = WaitOnType;
  461. function waitOnEventOrTimeout({
  462. target,
  463. name,
  464. delay = 0
  465. }) {
  466. return new Promise(function (resolve, reject) {
  467. if (typeof target !== "object" || !(name && typeof name === "string") || !(Number.isInteger(delay) && delay >= 0)) {
  468. throw new Error("waitOnEventOrTimeout - invalid parameters.");
  469. }
  470. function handler(type) {
  471. if (target instanceof EventBus) {
  472. target._off(name, eventHandler);
  473. } else {
  474. target.removeEventListener(name, eventHandler);
  475. }
  476. if (timeout) {
  477. clearTimeout(timeout);
  478. }
  479. resolve(type);
  480. }
  481. const eventHandler = handler.bind(null, WaitOnType.EVENT);
  482. if (target instanceof EventBus) {
  483. target._on(name, eventHandler);
  484. } else {
  485. target.addEventListener(name, eventHandler);
  486. }
  487. const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
  488. const timeout = setTimeout(timeoutHandler, delay);
  489. });
  490. }
  491. const animationStarted = new Promise(function (resolve) {
  492. window.requestAnimationFrame(resolve);
  493. });
  494. exports.animationStarted = animationStarted;
  495. function dispatchDOMEvent(eventName, args = null) {
  496. throw new Error("Not implemented: dispatchDOMEvent");
  497. }
  498. class EventBus {
  499. constructor(options) {
  500. this._listeners = Object.create(null);
  501. }
  502. on(eventName, listener, options = null) {
  503. this._on(eventName, listener, {
  504. external: true,
  505. once: options?.once
  506. });
  507. }
  508. off(eventName, listener, options = null) {
  509. this._off(eventName, listener, {
  510. external: true,
  511. once: options?.once
  512. });
  513. }
  514. dispatch(eventName) {
  515. const eventListeners = this._listeners[eventName];
  516. if (!eventListeners || eventListeners.length === 0) {
  517. return;
  518. }
  519. const args = Array.prototype.slice.call(arguments, 1);
  520. let externalListeners;
  521. eventListeners.slice(0).forEach(({
  522. listener,
  523. external,
  524. once
  525. }) => {
  526. if (once) {
  527. this._off(eventName, listener);
  528. }
  529. if (external) {
  530. (externalListeners || (externalListeners = [])).push(listener);
  531. return;
  532. }
  533. listener.apply(null, args);
  534. });
  535. if (externalListeners) {
  536. externalListeners.forEach(listener => {
  537. listener.apply(null, args);
  538. });
  539. externalListeners = null;
  540. }
  541. }
  542. _on(eventName, listener, options = null) {
  543. var _this$_listeners;
  544. const eventListeners = (_this$_listeners = this._listeners)[eventName] || (_this$_listeners[eventName] = []);
  545. eventListeners.push({
  546. listener,
  547. external: options?.external === true,
  548. once: options?.once === true
  549. });
  550. }
  551. _off(eventName, listener, options = null) {
  552. const eventListeners = this._listeners[eventName];
  553. if (!eventListeners) {
  554. return;
  555. }
  556. for (let i = 0, ii = eventListeners.length; i < ii; i++) {
  557. if (eventListeners[i].listener === listener) {
  558. eventListeners.splice(i, 1);
  559. return;
  560. }
  561. }
  562. }
  563. }
  564. exports.EventBus = EventBus;
  565. function clamp(v, min, max) {
  566. return Math.min(Math.max(v, min), max);
  567. }
  568. class ProgressBar {
  569. constructor(id, {
  570. height,
  571. width,
  572. units
  573. } = {}) {
  574. this.visible = true;
  575. this.div = document.querySelector(id + " .progress");
  576. this.bar = this.div.parentNode;
  577. this.height = height || 100;
  578. this.width = width || 100;
  579. this.units = units || "%";
  580. this.div.style.height = this.height + this.units;
  581. this.percent = 0;
  582. }
  583. _updateBar() {
  584. if (this._indeterminate) {
  585. this.div.classList.add("indeterminate");
  586. this.div.style.width = this.width + this.units;
  587. return;
  588. }
  589. this.div.classList.remove("indeterminate");
  590. const progressSize = this.width * this._percent / 100;
  591. this.div.style.width = progressSize + this.units;
  592. }
  593. get percent() {
  594. return this._percent;
  595. }
  596. set percent(val) {
  597. this._indeterminate = isNaN(val);
  598. this._percent = clamp(val, 0, 100);
  599. this._updateBar();
  600. }
  601. setWidth(viewer) {
  602. if (!viewer) {
  603. return;
  604. }
  605. const container = viewer.parentNode;
  606. const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
  607. if (scrollbarWidth > 0) {
  608. const doc = document.documentElement;
  609. doc.style.setProperty(LOADINGBAR_END_OFFSET_VAR, `${scrollbarWidth}px`);
  610. }
  611. }
  612. hide() {
  613. if (!this.visible) {
  614. return;
  615. }
  616. this.visible = false;
  617. this.bar.classList.add("hidden");
  618. }
  619. show() {
  620. if (this.visible) {
  621. return;
  622. }
  623. this.visible = true;
  624. this.bar.classList.remove("hidden");
  625. }
  626. }
  627. exports.ProgressBar = ProgressBar;
  628. function moveToEndOfArray(arr, condition) {
  629. const moved = [],
  630. len = arr.length;
  631. let write = 0;
  632. for (let read = 0; read < len; ++read) {
  633. if (condition(arr[read])) {
  634. moved.push(arr[read]);
  635. } else {
  636. arr[write] = arr[read];
  637. ++write;
  638. }
  639. }
  640. for (let read = 0; write < len; ++read, ++write) {
  641. arr[write] = moved[read];
  642. }
  643. }
  644. function getActiveOrFocusedElement() {
  645. let curRoot = document;
  646. let curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  647. while (curActiveOrFocused && curActiveOrFocused.shadowRoot) {
  648. curRoot = curActiveOrFocused.shadowRoot;
  649. curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  650. }
  651. return curActiveOrFocused;
  652. }