Skip to content

Commit

Permalink
Improve LinkListGroupFromArray (#49)
Browse files Browse the repository at this point in the history
* Improve LinkListGroupFromArray

* CC

* New interface

* CC

* Update ILinklistGroupFromArray.php

---------

Co-authored-by: Robert Vogel <[email protected]>
  • Loading branch information
DvogelHallowelt and osnard authored Oct 16, 2024
1 parent 523fd4b commit 62ee107
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
6 changes: 3 additions & 3 deletions resources/templates/linklist-group-from-array.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul {{#cnt-id}} id="{{.}}" {{/cnt-id}}class="list-group{{#cnt-class}} {{.}}{{/cnt-class}}"{{#cnt-aria}} {{{.}}}{{/cnt-aria}} role="group">
<ul {{#cnt-id}} id="{{.}}" {{/cnt-id}}class="list-group{{#cnt-class}} {{.}}{{/cnt-class}}"{{#cnt-aria}} {{{.}}}{{/cnt-aria}}{{#cnt-role}} role="{{.}}"{{/cnt-role}}>
{{#links}}
<li class="list-group-item">
<a{{#id}} id="{{.}}"{{/id}}{{#class}} class="{{.}}"{{/class}} href="{{href}}"{{#title}} title="{{.}}"{{/title}}{{#data}} {{{.}}}{{/data}}{{#aria}} {{{.}}}{{/aria}}{{#rel}} rel="{{.}}"{{/rel}}{{#target}} target="{{.}}"{{/target}}>{{{text}}}</a>
<li class="list-group-item"{{#item-role}} role="{{.}}"{{/item-role}}>
<a{{#id}} id="{{.}}"{{/id}}{{#class}} class="{{.}}"{{/class}} href="{{href}}"{{#title}} title="{{.}}"{{/title}}{{#data}} {{{.}}}{{/data}}{{#aria}} {{{.}}}{{/aria}}{{#rel}} rel="{{.}}"{{/rel}}{{#target}} target="{{.}}"{{/target}}{{#role}} role="{{.}}"{{/role}}>{{{text}}}</a>
</li>
{{/links}}
</ul>
21 changes: 19 additions & 2 deletions src/Component/SimpleLinklistGroupFromArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace MWStake\MediaWiki\Component\CommonUserInterface\Component;

use MWStake\MediaWiki\Component\CommonUserInterface\ILinklistGroupFromArray;
use MWStake\MediaWiki\Component\CommonUserInterface\IListRoleProvider;

class SimpleLinklistGroupFromArray extends ComponentBase implements ILinklistGroupFromArray {
class SimpleLinklistGroupFromArray extends ComponentBase implements ILinklistGroupFromArray, IListRoleProvider {

/**
* @param array $options
Expand All @@ -15,7 +16,9 @@ public function __construct( $options ) {
'id' => '',
'links' => [],
'classes' => [],
'aria' => []
'aria' => [],
'role' => '',
'item-role' => ''
],
$options
);
Expand Down Expand Up @@ -48,4 +51,18 @@ public function getContainerClasses(): array {
public function getAriaAttributes(): array {
return $this->options['aria'];
}

/**
* @inheritDoc
*/
public function getContainerRole(): string {
return $this->options['role'];
}

/**
* @inheritDoc
*/
public function getItemRole(): string {
return $this->options['item-role'];
}
}
16 changes: 16 additions & 0 deletions src/IListRoleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MWStake\MediaWiki\Component\CommonUserInterface;

interface IListRoleProvider {

/**
* @return string
*/
public function getContainerRole(): string;

/**
* @return string
*/
public function getItemRole(): string;
}
22 changes: 22 additions & 0 deletions src/Renderer/LinklistGroupFromArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MWStake\MediaWiki\Component\CommonUserInterface\DataAttributesBuilder;
use MWStake\MediaWiki\Component\CommonUserInterface\IComponent;
use MWStake\MediaWiki\Component\CommonUserInterface\ILinklistGroupFromArray;
use MWStake\MediaWiki\Component\CommonUserInterface\IListRoleProvider;

class LinklistGroupFromArray extends RendererBase {

Expand Down Expand Up @@ -72,6 +73,27 @@ public function getRendererDataTreeNode( $component, $subComponentNodes ): array
]
);
}

if ( $component instanceof IListRoleProvider ) {
$role = $component->getContainerRole();
if ( $role !== '' ) {
$templateData = array_merge(
$templateData,
[
'cnt-role' => $role
]
);
}
$itemRole = $component->getItemRole();
if ( $itemRole !== '' ) {
$templateData = array_merge(
$templateData,
[
'item-role' => $itemRole
]
);
}
}
} else {
throw new Exception( "Can not extract data from " . get_class( $component ) );
}
Expand Down

0 comments on commit 62ee107

Please sign in to comment.