diff options
author | Ray Cromwell <cromwellian@gmail.com> | 2009-05-19 23:26:11 +0000 |
---|---|---|
committer | Ray Cromwell <cromwellian@gmail.com> | 2009-05-19 23:26:11 +0000 |
commit | 8d1d52abb942cb299c48191ca30e8e243b53484b (patch) | |
tree | 521d99666efa55b80a26a52a23470734f73d7172 /gwtquery-core | |
parent | 10597fd6dcc833f720475cbf60a0a35b8393e2b2 (diff) | |
download | gwtquery-8d1d52abb942cb299c48191ca30e8e243b53484b.tar.gz gwtquery-8d1d52abb942cb299c48191ca30e8e243b53484b.zip |
Fix up star ratings.
Diffstat (limited to 'gwtquery-core')
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java | 4 | ||||
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Ratings.java | 202 |
2 files changed, 119 insertions, 87 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index 59e2da10..225cb51c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -48,6 +48,10 @@ public class Properties extends JavaScriptObject { return props;
}-*/;
+ public final native boolean defined(String name) /*-{
+ return this[name] != undefined;
+ }-*/;
+
public final native String get(String name) /*-{
return this[name];
}-*/;
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Ratings.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Ratings.java index fc13d2e2..ae8aa918 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Ratings.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Ratings.java @@ -7,6 +7,8 @@ import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.JSArray; import com.google.gwt.query.client.Plugin; +import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.Regexp; import com.google.gwt.query.client.SelectorEngine; import com.google.gwt.user.client.Event; @@ -71,6 +73,10 @@ public class Ratings extends GQuery { } else { control = new Control(); control.setSerial(raters.bumpCount()); + Properties metadata = getMetaData(input.get(0).getClassName()); + if (metadata != null && metadata.defined("split")) { + control.setSplit(metadata.getInt("split")); + } // create rating element rater = $("<span class=\"star-rating-control\"/>"); input.before(rater); @@ -214,15 +220,26 @@ public class Ratings extends GQuery { rater.data("rating", control); star.data("rating", control); context.data("rating", raters); - - // Initialize ratings (first draw) - $(".rating-to-be-drawn").as(Ratings).draw() - .removeClass("rating-to-be-drawn"); } + // Initialize ratings (first draw) + $(".rating-to-be-drawn").as(Ratings).draw() + .removeClass("rating-to-be-drawn"); return this; } + private Properties getMetaData(String className) { + if (className.indexOf("{") == -1) { + return Properties.createObject().cast(); + } + Regexp re = new Regexp("{(.*)}"); + JSArray match = re.exec(className); + if (match != null && match.size() > 1) { + return Properties.create(match.getStr(1)); + } + return Properties.createObject().cast(); + } + public Ratings blurStar() { return this; } @@ -237,122 +254,133 @@ public class Ratings extends GQuery { } public Ratings fill() { - Control control = (Control) this.data("rating"); - if (control == null) { - return this; - } - // do not execute when control is in read-only mode - if (control.isReadOnly()) { - return this; + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } + + // Reset all stars and highlight them up to this element + self.as(Ratings).drain(); + self.prevAll().andSelf().filter(".rater-" + control.getSerial()) + .addClass("star-rating-hover"); + } - // Reset all stars and highlight them up to this element - drain(); - this.prevAll().andSelf().filter(".rater-" + control.getSerial()) - .addClass("star-rating-hover"); + return this; } public Ratings draw() { - Control control = (Control) this.data("rating"); - if (control == null) { - return this; - } - // do not execute when control is in read-only mode - if (control.isReadOnly()) { - return this; - } - // Clear all stars - this.drain(); - // Set control value - if (control.getCurrent() != null) { - ((GQuery) control.current.data("rating.input")) - .attr("checked", "checked"); - control.current.prevAll().andSelf().filter(".rater-" + control.serial) - .addClass("star-rating-on"); - } else { - $(control.getInputs()).removeAttr("checked"); - } + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } - // Show/hide 'cancel' button + // Clear all stars + self.as(Ratings).drain(); + // Set control value + if (control.getCurrent() != null) { + ((GQuery) control.current.data("rating.input")) + .attr("checked", "checked"); + control.current.prevAll().andSelf().filter(".rater-" + control.serial) + .addClass("star-rating-on"); + } else { + $(control.getInputs()).removeAttr("checked"); + } + + // Show/hide 'cancel' button // control.cancel[control.readOnly || control.required?'hide':'show'](); - // Add/remove read-only classes to remove hand pointer - this.siblings().toggleClass("star-rating-readonly", control.isReadOnly()); + // Add/remove read-only classes to remove hand pointer + self.siblings().toggleClass("star-rating-readonly", control.isReadOnly()); + } + return this; } public Ratings selectStar(int value) { - Control control = (Control) this.data("rating"); - if (control == null) { - return this; - } - // do not execute when control is in read-only mode - if (control.isReadOnly()) { - return this; + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } + + control.setCurrent(null); + $(control.getStar(value)).as(Ratings).selectStar(); } - control.setCurrent(null); - return $(control.getStar(value)).as(Ratings).selectStar(); + return this; } public Ratings selectStar() { - Control control = (Control) this.data("rating"); - if (control == null) { - return this; - } - // do not execute when control is in read-only mode - if (control.isReadOnly()) { - return this; - } - - control.current = get(0).getTagName().equalsIgnoreCase("INPUT") - ? (GQuery) data("rating.star") - : is(".rater-" + control.getSerial()) ? this : null; + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } - // Update rating control state - data("rating", control); - // Update display - this.draw(); - // find data for event - GQuery input = $( - control.current != null ? (GQuery) control.current.data("rating.input") - : null); - // click callback, as requested here: http://plugins.jquery.com/node/1655 + control.current = self.get(0).getTagName().equalsIgnoreCase("INPUT") + ? (GQuery) self.data("rating.star") + : self.is(".rater-" + control.getSerial()) ? this : null; + + // Update rating control state + self.data("rating", control); + // Update display + self.as(Ratings).draw(); + // find data for event + GQuery input = $(control.current != null ? (GQuery) control.current + .data("rating.input") : null); + // click callback, as requested here: http://plugins.jquery.com/node/1655 // if(control.callback) control.callback.apply(input[0], [input.val(), $('a', control.current)[0]]);// callback event + } + return this; } - public Ratings selectStar(String value) { - Control control = (Control) this.data("rating"); + private Control nullControlIfShouldSkipStar(GQuery q) { + Control control = (Control) q.data("rating"); if (control == null) { - return this; + return null; } // do not execute when control is in read-only mode if (control.isReadOnly()) { - return this; + return null; } - control.setCurrent(null); - - NodeList<Element> stars = control.getStars(); + return control; + } - for (int i = 0; i < stars.getLength(); i++) { - String val = ((GQuery) $(stars.getItem(i)).data("rating.input")).val(); - if (val != null && val.equals(value)) { - $(stars.getItem(i)).as(Ratings).selectStar(); + public Ratings selectStar(String value) { + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } + control.setCurrent(null); + NodeList<Element> stars = control.getStars(); + for (int i = 0; i < stars.getLength(); i++) { + String val = ((GQuery) $(stars.getItem(i)).data("rating.input")).val(); + if (val != null && val.equals(value)) { + $(stars.getItem(i)).as(Ratings).selectStar(); + } } } return this; } public Ratings drain() { - Control control = (Control) this.data("rating"); - if (control == null) { - return this; - } - // do not execute when control is in read-only mode - if (control.isReadOnly()) { - return this; + for (Element e : elements()) { + GQuery self = $(e); + Control control = nullControlIfShouldSkipStar(self); + if (control == null) { + continue; + } + control.rater.children().filter(".rater-" + control.getSerial()) + .removeClass("star-rating-on").removeClass("star-rating-hover"); } - control.rater.children().filter(".rater-" + control.getSerial()) - .removeClass("star-rating-on").removeClass("star-rating-hover"); return this; } |