diff --git a/README.md b/README.md
index 7d36ebc..4eab179 100644
--- a/README.md
+++ b/README.md
@@ -20,20 +20,32 @@ The Etherpad jQuery Plugin Example plugin easily allows you to add and access a
# Available options and parameters
'host' : 'http://beta.etherpad.org', // the host and port of the Etherpad instance, by default the foundation will host your pads for you
-'baseUrl' : '/p/', // The base URL of the pads
-'showControls' : false, // If you want to show controls IE bold, italic, etc.
-'showChat' : false, // If you want to show the chat button or not
-'showLineNumbers' : false, // If you want to show the line numbers or not
+'baseUrl' : '/p/', // The base URL of the pads
+'showControls' : false, // If you want to show controls IE bold, italic, etc.
+'showChat' : false, // If you want to show the chat button or not
+'showLineNumbers' : false, // If you want to show the line numbers or not
'userName' : 'unnamed', // The username you want to pass to the pad
-'useMonospaceFont' : false, // Use monospaced fonts
-'noColors' : false, // Disable background colors on author text
-'hideQRCode' : false, // Hide QR code
-'width' : 100, // The width of the embedded IFrame
-'height' : 100, // The height of the embedded IFrame
-'border' : 0, // The width of the border (make sure to append px to a numerical value)
-'borderStyle' : 'solid' // The CSS style of the border [none, dotted, dashed, solid, double, groove, ridge, inset, outset]
+'useMonospaceFont' : false, // Use monospaced fonts
+'noColors' : false, // Disable background colors on author text
+'hideQRCode' : false, // Hide QR code
+'width' : 100, // The width of the embedded IFrame
+'height' : 100, // The height of the embedded IFrame
+'border' : 0, // The width of the border (make sure to append px to a numerical value)
+'borderStyle' : 'solid', // The CSS style of the border [none, dotted, dashed, solid, double, groove, ridge, inset, outset]
+
+'parse' : false, //
+'poll' : 1000 // Require 'parse' != false, time in milliseconds, if > 300 will refresh the pads found with 'parse'
+
+# Use of the 'parse' option
+You can pass either
+
+- **a boolean true**
+ Will scan within the body
+- **a DOM element object**
+ Will scan within the DOM element
+
# Copyright
jQuery Etherpad plugin written by John McLear (c) Primary Technology 2011
Feel free to re-use, distribute, butcher, edit and whatever else you want.
diff --git a/js/etherpad.js b/js/etherpad.js
index 5c9b1d2..96cfe5f 100644
--- a/js/etherpad.js
+++ b/js/etherpad.js
@@ -1,5 +1,27 @@
(function( $ ){
-
+
+ // used to generate unique IDs
+ var padCount = window.__padCount = 0;
+
+ /**
+ * Performs AJAX calls to retreive the HTML content of a pad
+ * and puts it into a target
+ */
+ function fetchHTML(frameUrl, $target, cb) {
+ var contentsUrl = frameUrl + "/export/html";
+// var rawContentsUrl = frameUrl + "/export/html?raw";
+
+ $.get(contentsUrl, function(data) {
+ var html = data;
+ if ($target[0].nodeName.toLowerCase() == 'textarea') {
+ $target.val(html);
+ }
+ else {
+ $target.html(html);
+ }
+ });
+ }
+
$.fn.pad = function( options ) {
var settings = {
'host' : 'http://beta.etherpad.org',
@@ -16,81 +38,130 @@
'border' : 0,
'borderStyle' : 'solid',
'toggleTextOn' : 'Disable Rich-text',
- 'toggleTextOff' : 'Enable Rich-text'
+ 'toggleTextOff' : 'Enable Rich-text',
+
+ 'poll' : 3000
};
var $self = this;
if (!$self.length) return;
- if (!$self.attr('id')) throw new Error('No "id" attribute');
+
+ options = options || (options = {});
+ $.extend( settings, options );
+ if (console) console.info('Settings', settings);
+
+ if (options.parse) {
+ var context = (typeof options.parse != 'object' ? $('body')[0] : options.parse);
+
+ $('[data-pad-id]:not(.etherpad-lite-processed)')
+ .addClass('etherpad-lite-processed')
+ .each(function(){
+ var $display = $(this);
+ fetchHTML($display.attr('rel'), $display);
+ if (settings.poll > 500) {
+ $display
+ .data('pad-polling', setInterval(function(){
+ fetchHTML($display.attr('rel'), $display);
+ }, settings.poll))
+ .hover(function(){
+ $display.css('background-color', '#eee');
+ clearInterval($display.data('pad-polling'));
+ }, function(){
+ $display
+ .css('background-color', 'transparent')
+ .data('pad-polling', setInterval(function(){
+ fetchHTML($display.attr('rel'), $display);
+ }, settings.poll))
+ ;
+ })
+ ;
+ }
+ })
+ ;
+ return;
+ }
+
+
+ if (!$self.attr('id')) {
+ $self.attr('id', 'pad-'+ padCount);
+ padCount++;
+ }
var useValue = $self[0].tagName.toLowerCase() == 'textarea';
var selfId = $self.attr('id');
var epframeId = 'epframe'+ selfId;
- // This writes a new frame if required
+ var frameExists = $('#'+ epframeId).length;
+
if ( !options.getContents ) {
- if ( options ) {
- $.extend( settings, options );
- }
+// $.extend( settings, options );
- var iFrameLink = '';
-
-
- var $iFrameLink = $(iFrameLink);
+ // This writes a new frame if required
+ if (!frameExists) {
+ var iFrame = '';
+
+ if (useValue) {
+ $self.after(iFrame);
+ }
+ else {
+ $self.html(iFrame);
+ }
+ }
+ var $iFrame = $('#'+ epframeId);
+
if (useValue) {
- var $toggleLink = $(''+ settings.toggleTextOn +'').click(function(){
- var $this = $(this);
- $this.toggleClass('active');
- if ($this.hasClass('active')) $this.text(settings.toggleTextOff);
- $self.pad({getContents: true});
- return false;
- });
- $self
- .hide()
- .after($toggleLink)
- .after($iFrameLink)
- ;
+
+ if (!$('#'+ selfId +'-toggle').length) {
+ var $toggleLink = $(''+ settings.toggleTextOn +'').click(function(){
+ var $this = $(this);
+ $this.toggleClass('active');
+ if ($this.hasClass('active')) $this.text(settings.toggleTextOff);
+ $self.pad({
+ getContents: true,
+ returnValue: true
+ });
+ return false;
+ });
+
+ $self.after($toggleLink);
+ }
+
+ $self.hide();
+
}
else {
- $self.html(iFrameLink);
+ $self.html(iFrame);
}
}
// This reads the etherpad contents if required
else {
- var frameUrl = $('#'+ epframeId).attr('src').split('?')[0];
- var contentsUrl = frameUrl + "/export/html";
-
- // perform an ajax call on contentsUrl and write it to the parent
- $.get(contentsUrl, function(data) {
-
- if (useValue) {
- $self.val(data).show();
- }
- else {
- $self.html(data);
- }
-
- $('#'+ epframeId).remove();
- });
+ var $frame = $('#'+ epframeId).attr('src');
+ if (!$frame) return;
+ fetchHTML($frame.split('?')[0], $self);
}
+ if (settings.destroy) {
+ $self.show();
+ $('#'+ epframeId).remove();
+ $('#'+ selfId +'-toggle').remove();
+ }
return $self;
};