summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/mixins/GroupElement.php
blob: 93d3c7a18d6ebc72dfe84b140bb6f676aa6374a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace OOUI;

/**
 * Element containing a sequence of child elements.
 *
 * @abstract
 */
class GroupElement extends ElementMixin {
	/**
	 * List of items in the group.
	 *
	 * @var Element[]
	 */
	protected $items = array();

	public static $targetPropertyName = 'group';

	/**
	 * @param Element $element Element being mixed into
	 * @param array $config Configuration options
	 */
	public function __construct( Element $element, array $config = array() ) {
		// Parent constructor
		$target = isset( $config['group'] ) ? $config['group'] : new Tag( 'div' );
		parent::__construct( $element, $target, $config );
	}

	/**
	 * Check if there are no items.
	 *
	 * @return boolean Group is empty
	 */
	public function isEmpty() {
		return !count( $this->items );
	}

	/**
	 * Get items.
	 *
	 * @return Element[] Items
	 */
	public function getItems() {
		return $this->items;
	}

	/**
	 * Add items.
	 *
	 * Adding an existing item will move it.
	 *
	 * @param Element[] $items Items
	 * @param number $index Index to insert items at
	 * @chainable
	 */
	public function addItems( array $items, $index = null ) {
		foreach ( $items as $item ) {
			// Check if item exists then remove it first, effectively "moving" it
			$currentIndex = array_search( $item, $this->items );
			if ( $currentIndex !== false ) {
				$this->removeItems( array( $item ) );
				// Adjust index to compensate for removal
				if ( $currentIndex < $index ) {
					$index--;
				}
			}
			// Add the item
			$item->setElementGroup( $this );
		}

		if ( $index === null || $index < 0 || $index >= count( $this->items ) ) {
			$this->items = array_merge( $this->items, $items );
		} else {
			array_splice( $this->items, $index, 0, $items );
		}

		// Update actual target element contents to reflect our list
		$this->target->clearContent();
		call_user_func_array( array( $this->target, 'appendContent' ), $this->items );

		return $this;
	}

	/**
	 * Remove items.
	 *
	 * @param Element[] $items Items to remove
	 * @chainable
	 */
	public function removeItems( $items ) {
		foreach ( $items as $item ) {
			$index = array_search( $item, $this->items );
			if ( $index !== false ) {
				$item->setElementGroup( null );
				array_splice( $this->items, $index, 1 );
			}
		}

		// Update actual target element contents to reflect our list
		$this->target->clearContent();
		call_user_func_array( array( $this->target, 'appendContent' ), $this->items );

		return $this;
	}

	/**
	 * Clear all items.
	 *
	 * Items will be detached, not removed, so they can be used later.
	 *
	 * @chainable
	 */
	public function clearItems() {
		foreach ( $this->items as $item ) {
			$item->setElementGroup( null );
		}

		$this->items = array();
		$this->target->clearContent();

		return $this;
	}

	public function getConfig( &$config ) {
		$config['items'] = $this->items;
		return parent::getConfig( $config );
	}
}