Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-4199

add a hook to SubmitLink that allows to insert arbitrary JavaScript before code that submit the form

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.5.2
    • Fix Version/s: 1.5.3
    • Component/s: wicket
    • Labels:

      Description

      I need to execute some JavaScript before form is submmited and as method getTriggerJavaScrip is final I have to do it like

      SubmitLink link = new SubmitLink("link") {

      private static final long serialVersionUID = 1L;

      @Override
      protected void onComponentTag(ComponentTag tag)
      {
      super.onComponentTag(tag);
      // If we're disabled
      if (!isLinkEnabled())

      { disableLink(tag); }

      else
      {
      if (tag.getName().equalsIgnoreCase("a"))

      { tag.put("href", "#"); }

      tag.put("onclick", getTriggerJavaScript1());
      }
      }

      protected String getTriggerJavaScript1()
      {
      if (getForm() != null)
      {
      // find the root form - the one we are really going to submit
      Form<?> root = getForm().getRootForm();
      StringBuilder sb = new StringBuilder(100);
      // my code
      sb.append("document.getElementById('");
      sb.append(description.getMarkupId());
      sb.append("').value=CKEDITOR.instances.");
      sb.append(description.getMarkupId());
      sb.append(".getData();");
      // end of my code
      sb.append("var e=document.getElementById('");
      sb.append(root.getHiddenFieldId());
      sb.append("'); e.name=\'");
      sb.append(getInputName());
      sb.append("'; e.value='x';");
      sb.append("var f=document.getElementById('");
      sb.append(root.getMarkupId());
      sb.append("');");
      if (shouldInvokeJavaScriptFormOnsubmit())
      {
      if (getForm() != root)

      { sb.append("var ff=document.getElementById('"); sb.append(getForm().getMarkupId()); sb.append("');"); }

      else

      { sb.append("var ff=f;"); }

      sb.append("if (ff.onsubmit != undefined)

      { if (ff.onsubmit()==false) return false; }

      ");
      }
      sb.append("f.submit();e.value='';e.name='';return false;");
      return sb.toString();
      }
      else

      { return null; }

      }
      };

      This ugly and moreover if form submiting logic changes my extended link might stop to work properly. I propose adding a method decorareTriggerJavaScript that allows users to decorate the form submiting script. e.g. in my case

      SubmitLink link = new SubmitLink("link") {
      private static final long serialVersionUID = 1L;

      @Override
      protected void decorareTriggerJavaScript(StringBuilder sb)

      { StringBuilder sb1 = new StringBuilder(100); sb1.append("document.getElementById('"); sb1.append(description.getMarkupId()); sb1.append("').value=CKEDITOR.instances."); sb1.append(description.getMarkupId()); sb1.append(".getData();"); sb.insert(0, sb1.toString()); }

      };

        Attachments

        1. SubmitLink.java.patch
          1.0 kB
          Ernesto Reinaldo Barreiro

          Activity

            People

            • Assignee:
              jthomerson Jeremy Thomerson
              Reporter:
              reiern70 Ernesto Reinaldo Barreiro
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified