ui_utils.js 20 KB

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