*/\r
package com.google.gwt.query.client.plugins;\r
\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.query.client.Function;\r
import com.google.gwt.query.client.GQuery;\r
import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.DateBoxWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.RichTextWidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions;\r
import com.google.gwt.query.client.plugins.widgets.TextBoxWidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.WidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.WidgetOptions;\r
-import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions;\r
import com.google.gwt.user.client.ui.Button;\r
import com.google.gwt.user.client.ui.TabPanel;\r
import com.google.gwt.user.client.ui.TextBox;\r
import com.google.gwt.user.client.ui.Widget;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
+import com.google.gwt.user.datepicker.client.DateBox;\r
\r
/**\r
* Widgets plugin for Gwt Query. Be careful, this plugin is still experimental.\r
return widgets(new ButtonWidgetFactory(), initFunctions);\r
}\r
\r
+ /**\r
+ * Create a {@link DateBox} widget for each selected element. The\r
+ * <code>initFunctions</code> will be called on each new {@link Button}\r
+ * created by passing them in parameter.\r
+ */\r
+ public Widgets datebox(Function... initFunctions) {\r
+ return widgets(new DateBoxWidgetFactory(), initFunctions);\r
+ }\r
+ \r
+ public Widgets richtext(Function... initFunctions) {\r
+ return widgets(new RichTextWidgetFactory(), initFunctions);\r
+ }\r
+\r
+\r
/**\r
* Create an return a {@link TextBox} widget with the first element of the\r
* query.The <code>initFunctions</code> will be called on the new\r
*/\r
public <W extends Widget> Widgets widgets(WidgetFactory<W> factory,\r
Function... initFunctions) {\r
-\r
List<Element> result = new ArrayList<Element>();\r
-\r
for (Element e : elements()) {\r
- result.add(widget(e, factory, initFunctions).getElement());\r
+ W w = widget(e, factory, initFunctions);\r
+ if (w != null) {\r
+ result.add(w.getElement());\r
+ }\r
}\r
-\r
return $(result).as(Widgets);\r
}\r
\r
\r
}\r
\r
+\r
}\r
--- /dev/null
+package com.google.gwt.query.client.plugins.widgets;
+
+public interface Attachable {
+ void attach();
+}
--- /dev/null
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import java.util.Date;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.i18n.client.DateTimeFormat;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.datepicker.client.DateBox;
+import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
+
+/**
+ * Factory used to create a {@link DateBox} widget. A {@link DateBox} is created
+ * if the element is a <i>input</i> with type text, a <i>div</i> or a<i>span</i> element.
+ *
+ * The content of the element has to be empty, a valid date or a valid date-format string.
+ */
+public class DateBoxWidgetFactory implements WidgetFactory<DateBox> {
+
+ // DateBox needs to call the onAttach method
+ public static class AttachableDateBox extends DateBox implements Attachable {
+ public void attach(){
+ onAttach();
+ RootPanel.detachOnWindowClose(this);
+ }
+ }
+
+ public DateBox create(Element e) {
+ String v = null;
+ if ($(e).filter("input[type='text']").size() == 1) {
+ v = GQuery.$(e).val();
+ } else if (WidgetsUtils.matchesTags(e, "div", "span")) {
+ v = GQuery.$(e).text();
+ }
+ if (v!=null) {
+ DateBox w = create(v);
+ WidgetsUtils.replace(e, w);
+ return w;
+ }
+ return null;
+ }
+
+ @SuppressWarnings("deprecation")
+ private DateBox create(String v) {
+ Date d = new Date();
+ DateTimeFormat f = null;
+ if (v != null) {
+ try {
+ d = new Date(v);
+ } catch (Exception e) {
+ try {
+ f = DateTimeFormat.getFormat(v);
+ } catch (Exception e2) {
+ }
+ }
+ }
+ DateBox b = new AttachableDateBox();
+ b.setValue(d);
+ if (f != null) {
+ b.setFormat(new DefaultFormat(f));
+ }
+ return b;
+ }
+}
\ No newline at end of file
--- /dev/null
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.RichTextArea;
+
+/**
+ * Factory used to create a {@link RichTextArea} widget.
+ * A {@link Button} is created if the element is a <i>textarea</i>, <i>div></i> or <i>span</i>.
+ * The content of the element will be copied to the rich text area.
+ */
+// TODO: inject page styles to the iframe. Provide a Toolbar.
+public class RichTextWidgetFactory implements WidgetFactory<RichTextArea> {
+
+
+ public static class AttachableRichTextArea extends RichTextArea implements Attachable {
+ public void attach(){
+ onAttach();
+ // This fails with richtextarea,
+ // RootPanel.detachOnWindowClose(this);
+ }
+ }
+
+ public RichTextArea create(Element e) {
+ String v = null;
+ if ("textarea".equalsIgnoreCase(e.getTagName())) {
+ v = $(e).val();
+ } else if (WidgetsUtils.matchesTags(e, "div", "span")) {
+ v = $(e).html();
+ }
+ if (v != null) {
+ RichTextArea w = create(v);
+ WidgetsUtils.replace(e, w);
+ return w;
+ }
+ return null;
+ }
+
+ private RichTextArea create(String v) {
+ RichTextArea b = new AttachableRichTextArea();
+ b.setHTML(v);
+ return b;
+ }
+
+}
\ No newline at end of file
import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.Widget;
public class WidgetsUtils {
*/
static void replace(Element oldElement, Element newElement) {
assert oldElement != null && newElement != null;
-
GQuery.$(oldElement).replaceWith(newElement);
- }
+ String c = oldElement.getClassName();
+ if (!c.isEmpty()) {
+ newElement.addClassName(c);
+ }
+ }
+
+ static void replace(Element e, Widget widget) {
+ assert e != null && widget != null;
+ replace(e, widget.getElement());
+
+ if (widget instanceof Attachable) {
+ ((Attachable)widget).attach();
+ }
+ }
+
+
}
import com.google.gwt.user.client.ui.Button;\r
import com.google.gwt.user.client.ui.Label;\r
import com.google.gwt.user.client.ui.PopupPanel;\r
+import com.google.gwt.user.client.ui.RichTextArea;\r
import com.google.gwt.user.client.ui.Widget;\r
+import com.google.gwt.user.datepicker.client.DateBox;\r
\r
public class GwtQueryWidgetModule implements EntryPoint {\r
\r
});\r
\r
$("#tabs").as(Widgets).tabPanel();\r
+\r
+ DateBox b = $(".date").as(Widgets).datebox().widget();\r
+ System.out.println(b);\r
\r
+ RichTextArea a = $(".editable").as(Widgets).richtext().widget(0);\r
+ System.out.println(a);\r
\r
-\r
}\r
\r
}\r
<button class="btn">Make me a button 4!</button>\r
<div class="btn">Make me a button 5!</div>\r
<div class="btn">Make me a button 6!</div>\r
+<input type="text" class="date" value="dd/MM/yyyy"/>\r
</div>\r
\r
<div class="inputText">I will be an Input</div>\r
lacus.</div>\r
\r
<h3>Third tab</h3>\r
-<div>Vestibulum turpis eros, tempor et hendrerit sit amet,\r
+<div class='editable'>Vestibulum turpis eros, tempor et hendrerit sit amet,\r
hendrerit ut urna. Integer aliquam, neque non sollicitudin pulvinar,\r
orci neque faucibus mauris, a molestie massa ligula vitae eros. Proin\r
egestas, purus in pharetra aliquam, dolor odio faucibus lorem, ac\r