Context
The real free software (GPLv3) application (motivating this question) that I am coding is the MELT monitor on github (FWIW, I am at commit 56a83d358d3966eddb55... on February 11th, 2016). It is on Linux/x86_64/Debian, Firefox/44, JQuery-2.2.0, JqueryUI 1.11.4, clipboard.js 1.5.8....
A more open (but without code) related question was downvoted on Programmers. So I made a JsFiddle example for this very question, but that JsFiddle example is probably very wrong.
I have a (dynamically generated) HTML document with some
<span>
-s ofclass='momitemref_cl'
like<span class='momitemref_cl'>this</span>
etc... (the inside content of such spans is always a single word likethis
, actually some variable name or identifier in some DSL of mine). My goal is to have a dynamically generated menu for each such spans to enable some operations on them, in particular: hilight every occurrence of that word, and copy & select the word to the browser's and desktop's clipboard.
There are many questions here about copying to the clipboard in Javascript, such as this one. Several of them mention Zeno Rocha's clipboard.js which I am trying to use.
Code with explanation
To explain some of the JsFiddle: I am creating the JQueryUI menu with
// make a menu for the given span
function make_menu(spa) {
console.log("make_menu spa=", spa);
var name = $(spa).text();
mom_menuitemcount++;
var menuid = "menuid_" + mom_menuitemcount;
console.log("make_menu name=", name, " menuid=", menuid);
$maindiv.after("<ul class='mommenu_cl' id='" + menuid + "'>"
+ "<li class='ui-state-disabled'>* <i>" + name + "</i> *</li>"
// the text inside the following <li> matters
+ "<li>Hilight</li>" + "<li>Copy</li>" + "</ul>");
var mymenu = $('#' + menuid);
mymenu.menu({
select: function(ev, ui) {
var uitem = ui.item;
var utext = uitem.text();
console.log("mymenu-select ev=", ev, " ui=", ui, " name=", name,
" uitem=", uitem, " utext=", utext);
switch (utext) {
case "Hilight":
$maindiv.find(".momitemref_cl").each(function(ix, el) {
var etext = $(el).text();
console.log("hilighting el=", el, " etext=", etext);
if (etext == name) {
$(el).toggleClass("momhilight_cl");
}
});
break;
case "Copy":
//// what should go here?
break;
};
setTimeout(200, destroy_menu);
},
position: {
my: "left top",
at: "bottom left",
of: spa
}
});
mom_menuitem = mymenu;
console.log("make_menu spa=", spa, " mymenu=", mymenu);
return mymenu;
}
In the above code, I don't know what to put in //// what should go here?
; perhaps it might be something similar to:
uitem.select();
document.execCommand('Copy');
but AFAIU this don't work as expected, and I feel that clipboard operations need the user event (e.g. a mouse click or down) trigerring them.
The clipboard is initialized with
$clipboardh = new Clipboard(".momitemref_cl", {
text: function (trig) {
console.log("clipboard text trig=", trig);
/// some code is missing here probably
};
});
I guess (but I am not sure) that the /// some code is missing here probably
should return the text value of the relevant span.
Questions
So I don't know how to copy the span's content to the clipboard (//// what should go here?
....) and how to put into the clipboard its text (/// some code is missing here probably
....)
The full MVCE code is this JsFiddle
In other words, how to use clipboard.js
from a JQueryUI menu item to copy a span
's content to the browser's clipboard ??
Perhaps the entire approach is wrong, and perhaps I should use contextmenu
(but then, how to customize it only for span
-s of class='momitemref_cl'
?)
To make things even more complex, I actually have in MELT monitor (but not in this JsFiddle...) two CSS classes mom_itemref_cl
& mom_itemval_cl
that I want to behave likewise.
PS. I messed the JsFiddle when copying code here. I updated it, https://jsfiddle.net/bstarynk/g2notLd7/132/
NB. I could be satisfied with an answer working only on recent Firefox & Chrome.