2016-05-26 14:53:52 -07:00
|
|
|
// 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() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Polymer({
|
|
|
|
is: 'gr-selection-action-box',
|
|
|
|
|
2016-06-09 16:08:04 -07:00
|
|
|
/**
|
|
|
|
* Fired when the comment creation action was taken (hotkey, click).
|
|
|
|
*
|
|
|
|
* @event create-comment
|
|
|
|
*/
|
|
|
|
|
2016-05-26 14:53:52 -07:00
|
|
|
properties: {
|
|
|
|
keyEventTarget: {
|
|
|
|
type: Object,
|
2017-05-16 14:56:03 -07:00
|
|
|
value() { return document.body; },
|
2016-05-26 14:53:52 -07:00
|
|
|
},
|
|
|
|
range: {
|
|
|
|
type: Object,
|
|
|
|
value: {
|
|
|
|
startLine: NaN,
|
|
|
|
startChar: NaN,
|
|
|
|
endLine: NaN,
|
|
|
|
endChar: NaN,
|
|
|
|
},
|
|
|
|
},
|
2017-11-16 12:46:46 -08:00
|
|
|
positionBelow: Boolean,
|
2016-05-26 14:53:52 -07:00
|
|
|
side: {
|
|
|
|
type: String,
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
behaviors: [
|
|
|
|
Gerrit.KeyboardShortcutBehavior,
|
|
|
|
],
|
|
|
|
|
2016-06-09 16:08:04 -07:00
|
|
|
listeners: {
|
2017-05-16 14:56:03 -07:00
|
|
|
mousedown: '_handleMouseDown', // See https://crbug.com/gerrit/4767
|
2016-06-09 16:08:04 -07:00
|
|
|
},
|
|
|
|
|
2016-11-15 17:01:15 -08:00
|
|
|
keyBindings: {
|
2017-05-16 14:56:03 -07:00
|
|
|
c: '_handleCKey',
|
2016-11-15 17:01:15 -08:00
|
|
|
},
|
|
|
|
|
2017-05-16 14:56:03 -07:00
|
|
|
placeAbove(el) {
|
2017-06-26 15:38:24 -07:00
|
|
|
Polymer.dom.flush();
|
2017-05-16 14:56:03 -07:00
|
|
|
const rect = this._getTargetBoundingRect(el);
|
2017-11-16 12:46:46 -08:00
|
|
|
const boxRect = this.$.tooltip.getBoundingClientRect();
|
2017-05-16 14:56:03 -07:00
|
|
|
const parentRect = this.parentElement.getBoundingClientRect();
|
2016-05-31 16:06:32 -07:00
|
|
|
this.style.top =
|
2017-06-26 15:38:24 -07:00
|
|
|
rect.top - parentRect.top - boxRect.height - 6 + 'px';
|
2016-05-31 16:06:32 -07:00
|
|
|
this.style.left =
|
2016-06-15 11:57:36 -07:00
|
|
|
rect.left - parentRect.left + (rect.width - boxRect.width) / 2 + 'px';
|
2016-05-31 16:06:32 -07:00
|
|
|
},
|
|
|
|
|
2017-11-16 12:46:46 -08:00
|
|
|
placeBelow(el) {
|
|
|
|
Polymer.dom.flush();
|
|
|
|
const rect = this._getTargetBoundingRect(el);
|
|
|
|
const boxRect = this.$.tooltip.getBoundingClientRect();
|
|
|
|
const parentRect = this.parentElement.getBoundingClientRect();
|
|
|
|
this.style.top =
|
|
|
|
rect.top - parentRect.top + boxRect.height - 6 + 'px';
|
|
|
|
this.style.left =
|
|
|
|
rect.left - parentRect.left + (rect.width - boxRect.width) / 2 + 'px';
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:56:03 -07:00
|
|
|
_getTargetBoundingRect(el) {
|
|
|
|
let rect;
|
2016-06-09 16:08:04 -07:00
|
|
|
if (el instanceof Text) {
|
2017-05-16 14:56:03 -07:00
|
|
|
const range = document.createRange();
|
2016-05-26 14:53:52 -07:00
|
|
|
range.selectNode(el);
|
|
|
|
rect = range.getBoundingClientRect();
|
|
|
|
range.detach();
|
|
|
|
} else {
|
|
|
|
rect = el.getBoundingClientRect();
|
|
|
|
}
|
2016-05-31 16:06:32 -07:00
|
|
|
return rect;
|
2016-05-26 14:53:52 -07:00
|
|
|
},
|
|
|
|
|
2017-05-16 14:56:03 -07:00
|
|
|
_handleCKey(e) {
|
2016-12-07 18:02:34 -08:00
|
|
|
if (this.shouldSuppressKeyboardShortcut(e) ||
|
|
|
|
this.modifierPressed(e)) { return; }
|
2016-11-15 17:01:15 -08:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
this._fireCreateComment();
|
2016-05-26 14:53:52 -07:00
|
|
|
},
|
2016-06-09 16:08:04 -07:00
|
|
|
|
2017-05-16 14:56:03 -07:00
|
|
|
_handleMouseDown(e) {
|
2017-05-12 13:53:44 -07:00
|
|
|
if (e.button !== 0) { return; } // 0 = main button
|
2016-10-12 15:29:43 -07:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2016-06-09 16:08:04 -07:00
|
|
|
this._fireCreateComment();
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:56:03 -07:00
|
|
|
_fireCreateComment() {
|
2016-06-09 16:08:04 -07:00
|
|
|
this.fire('create-comment', {side: this.side, range: this.range});
|
|
|
|
},
|
2016-05-26 14:53:52 -07:00
|
|
|
});
|
|
|
|
})();
|