/**
 * Shows and hides an element that depends on certain selected pages when setting up conditions.
 * 
 * @author	Matthias Schmidt
 * @copyright	2001-2017 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @module	WoltLabSuite/Core/Controller/Condition/Page/Dependence
 */
define(['Dom/ChangeListener', 'Dom/Traverse', 'EventHandler', 'ObjectMap'], function(DomChangeListener, DomTraverse, EventHandler, ObjectMap) {
	"use strict";
	
	var _pages = elBySelAll('input[name="pageIDs[]"]');
	var _dependentElements = [];
	var _pageIds = new ObjectMap();
	var _hiddenElements = new ObjectMap();
	
	var _didInit = false;
	
	return {
		register: function(dependentElement, pageIds) {
			_dependentElements.push(dependentElement);
			_pageIds.set(dependentElement, pageIds);
			_hiddenElements.set(dependentElement, []);
			
			if (!_didInit) {
				for (var i = 0, length = _pages.length; i < length; i++) {
					_pages[i].addEventListener('change', this._checkVisibility.bind(this));
				}
				
				_didInit = true;
			}
			
			// remove the dependent element before submit if it is hidden
			DomTraverse.parentByTag(dependentElement, 'FORM').addEventListener('submit', function() {
				if (dependentElement.style.getPropertyValue('display') === 'none') {
					dependentElement.remove();
				}
			});
			
			this._checkVisibility();
		},
		
		/**
		 * Checks if any of the relevant pages is selected. If that is the case, the dependent
		 * element is shown, otherwise it is hidden.
		 * 
		 * @private
		 */
		_checkVisibility: function() {
			var dependentElement, page, pageIds;
			
			depenentElementLoop: for (var i = 0, length = _dependentElements.length; i < length; i++) {
				dependentElement = _dependentElements[i];
				pageIds = _pageIds.get(dependentElement);
				
				for (var j = 0, length2 = _pages.length; j < length2; j++) {
					page = _pages[j];
					
					if (page.checked && pageIds.indexOf(~~page.value) !== -1) {
						this._showDependentElement(dependentElement);
						
						continue depenentElementLoop;
					}
				}
				
				this._hideDependentElement(dependentElement);
			}
			
			EventHandler.fire('com.woltlab.wcf.pageConditionDependence', 'checkVisivility');
		},
		
		_hideDependentElement: function(dependentElement) {
			elHide(dependentElement);
			
			var hiddenElements = _hiddenElements.get(dependentElement);
			for (var i = 0, length = hiddenElements.length; i < length; i++) {
				elHide(hiddenElements[i]);
			}
			
			_hiddenElements.set(dependentElement, []);
		},
		
		_showDependentElement: function(dependentElement) {
			elShow(dependentElement);
			
			// make sure that all parent elements are also visible
			var parentNode = dependentElement;
			while ((parentNode = parentNode.parentNode) && parentNode instanceof Element) {
				if (parentNode.style.getPropertyValue('display') === 'none') {
					_hiddenElements.get(dependentElement).push(parentNode);
				}
				
				elShow(parentNode);
			}
		}
	};
});

@@ -1415,18 +1415,12 @@
  if 
-any of the
+only
  rel
@@ -1431,18 +1431,19 @@
 t pages 
-is
+are
  selecte
@@ -1624,16 +1624,51 @@
  pageIds
+, checkedPageIds, irrelevantPageIds
 ;%0A%09%09%09%0A%09%09
@@ -1853,24 +1853,49 @@
 ment);%0A%09%09%09%09%0A
+%09%09%09%09checkedPageIds = %5B%5D;%0A
 %09%09%09%09for (var
@@ -2001,106 +2001,188 @@
 cked
- && pageIds.indexOf(~~page.value) !== -1) %7B
+) %7B%0A%09%09%09%09%09%09checkedPageIds.push(~~page.value);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 %0A%09%09%09%09
+%0A%09
 %09%09
-this._showDependentElement(dependentElement);%0A%09%09
+%09irrelevantPageIds = checkedPageIds.filter(function(pageId) %7B%0A%09%09%09%09%09return pageIds.indexOf(pageId) === -1;%0A%09%09%09%09%7D);%0A
 %09%09%09%09
@@ -2190,24 +2190,87 @@
 %09%09%09%09
-%09%09
-continue d
+if (!checkedPageIds.length %7C%7C irrelevantPageIds.length) %7B%0A%09%09%09%09%09this._hideD
 epen
+d
 entE
@@ -2279,15 +2279,28 @@
 ment
-Loop
+(dependentElement)
 ;%0A
-%09
 %09%09%09%09
@@ -2309,29 +2309,30 @@
 %09%09%09%09
-%7D
+else %7B
 %0A%09%09%09%09
-%0A%09%09%09
 %09this._
-hide
+show
 Depe
@@ -2363,16 +2363,22 @@
 ement);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
@@ -2470,16 +2470,135 @@
 %09%09%7D,%0A%09%09%0A
+%09%09/**%0A%09%09 * Hides all elements that depend on the given element.%0A%09%09 * %0A%09%09 * @param%09%7BHTMLElement%7D%09dependentElement%0A%09%09 */%0A
 %09%09_hideD
@@ -2903,16 +2903,135 @@
 %09%09%7D,%0A%09%09%0A
+%09%09/**%0A%09%09 * Shows all elements that depend on the given element.%0A%09%09 * %0A%09%09 * @param%09%7BHTMLElement%7D%09dependentElement%0A%09%09 */%0A
 %09%09_showD

Update behavior of page dependence condition controller