Files
gerrit/polygerrit-ui/app/behaviors/gr-tooltip-behavior/gr-tooltip-behavior.js
Wyatt Allen 7dfa26e1c1 Display warning in show all files link when there are many files
If a change contains many more files than the default-shown number of
files (more than 1,000 files) the show-all button may briefly lock-up
the browser while they render. Thanks to I50da91908, the performance is
vastly improved, but can still cause the page to be briefly unresponsive
on extremely large changes.

With this change, a tooltip warns the user that clicking show-all may
cause brief unresponsiveness. Because the link may or may not have a
tooltip, GR-TOOLTIP-BEHAVIOR is upgraded to use configuration that is
not set at "attached" time.

Bug: Issue 5657
Change-Id: I850a63d5561576b2f9b33e347c583adfee2aa695
2017-04-17 11:07:08 -07:00

131 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (C) 2016 The Android Open Source Project
//
// 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.
(function(window) {
'use strict';
var BOTTOM_OFFSET = 7.2; // Height of the arrow in tooltip.
/** @polymerBehavior Gerrit.TooltipBehavior */
var TooltipBehavior = {
properties: {
hasTooltip: {
type: Boolean,
observer: '_setupTooltipListeners',
},
_isTouchDevice: {
type: Boolean,
value: function() {
return 'ontouchstart' in document.documentElement;
},
},
_tooltip: Element,
_titleText: String,
_hasSetupTooltipListeners: {
type: Boolean,
value: false,
},
},
detached: function() {
this._handleHideTooltip();
this.unlisten(window, 'scroll', '_handleWindowScroll');
},
_setupTooltipListeners: function() {
if (this._hasSetupTooltipListeners || !this.hasTooltip) { return; }
this._hasSetupTooltipListeners = true;
this.addEventListener('mouseenter', this._handleShowTooltip.bind(this));
this.addEventListener('mouseleave', this._handleHideTooltip.bind(this));
this.addEventListener('tap', this._handleHideTooltip.bind(this));
this.listen(window, 'scroll', '_handleWindowScroll');
},
_handleShowTooltip: function(e) {
if (this._isTouchDevice) { return; }
if (!this.hasAttribute('title') ||
this.getAttribute('title') === '' ||
this._tooltip) {
return;
}
// Store the title attribute text then set it to an empty string to
// prevent it from showing natively.
this._titleText = this.getAttribute('title');
this.setAttribute('title', '');
var tooltip = document.createElement('gr-tooltip');
tooltip.text = this._titleText;
tooltip.maxWidth = this.getAttribute('max-width');
// Set visibility to hidden before appending to the DOM so that
// calculations can be made based on the elements size.
tooltip.style.visibility = 'hidden';
Polymer.dom(document.body).appendChild(tooltip);
this._positionTooltip(tooltip);
tooltip.style.visibility = null;
this._tooltip = tooltip;
},
_handleHideTooltip: function(e) {
if (this._isTouchDevice) { return; }
if (!this.hasAttribute('title') ||
this._titleText == null) {
return;
}
this.setAttribute('title', this._titleText);
if (this._tooltip && this._tooltip.parentNode) {
this._tooltip.parentNode.removeChild(this._tooltip);
}
this._tooltip = null;
},
_handleWindowScroll: function(e) {
if (!this._tooltip) { return; }
this._positionTooltip(this._tooltip);
},
_positionTooltip: function(tooltip) {
var rect = this.getBoundingClientRect();
var boxRect = tooltip.getBoundingClientRect();
var parentRect = tooltip.parentElement.getBoundingClientRect();
var top = rect.top - parentRect.top;
var left = rect.left - parentRect.left + (rect.width - boxRect.width) / 2;
var right = parentRect.width - left - boxRect.width;
if (left < 0) {
tooltip.updateStyles({
'--gr-tooltip-arrow-center-offset': left + 'px',
});
} else if (right < 0) {
tooltip.updateStyles({
'--gr-tooltip-arrow-center-offset': (-0.5 * right) + 'px',
});
}
tooltip.style.left = Math.max(0, left) + 'px';
tooltip.style.top = Math.max(0, top) + 'px';
tooltip.style.transform = 'translateY(calc(-100% - ' + BOTTOM_OFFSET +
'px))';
},
};
window.Gerrit = window.Gerrit || {};
window.Gerrit.TooltipBehavior = TooltipBehavior;
})(window);