Florianschmidtwelzow has uploaded a new change for review. https://gerrit.wikimedia.org/r/269740
Change subject: [WIP] Beta: Add userlinks to mobile main menu ...................................................................... [WIP] Beta: Add userlinks to mobile main menu Adds a talk and contributions button to the mobile menu. ToDos: * Find an icon for the contributions button * Several FIXME's * General acceptance Bug: T117970 Change-Id: I47285d7748d06d8f17dddffbdaaec272508cd9e9 --- M includes/MenuBuilder.php M includes/skins/SkinMinervaBeta.php M resources/mobile.mainMenu/mainmenu.less 3 files changed, 164 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/40/269740/1 diff --git a/includes/MenuBuilder.php b/includes/MenuBuilder.php index c35c334..cdc923d 100644 --- a/includes/MenuBuilder.php +++ b/includes/MenuBuilder.php @@ -20,11 +20,14 @@ $result = array( 'name' => $entry->getName(), 'components' => $entry->getComponents(), + 'class' => '', ); if ( $entry->isJSOnly() ) { - $result['class'] = 'jsonly'; + $result['class'] .= 'jsonly'; } + + $result['class'] .= ' ' . implode( $entry->getCSSClass(), ' ' ); return $result; }; @@ -33,20 +36,57 @@ } /** + * Get a specific entry identified by the given name. + * + * @param string $name Name of the entry to retrieve + * @return null|MenuEntry null, if the entry doesn't exist, the entry otherwise + */ + public function getEntry( $name ) { + $id = $this->search( $name ); + if ( $id === -1 ) { + return null; + } + + return $this->entries[$id]; + } + + /** * Insert an entry into the menu. * * @param string $name A unique name identifying the menu entry * @param boolean [$isJSOnly] Whether the menu entry works without JS + * @param array $cssClass The CSS classes for this entry * @throws DomainException When the entry already exists * @return MenuEntry */ - public function insert( $name, $isJSOnly = false ) { + public function insert( $name, $isJSOnly = false, $cssClass = array() ) { if ( $this->search( $name ) !== -1 ) { throw new DomainException( "The \"${name}\" entry already exists." ); } - $this->entries[] = $entry = new MenuEntry( $name, $isJSOnly ); + $this->entries[] = $entry = new MenuEntry( $name, $isJSOnly, $cssClass ); + return $entry; + } + + /** + * Replace an already inserted element with the given new element. You should + * retrieve the inserted element with self::getEntry() first and extend it, so + * the elements doesn't get lost. + * + * The entry that should be replaced is identified by the name of the given new + * entry. If the entry isn't added already, it will be inserted as a new one. + * + * @param MenuEntry $entry The entry to replace the already inserted one with + * @param boolean [$isJSOnly] Whether the menu entry works without JS + * @return MenuEntry + */ + public function replaceEntry( MenuEntry $entry, $isJSOnly = false ) { + $id = $this->search( $entry->getName() ); + if ( $id === -1 ) { + return $this->insert( $entry->getName(), $isJSOnly ); + } + $this->entries[$id] = $entry; return $entry; } @@ -57,10 +97,8 @@ * @return integer If the menu entry exists, then the 0-based index of the entry; otherwise, -1 */ private function search( $name ) { - $count = count( $this->entries ); - - for ( $i = 0; $i < $count; ++$i ) { - if ( $this->entries[$i]->getName() === $name ) { + foreach ( $this->entries as $i => $entry ) { + if ( $entry->getName() === $name ) { return $i; } } @@ -103,15 +141,18 @@ private $name; private $isJSOnly; private $components; + private $cssClass; /** * @param string $name * @param boolean $isJSOnly Whether the entry works without JS + * @param array $cssClass The CSS classes for this entry */ - public function __construct( $name, $isJSOnly ) { + public function __construct( $name, $isJSOnly, $cssClass = array() ) { $this->name = $name; $this->isJSOnly = $isJSOnly; $this->components = array(); + $this->cssClass = $cssClass; } /** @@ -139,6 +180,24 @@ } /** + * Searches for a menu entry by name. + * + * @param string $name + * @return integer If the menu entry exists, then the 0-based index of the entry; otherwise, -1 + */ + private function search( $name ) { + $count = count( $this->components ); + + foreach ( $this->components as $i => $component ) { + if ( $component['text'] === $name ) { + return $i; + } + } + + return -1; + } + + /** * Add a link to the entry. * * An entry can have zero or more links. @@ -160,4 +219,39 @@ return $this; } + + /** + * Removes a component from this MenuEntry identified by the text of the component. + * + * CAUTION: The name is probably unique. However, you shouldn't expect that, components + * hasn't the requirement to have a unique identifier. This function will remove the first + * occurence of a component with the given text, but no further occurences. + * + * @param string $text The label of the component to remove + * @param boolean $retrieve Whether you want to retrieve the component, if it was removed + * @return boolean|array True, if the component was removed successfully, false if not, array + * if the component was removed and you set $retrieve to true (the component contents) + */ + public function removeComponent( $text, $retrieve = false ) { + $id = $this->search( $text ); + if ( $id !== -1 ) { + $comp = $this->components[$id]; + unset( $this->components[$id] ); + if ( $retrieve ) { + return $comp; + } else { + return true; + } + } + return false; + } + + /** + * Returns the CSS classes added for this entry. + * + * @return array + */ + public function getCSSClass() { + return $this->cssClass; + } } diff --git a/includes/skins/SkinMinervaBeta.php b/includes/skins/SkinMinervaBeta.php index b543fab..7b4d0d9 100644 --- a/includes/skins/SkinMinervaBeta.php +++ b/includes/skins/SkinMinervaBeta.php @@ -2,6 +2,7 @@ /** * SkinMinervaBeta.php */ +use MobileFrontend\MenuBuilder; /** * Beta-Implementation of stable class SkinMinerva @@ -161,4 +162,54 @@ $templateParser->processTemplate( 'user_page_links', $data ) ); } } + + protected function insertLogInOutLink( MenuBuilder $menu ) { + parent::insertLogInOutLink( $menu ); + + $user = $this->getUser(); + + // change the menu only for logged in users for now + // FIXME: Is this condition really needed? + if ( $user->isLoggedIn() ) { + // get the auth entry to check, if it's there already (this function isn't meant to + // add the module if it doesn't exist already) + $entry = $menu->getEntry( 'auth' ); + + if ( $entry ) { + // remove and retrieve the logout link component (to add it later to the new + // line component) + $comp = $entry->removeComponent( + wfMessage( 'mobile-frontend-main-menu-logout' )->escaped(), + true + ); + // replace the original "auth" entry with the one without logout link + $menu->replaceEntry( $entry ); + + // create a new component which will hold the userlinks + $newLineEntry = $menu->insert( 'userlinks', false, array( 'beta-userlinks' ) ); + // FIXME: Isn't there a better solution? :/ + // the last attributes are "attributes" and MenuBuilder::addComponent expect them + // to be an array. Currently they are part of the whole array + if ( count( $comp ) > 3 ) { + $attrs = array_slice( $comp, 3 ); + $comp = array_slice( $comp, 0, 3 ); + } + // add the new talk page icon + $newLineEntry + ->addComponent( + wfMessage( 'mobile-frontend-main-menu-talkpage' )->escaped(), + $user->getTalkPage()->getLocalURL(), + MobileUI::iconClass( 'talk', 'element', 'secondary-action truncated-text' ) + ) + ->addComponent( + wfMessage( 'mobile-frontend-main-menu-contribs' )->escaped(), + SpecialPage::getTitleFor( 'Contributions', $user->getName() )->getLocalURL(), + // FIXME: Needs own icon + MobileUI::iconClass( 'talk', 'element', 'secondary-action truncated-text' ) + ); + // add the old logout button + call_user_func_array( array( $newLineEntry, 'addComponent' ), $comp + array( $attrs ) ); + } + } + } } diff --git a/resources/mobile.mainMenu/mainmenu.less b/resources/mobile.mainMenu/mainmenu.less index 02985a8..dba89ba 100644 --- a/resources/mobile.mainMenu/mainmenu.less +++ b/resources/mobile.mainMenu/mainmenu.less @@ -76,6 +76,17 @@ // 1px for the logout icon border-left margin-right: @menuLinkLineHeight * 2 + 1; } + + // FIXME: put this into it's own beta-module + .beta-userlinks { + text-align: right; + + a { + text-align: left; + display: inline-block; + position: inherit; + } + } } ul { -- To view, visit https://gerrit.wikimedia.org/r/269740 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I47285d7748d06d8f17dddffbdaaec272508cd9e9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits