1919 */
2020package com .xpn .xwiki .objects .classes ;
2121
22+ import java .util .LinkedHashMap ;
23+ import java .util .Map ;
24+
2225import org .apache .ecs .xhtml .input ;
26+ import org .xwiki .velocity .tools .EscapeTool ;
2327import org .xwiki .xml .XMLUtils ;
2428
2529import com .xpn .xwiki .XWiki ;
3034import com .xpn .xwiki .objects .StringProperty ;
3135import com .xpn .xwiki .objects .meta .PropertyMetaClass ;
3236
37+ import static org .apache .commons .lang .StringEscapeUtils .escapeJavaScript ;
38+
3339public class StringClass extends PropertyClass
3440{
3541 private static final long serialVersionUID = 1L ;
@@ -89,7 +95,8 @@ public BaseProperty newProperty()
8995 }
9096
9197 @ Override
92- public void displayEdit (StringBuffer buffer , String name , String prefix , BaseCollection object , XWikiContext context )
98+ public void displayEdit (StringBuffer buffer , String name , String prefix , BaseCollection object ,
99+ XWikiContext context )
93100 {
94101 input input = new input ();
95102 input .setAttributeFilter (new XMLAttributeValueFilter ());
@@ -105,23 +112,9 @@ public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCol
105112 input .setDisabled (isDisabled ());
106113
107114 if (isPicker ()) {
108- input .setClass ("suggested" );
109- String path = "" ;
110- XWiki xwiki = context .getWiki ();
111- path = xwiki .getURL ("Main.WebHome" , "view" , context );
112-
113- String classname = this .getObject ().getName ();
114- String fieldname = this .getName ();
115- String secondCol = "-" , firstCol = "-" ;
116-
117- String script =
118- "\" " + path + "?xpage=suggest&classname=" + classname + "&fieldname=" + fieldname + "&firCol="
119- + firstCol + "&secCol=" + secondCol + "&\" " ;
120- String varname = "\" input\" " ;
121- input .setOnFocus ("new ajaxSuggest(this, {script:" + script + ", varname:" + varname + "} )" );
115+ displayPickerEdit (input );
122116 }
123-
124- buffer .append (input .toString ());
117+ buffer .append (input );
125118 }
126119
127120 @ Override
@@ -133,4 +126,33 @@ public void displayView(StringBuffer buffer, String name, String prefix, BaseCol
133126 buffer .append (XMLUtils .escapeElementText (property .toText ()));
134127 }
135128 }
129+
130+ private void displayPickerEdit (input input )
131+ {
132+ input .setClass ("suggested" );
133+ XWikiContext xWikiContext = getXWikiContext ();
134+ XWiki xwiki = xWikiContext .getWiki ();
135+ String path = xwiki .getURL ("Main.WebHome" , "view" , xWikiContext );
136+ StringBuilder stringBuilder = new StringBuilder ();
137+ stringBuilder .append (path );
138+ stringBuilder .append ('?' );
139+ stringBuilder .append (new EscapeTool ().url (getParametersMap ()));
140+ stringBuilder .append ('&' );
141+ input .setOnFocus (String .format ("new ajaxSuggest(this, {script:\" %s\" , varname:\" input\" } )" ,
142+ escapeJavaScript (stringBuilder .toString ())));
143+ }
144+
145+ private Map <String , String > getParametersMap ()
146+ {
147+ String dash = "-" ;
148+ // Using a linked hash map to keep the order of the keys stable when generating the query parameters, which
149+ // is especially handy for testing, but could be useful in other scenarios.
150+ Map <String , String > parameters = new LinkedHashMap <>();
151+ parameters .put ("xpage" , "suggest" );
152+ parameters .put ("classname" , getObject ().getName ());
153+ parameters .put ("fieldname" , getName ());
154+ parameters .put ("firCol" , dash );
155+ parameters .put ("secCol" , dash );
156+ return parameters ;
157+ }
136158}
0 commit comments