Author: pyaschenko
Date: 2009-11-16 12:19:59 -0500 (Mon, 16 Nov 2009)
New Revision: 15886
Added:
root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces-queue.js
Modified:
root/examples/trunk/components/core-demo/pom.xml
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/html/AjaxStatusRenderer.java
Log:
ajax queue prototype (not tested yet)
Modified: root/examples/trunk/components/core-demo/pom.xml
===================================================================
--- root/examples/trunk/components/core-demo/pom.xml 2009-11-16 07:47:06 UTC (rev 15885)
+++ root/examples/trunk/components/core-demo/pom.xml 2009-11-16 17:19:59 UTC (rev 15886)
@@ -66,6 +66,16 @@
<version>1.5.8</version>
<scope>provided</scope>
</dependency>
+<dependency>
+<groupId>com.sun.faces</groupId>
+<artifactId>jsf-api</artifactId>
+<version>2.0.2-SNAPSHOT</version>
+</dependency>
+<dependency>
+<groupId>com.sun.faces</groupId>
+<artifactId>jsf-impl</artifactId>
+<version>2.0.2-SNAPSHOT</version>
+</dependency>
</dependencies>
</project>
\ No newline at end of file
Added: root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces-queue.js
===================================================================
--- root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces-queue.js
(rev 0)
+++
root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces-queue.js 2009-11-16
17:19:59 UTC (rev 15886)
@@ -0,0 +1,202 @@
+(function(jQuery, richfaces, jsf) {
+
+ richfaces.ajax = richfaces.ajax || {};
+ richfaces.ajax.request = jsf.ajax.request;
+ jsf.ajax.request = function request(source, event, options) {
+ //richfaces.ajax.request(source, event, options);
+ richfaces.queue.push(source, event, options);
+ }
+
+ richfaces.queue = function(opts){
+
+ var queueEntry = function(query, event, options) {
+ this.query = query;
+ this.options = options || {};
+ this.event = event;
+
+ this.similarityGroupingId = this.options.similarityGroupingId;
+ this.eventsCount = 1;
+ };
+
+ jQuery.extend(queueEntry.prototype, {
+
+ isIgnoreDupResponses: function() {
+ return this.options.ignoreDupResponses;
+ },
+
+ getSimilarityGroupingId: function() {
+ return this.similarityGroupingId;
+ },
+
+ setSimilarityGroupingId: function(id) {
+ this.similarityGroupingId = id;
+ },
+
+ ondrop: function() {
+ var callback = this.options.onqueuerequestdrop;
+ if (callback) {
+ callback.call(this.queue, this.query, this.options, this.event);
+ }
+ },
+
+ onRequestDelayPassed: function() {
+ this.readyToSubmit = true;
+ this.queue.submitFirst(); // TODO: here we need to call query.submitFirst
+ },
+
+ startTimer: function() {
+ var delay = this.options.requestDelay;
+
+ log.debug("Queue will wait " + (delay || 0) + "ms before
submit");
+
+ if (delay) {
+ var _this = this;
+ this.timer = setTimeout(function() {
+ try {
+ _this.onRequestDelayPassed();
+ } finally {
+ _this.timer = undefined;
+ _this = undefined;
+ }
+ }, delay);
+ } else {
+ this.onRequestDelayPassed();
+ }
+ },
+
+ stopTimer: function() {
+ if (this.timer) {
+ clearTimeout(this.timer);
+ this.timer = undefined;
+ }
+ },
+
+ clearEntry: function() { //???
+ this.stopTimer();
+ if (this.request) {
+ this.request.shouldNotifyQueue = false;
+ this.request = undefined;
+ }
+ },
+
+ getEventsCount: function() {
+ return this.eventsCount;
+ },
+
+ setEventsCount: function(newCount) {
+ this.eventsCount = newCount;
+ },
+
+ });
+
+ // TODO: add this two variables to richfaces and report bug to jsf about constants
+ var jsfDataTypes = {'event' : 'event'};
+ var jsfEventNames = {'success' : 'success'};
+
+ var log = richfaces.log;
+ var items = [];
+ var queueOptions = opts || {};
+ var last;
+
+ var onError = function (data) {
+ log.debug("richfaces.queue: ajax submit error");
+ last = null;
+ submitFirst();
+ };
+
+ var onComplete = function (data) {
+ if (jsfDataTypes[data.type]=='event' &&
jsfEventNames[data.status]=='success') {
+ log.debug("richfaces.queue: ajax submit successfull");
+ last = null;
+ submitFirst();
+ }
+ };
+
+ jsf.ajax.addOnEvent(onComplete);
+ jsf.ajax.addOnError(onError);
+
+ var submitFirst = function() {
+ if (last) {
+ log.debug("richfaces.queue: Waiting for previous submit results");
+ return;
+ }
+ if (isEmpty()) {
+ log.debug("richfaces.queue: Nothing to submit");
+ return;
+ }
+ last = items.shift();
+ if (last.readyToSubmit) {
+ log.debug("richfaces.queue: will submit request NOW");
+ last.query.appendParameter("AJAX:EVENTS_COUNT", last.eventsCount);
+ richfaces.ajax.request(last.query, last.event, last.options);
+ if (last.options.queueonsubmit) {
+ last.options.queueonsubmit.call(last);
+ }
+ }
+ };
+
+ var isEmpty = function() {
+ return (getSize()==0)
+ };
+ var getSize = function() {
+ return items.length;
+ };
+
+ var getLastEntry = function () {
+ var lastIdx = items.length - 1;
+ return items[lastIdx];
+ };
+
+ var updateLastEntry = function (entry) {
+ var lastIdx = items.length - 1;
+ items[lastIdx] = entry;
+ };
+
+ return {
+
+ getSize: getSize,
+ isEmpty: isEmpty,
+
+ push: function (source, event, options) {
+ var entry = new queueEntry(source, event, options);
+ var similarityGroupingId = entry.getSimilarityGroupingId();
+
+ var lastEntry = getLastEntry();
+
+ if (lastEntry) {
+ if (lastEntry.getSimilarityGroupingId() == similarityGroupingId) {
+ log.debug("Similar request currently in queue");
+
+ log.debug("Combine similar requests and reset timer");
+
+ lastEntry.stopTimer();
+ entry.setEventsCount(lastEntry.getEventsCount() + 1);
+
+ this.items[lastIdx] = entry;
+ } else {
+ log.debug("Last queue entry is not the last anymore. Stopping requestDelay
timer and marking entry as ready for submission")
+
+ lastEntry.stopTimer();
+ lastEntry.setSimilarityGroupingId(undefined);
+ lastEntry.readyToSubmit = true;
+
+ items.push(entry);
+ log.debug("New request added to queue. Queue similarityGroupingId changed to
" + similarityGroupingId);
+ }
+ } else {
+ items.push(entry);
+ log.debug("New request added to queue. Queue similarityGroupingId changed to
" + similarityGroupingId);
+ }
+
+ entry.startTimer();
+ submitFirst();
+ },
+
+ pop: function () {
+ },
+
+ clear: function () {
+ }
+ }
+ }();
+}(jQuery, RichFaces, jsf));
\ No newline at end of file
Modified:
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java
===================================================================
---
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java 2009-11-16
07:47:06 UTC (rev 15885)
+++
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java 2009-11-16
17:19:59 UTC (rev 15886)
@@ -48,7 +48,7 @@
*
*/
@ResourceDependencies({@ResourceDependency(library = "javax.faces", name =
"jsf.js") ,
- @ResourceDependency(name = "jquery.js") ,
@ResourceDependency(name = "richfaces.js") })
+ @ResourceDependency(name = "jquery.js") ,
@ResourceDependency(name = "richfaces.js"), @ResourceDependency(name =
"richfaces-queue.js") })
public abstract class AjaxCommandRendererBase extends RendererBase {
private static final Logger LOG = RichfacesLogger.RENDERKIT.getLogger();
Modified:
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/html/AjaxStatusRenderer.java
===================================================================
---
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/html/AjaxStatusRenderer.java 2009-11-16
07:47:06 UTC (rev 15885)
+++
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/html/AjaxStatusRenderer.java 2009-11-16
17:19:59 UTC (rev 15886)
@@ -47,7 +47,8 @@
@ResourceDependencies( {
@ResourceDependency(library = "javax.faces", name = "jsf.js"),
@ResourceDependency(name = "jquery.js"),
- @ResourceDependency(name = "richfaces.js") })
+ @ResourceDependency(name = "richfaces.js"),
+ @ResourceDependency(name = "richfaces-queue.js") })
public class AjaxStatusRenderer extends RendererBase {
private static final String START = "start";