2020import org .apache .wicket .ajax .attributes .AjaxRequestAttributes ;
2121import org .wicketstuff .dashboard .WidgetLocation ;
2222
23- import com .google .gson .Gson ;
23+ import com .github .openjson .JSONArray ;
24+ import com .github .openjson .JSONObject ;
2425
2526/**
2627 * @author Decebal Suiu
@@ -37,54 +38,21 @@ public abstract class SortableAjaxBehavior extends AbstractDefaultAjaxBehavior {
3738 protected void updateAjaxAttributes (AjaxRequestAttributes attributes ) {
3839 super .updateAjaxAttributes (attributes );
3940
40- StringBuilder buffer = new StringBuilder ();
41- buffer .append ("var data = serializeWidgetLocations();" );
42- buffer .append ("return {'" + JSON_DATA + "': data};" );
43-
44- attributes .getDynamicExtraParameters ().add (buffer );
41+ attributes .getDynamicExtraParameters ()
42+ .add ("var data = serializeWidgetLocations();" +
43+ "return {'" + JSON_DATA + "': data};" );
4544 }
4645
4746 @ Override
4847 protected void respond (AjaxRequestTarget target ) {
49- String jsonData = getComponent ().getRequest ().getRequestParameters ().getParameterValue (JSON_DATA ).toString ();
50- Item [] items = getItems (jsonData );
5148 Map <String , WidgetLocation > locations = new HashMap <String , WidgetLocation >();
52- for (Item item : items ) {
53- WidgetLocation location = new WidgetLocation (item .column , item .sortIndex );
54- locations .put (item .widget , location );
49+ String jsonData = getComponent ().getRequest ().getRequestParameters ().getParameterValue (JSON_DATA ).toString ();
50+ JSONArray arr = new JSONArray (jsonData );
51+ for (int i = 0 ; i < arr .length (); ++i ) {
52+ JSONObject obj = arr .getJSONObject (i );
53+ locations .put (obj .getString ("widget" ),
54+ new WidgetLocation (obj .getInt ("column" ), obj .getInt ("sortIndex" )));
5555 }
56-
5756 onSort (target , locations );
5857 }
59-
60- private Item [] getItems (String jsonData ) {
61- Gson gson = new Gson ();
62- Item [] items = gson .fromJson (jsonData , Item [].class );
63- /*
64- System.out.println(items.length);
65- for (Item item : items) {
66- System.out.println(item);
67- }
68- */
69-
70- return items ;
71- }
72-
73- static class Item {
74- public int column ;
75- public String widget ;
76- public int sortIndex ;
77-
78- @ Override
79- public String toString () {
80- StringBuffer buffer = new StringBuffer ();
81- buffer .append ("Item[" );
82- buffer .append ("column = " ).append (column );
83- buffer .append (" widget = " ).append (widget );
84- buffer .append (" sortIndex = " ).append (sortIndex );
85- buffer .append ("]" );
86-
87- return buffer .toString ();
88- }
89- }
9058}
0 commit comments