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(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?
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?
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?
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
Re: Client-side treeView
jWeekend wrote: Paul, Take a look at Alastair's presenttaion called something like A Drag And Drop List Editor [1] and the accompanying source code. I don't think we have a tree publicly available yet in WiQuery [2] but for sure you should get some good ideas there even if you don't want to use it. Regards - Cemal jWeekend OO Java Technologies, Wicket Training and Development http://jWeekend.com [1] http://jweekend.com/dev/ArticlesPage [2] http://code.google.com/p/wiquery/ Thanks Cemal, The jweekend example is quite helpful, I'm trying to understand it better to write my own js component. -- View this message in context: http://www.nabble.com/Client-side-treeView-tp25775360p25788056.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: Client-side treeView
Michael O'Cleirigh wrote: Hi Paul, Most of the wicket + javascript integrations in wicket-stuff (http://wicketstuff.org/confluence/display/STUFFWIKI/Wiki) will show how communication between wicket and javascript can work. They can get a little messy but once implemented are nice/simple to deal with since they are self contained. When the page is rendered custom javascript is also rendered that connects the component javascript (in your case the stock tree table) with the wicket components (in your case the hiddenField.getMarkupID() that will be used to fill in the selection details). This can be done by rendering out the wicket markup id details into a global variable in the DOM or as a property in a custom object in the DOM. In the case of Palette it uses its own DOM object called Wicket.Palette (see palette.js which is adjacent to Palette.java) This is where the work is done to move values between the choicesComponent and selectionComponent. The recorderComponent is a customized HiddenField that stores the list of values that are shown in the selectionComponent. The buttons are configured with onclick actions which are connected to the contextualized Wicket.Palette.action functions. Each of the Wicket.Palette.action functions expect three arguments: 1. the markup id of the choices component 2. the markup id of the selection component 3. the markup id of the recorder component. Which are filled in automatically for the palette being rendered. For your example you could create a custom hidden field like this and define the converter so that it will have a comma seperated string content but resolve into a list of Elements: new HiddenFieldListElement(id) { /* (non-Javadoc) * @see org.apache.wicket.Component#getConverter(java.lang.Class) */ @Override public IConverter getConverter(Class? type) { return new IConverter() { @Override public String convertToString(ListElement valueList, Locale locale) { // convert value list into a comma seperated string like A,B,C... return string; } @Override public Object convertToObject(String value, Locale locale) { // convert a comma seperated string A,B,C... back into the list of elements String[] elements = value.split(,); ListElement elementList = new LinkedListElement(); for (String e : elements) { // convert } return elementList; } }; }}; Then your javascript hooks for the on selection will append the id value of the selected element into the hidden field. When the form posts the formsubmittingComponent.onSubmit() action can just call hiddenField.getModelObject() to get the list of selected elements to process. Hope this helps you get started, Mike - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org Hi, Mike - thank you very much for your reply. I am following the examples on wicket-stuff and jweekend to write my own js tree-selector component. If I shall finish it, I will post my code to this forum to thank all the help I got. -- View this message in context: http://www.nabble.com/Client-side-treeView-tp25775360p25788264.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: Client-side treeView
Michael O'Cleirigh wrote: Hi Paul, What I would do would be to find a javascript implementation that does what you want and then have the selection events fill in a hidden field which will then be available on the server side when the form is posted. This would be similar to how the Palette works in wicket-extensions You then define a custom IConverter to convert the string input from the hidden field into the IModelListLeaf or IModelListNode or what ever makes sense for your model.. There is probably something in wicket-stuff already but here is a link to a jquery plugin for a tree table: http://blog.cubicphuse.nl/2008/11/12/jquery-treetable-2-0 Regards, Mike Thanks Mike... Is there any document on how to write such a component like Palette that communicates with javascript? I just skipped through the Palette java an js source code and found they are kind of hard to understand. Wicket in Action does not seem to cover this topic either. -- View this message in context: http://www.nabble.com/Client-side-treeView-tp25775360p25776230.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