UNPKG

4.15 kBJavaScriptView Raw
1import { applyCssTransform } from '@angular2-material/core';
2/**
3 * A strategy for positioning overlays. Using this strategy, an overlay is given an
4 * explicit position relative to the browser's viewport.
5 */
6export var GlobalPositionStrategy = (function () {
7 function GlobalPositionStrategy() {
8 this._cssPosition = 'absolute';
9 this._top = '';
10 this._bottom = '';
11 this._left = '';
12 this._right = '';
13 /** Array of individual applications of translateX(). Currently only for centering. */
14 this._translateX = [];
15 /** Array of individual applications of translateY(). Currently only for centering. */
16 this._translateY = [];
17 }
18 /** Sets the element to usee CSS position: fixed */
19 GlobalPositionStrategy.prototype.fixed = function () {
20 this._cssPosition = 'fixed';
21 return this;
22 };
23 /** Sets the element to usee CSS position: absolute. This is the default. */
24 GlobalPositionStrategy.prototype.absolute = function () {
25 this._cssPosition = 'absolute';
26 return this;
27 };
28 /** Sets the top position of the overlay. Clears any previously set vertical position. */
29 GlobalPositionStrategy.prototype.top = function (value) {
30 this._bottom = '';
31 this._translateY = [];
32 this._top = value;
33 return this;
34 };
35 /** Sets the left position of the overlay. Clears any previously set horizontal position. */
36 GlobalPositionStrategy.prototype.left = function (value) {
37 this._right = '';
38 this._translateX = [];
39 this._left = value;
40 return this;
41 };
42 /** Sets the bottom position of the overlay. Clears any previously set vertical position. */
43 GlobalPositionStrategy.prototype.bottom = function (value) {
44 this._top = '';
45 this._translateY = [];
46 this._bottom = value;
47 return this;
48 };
49 /** Sets the right position of the overlay. Clears any previously set horizontal position. */
50 GlobalPositionStrategy.prototype.right = function (value) {
51 this._left = '';
52 this._translateX = [];
53 this._right = value;
54 return this;
55 };
56 /**
57 * Centers the overlay horizontally with an optional offset.
58 * Clears any previously set horizontal position.
59 */
60 GlobalPositionStrategy.prototype.centerHorizontally = function (offset) {
61 if (offset === void 0) { offset = '0px'; }
62 this._left = '50%';
63 this._right = '';
64 this._translateX = ['-50%', offset];
65 return this;
66 };
67 /**
68 * Centers the overlay vertically with an optional offset.
69 * Clears any previously set vertical position.
70 */
71 GlobalPositionStrategy.prototype.centerVertically = function (offset) {
72 if (offset === void 0) { offset = '0px'; }
73 this._top = '50%';
74 this._bottom = '';
75 this._translateY = ['-50%', offset];
76 return this;
77 };
78 /**
79 * Apply the position to the element.
80 * TODO: internal
81 */
82 GlobalPositionStrategy.prototype.apply = function (element) {
83 element.style.position = this._cssPosition;
84 element.style.top = this._top;
85 element.style.left = this._left;
86 element.style.bottom = this._bottom;
87 element.style.right = this._right;
88 // TODO(jelbourn): we don't want to always overwrite the transform property here,
89 // because it will need to be used for animations.
90 var tranlateX = this._reduceTranslateValues('translateX', this._translateX);
91 var translateY = this._reduceTranslateValues('translateY', this._translateY);
92 applyCssTransform(element, tranlateX + " " + translateY);
93 return Promise.resolve(null);
94 };
95 /** Reduce a list of translate values to a string that can be used in the transform property */
96 GlobalPositionStrategy.prototype._reduceTranslateValues = function (translateFn, values) {
97 return values.map(function (t) { return (translateFn + "(" + t + ")"); }).join(' ');
98 };
99 return GlobalPositionStrategy;
100}());
101
102//# sourceMappingURL=global-position-strategy.js.map