Last active
December 15, 2015 18:29
-
-
Save haraldmartin/5303714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//= require "cookie" | |
var CopyBuffer = Class.create({ | |
initialize: function(options) { | |
this.formats = ['text', 'html']; | |
this.format = this.formats[0]; | |
this.stack = []; | |
Object.extend((this.options = {}), options || {}); | |
for (var i in this.options) this[i] = $(this.options[i]); | |
if (!this.copyMethod) throw("copyMethod is required"); | |
this.register(); | |
}, | |
register: function() { | |
// this.clearer.observe('click', this.handleClear.bind(this)); | |
}, | |
handleClear: function(event) { | |
event && event.stop(); | |
this.clear(); | |
this.clearer.removeClassName('hover'); | |
$(document.body).removeClassName('clipboard-has-contents'); | |
}, | |
push: function(characterObject) { | |
this.stack.push(characterObject); | |
$(document.body).addClassName("clipboard-has-contents"); | |
this.update(); | |
}, | |
pushAndNotify: function(element) { | |
element = $(element); | |
this.clickedElement = element; | |
this.push({ | |
text: element.innerHTML, | |
html: element.readAttribute('data-entity') | |
}); | |
this.notify(); | |
}, | |
clear: function() { | |
this.stack.clear(); | |
this.update(); | |
}, | |
update: function() { | |
this.element.update(this.toHTML()); | |
this.copy(); | |
}, | |
notify: function() { | |
var color = { html: '#33ff00', text: '#FFFB14' }[this.format], | |
element = $(this.clickedElement); | |
element | |
.setStyle({ color: 'black' }) | |
.highlight({ | |
duration: 0.75, startcolor: color, endcolor: '#FFFFFF', restorecolor: '#FFFFFF', | |
afterFinish: function(effect) { | |
// effect.element.style.color = 'auto'; | |
} | |
} | |
); | |
}, | |
toHTML: function() { | |
var chars, open = '<span>', close = '</span>'; | |
if (this.format == 'html') close += ' '; | |
chars = this.inFormat().join(close + open); | |
return open + chars.replace(/&/g, '&') + close; | |
}, | |
toString: function() { | |
return this.inFormat().join('').replace(/&/g, '&') || ' '; | |
}, | |
inFormat: function() { | |
return this.stack.pluck(this.format); | |
}, | |
setFormat: function(format, dontUpdate) { | |
if (!this.formats.include(format)) return; | |
if (this.format === format) return; | |
var oldFormat = this.format, newFormat = format; | |
// update format change link | |
var newHTML = 'As ' + oldFormat.toUpperCase().replace(/TEXT/, 'Text'); | |
$('text_html_toggler').update(newHTML); | |
// set new format | |
$(document.body).removeClassName('as-' + oldFormat).addClassName('as-' + newFormat); | |
this.format = newFormat; | |
if (!dontUpdate) this.update(); | |
// save format | |
Cookie.create('format', newFormat, 60); | |
}, | |
setFormatFromCookie: function() { | |
if (Cookie.exist('format')) { | |
var cookie = Cookie.read('format'); | |
this.setFormat(cookie, true); | |
} | |
}, | |
toggleFormat: function() { | |
var newFormat = this.format == 'html' ? 'text' : 'html'; | |
this.setFormat(newFormat); | |
Clipboard.moveAway(); | |
}, | |
copy: function() { | |
this.copyMethod(this.toString()); | |
}, | |
replace: function(element) { | |
this.stack.clear(); | |
this.pushAndNotify(element); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment