2
0
forked from Wavyzz/dolibarr

NEW Add property "copytoclipboard" in modulebuilder

This commit is contained in:
Laurent Destailleur
2021-08-24 13:19:53 +02:00
parent 56c314c671
commit 366fa13fc0
7 changed files with 64 additions and 21 deletions

View File

@@ -232,23 +232,39 @@ print '
jQuery(\'.clipboardCPButton, .clipboardCPValueToPrint\').click(function() {
/* console.log(this.parentNode); */
console.log("We click on a clipboardCPButton or clipboardCPValueToPrint class");
if (window.getSelection) {
selection = window.getSelection();
console.log("We click on a clipboardCPButton or clipboardCPValueToPrint class and we want to copy content of clipboardCPValue class");
if (window.getSelection) {
range = document.createRange();
/* We select value to print using the parent. */
/* We should use the class clipboardCPValue but it may have several element with copy/paste so class to select is not enough */
range.selectNodeContents(this.parentNode.firstChild);
selection.removeAllRanges();
selection.addRange( range );
selection = window.getSelection(); /* get the object used for selection */
selection.removeAllRanges(); /* clear current selection */
selection.addRange(range); /* make the new selection with the value to copy */
}
document.execCommand( \'copy\' );
/* copy selection into clipboard */
var succeed;
try {
succeed = document.execCommand(\'copy\');
} catch(e) {
succeed = false;
}
/* Remove the selection to avoid to see the hidden field to copy selected */
window.getSelection().removeAllRanges();
/* Show message */
var lastchild = this.parentNode.lastChild;
var tmp = lastchild.innerHTML
if (succeed) {
lastchild.innerHTML = \''.dol_escape_js($langs->trans('CopiedToClipboard')).'\';
} else {
lastchild.innerHTML = \''.dol_escape_js($langs->trans('Error')).'\';
}
setTimeout(() => { lastchild.innerHTML = tmp; }, 1000);
});
});'."\n";

View File

@@ -1286,9 +1286,9 @@ function dol_escape_json($stringtoescape)
* Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
*
* @param string $stringtoescape String to escape
* @param int $keepb 1=Keep b tags, 0=remove them completeley
* @param int $keepb 1=Keep b tags, 0=remove them completely
* @param int $keepn 1=Preserve \r\n strings (otherwise, replace them with escaped value). Set to 1 when escaping for a <textarea>.
* @param string $noescapetags '' or 'common' or list of tags to not escape
* @param string $noescapetags '' or 'common' or list of tags to not escape. TODO Does not works yet when there is attributes to tag.
* @param int $escapeonlyhtmltags 1=Escape only html tags, not the special chars like accents.
* @return string Escaped string
* @see dol_string_nohtmltag(), dol_string_nospecial(), dol_string_unaccent()
@@ -1296,7 +1296,7 @@ function dol_escape_json($stringtoescape)
function dol_escape_htmltag($stringtoescape, $keepb = 0, $keepn = 0, $noescapetags = '', $escapeonlyhtmltags = 0)
{
if ($noescapetags == 'common') {
$noescapetags = 'html,body,a,b,em,i,u,ul,li,br,div,img,font,p,span,strong,table,tr,td,th,tbody';
$noescapetags = 'html,body,a,b,em,hr,i,u,ul,li,br,div,img,font,p,span,strong,table,tr,td,th,tbody';
}
// escape quotes and backslashes, newlines, etc.
@@ -1316,15 +1316,16 @@ function dol_escape_htmltag($stringtoescape, $keepb = 0, $keepn = 0, $noescapeta
return htmlspecialchars($tmp, ENT_COMPAT, 'UTF-8');
} else {
// Escape tags to keep
// TODO Does not works yet when there is attributes to tag
$tmparrayoftags = array();
if ($noescapetags) {
$tmparrayoftags = explode(',', $noescapetags);
}
if (count($tmparrayoftags)) {
foreach ($tmparrayoftags as $tagtoreplace) {
$tmp = str_ireplace('<'.$tagtoreplace.'>', '__BEGINTAGTOREPLACE'.$tagtoreplace.'__', $tmp);
$tmp = str_ireplace('</'.$tagtoreplace.'>', '__ENDTAGTOREPLACE'.$tagtoreplace.'__', $tmp);
$tmp = str_ireplace('<'.$tagtoreplace.' />', '__BEGINENDTAGTOREPLACE'.$tagtoreplace.'__', $tmp);
}
}
@@ -1334,6 +1335,7 @@ function dol_escape_htmltag($stringtoescape, $keepb = 0, $keepn = 0, $noescapeta
foreach ($tmparrayoftags as $tagtoreplace) {
$result = str_ireplace('__BEGINTAGTOREPLACE'.$tagtoreplace.'__', '<'.$tagtoreplace.'>', $result);
$result = str_ireplace('__ENDTAGTOREPLACE'.$tagtoreplace.'__', '</'.$tagtoreplace.'>', $result);
$result = str_ireplace('__BEGINENDTAGTOREPLACE'.$tagtoreplace.'__', '<'.$tagtoreplace.' />', $result);
}
}
@@ -10453,7 +10455,7 @@ function readfileLowMemory($fullpath_original_file_osencoded, $method = -1)
/**
* Create a button to copy $valuetocopy in the clipboard.
* Code that handle the click is inside lib_foot.jsp.php
* Code that handle the click is inside lib_foot.jsp.php.
*
* @param string $valuetocopy The value to print
* @param int $showonlyonhover Show the copy-paste button only on hover
@@ -10469,10 +10471,11 @@ function showValueWithClipboardCPButton($valuetocopy, $showonlyonhover = 1, $tex
$showonlyonhover = 0;
}*/
$tag = 'span'; // Using div does not work when using the js copy code.
if ($texttoshow) {
$result = '<span class="clipboardCP'.($showonlyonhover ? ' clipboardCPShowOnHover' : '').'"><span class="clipboardCPValue hidewithsize">'.$valuetocopy.'</span><span class="clipboardCPValueToPrint">'.$texttoshow.'</span><span class="clipboardCPButton far fa-clipboard opacitymedium paddingleft paddingright"></span><span class="clipboardCPText opacitymedium"></span></span>';
$result = '<span class="clipboardCP'.($showonlyonhover ? ' clipboardCPShowOnHover' : '').'"><'.$tag.' class="clipboardCPValue hidewithsize">'.dol_escape_htmltag($valuetocopy, 1, 1).'</'.$tag.'><span class="clipboardCPValueToPrint">'.dol_escape_htmltag($texttoshow, 1, 1).'</span><span class="clipboardCPButton far fa-clipboard opacitymedium paddingleft paddingright"></span><span class="clipboardCPText opacitymedium"></span></span>';
} else {
$result = '<span class="clipboardCP'.($showonlyonhover ? ' clipboardCPShowOnHover' : '').'"><span class="clipboardCPValue">'.$valuetocopy.'</span><span class="clipboardCPButton far fa-clipboard opacitymedium paddingleft paddingright"></span><span class="clipboardCPText opacitymedium"></span></span>';
$result = '<span class="clipboardCP'.($showonlyonhover ? ' clipboardCPShowOnHover' : '').'"><'.$tag.' class="clipboardCPValue">'.dol_escape_htmltag($valuetocopy, 1, 1).'</'.$tag.'><span class="clipboardCPButton far fa-clipboard opacitymedium paddingleft paddingright"></span><span class="clipboardCPText opacitymedium"></span></span>';
}
return $result;

View File

@@ -66,9 +66,13 @@ foreach ($object->fields as $key => $val) {
print '">';
if (!empty($val['help'])) {
print $form->textwithpicto($langs->trans($val['label']), $langs->trans($val['help']));
} else {
if (isset($val['copytoclipboard']) && $val['copytoclipboard'] == 1) {
print showValueWithClipboardCPButton($value, 0, $langs->transnoentitiesnoconv($val['label']));
} else {
print $langs->trans($val['label']);
}
}
print '</td>';
print '<td class="valuefield fieldname_'.$key;
if ($val['type'] == 'text') {
@@ -86,9 +90,14 @@ foreach ($object->fields as $key => $val) {
$labellang = ($value ? $langs->trans('Language_'.$value) : '');
print picto_from_langcode($value, 'class="paddingrightonly saturatemedium opacitylow"');
print $labellang;
} else {
if (isset($val['copytoclipboard']) && $val['copytoclipboard'] == 2) {
$out = $object->showOutputField($val, $key, $value, '', '', '', 0);
print showValueWithClipboardCPButton($out, 0, $out);
} else {
print $object->showOutputField($val, $key, $value, '', '', '', 0);
}
}
//print dol_escape_htmltag($object->$key, 1, 1);
if (in_array($val['type'], array('text', 'html'))) {
print '</div>';

View File

@@ -90,6 +90,7 @@ class KnowledgeRecord extends CommonObject
* 'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
* 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
* 'comment' is not used. You can store here any text of your choice. It is not used by application.
* 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value)
*
* Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
*/
@@ -101,9 +102,9 @@ class KnowledgeRecord extends CommonObject
public $fields=array(
'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>10, 'notnull'=>1, 'default'=>'(PROV)', 'visible'=>5, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object"),
'question' => array('type'=>'text', 'label'=>'Question', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'csslist'=>'tdoverflow300'),
'answer' => array('type'=>'html', 'label'=>'Solution', 'enabled'=>'1', 'position'=>50, 'notnull'=>0, 'visible'=>3, 'csslist'=>'tdoverflow300'),
'lang' => array('type'=>'varchar(6)', 'label'=>'Language', 'enabled'=>'1', 'position'=>60, 'notnull'=>0, 'visible'=>1),
'question' => array('type'=>'text', 'label'=>'Question', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'csslist'=>'tdoverflow300', 'copytoclipboard'=>1),
'lang' => array('type'=>'varchar(6)', 'label'=>'Language', 'enabled'=>'1', 'position'=>40, 'notnull'=>0, 'visible'=>1),
'answer' => array('type'=>'html', 'label'=>'Solution', 'enabled'=>'1', 'position'=>50, 'notnull'=>0, 'visible'=>3, 'csslist'=>'tdoverflow300', 'copytoclipboard'=>1),
'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>'1', 'position'=>500, 'notnull'=>1, 'visible'=>-2,),
'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>'1', 'position'=>501, 'notnull'=>0, 'visible'=>-2,),
'last_main_doc' => array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>'1', 'position'=>600, 'notnull'=>0, 'visible'=>0,),

View File

@@ -92,6 +92,8 @@ class MyObject extends CommonObject
* 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
* 'comment' is not used. You can store here any text of your choice. It is not used by application.
* 'validate' is 1 if need to validate with $this->validateField()
* 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value)
*
* Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
*/

View File

@@ -6815,7 +6815,7 @@ div.phpdebugbar-widgets-templates a.phpdebugbar-widgets-editor-link:before
/* For copy-paste feature */
/* ============================================================================== */
span.clipboardCPValueToPrint {
span.clipboardCPValueToPrint, div.clipboardCPValueToPrint {
display: inline-block;
}
span.clipboardCPValue.hidewithsize {
@@ -6824,6 +6824,12 @@ span.clipboardCPValue.hidewithsize {
color: transparent;
white-space: nowrap;
}
div.clipboardCPValue.hidewithsize {
width: 0 !important;
display: none;
color: transparent;
white-space: nowrap;
}
.clipboardCPShowOnHover .clipboardCPButton {
display: none;

View File

@@ -6690,7 +6690,7 @@ div.phpdebugbar-widgets-templates a.phpdebugbar-widgets-editor-link:before
/* For copy-paste feature */
/* ============================================================================== */
span.clipboardCPValueToPrint {
span.clipboardCPValueToPrint, div.clipboardCPValueToPrint {
display: inline-block;
}
span.clipboardCPValue.hidewithsize {
@@ -6699,6 +6699,12 @@ span.clipboardCPValue.hidewithsize {
color: transparent;
white-space: nowrap;
}
div.clipboardCPValue.hidewithsize {
width: 0 !important;
display: none;
color: transparent;
white-space: nowrap;
}
.clipboardCPShowOnHover .clipboardCPButton {
display: none;