click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r782807 - in /incubator/click/trunk/click/examples: src/org/apache/click/examples/page/ajax/ src/org/apache/click/examples/service/ webapp/WEB-INF/ webapp/ajax/ webapp/ajax/images/
Date Mon, 08 Jun 2009 21:53:17 GMT
Author: sabob
Date: Mon Jun  8 21:53:17 2009
New Revision: 782807

URL: http://svn.apache.org/viewvc?rev=782807&view=rev
Log:
replace Rico LiveGrid with a jQuery "load while scrolling" demo

Added:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxLiveScroller.java
    incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.htm
    incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.js
Removed:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxCustomerLiveGrid.java
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxDataGrid.java
    incubator/click/trunk/click/examples/webapp/ajax/ajax-select-include.htm
    incubator/click/trunk/click/examples/webapp/ajax/images/
Modified:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
    incubator/click/trunk/click/examples/webapp/WEB-INF/menu.xml

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxLiveScroller.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxLiveScroller.java?rev=782807&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxLiveScroller.java
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxLiveScroller.java
Mon Jun  8 21:53:17 2009
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.page.ajax;
+
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Resource;
+import org.apache.click.element.JsImport;
+import org.apache.click.element.JsScript;
+import org.apache.click.examples.page.BorderPage;
+import org.apache.click.examples.service.CustomerService;
+import org.apache.click.util.ClickUtils;
+import org.apache.commons.lang.math.NumberUtils;
+import org.springframework.stereotype.Component;
+
+/**
+ * Demonstrates how to to dynamically load more customer while scrolling down
+ * the Page.
+ *
+ * @author Bob Schellink
+ */
+@Component
+public class AjaxLiveScroller extends BorderPage {
+
+    @Resource(name="customerService")
+    private CustomerService customerService;
+
+    // Specifies the number of customers to retrieve at a time
+    private int pageSize = 10;
+
+    public void onGet() {
+        // Check if the offset parameter was received.
+        int offset = NumberUtils.toInt(getContext().getRequest().getParameter("offset"));
+
+        if (offset < 0) {
+            // If no offset was given, we assume this is not an Ajax request
+            // and set the offset to 0
+            offset = 0;
+
+            // We also set a message to display
+            addModel("msg", "Top customers");
+        }
+
+        // Return customers between the given offset and pageSize
+        addModel("customers", customerService.getTopCustomersForPage(offset, pageSize));
+    }
+
+    public List getHeadElements() {
+        // Lazily load head elements and ensure they are only loaded once
+        if (headElements == null) {
+            headElements = super.getHeadElements();
+
+            // Add the jQuery library
+            headElements.add(new JsImport("/assets/js/jquery-1.3.2.js"));
+
+            // Create a default model and add the pageSize variable to pass to
+            // the JavaScript template: ajax-live-scroller.js
+            Map model = ClickUtils.createTemplateModel(this, getContext());
+            model.put("pageSize", pageSize);
+
+            // Note the actual JavaScript necessary to setup the dynamic scrolling
+            // is specified in the Page JavaScript template: ajax-live-scroller.js.
+            headElements.add(new JsScript("/ajax/ajax-live-scroller.js", model));
+        }
+        return headElements;
+    }
+
+    public String getTemplate() {
+        // For Ajax requests we want to render the Page template only as there is
+        // no need to include the Border template in the response
+        if (getContext().isAjaxRequest()) {
+            return getPath();
+        } else {
+            return super.getTemplate();
+        }
+    }
+}

Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java?rev=782807&r1=782806&r2=782807&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
(original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/service/CustomerService.java
Mon Jun  8 21:53:17 2009
@@ -217,6 +217,25 @@
     }
 
     @SuppressWarnings("unchecked")
+    public List<Customer> getTopCustomersForPage(int offset, int pageSize) {
+        List list = getCustomersSortedBy(Customer.HOLDINGS_PROPERTY, false);
+
+        List pageList = new ArrayList(pageSize);
+        for (int i = 0; i < pageSize; i++) {
+            // Increment row index with the offset
+            int rowIndex = offset + i;
+
+            // Guard against rowIndex that moves past the end of the list
+            if (rowIndex >= list.size()) {
+                break;
+            }
+            pageList.add(list.get(rowIndex));
+        }
+
+        return pageList;
+    }
+
+    @SuppressWarnings("unchecked")
     public List<Customer> getInvestmentCatetories() {
         List categories = new ArrayList();
 

Modified: incubator/click/trunk/click/examples/webapp/WEB-INF/menu.xml
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/WEB-INF/menu.xml?rev=782807&r1=782806&r2=782807&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/webapp/WEB-INF/menu.xml (original)
+++ incubator/click/trunk/click/examples/webapp/WEB-INF/menu.xml Mon Jun  8 21:53:17 2009
@@ -49,9 +49,9 @@
     <menu label="Page Imports" path="general/page-imports-example.htm"/>
     <menu label="Page Security" path="security/secure.htm"/>
     <menu separator="true"/>
-    <menu label="AJAX Accordian" path="ajax/ajax-accordian.htm"/>
+    <menu label="AJAX Accordion" path="ajax/ajax-accordion.htm"/>
     <menu label="AJAX Auto Complete Field" path="ajax/auto-complete.htm"/>
-    <menu label="AJAX Data Grid" path="ajax/ajax-data-grid.htm"/>
+    <menu label="AJAX Live Scroller " path="ajax/ajax-live-scroller.htm"/>
     <menu label="AJAX Select" path="ajax/ajax-select.htm"/>
     <menu separator="true"/>
     <menu label="JSP Hello World" path="jsp/hello-world.htm"/>

Added: incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.htm
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.htm?rev=782807&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.htm (added)
+++ incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.htm Mon Jun  8 21:53:17
2009
@@ -0,0 +1,60 @@
+<!--
+#* Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.*#
+-->
+#if ($customers.size() == 0)
+  LAST_RECORD
+
+#else
+  #if ($msg)
+    Demonstration using <a target="_blank" class="external" href="http://en.wikipedia.org/wiki/AJAX">AJAX</a>
+    to dynamically load content while scrolling. This example uses the
+    <a target="_blank" class="external" href="http://jquery.com/">jQuery</a>
JavaScript library.
+    <h3>$msg</h3>
+  #end
+  
+  #foreach ($customer in $customers)
+  <div id="customer-$customer.id" class="customer" style="border:1px solid black; background:#eeeeee;
padding: 4px; margin-bottom: 20px;">
+      <table>
+          <tr>
+              <td><b>Name:</b></td>
+              <td><b>$customer.name</b></td>
+          </tr>
+          <tr>
+              <td><b>Email:</b></td>
+              <td>$format.email($customer.email)</td>
+          </tr>
+          <tr>
+              <td><b>Age:</b></td>
+              <td>$format.string($customer.age)</td>
+          </tr>
+          <tr>
+              <td><b>Holdings:</b></td>
+              <td>$format.currency($customer.holdings)</td>
+          </tr>
+          <tr>
+              <td><b>Investments:</b></td>
+              <td>$format.string($customer.investments)</td>
+          </tr>
+          <tr>
+              <td><b>Join Date:</b></td>
+              <td>$format.date($customer.dateJoined)</td>
+          </tr>
+      </table>
+</div>
+  #end
+#end

Added: incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.js
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.js?rev=782807&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.js (added)
+++ incubator/click/trunk/click/examples/webapp/ajax/ajax-live-scroller.js Mon Jun  8 21:53:17
2009
@@ -0,0 +1,69 @@
+$(document).ready(function(){
+    // Dynamically create a fixed scroll indicator at the bottom right corner of the screen
+    jQuery("<div id='scroll-indicator' style='border: 1px solid white;"
+        + "background: #EEEEEE; padding: 10px; position: fixed; display: none;"
+        + "right: 25; bottom: 25; width: 100px;"
+
+        // IE6 doesn't support fixed positioning. Workaround below
+        + "_position:absolute;_top:expression(-25 + document.body.scrollTop+document.body.clientHeight-this.clientHeight);"
+
+        + "'>Loading...</div>")
+        .insertAfter(jQuery('table.page')); // Insert the indicator after the table defined
in border-template.htm
+
+    // Current number of records loaded from server
+    var offset = 0;
+
+    // Indicates the number of records to return from server. The PageSize value
+    // is passed in from the AjaxLiveScroller.java Page.
+    var pageSize = $pageSize;
+
+    // Flag indicating if data is being loaded from server
+    var loading = false;
+
+    function loadMoreData() {
+        // Increment offset with the specified number of pages
+        offset+=pageSize;
+
+        // Toggle flag to indicate that loading started
+        loading = true;
+
+        // Show the scroll indicator
+        jQuery('#scroll-indicator').css("display", "block");
+
+        jQuery.get('${context}${path}?offset=' + offset, function(data){
+                // Toggle flag to indicate that loading is finished
+                loading = false;
+
+                // If the server returns the "LAST_RECORD" indicator, we
+                // can unbind the window scroll event as no further data
+                // will be returned
+                if (data.indexOf("LAST_RECORD") >= 0) {
+                    $(window).unbind('scroll');
+                } else {
+                    // Otherwise we add the new data after the last customer
+                    $(".customer:last").after(data);
+                }
+
+                jQuery('#scroll-indicator').css("display", "none");
+            });
+    }
+
+    // Bind a scroll event to the window to detect if more data needs to be loaded
+    $(window).bind('scroll', function() {
+
+        // If we are currently retrieving data, don't load again
+        if (loading) {
+            return;
+        }
+
+        // When scrolling near the bottom of the page, load more data from server
+        if (nearEndOfPage()) {
+            loadMoreData();
+        }
+    });
+
+    // Return true if scrolling is 85% at the end of the page
+    function nearEndOfPage() {
+        return $(window).scrollTop() > (0.85 * $(document).height() - $(window).height());
+    }
+})



Mime
View raw message