Author: psharples
Date: Thu Oct 18 13:03:32 2012
New Revision: 1399646

URL: http://svn.apache.org/viewvc?rev=1399646&view=rev
Log:
Updates so that it is possible in javascript to dynamically add widgets, not 
only to the first region within a page, but any region found in the page.

Modified:
    
rave/trunk/rave-components/rave-web/src/main/java/org/apache/rave/portal/web/api/rpc/PageApi.java
    rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave.js
    rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave_api.js

Modified: 
rave/trunk/rave-components/rave-web/src/main/java/org/apache/rave/portal/web/api/rpc/PageApi.java
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-web/src/main/java/org/apache/rave/portal/web/api/rpc/PageApi.java?rev=1399646&r1=1399645&r2=1399646&view=diff
==============================================================================
--- 
rave/trunk/rave-components/rave-web/src/main/java/org/apache/rave/portal/web/api/rpc/PageApi.java
 (original)
+++ 
rave/trunk/rave-components/rave-web/src/main/java/org/apache/rave/portal/web/api/rpc/PageApi.java
 Thu Oct 18 13:03:32 2012
@@ -84,6 +84,36 @@ public class PageApi {
     }
 
     /**
+     * Adds a widget to the given page region
+     *
+     * @param pageId
+     *            the ID of the {@link org.apache.rave.portal.model.Page} to 
add
+     *            the widget to
+     * @param widgetId
+     *            the ID of the {@link org.apache.rave.portal.model.Widget} to
+     *            add do the page
+     * @param regionId
+     *            the ID of the {@link org.apache.rave.portal.model.Region} to
+     *            add the the widget to
+     * @return a {@link RpcOperation} containing the new widget instance (
+     *         {@link org.apache.rave.portal.model.RegionWidget }) or any 
errors
+     *         encountered while performing the RPC operation
+     */
+    @ResponseBody
+    @RequestMapping(method = RequestMethod.POST, value = 
"{pageId}/widget/add/region/{regionId}")
+    public RpcResult<RegionWidget> addWidgetToPageRegion(
+            @PathVariable final long pageId, @RequestParam final long 
widgetId, @PathVariable final long regionId) {
+
+        return new RpcOperation<RegionWidget>() {
+            @Override
+            public RegionWidget execute() {
+                return pageService.addWidgetToPageRegion(pageId, widgetId, 
regionId);
+            }
+        }.getResult();
+    }
+    
+
+    /**
      * Moves a widget to a new location
      * <p/>
      * Moves can take place within a region, region to region, or between pages

Modified: rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave.js
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave.js?rev=1399646&r1=1399645&r2=1399646&view=diff
==============================================================================
--- rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave.js 
(original)
+++ rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave.js Thu 
Oct 18 13:03:32 2012
@@ -1002,19 +1002,28 @@ var rave = rave || (function () {
         openAjaxHub = null;
     }
 
-    function renderNewWidget(regionWidgetId, init){
+    function renderNewWidget(regionWidgetId, init, regionId){
         // When run as the callback argument supplied to 
rave.api.rpc.addWidgetToPage
+        // or rave.api.rpc.addWidgetToPageRegion
         // this method will render the widget in the current page.
         // load widget into a placeholder element
         var placeholder = document.createElement("div");
         
$(placeholder).load(rave.getContext()+"api/rest/regionwidget/"+regionWidgetId, 
function(){
-            var $firstRegion = $(".region:not(.region-locked):first")
-            var firstRegionId = ($firstRegion).attr('id');
-            // prepend to first region
-            var region = $("#"+firstRegionId);
+            var region = null;
+            if(regionId != "undefined"){
+                // get specified region
+                region = $("#region-"+regionId+"-id");
+            }
+            else{
+                var $firstRegion = $(".region:not(.region-locked):first")
+                var firstRegionId = ($firstRegion).attr('id');
+                // prepend to first region
+                region = $("#"+firstRegionId);
+            }
             region.prepend(placeholder);
             // remove the placeholder around the widget-wrapper
             region.children(":first").children(":first").unwrap();
+            
             if(init){
                 initializeWidgets();
                 rave.styleWidgetButtons(regionWidgetId);

Modified: 
rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave_api.js
URL: 
http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave_api.js?rev=1399646&r1=1399645&r2=1399646&view=diff
==============================================================================
--- rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave_api.js 
(original)
+++ rave/trunk/rave-portal-resources/src/main/webapp/static/script/rave_api.js 
Thu Oct 18 13:03:32 2012
@@ -264,6 +264,30 @@ rave.api = rave.api || (function() {
                 }).error(handleError);
         }
 
+        function addWidgetToPageRegion(args) {
+            $.post(rave.getContext() + path + "page/" + args.pageId + 
"/widget/add/region/"+ args.regionId,
+                {
+                    widgetId: args.widgetId
+                },
+                function(result) {
+                    if (result.error) {
+                        handleRpcError(result);
+                    } else {
+                        var widgetTitle = 
rave.getClientMessage("widget.add_prefix");
+                        var addedWidget = result.result != undefined ? 
result.result.widget : undefined;
+
+                        if (addedWidget != undefined && addedWidget.title != 
undefined && addedWidget.title.length > 0) {
+                            widgetTitle = addedWidget.title;
+                        }
+                        // if a callback is supplied, invoke it with the 
regionwidget id
+                        if (args.successCallback && addedWidget != undefined){
+                            args.successCallback(result.result.id);
+                        }
+                        rave.showInfoMessage(widgetTitle + ' ' + 
rave.getClientMessage("widget.add_suffix"));
+                    }
+                }).error(handleError);
+        }
+
         function deleteWidgetOnPage(args) {
             $.post(rave.getContext() + path + "page/regionWidget/" + 
args.regionWidgetId + "/delete",
                 null,
@@ -300,6 +324,22 @@ rave.api = rave.api || (function() {
                 }).error(handleError);
         }
 
+        function getPage(args){
+            $.get(rave.getContext() + path + "page/get",
+                    {
+                        pageId: args.pageId
+                    },
+                    function(result) {
+                        if (result.error) {
+                            handleRpcError(result);
+                        } else {
+                            if (typeof args.successCallback == 'function') {
+                                args.successCallback(result);
+                            }
+                        }
+                    }).error(handleError);
+        }
+
         function movePage(args) {
             // the moveAfterPageId attribute could be undefined if moving
             // to the first position. In that case don't send a moveAfterPageId
@@ -626,8 +666,10 @@ rave.api = rave.api || (function() {
         return {
             moveWidget : moveWidgetOnPage,
             addWidgetToPage : addWidgetToPage,
+            addWidgetToPageRegion: addWidgetToPageRegion,
             removeWidget : deleteWidgetOnPage,
             addPage: addPage,
+            getPage: getPage,
             updatePagePrefs: updatePagePrefs,
             getPagePrefs: getPagePrefs,
             movePage: movePage,


Reply via email to