Move descendedFromClass into a behavior

Change-Id: Ie592a1b2d22cfc0ba32514fa4a48f68f7ed9eeb6
This commit is contained in:
Wyatt Allen
2018-01-31 18:23:02 -08:00
parent 62f3ecbda7
commit 117e449efb
7 changed files with 124 additions and 18 deletions

View File

@@ -0,0 +1,45 @@
<!--
Copyright (C) 2018 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.
-->
<script>
(function(window) {
'use strict';
window.Gerrit = window.Gerrit || {};
/** @polymerBehavior Gerrit.DomUtilBehavior */
Gerrit.DomUtilBehavior = {
/**
* Are any ancestors of the element (or the element itself) members of the
* given class.
* @param {!Element} element
* @param {string} className
* @param {Element=} opt_stopElement If provided, stop traversing the
* ancestry when the stop element is reached. The stop element's class
* is not checked.
* @return {boolean}
*/
descendedFromClass(element, className, opt_stopElement) {
let isDescendant = element.classList.contains(className);
while (!isDescendant && element.parentElement &&
(!opt_stopElement || element.parentElement !== opt_stopElement)) {
isDescendant = element.classList.contains(className);
element = element.parentElement;
}
return isDescendant;
},
};
})(window);
</script>

View File

@@ -0,0 +1,68 @@
<!DOCTYPE html>
<!--
Copyright (C) 2018 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.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>dom-util-behavior</title>
<script src="../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../test/common-test-setup.html"/>
<link rel="import" href="dom-util-behavior.html">
<test-fixture id="nested-structure">
<template>
<test-element></test-element>
<div>
<div class="a">
<div class="b">
<div class="c"></div>
</div>
</div>
</div>
</template>
</test-fixture>
<script>
suite('dom-util-behavior tests', () => {
let element;
let divs;
suiteSetup(() => {
// Define a Polymer element that uses this behavior.
Polymer({
is: 'test-element',
behaviors: [Gerrit.DomUtilBehavior],
});
});
setup(() => {
const testDom = fixture('nested-structure');
element = testDom[0];
divs = testDom[1];
});
test('descendedFromClass', () => {
// .c is a child of .a and not vice versa.
assert.isTrue(element.descendedFromClass(divs.querySelector('.c'), 'a'));
assert.isFalse(element.descendedFromClass(divs.querySelector('.a'), 'c'));
// Stops at stop element.
assert.isFalse(element.descendedFromClass(divs.querySelector('.c'), 'a',
divs.querySelector('.b')));
});
});
</script>

View File

@@ -16,6 +16,7 @@ limitations under the License.
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../behaviors/async-foreach-behavior/async-foreach-behavior.html">
<link rel="import" href="../../../behaviors/dom-util-behavior/dom-util-behavior.html">
<link rel="import" href="../../../behaviors/keyboard-shortcut-behavior/keyboard-shortcut-behavior.html">
<link rel="import" href="../../../behaviors/gr-patch-set-behavior/gr-patch-set-behavior.html">
<link rel="import" href="../../../styles/shared-styles.html">

View File

@@ -129,6 +129,7 @@
behaviors: [
Gerrit.AsyncForeachBehavior,
Gerrit.DomUtilBehavior,
Gerrit.KeyboardShortcutBehavior,
Gerrit.PatchSetBehavior,
Gerrit.PathListBehavior,
@@ -430,7 +431,7 @@
}
// If the user clicked the link, defer to the native link behavior.
if (this._descendedFromClass(e.target, 'pathLink')) { return; }
if (this.descendedFromClass(e.target, 'pathLink')) { return; }
e.preventDefault();
this._togglePathExpanded(path);
@@ -923,14 +924,5 @@
_computeReviewedText(isReviewed) {
return isReviewed ? 'MARK UNREVIEWED' : 'MARK REVIEWED';
},
_descendedFromClass(element, className) {
let isDescendant = element.classList.contains(className);
while (!isDescendant && element.parentElement) {
isDescendant = element.classList.contains(className);
element = element.parentElement;
}
return isDescendant;
},
});
})();

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../behaviors/dom-util-behavior/dom-util-behavior.html">
<link rel="import" href="../../../styles/shared-styles.html">
<dom-module id="gr-diff-selection">

View File

@@ -49,6 +49,10 @@
down: '_handleDown',
},
behaviors: [
Gerrit.DomUtilBehavior,
],
attached() {
this.classList.add(SelectionClass.RIGHT);
},
@@ -127,14 +131,8 @@
* @return {boolean}
*/
_elementDescendedFromClass(element, className) {
while (!element.classList.contains(className)) {
if (!element.parentElement ||
element === this.diffBuilder.diffElement) {
return false;
}
element = element.parentElement;
}
return true;
return this.descendedFromClass(element, className,
this.diffBuilder.diffElement);
},
_handleCopy(e) {

View File

@@ -182,6 +182,7 @@ limitations under the License.
'async-foreach-behavior/async-foreach-behavior_test.html',
'base-url-behavior/base-url-behavior_test.html',
'docs-url-behavior/docs-url-behavior_test.html',
'dom-util-behavior/dom-util-behavior_test.html',
'keyboard-shortcut-behavior/keyboard-shortcut-behavior_test.html',
'rest-client-behavior/rest-client-behavior_test.html',
'gr-access-behavior/gr-access-behavior_test.html',