1- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
1+ /* ------------------------------------------------------------------
2+ copy.js – unified copy handling for:
3+ 1Column-wide copy (your original implementation)
4+ 2Row-specific copy (button inside the Query Name cell)
5+ ------------------------------------------------------------------ */
6+
7+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
28 const messageElement = document . getElementById ( 'message' ) ;
39
10+ /* --------------------------------------------------------------
11+ Helper: show the temporary "Copied!" tooltip near a button
12+ -------------------------------------------------------------- */
13+ function showCopyMessage ( btn ) {
14+ const rect = btn . getBoundingClientRect ( ) ;
15+ messageElement . style . left = `${ rect . left + window . scrollX } px` ;
16+ messageElement . style . top = `${ rect . bottom + window . scrollY + 10 } px` ;
17+ messageElement . classList . add ( 'show' ) ;
18+ setTimeout ( ( ) => messageElement . classList . remove ( 'show' ) , 1000 ) ;
19+ }
20+
21+ /* --------------------------------------------------------------
22+ 1Column-wide copy - keep your original logic unchanged
23+ -------------------------------------------------------------- */
424 document . querySelectorAll ( '.copyButton' ) . forEach ( button => {
5- button . addEventListener ( 'click' , function ( event ) {
25+ button . addEventListener ( 'click' , function ( ) {
626 const tableId = button . getAttribute ( 'data-table-id' ) ;
727 const columnIndex = parseInt ( button . getAttribute ( 'data-column-index' ) , 10 ) ;
8-
928 const table = document . getElementById ( tableId ) ;
29+
1030 if ( ! table ) {
1131 console . error ( 'Table not found:' , tableId ) ;
1232 return ;
1333 }
1434
1535 const rows = table . getElementsByTagName ( 'tr' ) ;
16- let columnValues = [ ] ;
36+ const columnValues = [ ] ;
1737
1838 for ( let row of rows ) {
1939 const cell = row . getElementsByTagName ( 'td' ) [ columnIndex ] ;
@@ -23,20 +43,46 @@ document.addEventListener('DOMContentLoaded', function() {
2343 }
2444
2545 const columnText = columnValues . join ( '\n' ) ;
26- console . log ( 'Text to copy:' , columnText ) ; // Log the text to be copied
27-
28- navigator . clipboard . writeText ( columnText ) . then ( function ( ) {
29- // Position the message near the clicked button
30- const buttonRect = button . getBoundingClientRect ( ) ;
31- messageElement . style . left = `${ buttonRect . left + window . scrollX } px` ;
32- messageElement . style . top = `${ buttonRect . bottom + window . scrollY + 10 } px` ;
33- messageElement . classList . add ( 'show' ) ;
34- setTimeout ( ( ) => {
35- messageElement . classList . remove ( 'show' ) ;
36- } , 1000 ) ; // Hide the message after 2 seconds
37- } ) . catch ( function ( err ) {
38- console . error ( 'Could not copy text: ' , err ) ;
39- } ) ;
46+ console . log ( 'Text to copy (column):' , columnText ) ;
47+
48+ navigator . clipboard . writeText ( columnText )
49+ . then ( ( ) => showCopyMessage ( button ) )
50+ . catch ( err => console . error ( 'Could not copy column text:' , err ) ) ;
51+ } ) ;
52+ } ) ;
53+
54+ /* --------------------------------------------------------------
55+ 2Row-specific copy – new logic for the button inside the name cell
56+ Use class "copyBtnRow" (or reuse "copyButton" if you prefer)
57+ -------------------------------------------------------------- */
58+ document . querySelectorAll ( '.copyBtnRow' ) . forEach ( button => {
59+ button . addEventListener ( 'click' , function ( ) {
60+ // Find the row that contains this button
61+ const row = button . closest ( 'tr' ) ;
62+ if ( ! row ) {
63+ console . error ( 'Button not inside a table row' ) ;
64+ return ;
65+ }
66+
67+ // Assuming the query text is in the second cell (index 1)
68+ const queryCell = row . cells [ 1 ] ;
69+ if ( ! queryCell ) {
70+ console . error ( 'Expected query text cell missing' ) ;
71+ return ;
72+ }
73+
74+ const queryText = queryCell . textContent . trim ( ) ;
75+ console . log ( 'Text to copy (row):' , queryText ) ;
76+
77+ navigator . clipboard . writeText ( queryText )
78+ . then ( ( ) => {
79+ const originalLabel = button . textContent ;
80+ button . textContent = 'Copied!' ;
81+ setTimeout ( ( ) => {
82+ button . textContent = originalLabel ;
83+ } , 750 ) ;
84+ } )
85+ . catch ( err => console . error ( 'Could not copy row text:' , err ) ) ;
4086 } ) ;
4187 } ) ;
4288} ) ;
0 commit comments