Re: how to inject arbitrary javascript code to a component markup?

2009-10-11 Thread nino martinez wael
This is a clear case for a behavior, and would be simple too..
Like so:
https://wicket-stuff.svn.sourceforge.net/svnroot/wicket-stuff/trunk/wicketstuff-core/artwork-parent/artwork/src/main/java/org/wicketstuff/artwork/niftycorners/NiftyCornersBehavior.java



2009/10/10 Paul Huang paulhuan...@gmail.com

 Suppose I write my own wicket component called XYZ that have the following
 markup

 wicket:panel
 div wicket:id=id_xxx
 
 /div
 /wicket:panel

 How can I inject some js code into this markup so when it's rendered in a
 page, I got something like

 wicket:panel
 div wicket:id=id_xxx
  script type=text/javascript
 var data=[abc, efg];
 document.write(data[0]);
 /script
 .
 div
 wicket:panel

 You may ask why I dont simply keep the js code into the component markup
 XYZ.html, this is because the value of data variable will be provided
 by
 the user and is not fixed.



Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread McIlwee, Craig
If your component is a MarkupContainer you can override 
getAssociatedMarkupStream(boolean) and build the markup on the fly.  So maybe 
still have the HTML file that you read in as a template with some place holder 
string and in the override you replace the place holder with stuff you want to 
be assigned to the data variable.  Then use a StringResourceStream to create a 
MarkupResourceStream, use SimpleMarkupLoader to create a Markup instance from 
the MarkupResourceStream, and finally create a MarkupStream with your Markup 
instance.

One thing to note is that if your markup container has child components that 
will be updated via AJAX AND you don't have an HTML file (you build the entire 
string on the fly) then you may also have to override hasAssociatedMarkupStream 
and always return true else the component being updated won't be able to locate 
its parent.

Craig
  _  

From: Paul Huang [mailto:paulhuan...@gmail.com]
To: users@wicket.apache.org
Sent: Sat, 10 Oct 2009 08:34:15 -0400
Subject: how to inject arbitrary javascript code to a component markup?

Suppose I write my own wicket component called XYZ that have the following
  markup
  
  wicket:panel
  div wicket:id=id_xxx
  
  /div
  /wicket:panel
  
  How can I inject some js code into this markup so when it's rendered in a
  page, I got something like
  
  wicket:panel
  div wicket:id=id_xxx
   script type=text/javascript
   var data=[abc, efg];
   document.write(data[0]);
  /script
  .
  div
  wicket:panel
  
  You may ask why I dont simply keep the js code into the component markup
  XYZ.html, this is because the value of data variable will be provided by
  the user and is not fixed.


Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread PaulH98



McIlwee, Craig wrote:
 
 If your component is a MarkupContainer you can override
 getAssociatedMarkupStream(boolean) and build the markup on the fly.  So
 maybe still have the HTML file that you read in as a template with some
 place holder string and in the override you replace the place holder with
 stuff you want to be assigned to the data variable.  Then use a
 StringResourceStream to create a MarkupResourceStream, use
 SimpleMarkupLoader to create a Markup instance from the
 MarkupResourceStream, and finally create a MarkupStream with your Markup
 instance.
 
 One thing to note is that if your markup container has child components
 that will be updated via AJAX AND you don't have an HTML file (you build
 the entire string on the fly) then you may also have to override
 hasAssociatedMarkupStream and always return true else the component being
 updated won't be able to locate its parent.
 
 Craig  
 


Here is the java code of my component CategorySelectPanel that extends
Panel



 public class CategorySelectPanel extends Panel{
   private static final long serialVersionUID = 1L;
   public CategorySelectPanel(String wicketId){
   super(wicketId);
   WebMarkupContainer cat0=new WebMarkupContainer(cat0);
   cat0.setOutputMarkupId(true);
   add(cat0);
   MarkupStream ms=getAssociatedMarkupStream(true); 
   System.out.println(ms.toString());
   
   }
 }
 

and here is the associated markup file CategorySelectPanel.html


 wicket:panel
   div wicket:id=cat0 class=cat0/div
 /wicket:panel
 
 


Now I have  a simple test


 
 public class TestPage extends WebPage{
   public TestPage(){
   CategorySelectPanel a= new CategorySelectPanel(csp);
   add(a);
   }
 }
 
with the the following page html 


 
 html
 head
 meta http-equiv=Content-Type content=text/html; charset=ISO-8859-1
 titleInsert title here/title
 /head
 body
 
 /body
 /html
 
 

when I run the test, I got the following exception 



 WicketMessage: Can't instantiate page using constructor public
 com.tree.TestPage()
 
 Root cause:
 
 java.lang.IllegalStateException: No Page found for component
 [MarkupContainer [Component id = csp]]
 at org.apache.wicket.Component.getPage(Component.java:1763)
 at
 org.apache.wicket.markup.html.WebMarkupContainer.getMarkupType(WebMarkupContainer.java:60)
 at
 org.apache.wicket.markup.DefaultMarkupCacheKeyProvider.getCacheKey(DefaultMarkupCacheKeyProvider.java:57)
 at org.apache.wicket.markup.MarkupCache.getMarkup(MarkupCache.java:291)
 at
 org.apache.wicket.markup.MarkupCache.getMarkupStream(MarkupCache.java:216)
 at
 org.apache.wicket.MarkupContainer.getAssociatedMarkupStream(MarkupContainer.java:351)
 at com.tree.CategorySelectPanel.init(CategorySelectPanel.java:37)
 at com.tree.TestPage.init(TestPage.java:7)
 
 

Basically, MarkupStream ms=getAssociatedMarkupStream(true)  caused this
exception. Can anyone shed some light on this? I am trying to Carig's
suggestion to get the MarkupStream and alter it with my stuff.


-- 
View this message in context: 
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component-markup--tp25833726p25834380.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



RE: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread Fatih Mehmet UCAR
Add a div to your page like below:

html
head
meta http-equiv=Content-Type content=text/html; 
charset=ISO-8859-1 titleInsert title here/title /head 
body

div wicket:id=csp/div

/body
/html

-Original Message-
From: PaulH98 [mailto:paulhuan...@gmail.com] 
Sent: 10 October 2009 14:58
To: users@wicket.apache.org
Subject: Re: how to inject arbitrary javascript code to a component markup?




McIlwee, Craig wrote:
 
 If your component is a MarkupContainer you can override
 getAssociatedMarkupStream(boolean) and build the markup on the fly.  So
 maybe still have the HTML file that you read in as a template with some
 place holder string and in the override you replace the place holder with
 stuff you want to be assigned to the data variable.  Then use a
 StringResourceStream to create a MarkupResourceStream, use
 SimpleMarkupLoader to create a Markup instance from the
 MarkupResourceStream, and finally create a MarkupStream with your Markup
 instance.
 
 One thing to note is that if your markup container has child components
 that will be updated via AJAX AND you don't have an HTML file (you build
 the entire string on the fly) then you may also have to override
 hasAssociatedMarkupStream and always return true else the component being
 updated won't be able to locate its parent.
 
 Craig  
 


Here is the java code of my component CategorySelectPanel that extends
Panel



 public class CategorySelectPanel extends Panel{
   private static final long serialVersionUID = 1L;
   public CategorySelectPanel(String wicketId){
   super(wicketId);
   WebMarkupContainer cat0=new WebMarkupContainer(cat0);
   cat0.setOutputMarkupId(true);
   add(cat0);
   MarkupStream ms=getAssociatedMarkupStream(true); 
   System.out.println(ms.toString());
   
   }
 }
 

and here is the associated markup file CategorySelectPanel.html


 wicket:panel
   div wicket:id=cat0 class=cat0/div
 /wicket:panel
 
 


Now I have  a simple test


 
 public class TestPage extends WebPage{
   public TestPage(){
   CategorySelectPanel a= new CategorySelectPanel(csp);
   add(a);
   }
 }
 
with the the following page html 


 
 html
 head
 meta http-equiv=Content-Type content=text/html; charset=ISO-8859-1
 titleInsert title here/title
 /head
 body
 
 /body
 /html
 
 

when I run the test, I got the following exception 



 WicketMessage: Can't instantiate page using constructor public
 com.tree.TestPage()
 
 Root cause:
 
 java.lang.IllegalStateException: No Page found for component
 [MarkupContainer [Component id = csp]]
 at org.apache.wicket.Component.getPage(Component.java:1763)
 at

org.apache.wicket.markup.html.WebMarkupContainer.getMarkupType(WebMarkupCont
ainer.java:60)
 at

org.apache.wicket.markup.DefaultMarkupCacheKeyProvider.getCacheKey(DefaultMa
rkupCacheKeyProvider.java:57)
 at org.apache.wicket.markup.MarkupCache.getMarkup(MarkupCache.java:291)
 at
 org.apache.wicket.markup.MarkupCache.getMarkupStream(MarkupCache.java:216)
 at

org.apache.wicket.MarkupContainer.getAssociatedMarkupStream(MarkupContainer.
java:351)
 at com.tree.CategorySelectPanel.init(CategorySelectPanel.java:37)
 at com.tree.TestPage.init(TestPage.java:7)
 
 

Basically, MarkupStream ms=getAssociatedMarkupStream(true)  caused this
exception. Can anyone shed some light on this? I am trying to Carig's
suggestion to get the MarkupStream and alter it with my stuff.


-- 
View this message in context:
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component
-markup--tp25833726p25834380.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



RE: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread PaulH98



Fatih Mehmet UCAR wrote:
 
 Add a div to your page like below:
 
 html
 head
 meta http-equiv=Content-Type content=text/html; 
 charset=ISO-8859-1 titleInsert title here/title /head 
 body
 
   div wicket:id=csp/div
 
 /body
 /html
 
 

Thanks Fatih, Indeed, I had div wicket:id=csp/div in my test page,
but somehow deleted it when posting my previous message. The same problem
persists. 

-- 
View this message in context: 
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component-markup--tp25833726p25834544.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread Igor Vaynberg
can you not just use a label whose model is the script.../script string?

-igor

On Sat, Oct 10, 2009 at 5:34 AM, Paul Huang paulhuan...@gmail.com wrote:
 Suppose I write my own wicket component called XYZ that have the following
 markup

 wicket:panel
 div wicket:id=id_xxx
 
 /div
 /wicket:panel

 How can I inject some js code into this markup so when it's rendered in a
 page, I got something like

 wicket:panel
 div wicket:id=id_xxx
  script type=text/javascript
     var data=[abc, efg];
     document.write(data[0]);
 /script
 .
 div
 wicket:panel

 You may ask why I dont simply keep the js code into the component markup
 XYZ.html, this is because the value of data variable will be provided by
 the user and is not fixed.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread Anton Veretennikov
For example I use this markup:

script wicket:id=focusScript/script

and associate it with simple class:

public class FocusScript extends Label {

  public FocusScript(String id, String focusFieldMarkupId, boolean selectAll) {
super(id, getFocusScript(focusFieldMarkupId, selectAll));
setEscapeModelStrings(false);
  }

  @Override
  protected void onComponentTag(ComponentTag tag) {
tag.put(language, javascript);
tag.put(type, text/javascript);
super.onComponentTag(tag);
  }

  @Override
  protected void onComponentTagBody(MarkupStream markupStream,
ComponentTag openTag) {
super.onComponentTagBody(markupStream, openTag);
checkComponentTag(openTag, script);
  }

  public static String getFocusScript(String focusFieldMarkupId,
boolean selectAll) {
return document.getElementById(' + focusFieldMarkupId + ').focus(); +
(selectAll?(document.getElementById(' +
focusFieldMarkupId + ').select()):);
  }


}


On Sat, Oct 10, 2009 at 11:11 PM, Igor Vaynberg igor.vaynb...@gmail.com wrote:
 can you not just use a label whose model is the script.../script string?

 -igor

 On Sat, Oct 10, 2009 at 5:34 AM, Paul Huang paulhuan...@gmail.com wrote:
 Suppose I write my own wicket component called XYZ that have the following
 markup

 wicket:panel
 div wicket:id=id_xxx
 
 /div
 /wicket:panel

 How can I inject some js code into this markup so when it's rendered in a
 page, I got something like

 wicket:panel
 div wicket:id=id_xxx
  script type=text/javascript
     var data=[abc, efg];
     document.write(data[0]);
 /script
 .
 div
 wicket:panel

 You may ask why I dont simply keep the js code into the component markup
 XYZ.html, this is because the value of data variable will be provided by
 the user and is not fixed.


 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org



-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread McIlwee, Craig
Didn't think of that approach, looks good.  But to clear up my previous 
suggestion since I guess I wasn't clear enough and its useful in other 
situations also, you need to _override_ getAssociatedMarkupStream(boolean), not 
just call it.

public static final String JAVASCRIPT_PLACEHOLDER = %JS_HERE%;
public MarkupStream getAssociatedMarkupStream(boolean throwEx) {
  String jsToInsert = ... // create your java script
  String html = ... // read HTML file via getClass().getResource
  html = html.replace(JAVASCRIPT_PLACEHOLDER, jsToInsert);
  IResourceStream srs = new StringResourceStream(html);
  MarkupResourceStream mrs = new MarkupResourceStream(srs);
  Markup markup = new SimpleMarkupLoader().loadMarkup(this, mrs, null, true);
  return new MarkupStream(markup);
}

and your panel

wicket:panel
!-- some markup here --
%JS_HERE%
!-- more markup --
/wicket:panel

Craig
  _  

From: Anton Veretennikov [mailto:anton.veretenni...@gmail.com]
To: users@wicket.apache.org
Sent: Sat, 10 Oct 2009 11:24:12 -0400
Subject: Re: how to inject arbitrary javascript code to a component markup?

For example I use this markup:
  
  script wicket:id=focusScript/script
  
  and associate it with simple class:
  
  public class FocusScript extends Label {
  
public FocusScript(String id, String focusFieldMarkupId, boolean selectAll) 
{
  super(id, getFocusScript(focusFieldMarkupId, selectAll));
  setEscapeModelStrings(false);
}
  
@Override
protected void onComponentTag(ComponentTag tag) {
  tag.put(language, javascript);
  tag.put(type, text/javascript);
  super.onComponentTag(tag);
}
  
@Override
protected void onComponentTagBody(MarkupStream markupStream,
  ComponentTag openTag) {
  super.onComponentTagBody(markupStream, openTag);
  checkComponentTag(openTag, script);
}
  
public static String getFocusScript(String focusFieldMarkupId,
  boolean selectAll) {
  return document.getElementById(' + focusFieldMarkupId + ').focus(); +
  (selectAll?(document.getElementById(' +
  focusFieldMarkupId + ').select()):);
}
  
  
  }
  
  
  On Sat, Oct 10, 2009 at 11:11 PM, Igor Vaynberg igor.vaynb...@gmail.com 
wrote:
   can you not just use a label whose model is the script.../script 
string?
  
   -igor
  
   On Sat, Oct 10, 2009 at 5:34 AM, Paul Huang paulhuan...@gmail.com wrote:
   Suppose I write my own wicket component called XYZ that have the following
   markup
  
   wicket:panel
   div wicket:id=id_xxx
   
   /div
   /wicket:panel
  
   How can I inject some js code into this markup so when it's rendered in a
   page, I got something like
  
   wicket:panel
   div wicket:id=id_xxx
script type=text/javascript
   var data=[abc, efg];
   document.write(data[0]);
   /script
   .
   div
   wicket:panel
  
   You may ask why I dont simply keep the js code into the component markup
   XYZ.html, this is because the value of data variable will be provided 
by
   the user and is not fixed.
  
  
   -
   To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
   For additional commands, e-mail: users-h...@wicket.apache.org
  
  
  
  -
  To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
  For additional commands, e-mail: users-h...@wicket.apache.org
  


Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread PaulH98



Anton Veretennikov wrote:
 
 For example I use this markup:
 
 script wicket:id=focusScript/script
 
 and associate it with simple class:
 
 public class FocusScript extends Label {
 
   public FocusScript(String id, String focusFieldMarkupId, boolean
 selectAll) {
 super(id, getFocusScript(focusFieldMarkupId, selectAll));
 setEscapeModelStrings(false);
   }
 
   @Override
   protected void onComponentTag(ComponentTag tag) {
 tag.put(language, javascript);
 tag.put(type, text/javascript);
 super.onComponentTag(tag);
   }
 
   @Override
   protected void onComponentTagBody(MarkupStream markupStream,
 ComponentTag openTag) {
 super.onComponentTagBody(markupStream, openTag);
 checkComponentTag(openTag, script);
   }
 
   public static String getFocusScript(String focusFieldMarkupId,
 boolean selectAll) {
 return document.getElementById(' + focusFieldMarkupId +
 ').focus(); +
 (selectAll?(document.getElementById(' +
 focusFieldMarkupId + ').select()):);
   }
 
 }
 
 

Thanks Igor and Anton. That approach works. One thing to remember is to call
setEscapeModelStrings(false) to insert string script /script as
it is, as Anton pointed out.



-- 
View this message in context: 
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component-markup--tp25833726p25837324.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: how to inject arbitrary javascript code to a component markup?

2009-10-10 Thread PaulH98



McIlwee, Craig wrote:
 
 Didn't think of that approach, looks good.  But to clear up my previous
 suggestion since I guess I wasn't clear enough and its useful in other
 situations also, you need to _override_
 getAssociatedMarkupStream(boolean), not just call it.
 
 public static final String JAVASCRIPT_PLACEHOLDER = %JS_HERE%;
 public MarkupStream getAssociatedMarkupStream(boolean throwEx) {
   String jsToInsert = ... // create your java script
   String html = ... // read HTML file via getClass().getResource
   html = html.replace(JAVASCRIPT_PLACEHOLDER, jsToInsert);
   IResourceStream srs = new StringResourceStream(html);
   MarkupResourceStream mrs = new MarkupResourceStream(srs);
   Markup markup = new SimpleMarkupLoader().loadMarkup(this, mrs, null,
 true);
   return new MarkupStream(markup);
 }
 
 and your panel
 
 wicket:panel
 !-- some markup here --
 %JS_HERE%
 !-- more markup --
 /wicket:panel
 Craig
 

Carig- Just tried your approach, it works as well. Thanks for your help.
Here is the complete code, just in case someone else may need it.



 @Override
   public MarkupStream getAssociatedMarkupStream(boolean throwEx) {
 String jsToInsert =my js code; // create your java script
 InputStream
 htmlStream=this.getClass().getResourceAsStream(YOUR_MARKUP_TEMPLATE.html);
 String html=convertStreamToString(htmlStream);
 html = html.replace(JAVASCRIPT_PLACEHOLDER, jsToInsert);
 IResourceStream srs = new StringResourceStream(html);
 MarkupResourceStream mrs = new MarkupResourceStream(srs);
 Markup markup = null;
   try {
   markup = new 
 SimpleMarkupLoader().loadMarkup(this, mrs, null, true);
   } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   } catch (ResourceStreamNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   }
 return new MarkupStream(markup);
   } 
   
   public String convertStreamToString(InputStream is) {
   /*
* To convert the InputStream to String we use the
 BufferedReader.readLine()
* method. We iterate until the BufferedReader return 
 null which
 means
* there's no more data to read. Each line will 
 appended to a
 StringBuilder
* and returned as String.
*/
   BufferedReader reader = new BufferedReader(new
 InputStreamReader(is));
   StringBuilder sb = new StringBuilder();
   
   String line = null;
   try {
   while ((line = reader.readLine()) != null) {
   sb.append(line + \n);
   }
   } catch (IOException e) {
   e.printStackTrace();
   } finally {
   try {
   is.close();
   } catch (IOException e) {
   e.printStackTrace();
   }
   }

   return sb.toString();
   }
 

-- 
View this message in context: 
http://www.nabble.com/how-to-inject-arbitrary-javascript-code-to-a-component-markup--tp25833726p25837543.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org