Adds ajax actions for clipboard items
authorMatthias Schmidt <gravatronics@live.com>
Sun, 18 Mar 2012 18:50:56 +0000 (19:50 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 18 Mar 2012 18:50:56 +0000 (19:50 +0100)
If a clipboard item's parameters contain `className` and `actionName`, an AJAX proxy action is executed with those parameters and the marked object ids.

This spares us writing that as event listeners for the `clipboardAction` event.

Additional changes:

* Adds `removeArrayValue` function.
* Adds the list item parameters as a third parameter for the event listener on `clipboardAction`.

wcfsetup/install/files/js/WCF.js

index 3d2002ca62765497e56f4d76bf17e8c64ed8c17c..1fbce1ed9fb1bde014be5af3c2dad3f5ca1d5b44 100644 (file)
@@ -54,6 +54,19 @@ var WCF = {};
  * Extends jQuery with additional methods.
  */
 $.extend(true, {
+       /**
+        * Returns the given value from the given array.
+        * 
+        * @param       array           array
+        * @param       mixed           element
+        * @return      array
+        */
+       removeArrayValue: function(array, value) {
+               return $.grep(array, function(element, index) {
+                       return value !== element;
+               });
+       },
+       
        /**
         * Escapes an ID to work with jQuery selectors.
         *
@@ -494,6 +507,12 @@ WCF.Clipboard = {
         */
        _hasMarkedItems: false,
        
+       /**
+        * list of ids of marked objects
+        * @var array
+        */
+       _markedObjectIDs: [],
+       
        /**
         * current page
         * @var string
@@ -558,13 +577,12 @@ WCF.Clipboard = {
        _loadMarkedItemsSuccess: function(data, textStatus, jqXHR) {
                for (var $typeName in data.markedItems) {
                        var $objectData = data.markedItems[$typeName];
-                       var $objectIDs = [];
                        for (var $i in $objectData) {
-                               $objectIDs.push($objectData[$i]);
+                               this._markedObjectIDs.push($objectData[$i]);
                        }
                        
                        // loop through all containers
-                       this._containers.each(function(index, container) {
+                       this._containers.each($.proxy(function(index, container) {
                                var $container = $(container);
                                
                                // typeName does not match, continue
@@ -573,12 +591,12 @@ WCF.Clipboard = {
                                }
                                
                                // mark items as marked
-                               $container.find('input.jsClipboardItem').each(function(innerIndex, item) {
+                               $container.find('input.jsClipboardItem').each($.proxy(function(innerIndex, item) {
                                        var $item = $(item);
-                                       if (WCF.inArray($item.data('objectID'), $objectIDs)) {
+                                       if (WCF.inArray($item.data('objectID'), this._markedObjectIDs)) {
                                                $item.attr('checked', 'checked');
                                        }
-                               });
+                               }, this));
                                
                                // check if there is a markAll-checkbox
                                $container.find('input.jsClipboardMarkAll').each(function(innerIndex, markAll) {
@@ -595,7 +613,7 @@ WCF.Clipboard = {
                                                $(markAll).attr('checked', 'checked');
                                        }
                                });
-                       });
+                       }, this));
                }
                
                // call success method to build item list editors
@@ -639,6 +657,13 @@ WCF.Clipboard = {
                var $isMarked = ($item.attr('checked')) ? true : false;
                var $objectIDs = [ $objectID ];
                
+               if ($isMarked) {
+                       this._markedObjectIDs.push($objectID);
+               }
+               else {
+                       this._markedObjectIDs = $.removeArrayValue(this._markedObjectIDs, $objectID);
+               }
+               
                // item is part of a container
                if ($item.data('hasContainer')) {
                        var $container = $('#' + $item.data('hasContainer'));
@@ -692,21 +717,24 @@ WCF.Clipboard = {
                        var $type = $container.data('type');
                        
                        // toggle state for all associated items
-                       $container.find('input.jsClipboardItem').each(function(index, containerItem) {
+                       $container.find('input.jsClipboardItem').each($.proxy(function(index, containerItem) {
                                var $containerItem = $(containerItem);
+                               var $objectID = $containerItem.data('objectID');
                                if ($isMarked) {
                                        if (!$containerItem.attr('checked')) {
                                                $containerItem.attr('checked', 'checked');
-                                               $objectIDs.push($containerItem.data('objectID'));
+                                               this._markedObjectIDs.push($objectID);
+                                               $objectIDs.push($objectID);
                                        }
                                }
                                else {
                                        if ($containerItem.attr('checked')) {
                                                $containerItem.removeAttr('checked');
-                                               $objectIDs.push($containerItem.data('objectID'));
+                                               this._markedObjectIDs = $.removeArrayValue(this._markedObjectIDs, $objectID);
+                                               $objectIDs.push($objectID);
                                        }
                                }
-                       });
+                       }, this));
                }
                
                // save new status
@@ -816,8 +844,20 @@ WCF.Clipboard = {
                        window.location.href = $url;
                }
                
+               if ($listItem.data('parameters').className && $listItem.data('parameters').actionName) {
+                       new WCF.Action.Proxy({
+                               autoSend: true,
+                               data: {
+                                       actionName: $listItem.data('parameters').actionName,
+                                       className: $listItem.data('parameters').className,
+                                       objectIDs: this._markedObjectIDs
+                               },
+                               success: $.proxy(this._loadMarkedItems, this)
+                       });
+               }
+               
                // fire event
-               $listItem.trigger('clipboardAction', [ $listItem.data('type'), $listItem.data('actionName') ]);
+               $listItem.trigger('clipboardAction', [ $listItem.data('type'), $listItem.data('actionName'), $listItem.data('parameters') ]);
        },
        
        /**