annotate templates/static/followlines.js @ 19:a90e510ccc4f

fix restart/move logs
author Vadim Filimonov <fffilimonov@yandex.ru>
date Mon, 05 Aug 2024 11:29:06 +0300
parents dfc36e7ed22c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
1 // followlines.js - JavaScript utilities for followlines UI
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
2 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
3 // Copyright 2017 Logilab SA <contact@logilab.fr>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
4 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
7
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
8 //** Install event listeners for line block selection and followlines action */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
9 document.addEventListener('DOMContentLoaded', function() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
10 var sourcelines = document.getElementsByClassName('sourcelines')[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
11 if (typeof sourcelines === 'undefined') {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
12 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
13 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
14 // URL to complement with "linerange" query parameter
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
15 var targetUri = sourcelines.dataset.logurl;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
16 if (typeof targetUri === 'undefined') {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
17 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
18 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
19
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
20 // Tag of children of "sourcelines" element on which to add "line
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
21 // selection" style.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
22 var selectableTag = sourcelines.dataset.selectabletag;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
23 if (typeof selectableTag === 'undefined') {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
24 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
25 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
26
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
27 var isHead = parseInt(sourcelines.dataset.ishead || "0");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
28
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
29 //* position "element" on top-right of cursor */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
30 function positionTopRight(element, event) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
31 var x = (event.clientX + 10) + 'px',
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
32 y = (event.clientY - 20) + 'px';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
33 element.style.top = y;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
34 element.style.left = x;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
35 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
36
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
37 // retrieve all direct *selectable* children of class="sourcelines"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
38 // element
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
39 var selectableElements = Array.prototype.filter.call(
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
40 sourcelines.children,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
41 function(x) { return x.tagName === selectableTag; });
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
42
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
43 var btnTitleStart = 'start following lines history from here';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
44 var btnTitleEnd = 'terminate line block selection here';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
45
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
46 //** return a <button> element with +/- spans */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
47 function createButton() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
48 var btn = document.createElement('button');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
49 btn.title = btnTitleStart;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
50 btn.classList.add('btn-followlines');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
51 var plusSpan = document.createElement('span');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
52 plusSpan.classList.add('followlines-plus');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
53 plusSpan.textContent = '+';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
54 btn.appendChild(plusSpan);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
55 var br = document.createElement('br');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
56 btn.appendChild(br);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
57 var minusSpan = document.createElement('span');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
58 minusSpan.classList.add('followlines-minus');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
59 minusSpan.textContent = '−';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
60 btn.appendChild(minusSpan);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
61 return btn;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
62 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
63
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
64 // extend DOM with CSS class for selection highlight and action buttons
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
65 var followlinesButtons = [];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
66 for (var i = 0; i < selectableElements.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
67 selectableElements[i].classList.add('followlines-select');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
68 var btn = createButton();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
69 followlinesButtons.push(btn);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
70 // insert the <button> as child of `selectableElements[i]` unless the
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
71 // latter has itself a child with a "followlines-btn-parent" class
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
72 // (annotate view)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
73 var btnSupportElm = selectableElements[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
74 var childSupportElms = btnSupportElm.getElementsByClassName(
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
75 'followlines-btn-parent');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
76 if ( childSupportElms.length > 0 ) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
77 btnSupportElm = childSupportElms[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
78 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
79 var refNode = btnSupportElm.childNodes[0]; // node to insert <button> before
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
80 btnSupportElm.insertBefore(btn, refNode);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
81 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
82
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
83 // ** re-initialize followlines buttons */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
84 function resetButtons() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
85 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
86 var btn = followlinesButtons[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
87 btn.title = btnTitleStart;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
88 btn.classList.remove('btn-followlines-end');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
89 btn.classList.remove('btn-followlines-hidden');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
90 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
91 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
92
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
93 var lineSelectedCSSClass = 'followlines-selected';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
94
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
95 //** add CSS class on selectable elements in `from`-`to` line range */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
96 function addSelectedCSSClass(from, to) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
97 for (var i = from; i <= to; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
98 selectableElements[i].classList.add(lineSelectedCSSClass);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
99 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
100 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
101
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
102 //** remove CSS class from previously selected lines */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
103 function removeSelectedCSSClass() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
104 var elements = sourcelines.getElementsByClassName(
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
105 lineSelectedCSSClass);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
106 while (elements.length) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
107 elements[0].classList.remove(lineSelectedCSSClass);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
108 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
109 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
110
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
111 // ** return the element of type "selectableTag" parent of `element` */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
112 function selectableParent(element) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
113 var parent = element.parentElement;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
114 if (parent === null) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
115 return null;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
116 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
117 if (element.tagName === selectableTag && parent.isSameNode(sourcelines)) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
118 return element;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
119 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
120 return selectableParent(parent);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
121 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
122
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
123 // ** update buttons title and style upon first click */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
124 function updateButtons(selectable) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
125 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
126 var btn = followlinesButtons[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
127 btn.title = btnTitleEnd;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
128 btn.classList.add('btn-followlines-end');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
129 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
130 // on clicked button, change title to "cancel"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
131 var clicked = selectable.getElementsByClassName('btn-followlines')[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
132 clicked.title = 'cancel';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
133 clicked.classList.remove('btn-followlines-end');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
134 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
135
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
136 //** add `listener` on "click" event for all `followlinesButtons` */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
137 function buttonsAddEventListener(listener) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
138 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
139 followlinesButtons[i].addEventListener('click', listener);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
140 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
141 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
142
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
143 //** remove `listener` on "click" event for all `followlinesButtons` */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
144 function buttonsRemoveEventListener(listener) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
145 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
146 followlinesButtons[i].removeEventListener('click', listener);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
147 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
148 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
149
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
150 //** event handler for "click" on the first line of a block */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
151 function lineSelectStart(e) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
152 var startElement = selectableParent(e.target.parentElement);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
153 if (startElement === null) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
154 // not a "selectable" element (maybe <a>): abort, keeping event
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
155 // listener registered for other click with a "selectable" target
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
156 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
157 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
158
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
159 // update button tooltip text and CSS
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
160 updateButtons(startElement);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
161
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
162 var startId = parseInt(startElement.id.slice(1));
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
163 startElement.classList.add(lineSelectedCSSClass); // CSS
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
164
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
165 // remove this event listener
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
166 buttonsRemoveEventListener(lineSelectStart);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
167
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
168 //** event handler for "click" on the last line of the block */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
169 function lineSelectEnd(e) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
170 var endElement = selectableParent(e.target.parentElement);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
171 if (endElement === null) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
172 // not a <span> (maybe <a>): abort, keeping event listener
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
173 // registered for other click with <span> target
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
174 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
175 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
176
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
177 // remove this event listener
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
178 buttonsRemoveEventListener(lineSelectEnd);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
179
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
180 // reset button tooltip text
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
181 resetButtons();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
182
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
183 // compute line range (startId, endId)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
184 var endId = parseInt(endElement.id.slice(1));
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
185 if (endId === startId) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
186 // clicked twice the same line, cancel and reset initial state
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
187 // (CSS, event listener for selection start)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
188 removeSelectedCSSClass();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
189 buttonsAddEventListener(lineSelectStart);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
190 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
191 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
192 var inviteElement = endElement;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
193 if (endId < startId) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
194 var tmp = endId;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
195 endId = startId;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
196 startId = tmp;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
197 inviteElement = startElement;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
198 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
199
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
200 addSelectedCSSClass(startId - 1, endId -1); // CSS
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
201
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
202 // append the <div id="followlines"> element to last line of the
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
203 // selection block
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
204 var divAndButton = followlinesBox(targetUri, startId, endId, isHead);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
205 var div = divAndButton[0],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
206 button = divAndButton[1];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
207 inviteElement.appendChild(div);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
208 // set position close to cursor (top-right)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
209 positionTopRight(div, e);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
210 // hide all buttons
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
211 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
212 followlinesButtons[i].classList.add('btn-followlines-hidden');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
213 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
214
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
215 //** event handler for cancelling selection */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
216 function cancel() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
217 // remove invite box
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
218 div.parentNode.removeChild(div);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
219 // restore initial event listeners
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
220 buttonsAddEventListener(lineSelectStart);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
221 buttonsRemoveEventListener(cancel);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
222 for (var i = 0; i < followlinesButtons.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
223 followlinesButtons[i].classList.remove('btn-followlines-hidden');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
224 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
225 // remove styles on selected lines
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
226 removeSelectedCSSClass();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
227 resetButtons();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
228 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
229
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
230 // bind cancel event to click on <button>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
231 button.addEventListener('click', cancel);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
232 // as well as on an click on any source line
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
233 buttonsAddEventListener(cancel);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
234 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
235
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
236 buttonsAddEventListener(lineSelectEnd);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
237
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
238 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
239
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
240 buttonsAddEventListener(lineSelectStart);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
241
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
242 //** return a <div id="followlines"> and inner cancel <button> elements */
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
243 function followlinesBox(targetUri, fromline, toline, isHead) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
244 // <div id="followlines">
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
245 var div = document.createElement('div');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
246 div.id = 'followlines';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
247
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
248 // <div class="followlines-cancel">
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
249 var buttonDiv = document.createElement('div');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
250 buttonDiv.classList.add('followlines-cancel');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
251
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
252 // <button>x</button>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
253 var button = document.createElement('button');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
254 button.textContent = 'x';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
255 buttonDiv.appendChild(button);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
256 div.appendChild(buttonDiv);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
257
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
258 // <div class="followlines-link">
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
259 var aDiv = document.createElement('div');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
260 aDiv.classList.add('followlines-link');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
261 aDiv.textContent = 'follow history of lines ' + fromline + ':' + toline + ':';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
262 var linesep = document.createElement('br');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
263 aDiv.appendChild(linesep);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
264 // link to "ascending" followlines
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
265 var aAsc = document.createElement('a');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
266 var url = targetUri + '?patch=&linerange=' + fromline + ':' + toline;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
267 aAsc.setAttribute('href', url);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
268 aAsc.textContent = 'older';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
269 aDiv.appendChild(aAsc);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
270
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
271 if (!isHead) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
272 var sep = document.createTextNode(' / ');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
273 aDiv.appendChild(sep);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
274 // link to "descending" followlines
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
275 var aDesc = document.createElement('a');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
276 aDesc.setAttribute('href', url + '&descend=');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
277 aDesc.textContent = 'newer';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
278 aDiv.appendChild(aDesc);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
279 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
280
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
281 div.appendChild(aDiv);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
282
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
283 return [div, button];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
284 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
285
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
286 }, false);