Skip to content

Instantly share code, notes, and snippets.

@mrlannigan
Created June 13, 2011 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlannigan/1022448 to your computer and use it in GitHub Desktop.
Save mrlannigan/1022448 to your computer and use it in GitHub Desktop.
WPquestions Plain Text jQuery Plugin
The plain text icon used can be downloaded from:
http://www.iconfinder.com/icondetails/9091/128/plain_text_icon
I used the 16x16 version of the above.
$(document).ready(function(){
$("pre code").plainTextDisplay();
});
/*
WP Questions Plain Text jQuery Plugin
Written by Julian Lannigan (julianlannigan.com)
Released to the public domain.
*/
(function($){
$.plainTextDisplay = function(el, options){
$.plainTextDisplay.defaultOptions = {
linkclass: "code_plainText",
linkstyle: "display:none;float:right;position:relative;z-index:9999;background-color:#F0F0F0;padding:5px; margin-right:-10px;opacity:0.7;",
linktext: "<img src=\"plain-text-icon.png\" title=\"Get the plain text\" alt=\"Get the plain text\" />",
ignoreOneLiners: true
};
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("plainTextDisplay", base);
base.init = function(){
base.options = $.extend({},$.plainTextDisplay.defaultOptions, options);
if (base.options.ignoreOneLiners && base.$el.html().indexOf("<br>", 0) == -1) {
return;
}
//Build the link
var link = document.createElement("a");
link.href = "javascript:void(0);";
$(link).attr('style', base.options.linkstyle);
link.className = base.options.linkclass;
link.innerHTML = base.options.linktext;
if (base.$el.closest("#thread-responses").length) {
$(link).css("marginRight", "20px"); //if the code block is in a threaded response push it left some more.
}
base.$el.parent().hover(base.onMouseEnter, base.onMouseLeave);
$(link).click(base.onPlainTextClick);
base.$el.before(link);
base.$el.data('plainTextLink', link);
};
base.convertEntities = function (targetCode) {
var ta = document.createElement("textarea"), returnCode;
if (typeof targetCode == "undefined") {
var targetCode = base.$el.html();
}
ta.innerHTML = targetCode.replace(/<br>/g, "");
returnCode = $(ta).text();
$(ta).remove();
return returnCode;
}
base.onMouseEnter = function() {
var theLink = base.$el.data('plainTextLink');
$(theLink).show();
$(document).bind('scroll', {link: theLink}, base.onDocScroll)
.trigger('scroll', [theLink]);
$(theLink).parent().bind('scroll', {link: theLink}, base.onCodeScroll)
.trigger('scroll', [theLink]);
}
base.onMouseLeave = function() {
var theLink = base.$el.data('plainTextLink');
$(theLink).hide().unbind('scroll', base.onCodeScroll);
$(document).unbind('scroll', base.onDocScroll);
}
base.onDocScroll = function(event, forcedTrigger) {
var link = $(event.data.link), dTop, pTop;
if (typeof forcedTrigger != "undefined") {
link = $(forcedTrigger);
}
if (link.filter(":visible").length) {
dTop = $(document).scrollTop();
pTop = link.parent().offset().top;
if (dTop < pTop) { //the link is still in view.
link[0].style.top = "-10px";
} else if (dTop >= pTop) {
link[0].style.top = (dTop-pTop-10)+"px";
}
}
}
base.onCodeScroll = function(event, forcedTrigger) {
var link = $(event.data.link), pLeft;
if (typeof forcedTrigger != "undefined") {
link = $(forcedTrigger);
}
if (link.filter(":visible").length) {
pLeft = link.parent().scrollLeft();
if (pLeft < 0) {pLeft = 0;}
link[0].style.left = pLeft+"px";
}
}
base.onPlainTextClick = function(event) {
var plainText, theHtml, cWin, cWinOptions, cWinWidth = 800, cWinHeight = 400;
plainText = base.escapeHtml(base.convertEntities());
theHtml = "<textarea style=\"width:99%;height:99%;\" onclick=\"try{this.focus();this.select();}catch(e){}\">"+plainText+"</textarea>";
cWinOptions = "top=" + ((screen.availHeight/2) - (cWinWidth / 2)) + ",left=" + ((screen.availWidth/2) - (cWinHeight / 2)) + ",width="+cWinWidth+",height="+cWinHeight+",resizable=1,toolbar=0,scrollbars=0,location=1,status=0,menubar=0";
cWin = $(document).data("wpq_plaintext_window");
if (typeof cWin == 'object' && !cWin.closed) {
cWin.close();
}
cWin = window.open("about:blank", "wpq_plaintext", cWinOptions);
cWin.document.open();
cWin.document.close();
cWin.document.body.innerHTML = theHtml;
cWin.document.title = "Plain Text from "+document.title;
$(document).data("wpq_plaintext_window", cWin);
}
base.escapeHtml = function(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
// Run initializer
base.init();
};
$.fn.plainTextDisplay = function(options){
return this.each(function(){
(new $.plainTextDisplay(this, options));
});
};
})(jQuery);
$(document).ready(function(){
$("pre code").plainTextDisplay();
});
/*
WP Questions Plain Text jQuery Plugin
Written by Julian Lannigan (julianlannigan.com)
Released to the public domain.
*/
(function(a){a.plainTextDisplay=function(b,c){a.plainTextDisplay.defaultOptions={linkclass:"code_plainText",linkstyle:"display:none;float:right;position:relative;z-index:9999;background-color:#F0F0F0;padding:5px; margin-right:-10px;opacity:0.7;",linktext:'<img src="plain-text-icon.png" title="Get the plain text" alt="Get the plain text" />',ignoreOneLiners:true};var d=this;d.$el=a(b);d.el=b;d.$el.data("plainTextDisplay",d);d.init=function(){d.options=a.extend({},a.plainTextDisplay.defaultOptions,c);if(d.options.ignoreOneLiners&&d.$el.html().indexOf("<br>",0)==-1){return}var b=document.createElement("a");b.href="javascript:void(0);";a(b).attr("style",d.options.linkstyle);b.className=d.options.linkclass;b.innerHTML=d.options.linktext;if(d.$el.closest("#thread-responses").length){a(b).css("marginRight","20px")}d.$el.parent().hover(d.onMouseEnter,d.onMouseLeave);a(b).click(d.onPlainTextClick);d.$el.before(b);d.$el.data("plainTextLink",b)};d.convertEntities=function(b){var c=document.createElement("textarea"),e;if(typeof b=="undefined"){var b=d.$el.html()}c.innerHTML=b.replace(/<br>/g,"");e=a(c).text();a(c).remove();return e};d.onMouseEnter=function(){var b=d.$el.data("plainTextLink");a(b).show();a(document).bind("scroll",{link:b},d.onDocScroll).trigger("scroll",[b]);a(b).parent().bind("scroll",{link:b},d.onCodeScroll).trigger("scroll",[b])};d.onMouseLeave=function(){var b=d.$el.data("plainTextLink");a(b).hide().unbind("scroll",d.onCodeScroll);a(document).unbind("scroll",d.onDocScroll)};d.onDocScroll=function(b,c){var d=a(b.data.link),e,f;if(typeof c!="undefined"){d=a(c)}if(d.filter(":visible").length){e=a(document).scrollTop();f=d.parent().offset().top;if(e<f){d[0].style.top="-10px"}else if(e>=f){d[0].style.top=e-f-10+"px"}}};d.onCodeScroll=function(b,c){var d=a(b.data.link),e;if(typeof c!="undefined"){d=a(c)}if(d.filter(":visible").length){e=d.parent().scrollLeft();if(e<0){e=0}d[0].style.left=e+"px"}};d.onPlainTextClick=function(b){var c,e,f,g,h=800,i=400;c=d.escapeHtml(d.convertEntities());e='<textarea style="width:99%;height:99%;" onclick="try{this.focus();this.select();}catch(e){}">'+c+"</textarea>";g="top="+(screen.availHeight/2-h/2)+",left="+(screen.availWidth/2-i/2)+",width="+h+",height="+i+",resizable=1,toolbar=0,scrollbars=0,location=1,status=0,menubar=0";f=a(document).data("wpq_plaintext_window");if(typeof f=="object"&&!f.closed){f.close()}f=window.open("about:blank","wpq_plaintext",g);f.document.open();f.document.close();f.document.body.innerHTML=e;f.document.title="Plain Text from "+document.title;a(document).data("wpq_plaintext_window",f)};d.escapeHtml=function(a){return a.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"&#039;")};d.init()};a.fn.plainTextDisplay=function(b){return this.each(function(){new a.plainTextDisplay(this,b)})}})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment