-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_floats_on_small_viewports-1878258-5-custom.patch
92 lines (89 loc) · 3.89 KB
/
fix_floats_on_small_viewports-1878258-5-custom.patch
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
diff --git a/floating_block.js b/floating_block.js
index 3c58340..96b3c90 100644
--- a/floating_block.js
+++ b/floating_block.js
@@ -39,7 +39,8 @@ Drupal.behaviors.blockFloat = {
// not to float outside of.
$(selector.toString() + ':not(.blockFloat-processed)', context).each(function (j, block) {
// Store information about the block to float.
- var blockInfo = [];
+ var blockInfo = [],
+ $this = $(this);
blockInfo.original_css = [];
blockInfo.original_css.left = Drupal.blockFloatCleanCSSValue($(block).css('left'));
blockInfo.original_css.top = Drupal.blockFloatCleanCSSValue($(block).css('top'));
@@ -48,6 +49,10 @@ Drupal.behaviors.blockFloat = {
blockInfo.reset = true;
blockInfo.original_identifier = 'blockFloat-' + Drupal.blockFloatStack().length;
+ // Store element dimensions in blockInfo for better performance.
+ blockInfo.blockWidth = $this.width();
+ blockInfo.blockHeight = $this.height();
+
// Store the selector for the container if it exists.
if (setting.container && $(setting.container.toString()).length > 0) {
blockInfo.container = setting.container;
@@ -100,6 +105,33 @@ Drupal.blockFloatTracker = function (blockInfo) {
return;
}
+ // Do not float if the sibling container is smaller than the block's container.
+ var contentRegion = $('#content'),
+ sidebarRegion = $('#sidebar-second');
+ if (contentRegion.height() < sidebarRegion.height()) {
+ return;
+ }
+
+ var documentWidth = $(document).width(),
+ // Window width calculation involves scrollbar calculation on IE.
+ viewportWidth = ($.browser.msie) ? $(window).width() + $.getScrollbarWidth() : $(window).width(),
+ viewportHeight = $(window).height(),
+ trueBlockHeight = parseInt(blockInfo.blockHeight) + parseInt(blockInfo.padding_top) + parseInt(blockInfo.padding_bottom);
+
+ // Do not float if the block to be floated is taller than the viewport itself.
+ if (trueBlockHeight > viewportHeight) {
+ // Reset the block in case it was caught floating during viewport change.
+ // console.log('block is taller than viewport');
+ Drupal.blockFloatResetPosition(block, blockInfo);
+ return;
+ }
+ // Do not float if the viewport is not wide enough to hold the page.
+ if (documentWidth > viewportWidth) {
+ // console.log('document is wider than the viewport');
+ Drupal.blockFloatResetPosition(block, blockInfo);
+ return;
+ }
+
// (Re)calculate some values if necessary.
if (blockInfo.scrollHeight != scrollHeight || blockInfo.reset) {
if (blockInfo.reset) {
@@ -225,4 +257,32 @@ if (!$('body').hasClass('blockFloat-processed')) {
$(window).resize(Drupal.blockFloatWindowResize);
}
+/**
+ * Calculate the size of the scrollbar as IE does not include this size when
+ * returning the width of the window object.
+ * Taken from https://github.com/brandonaaron/jquery-getscrollbarwidth/blob/master/jquery.getscrollbarwidth.js
+ */
+$.getScrollbarWidth = function() {
+ var scrollbarWidth = 0;
+
+ if (!scrollbarWidth) {
+ if ($.browser.msie) {
+ var $textarea1 = $('<textarea cols="10" rows="2"></textarea>')
+ .css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body'),
+ $textarea2 = $('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>')
+ .css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body');
+ scrollbarWidth = $textarea1.width() - $textarea2.width();
+ $textarea1.add($textarea2).remove();
+ } else {
+ var $div = $('<div />')
+ .css({ width: 100, height: 100, overflow: 'auto', position: 'absolute', top: -1000, left: -1000 })
+ .prependTo('body').append('<div />').find('div')
+ .css({ width: '100%', height: 200 });
+ scrollbarWidth = 100 - $div.width();
+ $div.parent().remove();
+ }
+ }
+ return scrollbarWidth;
+};
+
})(jQuery);