1 rio.Clipboard = { 2 /* 3 Only call copy on the platform specific keydown event scenario. 4 5 Mac: CMD + c 6 Win: CTRL + c 7 */ 8 copy: function(val, afterCopy) { 9 if (Prototype.Browser.WebKit) { 10 this.copyToClipboardForWebKit(val); 11 afterCopy.defer(); 12 return; 13 } 14 15 var clipboard = this.html(); 16 17 clipboard.value = val; 18 clipboard.setSelectionRange(0, clipboard.value.length); 19 clipboard.focus(); 20 21 var keyObserver = function(e) { 22 clipboard.stopObserving("keyup"); 23 clipboard.stopObserving("keydown"); 24 clipboard.stopObserving("keypress"); 25 26 afterCopy.defer(); 27 }.bindAsEventListener(this); 28 29 clipboard.observe("keyup", keyObserver); 30 clipboard.observe("keydown", keyObserver); 31 clipboard.observe("keypress", keyObserver); 32 }, 33 34 copyToClipboardForWebKit: function(textToCopy) { 35 var clipDoc = this.clipboardDoc(); 36 37 // Get the clipboard container (input box) and set its contents 38 var container = clipDoc.getElementById("clipContainer"); 39 container.value = textToCopy; 40 41 // Focus/Select the container containing the text to copy 42 container.focus(); 43 container.select(); 44 45 clipDoc.execCommand("copy", false, ""); 46 }, 47 48 clipboardDoc: function() { 49 if (!this._clipFrame) { 50 this._clipFrame = rio.Tag.iframe("", { 51 style: "position: absolute; top: -1000px" 52 }); 53 Element.body().insert(this._clipFrame); 54 55 // Insert an input box and switch it to design mode 56 var clipDoc = this._clipFrame.contentDocument; 57 clipDoc.body.innerHTML = "<textarea id='clipContainer'/>"; 58 clipDoc.designMode = "On"; 59 clipDoc.body.contentEditable = true; 60 } 61 return this._clipFrame.contentDocument; 62 }, 63 64 paste: function(callback) { 65 var clipboard = this.html(); 66 67 clipboard.value = ""; 68 clipboard.focus(); 69 70 var keyObserver = function() { 71 clipboard.stopObserving("keyup"); 72 clipboard.stopObserving("keydown"); 73 clipboard.stopObserving("keypress"); 74 75 (function() { 76 callback(clipboard.value); 77 }).defer(); 78 }; 79 80 clipboard.observe("keyup", keyObserver); 81 clipboard.observe("keydown", keyObserver); 82 clipboard.observe("keypress", keyObserver); 83 }, 84 85 html: function() { 86 if (!this._html) { 87 this._html = rio.Tag.textarea(""); 88 89 Element.body().insert(rio.Tag.div(this._html, { 90 style: "position: absolute; top: -10px; overflow: hidden; height:1px;" 91 })); 92 } 93 94 return this._html; 95 } 96 };