Skip to content

Commit aeaa073

Browse files
authored
New Feature for developers: Copyable cells in grids (#4029)
1 parent 2a9dcb2 commit aeaa073

File tree

8 files changed

+720
-520
lines changed

8 files changed

+720
-520
lines changed

app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* @method $this setFormat(string $value)
2828
* @method string getIndex()
2929
* @method bool getNoLink()
30+
* @method bool getCopyable()
31+
* @method $this setCopyable(bool $value)
3032
*/
3133
class Mage_Adminhtml_Block_Widget_Grid_Column extends Mage_Adminhtml_Block_Widget
3234
{
@@ -162,6 +164,10 @@ public function getRowField(Varien_Object $row)
162164
$renderedValue = call_user_func($frameCallback, $renderedValue, $row, $this, false);
163165
}
164166

167+
if ($this->getCopyable() && $text = $this->getRenderer()->getCopyableText($row)) {
168+
$renderedValue = '<span data-copy-text="' . $text . '">' . $renderedValue . '</span>';
169+
}
170+
165171
return $renderedValue;
166172
}
167173

app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Abstract.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,13 @@ public function renderCss()
163163
{
164164
return $this->getColumn()->getCssClass();
165165
}
166+
167+
/**
168+
* @param Varien_Object $row
169+
* @return string|null
170+
*/
171+
public function getCopyableText(Varien_Object $row)
172+
{
173+
return $this->_getValue($row);
174+
}
166175
}

js/mage/adminhtml/tools.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,47 @@ function sortNumeric(val1, val2)
731731
{
732732
return val1 - val2;
733733
}
734+
735+
/**
736+
* Adds copy icons to elements that have the class 'copy-text'
737+
*/
738+
function addCopyIcons() {
739+
if (navigator.clipboard === undefined) {
740+
return;
741+
}
742+
743+
const copyTexts = document.querySelectorAll('[data-copy-text]');
744+
copyTexts.forEach(copyText => {
745+
const iconElement = createCopyIconElement();
746+
copyText.parentNode.appendChild(iconElement);
747+
});
748+
}
749+
750+
/**
751+
* @return {HTMLElement} The created copy icon element
752+
*/
753+
function createCopyIconElement() {
754+
const copyIcon = document.createElement('span');
755+
copyIcon.classList.add('icon-copy');
756+
copyIcon.setAttribute('onclick', 'copyText(event)');
757+
copyIcon.setAttribute('title', Translator.translate('Copy text to clipboard'));
758+
759+
return copyIcon;
760+
}
761+
762+
/**
763+
* Copies the text from the data-text attribute of the clicked element to the clipboard
764+
*
765+
* @param {Event} event - The event object triggered by the click event
766+
*/
767+
function copyText(event) {
768+
event.stopPropagation();
769+
event.preventDefault();
770+
const copyIcon = event.currentTarget;
771+
const copyText = copyIcon.previousElementSibling.getAttribute('data-copy-text');
772+
navigator.clipboard.writeText(copyText);
773+
copyIcon.classList.add('icon-copy-copied');
774+
setTimeout(() => {
775+
copyIcon.classList.remove('icon-copy-copied');
776+
}, 1000);
777+
}

js/varien/js.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -703,47 +703,3 @@ function buttonDisabler() {
703703
button.disabled = true;
704704
});
705705
}
706-
707-
/**
708-
* Adds copy icons to elements that have the class 'copy-text'
709-
*/
710-
function addCopyIcons() {
711-
if (navigator.clipboard === undefined) {
712-
return;
713-
}
714-
715-
const copyTexts = document.querySelectorAll('[data-copy-text]');
716-
copyTexts.forEach(copyText => {
717-
const iconElement = createCopyIconElement();
718-
copyText.parentNode.appendChild(iconElement);
719-
});
720-
}
721-
722-
/**
723-
* @return {HTMLElement} The created copy icon element
724-
*/
725-
function createCopyIconElement() {
726-
const copyIcon = document.createElement('span');
727-
copyIcon.classList.add('icon-copy');
728-
copyIcon.setAttribute('onclick', 'copyText(event)');
729-
copyIcon.setAttribute('title', Translator.translate('Copy text to clipboard'));
730-
731-
return copyIcon;
732-
}
733-
734-
/**
735-
* Copies the text from the data-text attribute of the clicked element to the clipboard
736-
*
737-
* @param {Event} event - The event object triggered by the click event
738-
*/
739-
function copyText(event) {
740-
event.stopPropagation();
741-
event.preventDefault();
742-
const copyIcon = event.currentTarget;
743-
const copyText = copyIcon.previousElementSibling.getAttribute('data-copy-text');
744-
navigator.clipboard.writeText(copyText);
745-
copyIcon.classList.add('icon-copy-copied');
746-
setTimeout(() => {
747-
copyIcon.classList.remove('icon-copy-copied');
748-
}, 1000);
749-
}

skin/adminhtml/default/default/boxes.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ tr.dynamic-grid input.input-text { width:154px; }
220220
.available { color:#080; font-weight:bold; }
221221
.not-available { color:#800; }
222222

223+
/* Grid - Copyable Cells */
224+
.icon-copy {
225+
display: inline-block;
226+
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 111.07 122.88"><path d="M97.67 20.81h0l.01.02a13.38 13.38 0 0 1 9.46 3.93 13.32 13.32 0 0 1 3.9 9.42h.02v.02 75.28.01h-.02c-.01 3.68-1.51 7.03-3.93 9.46a13.32 13.32 0 0 1-9.42 3.9v.02h-.02-59.19-.01v-.02c-3.69-.01-7.04-1.5-9.46-3.93a13.37 13.37 0 0 1-3.91-9.42h0V34.2v-.01h.02c.01-3.69 1.52-7.04 3.94-9.46 2.41-2.4 5.73-3.9 9.42-3.91v-.02h.02l59.17.01h0zM.02 75.38L0 13.39v-.01h.02a13.44 13.44 0 0 1 3.93-9.46A13.37 13.37 0 0 1 13.37.01V0h.02 59.19c7.69 0 8.9 9.96.01 10.16H13.4h-.02v-.02c-.88 0-1.68.37-2.27.97-.59.58-.96 1.4-.96 2.27h.02v.01 3.17 58.81c0 8.26-10.15 8.72-10.15.01h0zm100.89 34.11V34.2v-.02h.02c0-.87-.37-1.68-.97-2.27-.59-.58-1.4-.96-2.28-.96v.02h-.01-59.19-.02v-.02c-.88 0-1.68.38-2.27.97-.59.58-.96 1.4-.96 2.27h.02v.01 75.28.02h-.02c0 .88.38 1.68.97 2.27s1.4.96 2.27.96v-.02h.01 59.19.02v.02c.87 0 1.68-.38 2.27-.97.59-.58.96-1.4.96-2.27h-.01 0 0 0z" fill-rule="evenodd"/></svg>');
227+
background-repeat: no-repeat;
228+
background-size: contain;
229+
cursor: pointer;
230+
width: 12px;
231+
height: 12px;
232+
margin: 2px 0 0 2px;
233+
}
234+
235+
.entry-edit-head .icon-copy { width: 16px; height: 16px; margin: 8px 0 0 5px; }
236+
237+
.icon-copy-copied { background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 30 30"><path d="M26.98 5.99a1 1 0 0 0-.687.303L11 21.586l-6.293-6.293a1 1 0 1 0-1.414 1.414l7 7a1 1 0 0 0 1.414 0l16-16a1 1 0 0 0-.727-1.717z"/></svg>'); }
223238

224239
/* ACCORDION
225240
*******************************************************************************/

0 commit comments

Comments
 (0)