Author: abelevich
Date: 2011-02-15 10:04:03 -0500 (Tue, 15 Feb 2011)
New Revision: 21667
Added:
trunk/ui/core/ui/src/main/resources/META-INF/resources/org.richfaces/poll.js
Modified:
trunk/ui/core/ui/src/main/java/org/richfaces/renderkit/html/AjaxPollRenderer.java
Log:
https://issues.jboss.org/browse/RF-7459 js api implementation. Migration to base
component.
Modified:
trunk/ui/core/ui/src/main/java/org/richfaces/renderkit/html/AjaxPollRenderer.java
===================================================================
---
trunk/ui/core/ui/src/main/java/org/richfaces/renderkit/html/AjaxPollRenderer.java 2011-02-15
13:09:59 UTC (rev 21666)
+++
trunk/ui/core/ui/src/main/java/org/richfaces/renderkit/html/AjaxPollRenderer.java 2011-02-15
15:04:03 UTC (rev 21667)
@@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
+import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
@@ -46,15 +47,19 @@
/**
* @author shura
*/
-@ResourceDependency(library = "org.richfaces", name = "ajax.reslib")
+@ResourceDependencies({@ResourceDependency(library = "org.richfaces", name =
"ajax.reslib"),
+ @ResourceDependency(library = "org.richfaces", name =
"base-component.reslib"),
+ @ResourceDependency(library = "org.richfaces", name =
"poll.js")})
@JsfRenderer
public class AjaxPollRenderer extends RendererBase {
public static final String COMPONENT_FAMILY = "org.richfaces.Poll";
public static final String RENDERER_TYPE = "org.richfaces.PollRenderer";
- private static final String AJAX_POLL_FUNCTION = "RichFaces.startPoll";
+ private static final String AJAX_POLL_FUNCTION = "new RichFaces.ui.Poll";
+ private static final String ENABLED = "enabled";
+
private void addComponentToAjaxRender(FacesContext context, UIComponent component) {
PartialViewContext pvc = context.getPartialViewContext();
pvc.getRenderIds().add(component.getClientId(context));
@@ -92,44 +97,42 @@
if (shouldRenderForm) {
String clientId = component.getClientId(context) +
RendererUtils.DUMMY_FORM_ID;
utils.encodeBeginForm(context, component, writer, clientId);
+ utils.encodeEndForm(context, writer);
}
+ writer.endElement(rootElementName);
// polling script.
writer.startElement(HtmlConstants.SCRIPT_ELEM, component);
writer.writeAttribute(HtmlConstants.TYPE_ATTR, "text/javascript",
null);
- StringBuffer script = new StringBuffer("\n");
- if (poll.isEnabled()) {
- JSFunction function = new JSFunction(AJAX_POLL_FUNCTION);
- Map<String, Object> options = new HashMap<String, Object>();
-
- RenderKitUtils.addToScriptHash(options, "interval",
poll.getInterval(), "1000");
- RenderKitUtils.addToScriptHash(options, "pollId",
component.getClientId(context));
- HandlersChain handlersChain = new HandlersChain(context, poll);
- handlersChain.addInlineHandlerFromAttribute(AbstractPoll.ON_TIMER);
- handlersChain.addBehaviors(AbstractPoll.TIMER);
- handlersChain.addAjaxSubmitFunction();
+ StringBuffer script = new StringBuffer("");
- String handler = handlersChain.toScript();
+ JSFunction function = new JSFunction(AJAX_POLL_FUNCTION);
+ Map<String, Object> options = new HashMap<String, Object>();
- if (handler != null) {
- JSFunctionDefinition timerHandler = new
JSFunctionDefinition(JSReference.EVENT);
- timerHandler.addToBody(handler);
- options.put(AbstractPoll.ON_TIMER, timerHandler);
- }
- function.addParameter(options);
- function.appendScript(script);
- } else {
-
script.append("RichFaces.stopPoll('").append(component.getClientId(context)).append("')");
+ RenderKitUtils.addToScriptHash(options, "interval", poll.getInterval(),
"1000");
+ //RenderKitUtils.addToScriptHash(options, "pollId",
component.getClientId(context));
+ HandlersChain handlersChain = new HandlersChain(context, poll);
+ handlersChain.addInlineHandlerFromAttribute(AbstractPoll.ON_TIMER);
+ handlersChain.addBehaviors(AbstractPoll.TIMER);
+ handlersChain.addAjaxSubmitFunction();
+
+ String handler = handlersChain.toScript();
+
+ if (handler != null) {
+ JSFunctionDefinition timerHandler = new
JSFunctionDefinition(JSReference.EVENT);
+ timerHandler.addToBody(handler);
+ options.put(AbstractPoll.ON_TIMER, timerHandler);
}
- script.append(";\n");
- writer.writeText(script.toString(), null);
+ if (poll.isEnabled()) {
+ options.put(ENABLED, true);
+ }
+ function.addParameter(component.getClientId(context));
+ function.addParameter(options);
+ //function.appendScript(script);
+ writer.writeText(function.toScript(), null);
+
writer.endElement(HtmlConstants.SCRIPT_ELEM);
-
- if (shouldRenderForm) {
- utils.encodeEndForm(context, writer);
- }
-
- writer.endElement(rootElementName);
+
}
/*
Added: trunk/ui/core/ui/src/main/resources/META-INF/resources/org.richfaces/poll.js
===================================================================
--- trunk/ui/core/ui/src/main/resources/META-INF/resources/org.richfaces/poll.js
(rev 0)
+++
trunk/ui/core/ui/src/main/resources/META-INF/resources/org.richfaces/poll.js 2011-02-15
15:04:03 UTC (rev 21667)
@@ -0,0 +1,67 @@
+(function($, rf) {
+ rf.ui = rf.ui || {};
+ var defaultOptions = {
+
+ };
+ rf.ui.Poll = function(componentId, options) {
+ $super.constructor.call(this, componentId, options);
+ this.id = componentId;
+ this.attachToDom();
+ this.interval = options.interval || 1000;
+ this.ontimer = options.ontimer;
+
+ this.pollElement = rf.getDomElement(this.id);
+
+ rf.ui.pollTracker = rf.ui.pollTracker || {};
+
+ if (options.enabled){
+ this.startPoll();
+ }
+
+
+ }
+
+ rf.BaseComponent.extend(rf.ui.Poll);
+ var $super = rf.ui.Poll.$super;
+ $.extend(rf.ui.Poll.prototype, (function() {
+ return {
+ name: "Poll",
+
+ startPoll: function() {
+ this.stopPoll();
+ var poll=this;
+ //this.setZeroRequestDelay(options);
+ rf.ui.pollTracker[poll.id] = window.setTimeout(function() {
+ try {
+ poll.ontimer.call(poll.pollElement || window);
+ poll.startPoll();
+ } catch (e) {
+ // TODO: handle exception
+ }
+ }, poll.interval);
+ },
+
+ stopPoll : function() {
+ if (rf.ui.pollTracker && rf.ui.pollTracker[this.id]) {
+ window.clearTimeout(rf.ui.pollTracker[this.id]);
+ delete rf.ui.pollTracker[this.id];
+ }
+ },
+
+ setZeroRequestDelay : function(options) {
+ if (typeof options.requestDelay == "undefined") {
+ options.requestDelay = 0;
+ }
+ },
+
+ destroy : function() {
+ this.stopPoll();
+ this.detach(this.id);
+ // call parent's destroy method
+ $super.destroy.call(this);
+ }
+
+ };
+ })());
+
+})(jQuery, RichFaces);
\ No newline at end of file