From 77be1aa86e304e726f5e28eaedd6f99583bccfb3 Mon Sep 17 00:00:00 2001 From: Jonathan Rosenbaum Date: Sat, 9 Dec 2017 07:19:13 +0000 Subject: [PATCH] Adds etherpad.js plugin library for #39! --- js/etherpad.js | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 js/etherpad.js diff --git a/js/etherpad.js b/js/etherpad.js new file mode 100644 index 0000000..f43eb8b --- /dev/null +++ b/js/etherpad.js @@ -0,0 +1,113 @@ +(function( $ ){ + + $.fn.pad = function( options ) { + var settings = { + 'host' : 'http://beta.etherpad.org', + 'baseUrl' : '/p/', + 'showControls' : false, + 'showChat' : false, + 'showLineNumbers' : false, + 'userName' : 'unnamed', + 'lang' : '', + 'useMonospaceFont' : false, + 'noColors' : false, + 'userColor' : false, + 'hideQRCode' : false, + 'alwaysShowChat' : false, + 'width' : 100, + 'height' : 100, + 'border' : 0, + 'borderStyle' : 'solid', + 'toggleTextOn' : 'Disable Rich-text', + 'toggleTextOff' : 'Enable Rich-text', + 'plugins' : {}, + 'rtl' : false + }; + + var $self = this; + if (!$self.length) return; + if (!$self.attr('id')) throw new Error('No "id" attribute'); + + var useValue = $self[0].tagName.toLowerCase() == 'textarea'; + var selfId = $self.attr('id'); + var epframeId = 'epframe'+ selfId; + // This writes a new frame if required + if ( !options.getContents ) { + if ( options ) { + $.extend( settings, options ); + } + + var pluginParams = ''; + for(var option in settings.plugins) { + pluginParams += '&' + option + '=' + settings.plugins[option] + } + + var iFrameLink = ''; + + + var $iFrameLink = $(iFrameLink); + + 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) + ; + } + else { + $self.html(iFrameLink); + } + } + + // This reads the etherpad contents if required + else { + var frameUrl = $('#'+ epframeId).attr('src').split('?')[0]; + var contentsUrl = frameUrl + "/export/html"; + var target = $('#'+ options.getContents); + + // perform an ajax call on contentsUrl and write it to the parent + $.get(contentsUrl, function(data) { + + if (target.is(':input')) { + target.val(data).show(); + } + else { + target.html(data); + } + + $('#'+ epframeId).remove(); + }); + } + + + return $self; + }; +})( jQuery ); \ No newline at end of file