Skip to content

Instantly share code, notes, and snippets.

@ddallala
Created March 25, 2010 17:41
Show Gist options
  • Save ddallala/343872 to your computer and use it in GitHub Desktop.
Save ddallala/343872 to your computer and use it in GitHub Desktop.
Copy Data to Clipboard (IE, Firefox)
copyToClipboard = function(data) {
data += '';
var IE=window.external&&(navigator.platform=="Win32"||(window.ScriptEngine&&ScriptEngine().indexOf("InScript")+1));
var FF=navigator.userAgent.toLowerCase();
var GC=(FF.indexOf("chrome")+1)?true:false;
var FF=(FF.indexOf("firefox")+1)?true:false;
var OP=window.opera&&window.print;
var NS=window.netscape&&!OP;
if(window.clipboardData||NS){
if(IE&&!FF){
if(!window.clipboardData.setData("Text",data)){
alert("Text was not copied to clipboard!");
return false
}
}else{
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
}catch(e){
if(confirm("Your current Internet Security settings do not allow data copying to clipboard. Do you want to learn more about enabling data copying to clipboard in your browser?"))
window.open('http://www.febooti.com/support/website-help/website-javascript-copy-clipboard.html');
else
alert("Text was not copied to clipboard!");
return false
}
try{
e=Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard)
}catch(e){
return false
}
try{
b=Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable)
}catch(e){
return false
}
b.addDataFlavor("text/unicode");
o=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
o.data=data;
b.setTransferData("text/unicode",o,data.length*2);
try{
t=Components.interfaces.nsIClipboard
} catch(e){
return false
}
e.setData(b,null,t.kGlobalClipboard)
}
} else {
alert("Your browser doesn't support copy to clipboard feature. Please select text and press CTRL+C.");
return false
}
alert("Text successfully copied to clipboard:\n\n"+data);
return false;
},
// Ext.ux.GridExtensions
Ext.namespace('Ext.ux');
Ext.ux.clearClipBoard = function(){
if (Ext.isIE) {
window.clipboardData.setData("Text", '');
}
};
Ext.ux.ClipboardGrid = function(config) {
this.CopyCellText = config.CopyCellText || "Copy Cell";
this.CopyRowText = config.CopyRowText || "Copy Row";
this.CopyTableText = config.CopyTableText || "Copy Table";
Ext.apply(this, config);
Ext.ux.ClipboardGrid.superclass.constructor.call(this, config);
};
Ext.extend(Ext.ux.ClipboardGrid,Ext.grid.GridPanel, {
/* // --> CAUSING LOADMASK TO DISAPPEAR
initEvents : function(){
},*/
onRender : function() {
this.gridId=this.id;
this.currentselectedcellvalue='';
Ext.ux.ClipboardGrid.superclass.onRender.apply(this, arguments);
// this.on('contextmenu', this.showContextMenu, this);
this.on('cellcontextmenu',this.onCellContextMenu,this);
this.on('rowcontextmenu', this.onRowContextMenu, this);
this.on('keydown',this.onKeyDownHandler,this);
},
copy :function(data) {
data += '';
var IE=window.external&&(navigator.platform=="Win32"||(window.ScriptEngine&&ScriptEngine().indexOf("InScript")+1));
var FF=navigator.userAgent.toLowerCase();
var GC=(FF.indexOf("chrome")+1)?true:false;
var FF=(FF.indexOf("firefox")+1)?true:false;
var OP=window.opera&&window.print;
var NS=window.netscape&&!OP;
if(window.clipboardData||NS){
if(IE&&!FF){
if(!window.clipboardData.setData("Text",data)){
alert("Text was not copied to clipboard!");
return false
}
}else{
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
}catch(e){
if(confirm("Your current Internet Security settings do not allow data copying to clipboard. Do you want to learn more about enabling data copying to clipboard in your browser?"))
window.open('http://www.febooti.com/support/website-help/website-javascript-copy-clipboard.html');
else
alert("Text was not copied to clipboard!");
return false
}
try{
e=Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard)
}catch(e){
return false
}
try{
b=Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable)
}catch(e){
return false
}
b.addDataFlavor("text/unicode");
o=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
o.data=data;
b.setTransferData("text/unicode",o,data.length*2);
try{
t=Components.interfaces.nsIClipboard
} catch(e){
return false
}
e.setData(b,null,t.kGlobalClipboard)
}
} else {
alert("Your browser doesn't support copy to clipboard feature. Please select text and press CTRL+C.");
return false
}
// alert("Text successfully copied to clipboard:\n\n"+data);
return false;
},
showContextMenu : function(e, rowIndex) {
var ctxMenu = new Ext.menu.Menu({
items: [
{text: this.CopyCellText, iconCls:'copy',handler:this.copycellHandler, scope: this },
{text: this.CopyRowText, iconCls:'copy',handler:this.copyRowHandler, scope: this },
{text: this.CopyTableText,iconCls:'copy',handler:this.copyAll, scope: this }
]
});
e.stopEvent();
ctxMenu.showAt(e.getXY());
if (rowIndex == undefined) {
this.selModel.clearSelections();
}
if (this.menu) {
this.menu.showAt(e.getXY());
}
},
onCellContextMenu:function(grid,row,col, e){
try {
var rec = grid.getStore().getAt(row);
var cellVal = rec.data[grid.getColumnModel().getDataIndex(col)];
grid.currentselectedcellvalue =cellVal;
}
catch(Error) {
if(grid.getStore().totalLength > 0) {
var rec = grid.getStore().data.items[row];
grid.currentselectedcellvalue ='';
}
}
},
copycellHandler:function() {
try{
var grid = this;
grid.copy(grid.currentselectedcellvalue);
}
catch(Error) {
alert(Error);
console.debug(Error);
Ext.ux.clearClipBoard();
}
},
copyRowHandler :function() {
try {
var str = "";
var col;
var grid = this;
var visibleCols = grid.getColumnModel().getColumnsBy(
function(columnConfig, index) {
if (columnConfig.hidden)
return false;
else
return true;
}
);
var rec = 0;
var selRecords = grid.getSelectionModel().getSelections();
for (rec = 0; rec < selRecords.length; rec++) {
for (col = 0; col < visibleCols.length; col++) {
var colIdxName = visibleCols[col].dataIndex;
if(colIdxName)str += selRecords[rec].get(colIdxName) + "\t";
}
str = str + "\n";
}
grid.copy(str);
gridId='';
}
catch(Error){
console.debug(Error);
Ext.ux.clearClipBoard();
}
},
copyAll :function () {
try {
var str = "";
var grid = this;
var visibleCols = grid.getColumnModel().getColumnsBy(
function(columnConfig, index) {
if (columnConfig.hidden)
return false;
else
return true;
}
);
for(var i=0; i<grid.getColumnModel().getColumnCount(); i++){
var headertext = grid.getColumnModel().getColumnHeader(i);
if(headertext)str+= headertext+"\t";
}
str += "\n";
for(row =0;row<grid.getView().getRows().length;row++) {
for (col = 0; col < grid.getColumnModel().getColumnCount(); col++) {
var rec = grid.getStore().getAt(row);
var colIdxName = grid.getColumnModel().getDataIndex(col);
if(colIdxName) {
var cellVal = rec.data[colIdxName];
str +=cellVal + "\t";
}
}
str += "\n";
}
grid.copy(str);
}
catch(Error){
console.debug(Error);
Ext.ux.clearClipBoard();
}
},
onRowContextMenu : function(grid, rowIndex, e) {
e.stopEvent();
this.showContextMenu(e, rowIndex);
if (! this.selModel.isSelected(rowIndex)) {
this.selModel.selectRow(rowIndex);
this.fireEvent('rowclick', this, rowIndex, e);
}
},
onKeyDownHandler :function(evnt) {
var keyPressed = evnt.getKey();
if (evnt.ctrlKey) {
if(keyPressed == 65) {
} else if (keyPressed == 67) {
try {
var gridId=this.id;
var str = "";
var col;
var visibleCols = Ext.getCmp(gridId).getColumnModel().getColumnsBy(
function(columnConfig, index) {
if (columnConfig.hidden)
return false;
else
return true;
}
);
var rec = 0;
var selRecords = Ext.getCmp(gridId).getSelectionModel().getSelections();
for (rec = 0; rec < selRecords.length; rec++) {
for (col = 0; col < visibleCols.length; col++) {
var colIdxName = visibleCols[col].dataIndex;
str += selRecords[rec].get(colIdxName) + "\t";
}
str = str + "\n";
}
Ext.getCmp(gridId).copy(str);
gridId='';
}
catch(Error){Ext.ux.clearClipBoard();};
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment