6 * Element containing a sequence of child elements.
12 * List of items in the group as Elements.
16 protected $items = [];
24 * @param array $config Configuration options
26 public function initializeGroupElement( array $config = [] ) {
28 $this->group = isset( $config['group'] ) ? $config['group'] : new Tag( 'div' );
30 $this->registerConfigCallback( function ( &$config ) {
31 $config['items'] = $this->items;
36 * Check if there are no items.
38 * @return bool Group is empty
40 public function isEmpty() {
41 return !count( $this->items );
47 * @return Element[] Items
49 public function getItems() {
56 * Adding an existing item will move it.
58 * @param Element[] $items Items
59 * @param number $index Index to insert items at
62 public function addItems( array $items, $index = null ) {
63 foreach ( $items as $item ) {
64 // Check if item exists then remove it first, effectively "moving" it
65 $currentIndex = array_search( $item, $this->items, true );
66 if ( $currentIndex !== false ) {
67 $this->removeItems( [ $item ] );
68 // Adjust index to compensate for removal
69 if ( $currentIndex < $index ) {
74 $item->setElementGroup( $this );
77 if ( $index === null || $index < 0 || $index >= count( $this->items ) ) {
78 $this->items = array_merge( $this->items, $items );
80 array_splice( $this->items, $index, 0, $items );
83 // Update actual target element contents to reflect our list
84 $this->group->clearContent();
85 $this->group->appendContent( $this->items );
93 * @param Element[] $items Items to remove
96 public function removeItems( $items ) {
97 foreach ( $items as $item ) {
98 $index = array_search( $item, $this->items, true );
99 if ( $index !== false ) {
100 $item->setElementGroup( null );
101 array_splice( $this->items, $index, 1 );
105 // Update actual target element contents to reflect our list
106 $this->group->clearContent();
107 $this->group->appendContent( $this->items );
115 * Items will be detached, not removed, so they can be used later.
119 public function clearItems() {
120 foreach ( $this->items as $item ) {
121 $item->setElementGroup( null );
125 $this->group->clearContent();