]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/mixins/GroupElement.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / GroupElement.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Element containing a sequence of child elements.
7  *
8  * @abstract
9  */
10 trait GroupElement {
11         /**
12          * List of items in the group as Elements.
13          *
14          * @var Element[]
15          */
16         protected $items = [];
17
18         /**
19          * @var Tag
20          */
21         protected $group;
22
23         /**
24          * @param array $config Configuration options
25          */
26         public function initializeGroupElement( array $config = [] ) {
27                 // Properties
28                 $this->group = isset( $config['group'] ) ? $config['group'] : new Tag( 'div' );
29
30                 $this->registerConfigCallback( function ( &$config ) {
31                         $config['items'] = $this->items;
32                 } );
33         }
34
35         /**
36          * Check if there are no items.
37          *
38          * @return bool Group is empty
39          */
40         public function isEmpty() {
41                 return !count( $this->items );
42         }
43
44         /**
45          * Get items.
46          *
47          * @return Element[] Items
48          */
49         public function getItems() {
50                 return $this->items;
51         }
52
53         /**
54          * Add items.
55          *
56          * Adding an existing item will move it.
57          *
58          * @param Element[] $items Items
59          * @param number $index Index to insert items at
60          * @return $this
61          */
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 ) {
70                                         $index--;
71                                 }
72                         }
73                         // Add the item
74                         $item->setElementGroup( $this );
75                 }
76
77                 if ( $index === null || $index < 0 || $index >= count( $this->items ) ) {
78                         $this->items = array_merge( $this->items, $items );
79                 } else {
80                         array_splice( $this->items, $index, 0, $items );
81                 }
82
83                 // Update actual target element contents to reflect our list
84                 $this->group->clearContent();
85                 $this->group->appendContent( $this->items );
86
87                 return $this;
88         }
89
90         /**
91          * Remove items.
92          *
93          * @param Element[] $items Items to remove
94          * @return $this
95          */
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 );
102                         }
103                 }
104
105                 // Update actual target element contents to reflect our list
106                 $this->group->clearContent();
107                 $this->group->appendContent( $this->items );
108
109                 return $this;
110         }
111
112         /**
113          * Clear all items.
114          *
115          * Items will be detached, not removed, so they can be used later.
116          *
117          * @return $this
118          */
119         public function clearItems() {
120                 foreach ( $this->items as $item ) {
121                         $item->setElementGroup( null );
122                 }
123
124                 $this->items = [];
125                 $this->group->clearContent();
126
127                 return $this;
128         }
129 }