Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Create pads either on a text file, or a "free" pad
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Jul 7, 2013
1 parent bf5f993 commit 327fee0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 31 deletions.
58 changes: 48 additions & 10 deletions core/src/plugins/editor.etherpad/class.EtherpadClient.php
Expand Up @@ -24,34 +24,53 @@ public function switchAction($actionName, $httpVars, $fileVars){

require_once("etherpad-client/etherpad-lite-client.php");
$client = new EtherpadLiteClient("nl8VJIWXZMHNj7aWj6rWy4CLct1mu97v",$this->baseURL."/api");
$userName = AuthService::getLoggedUser()->getId();
$res = $client->createAuthorIfNotExistsFor($userName, $userName);
$authorID = $res->authorID;
$res2 = $client->createGroupIfNotExistsFor("ajaxplorer");
$groupID = $res2->groupID;

if($actionName == "etherpad_create"){

if(isSet($httpVars["pad_name"])){

$padID = $httpVars["pad_name"];
$startContent = "";
if($httpVars["pad_type"] && $httpVars["pad_type"] == 'free'){
$padID = "FREEPAD__".$padID;
}

}else if(isSet($httpVars["file"])){

$startContent = file_get_contents($filename);
if(strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == "html"){
$startContentHTML = $startContent;
}
$padID = AJXP_Utils::slugify($httpVars["file"]);

}

$userName = AuthService::getLoggedUser()->getId();

$res = $client->createAuthorIfNotExistsFor($userName, $userName);
$authorID = $res->authorID;

$res2 = $client->createGroupIfNotExistsFor("ajaxplorer");
$groupID = $res2->groupID;

$resP = $client->listPads($res2->groupID);

$pads = $resP->padIDs;
if(!in_array($groupID.'$'.$padID, $pads)){
$res3 = $client->createGroupPad($groupID, $padID, $startContent);
$res3 = $client->createGroupPad($groupID, $padID, null);
if(isSet($startContentHTML)){
$client->setHTML($groupID.'$'.$padID, $startContentHTML);
}else if(!empty($startContent)){
$client->setText($groupID.'$'.$padID, $startContent);
}
}else{
// Check if content needs relaunch!
$test = $client->getText($groupID.'$'.$padID);
if(!empty($startContent) && $test->text != $startContent){
if(isSet($startContentHTML)){
$client->setHTML($groupID.'$'.$padID, $startContentHTML);
}else{
$client->setText($groupID.'$'.$padID, $startContent);
}
}
}

$res4 = $client->createSession($groupID, $authorID, time()+14400);
Expand All @@ -72,15 +91,34 @@ public function switchAction($actionName, $httpVars, $fileVars){

}else if ($actionName == "etherpad_save"){

$node = new AJXP_Node($filename);

$padID = $httpVars["pad_id"];
$res = $client->getText($padID);
if(isSet($startContentHTML)){
$res = $client->getHTML($padID);
}else{
$res = $client->getText($padID);
}

AJXP_Controller::applyHook("node.before_change", array($node, strlen($res->text)));
file_put_contents($filename, $res->text);
AJXP_Controller::applyHook("node.change", array($node, $node));

}else if ($actionName == "etherpad_close"){

// WE SHOULD DETECT IF THERE IS NOBODY CONNECTED ANYMORE, AND DELETE THE PAD.
$sessionID = $httpVars["session_id"];
$i = $client->getSessionInfo($sessionID);
$client->deleteSession($sessionID);

}else if($actionName == "etherpad_proxy_api"){

if($httpVars["api_action"] == "list_pads"){
$res = $client->listPads($groupID);
}else if($httpVars["api_action"] == "list_authors_for_pad"){
$res = $client->listAuthorsOfPad($httpVars["pad_id"]);
}
HTMLWriter::charsetHeader("application/json");
echo(json_encode($res));

}

Expand Down
79 changes: 76 additions & 3 deletions core/src/plugins/editor.etherpad/class.EtherpadLauncher.js
Expand Up @@ -42,7 +42,69 @@ Class.create("EtherpadLauncher", AbstractEditor, {
return false;
}.bind(this));
},


initEmptyPadPane : function(form){

var selector = form.down('[name="pad_list"]');
var textInput = form.down('[name="new_pad_name"]');
var joinButton = form.down('#join_pad');
fitHeightToBottom(form.down("#ether_frame"), null, null, true);

var con = new Connexion();
con.setParameters(new Hash({
get_action: 'etherpad_proxy_api',
api_action: 'list_pads'
}));
con.onComplete = function(transport){
var pads = $A(transport.responseJSON.padIDs);
var frees = $A();
var files = $A();
pads.each(function(el){
var label = el.split('$').pop();
if(label.startsWith('FREEPAD__')){
frees.push(label);
}else{
files.push(label);
}
});
selector.insert(new Element('optgroup',{label:'Free boards'}));
frees.each(function(e){
selector.insert(new Element('option', {value:e}).update(e.replace('FREEPAD__', '')));
});
selector.insert(new Element('option', {value:-1}).update('Create new pad'));

if(files.size()){
selector.insert(new Element('optgroup', {label:'Files'}));
files.each(function(e){
selector.insert(new Element('option', {value:e}).update(e));
});
}

selector.observe("change", function(){
textInput[(selector.getValue() == -1)?'show':'hide']();
});
textInput[(selector.getValue() == -1)?'show':'hide']();
};
con.sendAsync();

joinButton.observe("click", function(){

var conn = new Connexion();
conn.addParameter("get_action", "etherpad_create");
if(selector.getValue() == -1){
conn.addParameter("pad_name", textInput.getValue());
conn.addParameter("pad_type", 'free');
}else{
conn.addParameter("pad_name", selector.getValue());
}
conn.onComplete = function(transport){
var data = transport.responseJSON;
$("etherpad_container").down("#ether_frame").src = data.url;
}
conn.sendAsync();
});

},

open : function($super, nodeOrNodes){
$super(nodeOrNodes);
Expand All @@ -64,19 +126,30 @@ Class.create("EtherpadLauncher", AbstractEditor, {
this.setFullScreen();
attachMobileScroll(this.textarea, "vertical");
}

this.element.observe("editor:enterFSend", function(){
fitHeightToBottom($("ether_box").down("#ether_box_frame"));
});
this.element.observe("editor:resize", function(){
fitHeightToBottom($("ether_box").down("#ether_box_frame"));
});
this.element.observe("editor:exitFSend", function(){
fitHeightToBottom($("ether_box").down("#ether_box_frame"));
});

this.element.observeOnce("editor:close", function(){
var conn = new Connexion();
conn.addParameter("get_action", "etherpad_close");
conn.addParameter("file", this.node.getPath());
conn.addParameter("pad_id", this.padID);
conn.addParameter("session_id", this.sessionID);
conn.sendAsync();
});
}.bind(this));
},


saveFile : function(){
if(!this.padID) return;
if(!this.padID || !this.node) return;
var conn = new Connexion();
conn.addParameter("get_action", "etherpad_save");
conn.addParameter("file", this.node.getPath());
Expand Down
3 changes: 2 additions & 1 deletion core/src/plugins/editor.etherpad/i18n/en.php
@@ -1,6 +1,7 @@
<?php
$mess = array(
"1" => "Create an empty Pad",
"1" => "Collaboration board",
"1b"=> "Create an empty collaboration pad",
"2" => "Collaborative Editor",
"3" => "Real-time collaborative edition"
);
38 changes: 21 additions & 17 deletions core/src/plugins/editor.etherpad/manifest.xml
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<editor id="editor.etherpad" openable="true" iconClass="icon-edit" order="10" text="etherpad.2" title="etherpad.3" icon="edit.png" className="EtherpadLauncher" mimes="txt" formId="ether_box" label="CONF_MESSAGE[Etherpad]" description="CONF_MESSAGE[Collaborative edition of text files]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:../core.ajaxplorer/ajxp_registry.xsd">
<editor id="editor.etherpad" openable="true" iconClass="icon-comments-alt" order="10"
text="etherpad.2" title="etherpad.3" icon="edit.png" className="EtherpadLauncher"
mimes="txt,pad" formId="ether_box" label="CONF_MESSAGE[Etherpad]" description="CONF_MESSAGE[Collaborative edition of text files]"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:../core.ajaxplorer/ajxp_registry.xsd">
<client_settings>
<resources>
<js file="plugins/editor.etherpad/class.EtherpadLauncher.js" className="EtherpadLauncher"/>
<js file="plugins/editor.etherpad/class.EtherpadLauncher.js" className="EtherpadLauncher" autoload="true"/>
<i18n path="plugins/editor.etherpad/i18n" namespace="etherpad"/>
</resources>
</client_settings>
Expand All @@ -22,30 +25,26 @@
<registry_contributions>
<actions>
<action name="etherpad_create">
<gui src="" text="51" title="51">
<context dir="true" recycle="true" selection="false" actionBar="true" actionBarGroup="more"/>
<gui src="" text="etherpad.1" title="etherpad.1b" iconClass="icon-comments-alt">
<context dir="true" recycle="true" selection="false" actionBarGroup="utils" ajxpWidgets="UserWidget::logging_string"/>
</gui>
<processing>
<clientCallback prepareModal="true" dialogOkButtonOnly="true" dialogOpenForm="etherpad_container">
<dialogOnOpen><![CDATA[
var conn = new Connexion();
conn.addParameter("get_action", "etherpad_create");
conn.addParameter("pad_name", "new_pad_" + Math.floor(Math.random()*1001));
conn.onComplete = function(transport){
var url = transport.responseText;
$("etherpad_container").down("#ether_frame").src = url;
fitHeightToBottom($("etherpad_container").down("#ether_frame"));
}
conn.sendAsync();
]]></dialogOnOpen>
EtherpadLauncher.prototype.initEmptyPadPane(modal.getForm());
]]></dialogOnOpen>
<dialogOnComplete><![CDATA[
$("etherpad_container").down("#ether_frame").remove();
hideLightBox();
]]></dialogOnComplete>
$("etherpad_container").down("#ether_frame").remove();
hideLightBox();
]]></dialogOnComplete>
<dialogOnCancel><![CDATA[]]></dialogOnCancel>
</clientCallback>
<clientForm id="etherpad_container"><![CDATA[
<div id="etherpad_container" box_width="80%" box_height="80%" box_resize="true" box_padding="0">
<div class="action_bar" style="padding-left: 10px;border-bottom: 1px solid #ccc;padding-bottom: 5px;">
Join an existing board or create a new one: <select name="pad_list"></select> <input type="text" name="new_pad_name" placeholder="Board name" style="display:none;">
<a id="join_pad" style="float:none;" class="icon-comments-alt" href="#" title="Join" style=""><span message_id="235" class="actionbar_button_label">Join</span></a>
</div>
<iframe id="ether_frame" style="width:100%;border: none;" src=""></iframe>
</div>
]]></clientForm>
Expand All @@ -62,6 +61,11 @@
<serverCallback methodName="switchAction"/>
</processing>
</action>
<action name="etherpad_proxy_api">
<processing>
<serverCallback methodName="switchAction"/>
</processing>
</action>
</actions>
</registry_contributions>
<class_definition classname="EtherpadClient" filename="plugins/editor.etherpad/class.EtherpadClient.php"/>
Expand Down

0 comments on commit 327fee0

Please sign in to comment.