Added JS code for nested category lists
authorMarcel Werk <burntime@woltlab.com>
Wed, 17 Jul 2013 20:30:03 +0000 (22:30 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 17 Jul 2013 20:30:03 +0000 (22:30 +0200)
wcfsetup/install/files/js/WCF.js

index 38e9b3b8db7c30eeb6520b9cb5b401e8fac84a34..124d66d3c7d7aadf90f9f7036fd0da0ab0b528e0 100755 (executable)
@@ -9202,6 +9202,75 @@ $.widget('ui.wcfPages', {
        }
 });
 
+/**
+ * Namespace for category related classes.
+ */
+WCF.Category = { };
+
+/**
+ * Handles selection of categories.
+ */
+WCF.Category.NestedList = Class.extend({
+       /**
+        * list of categories
+        * @var object
+        */
+       _categories: { },
+       
+       /**
+        * Initializes the WCF.Category.NestedList object.
+        */
+       init: function() {
+               var self = this;
+               $('.jsCategory').each(function(index, category) {
+                       var $category = $(category).data('parentCategoryID', null).change($.proxy(self._updateSelection, self));
+                       self._categories[$category.val()] = $category;
+                       
+                       // find child categories
+                       var $childCategoryIDs = [ ];
+                       $category.parents('li').find('.jsChildCategory').each(function(innerIndex, childCategory) {
+                               var $childCategory = $(childCategory).data('parentCategoryID', $category.val()).change($.proxy(self._updateSelection, self));
+                               self._categories[$childCategory.val()] = $childCategory;
+                               $childCategoryIDs.push($childCategory.val());
+                               
+                               if ($childCategory.is(':checked')) {
+                                       $category.prop('checked', 'checked');
+                               }
+                       });
+                       
+                       $category.data('childCategoryIDs', $childCategoryIDs);
+               });
+       },
+       
+       /**
+        * Updates selection of categories.
+        * 
+        * @param       object          event
+        */
+       _updateSelection: function(event) {
+               var $category = $(event.currentTarget);
+               var $parentCategoryID = $category.data('parentCategoryID');
+               
+               if ($category.is(':checked')) {
+                       // child category
+                       if ($parentCategoryID !== null) {
+                               // mark parent category as checked
+                               this._categories[$parentCategoryID].prop('checked', 'checked');
+                       }
+               }
+               else {
+                       // top-level category
+                       if ($parentCategoryID === null) {
+                               // unmark all child categories
+                               var $childCategoryIDs = $category.data('childCategoryIDs');
+                               for (var $i = 0, $length = $childCategoryIDs.length; $i < $length; $i++) {
+                                       this._categories[$childCategoryIDs[$i]].prop('checked', false);
+                               }
+                       }
+               }
+       }
+});
+
 /**
  * Encapsulate eval() within an own function to prevent problems
  * with optimizing and minifiny JS.