make mobile renderer service a child of desktop renderer

This commit is contained in:
idradm 2016-12-27 15:34:21 +01:00
parent 36b582a164
commit 51e2cbfb18
2 changed files with 42 additions and 148 deletions

View file

@ -2,45 +2,10 @@
use Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper;
class MobileInfoboxRenderService extends WikiaService {
class MobileInfoboxRenderService extends PortableInfoboxRenderService {
const MEDIA_CONTEXT_INFOBOX_HERO_IMAGE = 'infobox-hero-image';
const MEDIA_CONTEXT_INFOBOX = 'infobox';
private static $templates = [
'wrapper' => 'PortableInfoboxWrapper.mustache',
'title' => 'PortableInfoboxItemTitle.mustache',
'header' => 'PortableInfoboxItemHeader.mustache',
'image' => 'PortableInfoboxItemImage.mustache',
'image-mobile' => 'PortableInfoboxItemImageMobile.mustache',
'image-mobile-wikiamobile' => 'PortableInfoboxItemImageMobileWikiaMobile.mustache',
'data' => 'PortableInfoboxItemData.mustache',
'group' => 'PortableInfoboxItemGroup.mustache',
'horizontal-group-content' => 'PortableInfoboxHorizontalGroupContent.mustache',
'navigation' => 'PortableInfoboxItemNavigation.mustache',
'hero-mobile' => 'PortableInfoboxItemHeroMobile.mustache',
'hero-mobile-wikiamobile' => 'PortableInfoboxItemHeroMobileWikiaMobile.mustache',
'image-collection' => 'PortableInfoboxItemImageCollection.mustache',
'image-collection-mobile' => 'PortableInfoboxItemImageCollectionMobile.mustache',
'image-collection-mobile-wikiamobile' => 'PortableInfoboxItemImageCollectionMobileWikiaMobile.mustache'
];
private $templateEngine;
private $imagesWidth;
function __construct() {
parent::__construct();
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
->setPrefix( self::getTemplatesDir() );
$this->imagesWidth = PortableInfoboxRenderServiceHelper::MOBILE_THUMBNAIL_WIDTH;
}
public static function getTemplatesDir() {
return dirname( __FILE__ ) . '/../templates';
}
public static function getTemplates() {
return self::$templates;
}
/**
* renders infobox
*
@ -86,81 +51,47 @@ class MobileInfoboxRenderService extends WikiaService {
return $output;
}
/**
* Produces HTML output for item type and data
*
* @param $type
* @param $template
* @param array $data
* @return string
*/
private function render( $type, $template, array $data ) {
protected function renderImage( $data ) {
$images = [ ];
$helper = new PortableInfoboxRenderServiceHelper();
for ( $i = 0; $i < count( $data ); $i++ ) {
$data[ $i ][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX;
$data[ $i ] = $helper->extendImageData( $data[ $i ], $this->imagesWidth );
if ( !!$data[ $i ] ) {
$images[] = $data[ $i ];
}
}
if ( count( $images ) === 0 ) {
return '';
}
// use different template for wikiamobile
if ( !$helper->isMercury() ) {
// always display only the first image on WikiaMobile
$data = $images[ 0 ];
$templateName = 'image-mobile-wikiamobile';
} else {
if ( count( $images ) === 1 ) {
$data = $images[ 0 ];
$templateName = 'image-mobile';
} else {
// more than one image means image collection
$data = $helper->extendImageCollectionData( $images );
$templateName = 'image-collection-mobile';
}
}
$data = SanitizerBuilder::createFromType( 'image' )->sanitize( $data );
return parent::render( $templateName, $data );
}
protected function render( $type, array $data ) {
$data = SanitizerBuilder::createFromType( $type )->sanitize( $data );
return $this->templateEngine->clearData()
->setData( $data )
->render( self::getTemplates()[ $template ] );
}
/**
* renders part of infobox
*
* @param string $type
* @param array $data
*
* @return bool|string - HTML
*/
private function renderItem( $type, array $data ) {
if ( $type === 'group' ) {
return $this->renderGroup( $data );
}
if ( $type === 'image' ) {
return $this->renderImage( $data );
}
return $this->render( $type, $type, $data );
}
/**
* renders group infobox component
*
* @param array $groupData
*
* @return string - group HTML markup
*/
private function renderGroup( $groupData ) {
$cssClasses = [ ];
$helper = new PortableInfoboxRenderServiceHelper();
$groupHTMLContent = '';
$dataItems = $groupData[ 'value' ];
$layout = $groupData[ 'layout' ];
$collapse = $groupData[ 'collapse' ];
if ( $layout === 'horizontal' ) {
$groupHTMLContent .= $this->renderItem(
'horizontal-group-content',
$helper->createHorizontalGroupData( $dataItems )
);
} else {
foreach ( $dataItems as $item ) {
$type = $item[ 'type' ];
if ( $helper->isTypeSupportedInTemplates( $type, self::getTemplates() ) ) {
$groupHTMLContent .= $this->renderItem( $type, $item[ 'data' ] );
}
}
}
if ( $collapse !== null && count( $dataItems ) > 0 && $dataItems[ 0 ][ 'type' ] === 'header' ) {
$cssClasses[] = 'pi-collapse';
$cssClasses[] = 'pi-collapse-' . $collapse;
}
return $this->render( 'group', 'group', [
'content' => $groupHTMLContent,
'cssClasses' => implode( ' ', $cssClasses )
] );
return parent::render( $type, $data );
}
/**
@ -194,40 +125,4 @@ class MobileInfoboxRenderService extends WikiaService {
return '';
}
private function renderImage( $data ) {
$images = [ ];
$helper = new PortableInfoboxRenderServiceHelper();
for ( $i = 0; $i < count( $data ); $i++ ) {
$data[ $i ][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX;
$data[ $i ] = $helper->extendImageData( $data[ $i ], $this->imagesWidth );
if ( !!$data[ $i ] ) {
$images[] = $data[ $i ];
}
}
if ( count( $images ) === 0 ) {
return '';
}
// use different template for wikiamobile
if ( !$helper->isMercury() ) {
// always display only the first image on WikiaMobile
$data = $images[ 0 ];
$templateName = 'image-mobile-wikiamobile';
} else {
if ( count( $images ) === 1 ) {
$data = $images[ 0 ];
$templateName = 'image-mobile';
} else {
// more than one image means image collection
$data = $helper->extendImageCollectionData( $images );
$templateName = 'image-collection-mobile';
}
}
return $this->render( 'image', $templateName, $data );
}
}

View file

@ -3,7 +3,7 @@
use Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper;
class PortableInfoboxRenderService extends WikiaService {
private static $templates = [
protected static $templates = [
'wrapper' => 'PortableInfoboxWrapper.mustache',
'title' => 'PortableInfoboxItemTitle.mustache',
'header' => 'PortableInfoboxItemHeader.mustache',
@ -20,14 +20,13 @@ class PortableInfoboxRenderService extends WikiaService {
'image-collection-mobile' => 'PortableInfoboxItemImageCollectionMobile.mustache',
'image-collection-mobile-wikiamobile' => 'PortableInfoboxItemImageCollectionMobileWikiaMobile.mustache'
];
private $templateEngine;
private $imagesWidth;
protected $templateEngine;
protected $imagesWidth;
function __construct() {
parent::__construct();
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
->setPrefix( self::getTemplatesDir() );
$this->imagesWidth = PortableInfoboxRenderServiceHelper::DEFAULT_DESKTOP_THUMBNAIL_WIDTH;
}
public static function getTemplatesDir() {
@ -88,7 +87,7 @@ class PortableInfoboxRenderService extends WikiaService {
* @param array $data
* @return string
*/
private function render( $type, array $data ) {
protected function render( $type, array $data ) {
return $this->templateEngine->clearData()
->setData( $data )
->render( self::getTemplates()[ $type ] );
@ -100,9 +99,9 @@ class PortableInfoboxRenderService extends WikiaService {
* @param string $type
* @param array $data
*
* @return bool|string - HTML
* @return string - HTML
*/
private function renderItem( $type, array $data ) {
protected function renderItem( $type, array $data ) {
if ( $type === 'group' ) {
return $this->renderGroup( $data );
}
@ -120,7 +119,7 @@ class PortableInfoboxRenderService extends WikiaService {
*
* @return string - group HTML markup
*/
private function renderGroup( $groupData ) {
protected function renderGroup( $groupData ) {
$cssClasses = [ ];
$helper = new PortableInfoboxRenderServiceHelper();
$groupHTMLContent = '';
@ -160,7 +159,7 @@ class PortableInfoboxRenderService extends WikiaService {
* @param $data
* @return string
*/
private function renderImage( $data ) {
protected function renderImage( $data ) {
$helper = new PortableInfoboxRenderServiceHelper();
$images = [ ];