-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathfixed-layer.js
1174 lines (1071 loc) · 34.4 KB
/
fixed-layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Pass} from '../pass';
import {Services} from '../services';
import {
assertDoesNotContainDisplay,
computedStyle,
getStyle,
getVendorJsPropertyName,
setImportantStyles,
setInitialDisplay,
setStyle,
setStyles,
toggle,
} from '../style';
import {closest, domOrderComparator, matches} from '../dom';
import {dev, user} from '../log';
import {endsWith} from '../string';
import {getMode} from '../mode';
import {remove} from '../utils/array';
const TAG = 'FixedLayer';
const DECLARED_FIXED_PROP = '__AMP_DECLFIXED';
const DECLARED_STICKY_PROP = '__AMP_DECLSTICKY';
const LIGHTBOX_MODE_ATTR = 'i-amphtml-lightbox';
const LIGHTBOX_ELEMENT_CLASS = 'i-amphtml-lightbox-element';
/**
* @param {!Element} el
* @return {boolean}
*/
function isLightbox(el) {
return el.tagName.indexOf('LIGHTBOX') !== -1;
}
/**
* The fixed layer is a *sibling* of the body element. I.e. it's a direct
* child of documentElement. It's used to manage the `position:fixed` and
* `position:sticky` elements in iOS-iframe case due to the
* https://bugs.webkit.org/show_bug.cgi?id=154399 bug, which is itself
* a result of workaround for the issue where scrolling is not supported
* in iframes (https://bugs.webkit.org/show_bug.cgi?id=149264).
* This implementation finds all elements that could be `fixed` or `sticky`
* and checks on major relayouts if they are indeed `fixed`/`sticky`.
* Some `fixed` elements may be moved into the "transfer layer".
*/
export class FixedLayer {
/**
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {!./vsync-impl.Vsync} vsync
* @param {number} borderTop
* @param {number} paddingTop
* @param {boolean} transfer
*/
constructor(ampdoc, vsync, borderTop, paddingTop, transfer) {
/** @const {!./ampdoc-impl.AmpDoc} */
this.ampdoc = ampdoc;
/** @private @const */
this.vsync_ = vsync;
/** @const @private {number} */
this.borderTop_ = borderTop;
/** @private {number} */
this.paddingTop_ = paddingTop;
/** @private {number} */
this.committedPaddingTop_ = paddingTop;
/** @private @const {boolean} */
this.transfer_ = transfer && ampdoc.isSingleDoc();
/** @private {?TransferLayerDef} */
this.transferLayer_ = null;
/** @private {number} */
this.counter_ = 0;
/** @const @private {!Array<!ElementDef>} */
this.elements_ = [];
/** @const @private {!Pass} */
this.updatePass_ = new Pass(ampdoc.win, () => this.update());
/** @private {?function()} */
this.hiddenObserverUnlistener_ = null;
/** @private {!Array<string>} */
this.fixedSelectors_ = [];
/** @private {!Array<string>} */
this.stickySelectors_ = [];
}
/**
* Informs FixedLayer that a lightbox was opened.
*
* - FixedLayer hides any transfer layer elements that may be overlayed on
* top of the lightbox, which is confusing UX.
* - When `onComplete` resolves, FixedLayer scans and transfers any fixed
* descendants of `lightbox`. This enables unjanky fixed elements in
* lightboxes on iOS.
*
* @param {!Element=} opt_lightbox
* @param {!Promise=} opt_onComplete Promise that resolves when lightbox
* UX completes e.g. open transition animation.
*/
enterLightbox(opt_lightbox, opt_onComplete) {
const transferLayer = this.getTransferLayer_();
if (transferLayer) {
transferLayer.setLightboxMode(true);
}
if (opt_lightbox && opt_onComplete) {
opt_onComplete.then(() =>
this.scanNode_(
dev().assertElement(opt_lightbox),
/* lightboxMode */ true
)
);
}
}
/**
* Reverses the actions performed by `enterLightbox()`.
*/
leaveLightbox() {
const transferLayer = this.getTransferLayer_();
if (transferLayer) {
transferLayer.setLightboxMode(false);
}
const fes = remove(this.elements_, (fe) => !!fe.lightboxed);
this.returnFixedElements_(fes);
if (!this.elements_.length) {
this.unobserveHiddenMutations_();
}
}
/**
* Must be always called after DOMReady.
* @return {boolean}
*/
setup() {
const viewer = Services.viewerForDoc(this.ampdoc);
if (!getMode().localDev && !viewer.isEmbedded()) {
// FixedLayer is not needed for standalone documents.
return false;
}
const root = this.ampdoc.getRootNode();
const stylesheets = root.styleSheets;
if (!stylesheets) {
return true;
}
this.fixedSelectors_.length = 0;
this.stickySelectors_.length = 0;
for (let i = 0; i < stylesheets.length; i++) {
const stylesheet = stylesheets[i];
// Rare but may happen if the document is being concurrently disposed.
if (!stylesheet) {
dev().error(TAG, 'Aborting setup due to null stylesheet.');
return true;
}
const {disabled, ownerNode} = stylesheet;
if (
disabled ||
!ownerNode ||
ownerNode.tagName != 'STYLE' ||
ownerNode.hasAttribute('amp-boilerplate') ||
ownerNode.hasAttribute('amp-runtime') ||
ownerNode.hasAttribute('amp-extension')
) {
continue;
}
// Don't dereference cssRules early to avoid "Cannot access rules"
// DOMException due to reading a CORS stylesheet e.g. font.
this.discoverSelectors_(stylesheet.cssRules);
}
this.scanNode_(root);
if (this.elements_.length > 0) {
this.observeHiddenMutations();
}
const platform = Services.platformFor(this.ampdoc.win);
if (this.elements_.length > 0 && !this.transfer_ && platform.isIos()) {
user().warn(
TAG,
'Please test this page inside of an AMP Viewer such' +
" as Google's because the fixed or sticky positioning might have" +
' slightly different layout.'
);
}
return true;
}
/**
* @param {!Node} node
* @param {boolean=} opt_lightboxMode
* @private
*/
scanNode_(node, opt_lightboxMode) {
this.trySetupSelectors_(node, opt_lightboxMode);
// Sort tracked elements in document order.
this.sortInDomOrder_();
this.update();
}
/**
* Begin observing changes to the hidden attribute.
* @visibleForTesting
*/
observeHiddenMutations() {
this.initHiddenObserver_();
}
/**
* Stop observing changes to the hidden attribute.
*/
unobserveHiddenMutations_() {
this.updatePass_.cancel();
const unlisten = this.hiddenObserverUnlistener_;
if (unlisten) {
unlisten();
this.hiddenObserverUnlistener_ = null;
}
}
/**
* Start observing changes to the hidden attribute, if we haven't already
* started.
*/
initHiddenObserver_() {
if (this.hiddenObserverUnlistener_) {
return;
}
const root = this.ampdoc.getRootNode();
const element = root.documentElement || root;
const hiddenObserver = Services.hiddenObserverForDoc(element);
this.hiddenObserverUnlistener_ = hiddenObserver.add(() => {
if (!this.updatePass_.isPending()) {
// Wait one animation frame so that other mutations may arrive.
this.updatePass_.schedule(16);
}
});
}
/**
* Updates the viewer's padding-top position and recalculates offsets of
* all elements. The padding update can be transient, in which case the
* UI itself is not updated leaving the blank space up top, which is invisible
* due to scroll position. This mode saves significant resources. However,
* eventhough layout is not updated, the fixed/sticky coordinates still need
* to be recalculated.
* @param {number} paddingTop
* @param {boolean} opt_transient
*/
updatePaddingTop(paddingTop, opt_transient) {
this.paddingTop_ = paddingTop;
if (!opt_transient) {
this.committedPaddingTop_ = paddingTop;
}
this.update();
}
/**
* Apply or reset transform style to fixed elements. The existing transition,
* if any, is disabled when custom transform is supplied.
* @param {?string} transform
*/
transformMutate(transform) {
// Unfortunately, we can't do anything with sticky elements here. Updating
// `top` in animation frames causes reflow on all platforms and we can't
// determine whether an element is currently docked to apply transform.
if (transform) {
// Apply transform style to all fixed elements
this.elements_.forEach((e) => {
if (e.fixedNow && e.top) {
setStyle(e.element, 'transition', 'none');
if (e.transform && e.transform != 'none') {
setStyle(e.element, 'transform', e.transform + ' ' + transform);
} else {
setStyle(e.element, 'transform', transform);
}
}
});
} else {
// Reset transform style to all fixed elements
this.elements_.forEach((e) => {
if (e.fixedNow && e.top) {
setStyles(e.element, {
transform: '',
transition: '',
});
}
});
}
}
/**
* Adds the element directly into the fixed/sticky layer, bypassing discovery.
* @param {!Element} element
* @param {boolean=} opt_forceTransfer If set to true, then the element needs
* to be forcefully transferred to the transfer layer. If false, then it
* will only receive top-padding compensation for the header and never be
* transferred.
* @return {!Promise}
*/
addElement(element, opt_forceTransfer) {
const added = this.setupElement_(
element,
/* selector */ '*',
/* position */ 'fixed',
opt_forceTransfer
);
if (!added) {
return Promise.resolve();
}
this.sortInDomOrder_();
// If this is the first element, we need to start the mutation observer.
// This'll only be created once.
this.observeHiddenMutations();
return this.update();
}
/**
* Removes the element from the fixed/sticky layer.
* @param {!Element} element
*/
removeElement(element) {
const fes = this.tearDownElement_(element);
this.returnFixedElements_(fes);
}
/**
* Returns fixed elements from the transfer layer.
* @param {!Array<ElementDef>} fes
* @private
*/
returnFixedElements_(fes) {
if (fes.length > 0 && this.transferLayer_) {
this.vsync_.mutate(() => {
for (let i = 0; i < fes.length; i++) {
const fe = fes[i];
if (fe.position == 'fixed') {
this.transferLayer_.returnFrom(fe);
}
}
});
}
}
/**
* Whether the element is declared as fixed in any of the user's stylesheets.
* Will include any matches, not necessarily currently fixed elements.
* @param {!Element} element
* @return {boolean}
*/
isDeclaredFixed(element) {
return !!element[DECLARED_FIXED_PROP];
}
/**
* Whether the element is declared as sticky in any of the user's stylesheets.
* Will include any matches, not necessarily currently sticky elements.
* @param {!Element} element
* @return {boolean}
*/
isDeclaredSticky(element) {
return !!element[DECLARED_STICKY_PROP];
}
/**
* Performs fixed/sticky actions.
* 1. Updates `top` styling if necessary.
* 2. On iOS/Iframe moves elements between fixed layer and BODY depending on
* whether they are currently visible and fixed
* @return {!Promise}
*/
update() {
// Some of the elements may no longer be in DOM.
/** @type {!Array<!ElementDef>} */
const toRemove = this.elements_.filter(
(fe) => !this.ampdoc.contains(fe.element)
);
toRemove.forEach((fe) => this.tearDownElement_(fe.element));
if (this.elements_.length == 0) {
return Promise.resolve();
}
// Clear out the update pass since we're doing the work now.
this.updatePass_.cancel();
// Next, the positioning-related properties will be measured. If a
// potentially fixed/sticky element turns out to be actually fixed/sticky,
// it will be decorated and possibly moved to a separate layer.
let hasTransferables = false;
return this.vsync_
.runPromise(
{
measure: (state) => {
const elements = this.elements_;
const autoTops = [];
const {win} = this.ampdoc;
// Notice that this code intentionally breaks vsync contract.
// Unfortunately, there's no way to reliably test whether or not
// `top` has been set to a non-auto value on all platforms. To work
// this around, this code compares `style.top` values with a new
// `style.bottom` value.
// 1. Unset top from previous mutates and set bottom to an extremely
// large value (to catch cases where sticky-tops are in a long way
// down inside a scroller).
for (let i = 0; i < elements.length; i++) {
setImportantStyles(elements[i].element, {
top: '',
bottom: '-9999vh',
transition: 'none',
});
}
// 2. Capture the `style.top` with this new `style.bottom` value. If
// this element has a non-auto top, this value will remain constant
// regardless of bottom.
for (let i = 0; i < elements.length; i++) {
autoTops.push(computedStyle(win, elements[i].element).top);
}
// 3. Cleanup the `style.bottom`.
for (let i = 0; i < elements.length; i++) {
setStyle(elements[i].element, 'bottom', '');
}
for (let i = 0; i < elements.length; i++) {
const fe = elements[i];
const {element, forceTransfer} = fe;
const style = computedStyle(win, element);
const {offsetWidth, offsetHeight, offsetTop} = element;
const {position = '', display = '', bottom, zIndex} = style;
const opacity = parseFloat(style.opacity);
const transform =
style[getVendorJsPropertyName(style, 'transform')];
let {top} = style;
const isFixed =
position === 'fixed' &&
(forceTransfer || (offsetWidth > 0 && offsetHeight > 0));
const isSticky = endsWith(position, 'sticky');
const isDisplayed = display !== 'none';
if (!isDisplayed || !(isFixed || isSticky)) {
state[fe.id] = {
fixed: false,
sticky: false,
transferrable: false,
top: '',
zIndex: '',
};
continue;
}
if (top === 'auto' || autoTops[i] !== top) {
if (
isFixed &&
offsetTop === this.committedPaddingTop_ + this.borderTop_
) {
top = '0px';
} else {
top = '';
}
}
// Transferability requires an element to be:
// 1. Greater than 0% opacity. That's a lot of work for no benefit.
// Additionally, transparent elements used for "service" needs and
// thus best kept in the original tree. The visibility, however,
// is not considered because `visibility` CSS is inherited.
// 2. Height < 300. This avoids transferring large sections of UI,
// e.g. publisher-customized amp-consent UI (#17995).
// 3. Has `top` or `bottom` CSS set. This ensures we only transfer
// fixed elements that are _not_ auto-positioned to avoid jumping
// position after transferring to the fixed layer (due to loss of
// parent positioning context). We could calculate this offset, but
// we don't (yet).
let isTransferrable = false;
if (isFixed) {
if (forceTransfer === true) {
isTransferrable = true;
} else if (forceTransfer === false) {
isTransferrable = false;
} else {
isTransferrable =
opacity > 0 && offsetHeight < 300 && !!(top || bottom);
}
}
if (isTransferrable) {
hasTransferables = true;
}
state[fe.id] = {
fixed: isFixed,
sticky: isSticky,
transferrable: isTransferrable,
top,
zIndex,
transform,
};
}
},
mutate: (state) => {
if (hasTransferables && this.transfer_) {
this.getTransferLayer_().update();
}
const elements = this.elements_;
for (let i = 0; i < elements.length; i++) {
const fe = elements[i];
const feState = state[fe.id];
// Fix a bug with Safari. For some reason, you cannot unset
// transition when it's important. You can, however, set it to a valid
// non-important value, then unset it.
setStyle(fe.element, 'transition', 'none');
// Note: This MUST be done after measurements are taken.
// Transitions will mess up everything and, depending on when paints
// happen, mutates of transition and bottom at the same time may be
// make the transition active.
setStyle(fe.element, 'transition', '');
if (feState) {
this.mutateElement_(fe, i, feState);
}
}
},
},
{}
)
.catch((error) => {
// Fail silently.
dev().error(TAG, 'Failed to mutate fixed elements:', error);
});
}
/**
* Calls `setupSelectors_` in a try-catch.
* Fails quietly with a dev error if call fails.
* This method should not be inlined to prevent TryCatch deoptimization.
* @param {!Node} root
* @param {boolean=} opt_lightboxMode
* @private
* @noinline
*/
trySetupSelectors_(root, opt_lightboxMode) {
try {
this.setupSelectors_(root, opt_lightboxMode);
} catch (e) {
// Fail quietly.
dev().error(TAG, 'Failed to setup fixed elements:', e);
}
}
/**
* Calls `setupElement_` for up to 10 elements matching each selector
* in `fixedSelectors` and for all selectors in `stickySelectors`.
* @param {!Node} root
* @param {boolean=} opt_lightboxMode
* @private
*/
setupSelectors_(root, opt_lightboxMode) {
for (let i = 0; i < this.fixedSelectors_.length; i++) {
const fixedSelector = this.fixedSelectors_[i];
const elements = root.querySelectorAll(fixedSelector);
for (let j = 0; j < elements.length; j++) {
if (this.elements_.length > 10) {
// We shouldn't have too many of `fixed` elements.
break;
}
this.setupElement_(
elements[j],
fixedSelector,
'fixed',
/* opt_forceTransfer */ undefined,
opt_lightboxMode
);
}
}
for (let i = 0; i < this.stickySelectors_.length; i++) {
const stickySelector = this.stickySelectors_[i];
const elements = root.querySelectorAll(stickySelector);
for (let j = 0; j < elements.length; j++) {
this.setupElement_(
elements[j],
stickySelector,
'sticky',
/* opt_forceTransfer */ undefined,
opt_lightboxMode
);
}
}
}
/**
* If the given element has a `style` attribute with a top/bottom CSS rule,
* display a user error. FixedLayer's implementation currently overrides
* top, bottom and a few other CSS rules.
* @param {!Element} element
* @private
*/
warnAboutInlineStylesIfNecessary_(element) {
if (
element.hasAttribute('style') &&
(getStyle(element, 'top') || getStyle(element, 'bottom'))
) {
user().error(
TAG,
'Inline styles with `top`, `bottom` and other ' +
'CSS rules are not supported yet for fixed or sticky elements ' +
'(#14186). Unexpected behavior may occur.',
element
);
}
}
/**
* This method records the potentially fixed or sticky element. One of a more
* critical functions - it records all selectors that may apply "fixed"
* or "sticky" to this element to check them later.
*
* @param {!Element} element
* @param {string} selector
* @param {string} position
* @param {boolean=} opt_forceTransfer If true, then the element will
* be forcibly transferred to the transfer layer.
* @param {boolean=} opt_lightboxMode If true, then descendants of lightboxes
* are allowed to be set up. Default is false.
* @return {boolean}
* @private
*/
setupElement_(
element,
selector,
position,
opt_forceTransfer,
opt_lightboxMode
) {
// Warn that pub-authored inline styles may be overriden by FixedLayer.
if (!opt_forceTransfer) {
this.warnAboutInlineStylesIfNecessary_(element);
}
// Ignore lightboxes because FixedLayer can interfere with their
// opening/closing animations (#19149).
if (isLightbox(element)) {
return false;
}
const isLightboxDescendant = closest(element, isLightbox);
if (!opt_lightboxMode && isLightboxDescendant) {
return false;
}
const elements = this.elements_;
// Avoid ancestor-descendant relationships in tracked elements to prevent
// "double top-offset" (#22860).
const removals = [];
for (let i = 0; i < elements.length; i++) {
const el = elements[i].element;
if (el === element) {
break;
}
// Early exit if element is a child of an already-tracked element...
if (el.contains(element)) {
return false;
}
// Remove the already-tracked element if it is a child of the new
// element...
if (element.contains(el)) {
removals.push(el);
}
}
for (let i = 0; i < removals.length; i++) {
this.removeElement(removals[i]);
}
let fe = null;
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
if (el.element == element && el.position == position) {
fe = el;
break;
}
}
const isFixed = position == 'fixed';
if (fe) {
if (!fe.selectors.includes(selector)) {
// Already seen.
fe.selectors.push(selector);
}
} else {
// A new entry.
const id = 'F' + this.counter_++;
element.setAttribute('i-amphtml-fixedid', id);
if (isFixed) {
element[DECLARED_FIXED_PROP] = true;
} else {
element[DECLARED_STICKY_PROP] = true;
}
fe = {
id,
element,
position,
selectors: [selector],
fixedNow: false,
stickyNow: false,
lightboxed: !!isLightboxDescendant,
};
elements.push(fe);
}
fe.forceTransfer = isFixed ? opt_forceTransfer : false;
return true;
}
/**
* Undoes set up by removing element record and and resets `top` style.
* Does _not_ return the element from the transfer layer.
*
* @param {!Element} element
* @return {!Array<!ElementDef>}
* @private
*/
tearDownElement_(element) {
const removed = [];
for (let i = 0; i < this.elements_.length; i++) {
const fe = this.elements_[i];
if (fe.element === element) {
if (!fe.lightboxed) {
this.vsync_.mutate(() => {
setStyle(element, 'top', '');
});
}
this.elements_.splice(i, 1);
removed.push(fe);
}
}
if (!this.elements_.length) {
this.unobserveHiddenMutations_();
}
return removed;
}
/**
* @private
*/
sortInDomOrder_() {
this.elements_.sort((fe1, fe2) => {
return domOrderComparator(fe1.element, fe2.element);
});
}
/**
* Mutates the fixed/sticky element. At this point it's determined that the
* element is indeed fixed/sticky. There are two main functions here:
* 1. `top` has to be updated to reflect viewer's paddingTop.
* 2. The element may need to be transfered to the separate fixed layer.
*
* @param {!ElementDef} fe
* @param {number} index
* @param {!ElementStateDef} state
* @private
*/
mutateElement_(fe, index, state) {
const {element, fixedNow: oldFixed} = fe;
fe.fixedNow = state.fixed;
fe.stickyNow = state.sticky;
fe.top = state.fixed || state.sticky ? state.top : '';
fe.transform = state.transform;
// Move back to the BODY layer and reset transfer z-index.
if (
oldFixed &&
(!state.fixed || !state.transferrable) &&
this.transferLayer_
) {
this.transferLayer_.returnFrom(fe);
}
// Update `top` to adjust position to the viewer's paddingTop. However,
// ignore lightboxed elements since lightboxes ignore the viewer header.
if (state.top && (state.fixed || state.sticky) && !fe.lightboxed) {
if (state.fixed || !this.transfer_) {
// Fixed positions always need top offsetting, as well as stickies on
// non iOS Safari.
setStyle(element, 'top', `calc(${state.top} + ${this.paddingTop_}px)`);
} else {
// On iOS Safari (this.transfer_ = true), stickies cannot
// have an offset because they are already offset by the padding-top.
if (this.committedPaddingTop_ === this.paddingTop_) {
// So, when the header is shown, just use top.
setStyle(element, 'top', state.top);
} else {
// When the header is not shown, we need to subtract the padding top.
setStyle(
element,
'top',
`calc(${state.top} - ${this.committedPaddingTop_}px)`
);
}
}
}
// Move element to the fixed layer.
if (this.transfer_ && state.fixed && state.transferrable) {
this.getTransferLayer_().transferTo(fe, index, state);
}
}
/**
* @return {?TransferLayerDef}
*/
getTransferLayer_() {
// This mode is only allowed for a single-doc case.
if (!this.transfer_ || this.transferLayer_) {
return this.transferLayer_;
}
const doc = this.ampdoc.win.document;
this.transferLayer_ = new TransferLayerBody(doc, this.vsync_);
return this.transferLayer_;
}
/**
* Find all `position:fixed` and `position:sticky` elements.
* @param {!Array<CSSRule>} rules
* @private
*/
discoverSelectors_(rules) {
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (
rule.type == /* CSSMediaRule */ 4 ||
rule.type == /* CSSSupportsRule */ 12
) {
this.discoverSelectors_(rule.cssRules);
continue;
}
if (rule.type == /* CSSStyleRule */ 1) {
const {selectorText} = rule;
const {position} = rule.style;
if (selectorText === '*' || !position) {
continue;
}
if (position === 'fixed') {
this.fixedSelectors_.push(selectorText);
} else if (endsWith(position, 'sticky')) {
this.stickySelectors_.push(selectorText);
}
}
}
}
/**
* @param {number} paddingTop
* @param {number} lastPaddingTop
* @param {number} duration
* @param {string} curve
* @param {boolean} transient
* @return {!Promise}
*/
animateFixedElements(paddingTop, lastPaddingTop, duration, curve, transient) {
this.updatePaddingTop(paddingTop, transient);
if (duration <= 0) {
return Promise.resolve();
}
// Add transit effect on position fixed element
const tr = (time) => {
return lastPaddingTop - paddingTop + (paddingTop - lastPaddingTop) * time;
};
return Animation.animate(
this.ampdoc.getRootNode(),
(time) => {
const p = tr(time);
this.transformMutate(`translateY(${p}px)`);
},
duration,
curve
).thenAlways(() => {
this.transformMutate(null);
});
}
}
/**
* @typedef {{
* id: string,
* selectors: !Array,
* element: !Element,
* position: string,
* placeholder: (?Element|undefined),
* fixedNow: boolean,
* stickyNow: boolean,
* top: (string|undefined),
* transform: (string|undefined),
* forceTransfer: (boolean|undefined),
* lightboxed: (boolean|undefined),
* }}
*/
let ElementDef;
/**
* @typedef {{
* fixed: boolean,
* sticky: boolean,
* transferrable: boolean,
* top: string,
* zIndex: string,
* }}
*/
let ElementStateDef;
/**
* The contract for transfer layer.
* @interface
*/
class TransferLayerDef {
/**
* @return {!Element}
*/
getRoot() {}
/**
* Update most current styles for the transfer layer.
*/
update() {}
/**
* Toggles internal state after entering or leaving lightbox mode.
* @param {boolean} unusedOn
*/
setLightboxMode(unusedOn) {}
/**
* Transfer the element from the body into the transfer layer.
* @param {!ElementDef} unusedFe
* @param {number} unusedIndex
* @param {!ElementStateDef} unusedState
*/
transferTo(unusedFe, unusedIndex, unusedState) {}
/**
* Return the element from the transfer layer back to the body.
* @param {!ElementDef} unusedFe
*/
returnFrom(unusedFe) {}
}
/**
* The parallel `<body>` element is created and fixed elements are moved into
* this element.
* @implements {TransferLayerDef}
*/
class TransferLayerBody {
/**
* @param {!Document} doc
* @param {!./vsync-impl.Vsync} vsync
*/
constructor(doc, vsync) {
/** @private @const {!Document} */
this.doc_ = doc;
/** @private @const {!./vsync-impl.Vsync} */
this.vsync_ = vsync;
/** @private @const {!Element} */