Fixes for Bug #320, Users tagging does not work if @ is in the middle of already typed text

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@117260 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2015-07-15 16:37:33 +00:00
parent 30a59d7599
commit 3e72d59d1c
2 changed files with 42 additions and 31 deletions

View File

@ -7,6 +7,9 @@
<dependent-module archiveName="gcube-widgets-1.9.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/gcube-widgets/gcube-widgets"> <dependent-module archiveName="gcube-widgets-1.9.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/gcube-widgets/gcube-widgets">
<dependency-type>uses</dependency-type> <dependency-type>uses</dependency-type>
</dependent-module> </dependent-module>
<dependent-module archiveName="pickitem-widget-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/pickitem-widget/pickitem-widget">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/> <property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/>
<property name="context-root" value="share-updates"/> <property name="context-root" value="share-updates"/>
</wb-module> </wb-module>

View File

@ -4,6 +4,7 @@
package org.gcube.portlets.user.shareupdates.client.view; package org.gcube.portlets.user.shareupdates.client.view;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateService; import org.gcube.portlets.user.shareupdates.client.ShareUpdateService;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateServiceAsync; import org.gcube.portlets.user.shareupdates.client.ShareUpdateServiceAsync;
@ -36,7 +37,7 @@ public class SuperPosedTextArea extends TextArea {
public final static int ARROW_UP = 38; public final static int ARROW_UP = 38;
public final static int ARROW_DOWN = 40; public final static int ARROW_DOWN = 40;
private ArrayList<String> mentionedUsers = new ArrayList<String>(); private HashSet<String> mentionedUsers = new HashSet<String>();
private ArrayList<String> hashtags = new ArrayList<String>(); private ArrayList<String> hashtags = new ArrayList<String>();
/** /**
@ -44,9 +45,10 @@ public class SuperPosedTextArea extends TextArea {
*/ */
public SuperPosedTextArea() { public SuperPosedTextArea() {
sinkEvents(Event.ONPASTE); sinkEvents(Event.ONPASTE);
sinkEvents(Event.ONKEYUP); sinkEvents(Event.ONKEYPRESS);
sinkEvents(Event.ONCONTEXTMENU); sinkEvents(Event.ONCONTEXTMENU);
sinkEvents(Event.ONKEYDOWN); sinkEvents(Event.ONKEYDOWN);
sinkEvents(Event.ONKEYUP);
//get the users //get the users
shareupdateService.getPortalUsers(new AsyncCallback<ArrayList<ItemBean>>() { shareupdateService.getPortalUsers(new AsyncCallback<ArrayList<ItemBean>>() {
@Override @Override
@ -69,8 +71,8 @@ public class SuperPosedTextArea extends TextArea {
public void onFailure(Throwable caught) { public void onFailure(Throwable caught) {
} }
}); });
DOM.setElementAttribute(getElement(), "id", "postTextArea"); getElement().setAttribute("id", "postTextArea");
bind(); bind();
} }
@ -100,10 +102,15 @@ public class SuperPosedTextArea extends TextArea {
t.schedule(100); t.schedule(100);
break; break;
} }
case Event.ONKEYPRESS: {
pickUserDlg.onKeyPress(this.getCursorPos(), event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
pickHashtagDlg.onKeyPress(this.getCursorPos(), event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText());
break;
}
case Event.ONKEYUP: { case Event.ONKEYUP: {
injectInDiv(getText()); injectInDiv(getText());
pickUserDlg.onKeyUp(event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText()); pickUserDlg.onKeyUp(event.getKeyCode());
pickHashtagDlg.onKeyUp(event.getKeyCode(), this.getAbsoluteLeft(), this.getAbsoluteTop()+this.getOffsetHeight(), getText()); pickHashtagDlg.onKeyUp(event.getKeyCode());
break; break;
} }
case Event.ONCONTEXTMENU: { case Event.ONCONTEXTMENU: {
@ -115,7 +122,7 @@ public class SuperPosedTextArea extends TextArea {
//avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline //avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline
if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) { if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) {
DOM.eventCancelBubble(event, true); DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event); event.preventDefault();
return; return;
} }
} }
@ -123,7 +130,7 @@ public class SuperPosedTextArea extends TextArea {
//avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline //avoid the arrow up to move the cursor at the beginning of the textbox and the TAB to move around inputs and enter to go newline
if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) { if (event.getKeyCode() == ARROW_UP || event.getKeyCode() == KeyCodes.KEY_TAB || event.getKeyCode() == KeyCodes.KEY_ENTER) {
DOM.eventCancelBubble(event, true); DOM.eventCancelBubble(event, true);
DOM.eventPreventDefault(event); event.preventDefault();
return; return;
} }
} }
@ -142,20 +149,21 @@ public class SuperPosedTextArea extends TextArea {
DOM.getElementById("highlighter").setInnerHTML(""); DOM.getElementById("highlighter").setInnerHTML("");
} }
private void injectInDiv(String textAreaText) { private void injectInDiv(String textAreaText) {
GWT.log("# injectInDiv: "+textAreaText);
String text; String text;
// parse the text: // parse the text:
// replace all the line braks by <br/>, and all the double spaces by the html version &nbsp; // replace all the line breaks by <br/>, and all the double spaces by the html version &nbsp;
text = textAreaText.replaceAll("(\r\n|\n)","<br />"); text = textAreaText.replaceAll("(\r\n|\n)","<br />");
text = text.replaceAll("\\s\\s","&nbsp;&nbsp;"); text = text.replaceAll("\\s\\s","&nbsp;&nbsp;");
for (String mentionedUser : mentionedUsers) { for (String mentionedUser : mentionedUsers) {
text = text.replaceAll(mentionedUser,"<span class=\"highlightedUser\">"+mentionedUser+"</span>"); text = text.replaceAll(mentionedUser,"<span class=\"highlightedUser\">"+mentionedUser+"</span>");
} }
// for (String hashtag : hashtags) { for (String hashtag : hashtags) {
// text = text.replaceAll(hashtag,"<span>"+hashtag+"</span>"); text = text.replaceAll(hashtag,"<span>"+hashtag+"</span>");
// } }
// re-inject the processed text into the div // re-inject the processed text into the div
DOM.getElementById("highlighter").setInnerHTML(text); DOM.getElementById("highlighter").setInnerHTML(text);
} }
@ -167,28 +175,28 @@ public class SuperPosedTextArea extends TextArea {
eventBus.addHandler(PickedItemEvent.TYPE, new PickedItemEventHandler() { eventBus.addHandler(PickedItemEvent.TYPE, new PickedItemEventHandler() {
@Override @Override
public void onSelectedItem(PickedItemEvent event) { public void onSelectedItem(PickedItemEvent event) {
String triggerChar = event.getTriggerChar();
char triggerChar = event.getTriggerChar();
String toAdd = event.getSelectedItem().getAlternativeName(); String toAdd = event.getSelectedItem().getAlternativeName();
if (triggerChar.compareTo("#") == 0) { //has to be treated differently becase the # char remain present in the text unlike the @ if (triggerChar == '#') { //has to be treated differently becase the # char remain present in the text unlike the @
hashtags.add(toAdd); hashtags.add(toAdd);
int tPos = getText().lastIndexOf(triggerChar);
setText(getText().substring(0, tPos) + toAdd);
Element highDiv = DOM.getElementById("highlighter"); String preceedingPart = getText().substring(0, event.getItemCursorIndexStart());
tPos = highDiv.getInnerHTML().lastIndexOf(triggerChar); String afterPart = getText().substring(event.getItemCursorIndexEnd()+1);
highDiv.setInnerHTML(highDiv.getInnerHTML().substring(0, tPos) + toAdd); //GWT.log("# BEFORE: "+preceedingPart);
setText(preceedingPart + toAdd + " " + afterPart);
injectInDiv(getText());
} }
if (event.getTriggerChar().compareTo("@") == 0) { if (triggerChar == '@') {
GWT.log("GOT @ EVENT! "+ event.getSelectedItem().getAlternativeName());
mentionedUsers.add(toAdd); mentionedUsers.add(toAdd);
String[] toSplit = getText().split(triggerChar); //get the preceeding part
setText(toSplit[0]+toAdd); String preceedingPart = getText().substring(0, event.getItemCursorIndexStart());
Element highDiv = DOM.getElementById("highlighter"); String afterPart = getText().substring(event.getItemCursorIndexEnd()+1);
setText(preceedingPart + toAdd + " " + afterPart);
injectInDiv(getText());
String[] htmlToSplit = highDiv.getInnerHTML().split(triggerChar); //get the preceeding part
String highLightedUser = "<span class=\"highlightedUser\">"+toAdd+"</span>";
highDiv.setInnerHTML(htmlToSplit[0]+highLightedUser);
} }
} }
}); });
@ -203,7 +211,7 @@ public class SuperPosedTextArea extends TextArea {
GWT.log(toReturn.toString()); GWT.log(toReturn.toString());
return toReturn; return toReturn;
} }
public ArrayList<String> getHashtags() { public ArrayList<String> getHashtags() {
ArrayList<String> toReturn = new ArrayList<String>(); ArrayList<String> toReturn = new ArrayList<String>();
for (String hashtag : hashtags) { for (String hashtag : hashtags) {