UNPKG

6.9 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.SplitPositionHandler = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const widgets_1 = require("@phosphor/widgets");
22let SplitPositionHandler = class SplitPositionHandler {
23 constructor() {
24 this.splitMoves = [];
25 this.currentMoveIndex = 0;
26 }
27 /**
28 * Set the position of a split handle asynchronously. This function makes sure that such movements
29 * are performed one after another in order to prevent the movements from overriding each other.
30 * When resolved, the returned promise yields the final position of the split handle.
31 */
32 setSplitHandlePosition(parent, index, targetPosition, options) {
33 const move = {
34 ...options,
35 parent, targetPosition, index,
36 started: false,
37 ended: false
38 };
39 return this.moveSplitPos(move);
40 }
41 /**
42 * Resize a side panel asynchronously. This function makes sure that such movements are performed
43 * one after another in order to prevent the movements from overriding each other.
44 * When resolved, the returned promise yields the final position of the split handle.
45 */
46 setSidePanelSize(sidePanel, targetSize, options) {
47 if (targetSize < 0) {
48 return Promise.reject(new Error('Cannot resize to negative value.'));
49 }
50 const parent = sidePanel.parent;
51 if (!(parent instanceof widgets_1.SplitPanel)) {
52 return Promise.reject(new Error('Widget must be contained in a SplitPanel.'));
53 }
54 let index = parent.widgets.indexOf(sidePanel);
55 if (index > 0 && (options.side === 'right' || options.side === 'bottom')) {
56 index--;
57 }
58 const move = {
59 ...options,
60 parent, targetSize, index,
61 started: false,
62 ended: false
63 };
64 return this.moveSplitPos(move);
65 }
66 moveSplitPos(move) {
67 return new Promise((resolve, reject) => {
68 move.resolve = resolve;
69 move.reject = reject;
70 if (this.splitMoves.length === 0) {
71 window.requestAnimationFrame(this.animationFrame.bind(this));
72 }
73 this.splitMoves.push(move);
74 });
75 }
76 animationFrame(time) {
77 const move = this.splitMoves[this.currentMoveIndex];
78 let rejectedOrResolved = false;
79 if (move.ended || move.referenceWidget && move.referenceWidget.isHidden) {
80 this.splitMoves.splice(this.currentMoveIndex, 1);
81 if (move.startPosition === undefined || move.targetPosition === undefined) {
82 move.reject('Panel is not visible.');
83 }
84 else {
85 move.resolve(move.targetPosition);
86 }
87 rejectedOrResolved = true;
88 }
89 else if (!move.started) {
90 this.startMove(move, time);
91 if (move.duration <= 0 || move.startPosition === undefined || move.targetPosition === undefined
92 || move.startPosition === move.targetPosition) {
93 this.endMove(move);
94 }
95 }
96 else {
97 const elapsedTime = time - move.startTime;
98 if (elapsedTime >= move.duration) {
99 this.endMove(move);
100 }
101 else {
102 const t = elapsedTime / move.duration;
103 const start = move.startPosition || 0;
104 const target = move.targetPosition || 0;
105 const pos = start + (target - start) * t;
106 move.parent.layout.moveHandle(move.index, pos);
107 }
108 }
109 if (!rejectedOrResolved) {
110 this.currentMoveIndex++;
111 }
112 if (this.currentMoveIndex >= this.splitMoves.length) {
113 this.currentMoveIndex = 0;
114 }
115 if (this.splitMoves.length > 0) {
116 window.requestAnimationFrame(this.animationFrame.bind(this));
117 }
118 }
119 startMove(move, time) {
120 if (move.targetPosition === undefined && move.targetSize !== undefined) {
121 const { clientWidth, clientHeight } = move.parent.node;
122 if (clientWidth && clientHeight) {
123 switch (move.side) {
124 case 'left':
125 move.targetPosition = Math.max(Math.min(move.targetSize, clientWidth), 0);
126 break;
127 case 'right':
128 move.targetPosition = Math.max(Math.min(clientWidth - move.targetSize, clientWidth), 0);
129 break;
130 case 'top':
131 move.targetPosition = Math.max(Math.min(move.targetSize, clientHeight), 0);
132 break;
133 case 'bottom':
134 move.targetPosition = Math.max(Math.min(clientHeight - move.targetSize, clientHeight), 0);
135 break;
136 }
137 }
138 }
139 if (move.startPosition === undefined) {
140 move.startPosition = this.getCurrentPosition(move);
141 }
142 move.startTime = time;
143 move.started = true;
144 }
145 endMove(move) {
146 if (move.targetPosition !== undefined) {
147 move.parent.layout.moveHandle(move.index, move.targetPosition);
148 }
149 move.ended = true;
150 }
151 getCurrentPosition(move) {
152 const layout = move.parent.layout;
153 let pos;
154 if (layout.orientation === 'horizontal') {
155 pos = layout.handles[move.index].offsetLeft;
156 }
157 else {
158 pos = layout.handles[move.index].offsetTop;
159 }
160 // eslint-disable-next-line no-null/no-null
161 if (pos !== null) {
162 return pos;
163 }
164 else {
165 return undefined;
166 }
167 }
168};
169SplitPositionHandler = (0, tslib_1.__decorate)([
170 (0, inversify_1.injectable)()
171], SplitPositionHandler);
172exports.SplitPositionHandler = SplitPositionHandler;
173//# sourceMappingURL=split-panels.js.map
\No newline at end of file