SHARE
TWEET

Untitled

a guest Oct 13th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
  2. index 70dd69d..bb952bc 100644
  3. --- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
  4. +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
  5. @@ -933,16 +933,6 @@
  6.          */
  7.         processEvaluation: function (context, node) {
  8.  
  9. -           // used to match evaluation scripts which manually call FunctionsExecuter's notify() when ready
  10. -           var scriptWithIdentifierR = new RegExp("\\(function\\(\\)\\{([a-zA-Z_]\\w*)\\|((.|\\n)*)?\\}\\)\\(\\);$");
  11. -
  12. -           /**
  13. -            * A regex used to split the text in (priority-)evaluate elements in the Ajax response
  14. -            * when there are scripts which require manual call of 'FunctionExecutor#notify()'
  15. -            * @type {RegExp}
  16. -            */
  17. -           var scriptSplitterR = new RegExp("\\(function\\(\\)\\{[\\s\\S]*?}\\)\\(\\);", 'gi');
  18. -
  19.             // get the javascript body
  20.             var text = Wicket.DOM.text(node);
  21.  
  22. @@ -950,61 +940,54 @@
  23.             var steps = context.steps;
  24.             var log = Wicket.Log;
  25.  
  26. -           var evaluateWithManualNotify = function (parameters, body) {
  27. -               return function(notify) {
  28. -                   var toExecute = "(function(" + parameters + ") {" + body + "})";
  29. -
  30. -                   try {
  31. -                       // do the evaluation in global scope
  32. -                       var f = window.eval(toExecute);
  33. -                       f(notify);
  34. -                   } catch (exception) {
  35. -                       log.error("Wicket.Ajax.Call.processEvaluation: Exception evaluating javascript: %s", text, exception);
  36. -                   }
  37. -                   return FunctionsExecuter.ASYNC;
  38. -               };
  39. -           };
  40. -
  41.             var evaluate = function (script) {
  42.                 return function(notify) {
  43. -                   // just evaluate the javascript
  44. +                   // start with no suspension
  45. +                   Wicket.Ajax._suspended = 0;
  46. +                   Wicket.Ajax.suspend = function() {
  47. +                       // keep track of suspensions
  48. +                       Wicket.Ajax._suspended++;
  49. +                      
  50. +                       var released = false;
  51. +                      
  52. +                       return function() {
  53. +                           // relase only once
  54. +                           if (released == false) {
  55. +                               released = true;
  56. +                              
  57. +                               // keep track of suspensions
  58. +                               Wicket.Ajax._suspended--;
  59. +                              
  60. +                               if (Wicket.Ajax._suspended == 0) {
  61. +                                   // last suspension released
  62. +                                   notify();
  63. +                               }
  64. +                           }
  65. +                       }
  66. +                   };
  67. +                  
  68. +                   // evaluate the javascript
  69.                     try {
  70.                         // do the evaluation in global scope
  71.                         window.eval(script);
  72.                     } catch (exception) {
  73.                         log.error("Ajax.Call.processEvaluation: Exception evaluating javascript: %s", text, exception);
  74.                     }
  75. -                   // continue to next step
  76. -                   return FunctionsExecuter.DONE;
  77. +                  
  78. +                   if (Wicket.Ajax._suspended) {
  79. +                       // suspended
  80. +                       return FunctionsExecuter.ASYNC;
  81. +                   } else {
  82. +                       // no suspension
  83. +                       Wicket.Ajax._suspended = undefined;
  84. +                       Wicket.Ajax.suspend = undefined;
  85. +                      
  86. +                       // continue to next step
  87. +                       return FunctionsExecuter.DONE;
  88. +                   }
  89.                 };
  90.             };
  91. -
  92. -           // test if the javascript is in form of identifier|code
  93. -           // if it is, we allow for letting the javascript decide when the rest of processing will continue
  94. -           // by invoking identifier();. This allows usage of some asynchronous/deferred logic before the next script
  95. -           // See WICKET-5039
  96. -           if (scriptWithIdentifierR.test(text)) {
  97. -               var scripts = [];
  98. -               var scr;
  99. -               while ( (scr = scriptSplitterR.exec(text) ) !== null ) {
  100. -                   scripts.push(scr[0]);
  101. -               }
  102. -
  103. -               for (var s = 0; s < scripts.length; s++) {
  104. -                   var script = scripts[s];
  105. -                   if (script) {
  106. -                       var scriptWithIdentifier = script.match(scriptWithIdentifierR);
  107. -                       if (scriptWithIdentifier) {
  108. -                           steps.push(evaluateWithManualNotify(scriptWithIdentifier[1], scriptWithIdentifier[2]));
  109. -                       }
  110. -                       else {
  111. -                           steps.push(evaluate(script));
  112. -                       }
  113. -                   }
  114. -               }
  115. -           } else {
  116. -               steps.push(evaluate(text));
  117. -           }
  118. +           steps.push(evaluate(text));
  119.         },
  120.  
  121.         // Adds a closure that processes a header contribution
  122. diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/IPartialPageRequestHandler.java b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/IPartialPageRequestHandler.java
  123. index da126a2..e7a2a52 100644
  124. --- a/wicket-core/src/main/java/org/apache/wicket/core/request/handler/IPartialPageRequestHandler.java
  125. +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/handler/IPartialPageRequestHandler.java
  126. @@ -64,15 +64,22 @@
  127.     void addChildren(MarkupContainer parent, Class<?> childCriteria);
  128.  
  129.     /**
  130. -    * Adds javascript that will be evaluated on the client side after components are replaced
  131. -    *
  132. -    * <p>If the javascript needs to do something asynchronously (i.e. needs to use window.setTimeout(), for example
  133. -    * to do animations) then the following special syntax may be used: <code>someFunctionName|myJsLogic(someFunctionName);</code>.
  134. -    * Wicket will transform it to: <code>function(someFunctionName){myJsLogic(someFunctionName);}</code> and your code
  135. -    * is responsible to execute <em>someFunctionName()</em> when the asynchronous task is finished. Once <em>someFunctionName</em>
  136. -    * is executed the next appended script will be executed. <strong>Important</strong>: it is highly recommended to
  137. -    * execute your code in try/finally to make sure <em>someFunctionName</em> is executed even an error happens in
  138. -    * your code, otherwise all following scripts wont be executed.</p>
  139. +    * Adds JavaScript that will be evaluated on the client side after components are replaced
  140. +    * <p>
  141. +    * If the JavaScript needs to do something asynchronously (i.e. needs to use window.setTimeout(), for example to do animations)
  142. +    * then the global method <code>Wicket.Ajax.suspend()</code> may be used, e.g.:
  143. +    * <pre>
  144. +    * target.prependJavaScript("jQuery('#id').fadeOut(1000, Wicket.Ajax.suspend());");
  145. +    * target.appendJavaScript("jQuery('#id').hide().fadeIn(1000, Wicket.Ajax.suspend());");
  146. +    * </pre>
  147. +    * <code>suspend()</code> returns a JavaScript function which your code is responsible to invoke when the
  148. +    * asynchronous task has finished (in the example above done automatically by jQuery fading methods).
  149. +    * Once this is done the next appended script will be executed.
  150. +    * <p>
  151. +    * <strong>Important</strong>: it is highly recommended to execute your code in try/finally to
  152. +    * make sure the suspension is released even an error happens in your code, otherwise all
  153. +    * following scripts wont be executed.
  154. +    * </p>
  155.      *
  156.      * @param javascript
  157.      */
  158. diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.html
  159. index 241c86e..ea078c7 100644
  160. --- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.html
  161. +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.html
  162. @@ -21,7 +21,12 @@
  163.  counter 2: <span wicket:id="c2"></span>
  164.  <br/>
  165.  <br/>
  166. -<a href="#" wicket:id="c1-link">counter1++ and shake</a>&#160;&#160;<a href="#" wicket:id="c2-link">counter2++ and highlight</a>
  167. +counter 3: <span wicket:id="c3"></span>
  168. +<br/>
  169. +<br/>
  170. +<a href="#" wicket:id="c1-link">counter1++ and shake</a>&#160;&#160;
  171. +<a href="#" wicket:id="c2-link">counter2++ and highlight</a>&#160;&#160;
  172. +<a href="#" wicket:id="c3-link">counter3++ and fade</a>
  173.  </div>
  174.  </wicket:extend>
  175.  </html>
  176. \ No newline at end of file
  177. diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.java
  178. index e286584..4b48677 100644
  179. --- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.java
  180. +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/EffectsPage.java
  181. @@ -36,40 +36,7 @@
  182.  {
  183.     private int counter1 = 0;
  184.     private int counter2 = 0;
  185. -
  186. -   /**
  187. -    * @return Value of counter1
  188. -    */
  189. -   public int getCounter1()
  190. -   {
  191. -       return counter1;
  192. -   }
  193. -
  194. -   /**
  195. -    * @param counter1
  196. -    *            New value for counter1
  197. -    */
  198. -   public void setCounter1(int counter1)
  199. -   {
  200. -       this.counter1 = counter1;
  201. -   }
  202. -
  203. -   /**
  204. -    * @return Value for counter2
  205. -    */
  206. -   public int getCounter2()
  207. -   {
  208. -       return counter2;
  209. -   }
  210. -
  211. -   /**
  212. -    * @param counter2
  213. -    *            New value for counter2
  214. -    */
  215. -   public void setCounter2(int counter2)
  216. -   {
  217. -       this.counter2 = counter2;
  218. -   }
  219. +   private int counter3 = 0;
  220.  
  221.     /**
  222.      * Constructor
  223. @@ -84,6 +51,10 @@
  224.         c2.setOutputMarkupId(true);
  225.         add(c2);
  226.  
  227. +       final Label c3 = new Label("c3", new PropertyModel<>(this, "counter3"));
  228. +       c3.setOutputMarkupId(true);
  229. +       add(c3);
  230. +
  231.         add(new AjaxLink<Void>("c1-link")
  232.         {
  233.             @Override
  234. @@ -91,8 +62,7 @@
  235.             {
  236.                 counter1++;
  237.                 target.add(c1);
  238. -               target.appendJavaScript(String.format("jQuery('#%s').effect('shake');",
  239. -                   c1.getMarkupId()));
  240. +               target.appendJavaScript(String.format("jQuery('#%s').effect('shake');", c1.getMarkupId()));
  241.             }
  242.  
  243.             @Override
  244. @@ -112,8 +82,29 @@
  245.                 counter2++;
  246.                 targetOptional.ifPresent(target -> {
  247.                     target.add(c2);
  248. -                   target.appendJavaScript(String.format("jQuery('#%s').effect('highlight');",
  249. -                           c2.getMarkupId()));
  250. +                   target.appendJavaScript(String.format("jQuery('#%s').effect('highlight');", c2.getMarkupId()));
  251. +               });
  252. +           }
  253. +
  254. +           @Override
  255. +           protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
  256. +           {
  257. +               attributes.setChannel(new AjaxChannel("effects", Type.DROP));
  258. +
  259. +               super.updateAjaxAttributes(attributes);
  260. +           }
  261. +       });
  262. +      
  263. +       add(new AjaxFallbackLink<Void>("c3-link")
  264. +       {
  265. +           @Override
  266. +           public void onClick(Optional<AjaxRequestTarget> targetOptional)
  267. +           {
  268. +               counter3++;
  269. +               targetOptional.ifPresent(target -> {
  270. +                   target.prependJavaScript(String.format("jQuery('#%s').fadeOut(1000, Wicket.Ajax.suspend());",c3.getMarkupId()));
  271. +                   target.add(c3);
  272. +                   target.appendJavaScript(String.format("jQuery('#%s').hide().fadeIn(1000, Wicket.Ajax.suspend());", c3.getMarkupId()));
  273.                 });
  274.             }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top