Skip to content

Commit

Permalink
Add addTitle to Panel and Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-claras committed Mar 31, 2014
1 parent 13cf80a commit 6952d0a
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/MyCLabs/MUIH/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct($id='', $title=null, $content=null)
{
$this->addClass('collapse-wrapper');

$this->link = new GenericTag('a', $title);
$this->link = new GenericTag('a');
$this->link->setAttribute('data-toggle', 'collapse');
$this->title = new GenericTag('legend', $this->link);

Expand All @@ -61,6 +61,8 @@ public function __construct($id='', $title=null, $content=null)
$this->setCollapseStateIndicators();

parent::__construct('fieldset', $content);

$this->setTitleContent($title);
}

/**
Expand Down
53 changes: 50 additions & 3 deletions src/MyCLabs/MUIH/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
class Modal extends GenericTag
{
public static $defaultDismiss = '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';

/**
* @var GenericTag
Expand Down Expand Up @@ -39,15 +40,16 @@ class Modal extends GenericTag
* @param string $content
* @param string $header
* @param string $footer
* @param bool $withDismissButton
*/
public function __construct($content=null, $header=null, $footer=null)
public function __construct($content=null, $header=null, $footer=null, $withDismissButton=true)
{
$this->addClass('modal');

$this->header = new GenericTag('div', $header);
$this->header = new GenericTag('div');
$this->header->addClass('modal-header');

$this->footer = new GenericTag('div', $footer);
$this->footer = new GenericTag('div');
$this->footer->addClass('modal-footer');

$this->body = new GenericTag('div');
Expand All @@ -61,6 +63,16 @@ public function __construct($content=null, $header=null, $footer=null)


parent::__construct('div', $content);

if ($header !== null) {
$this->addTitle($header);
}
if ($footer !== null) {
$this->setFooterContent($footer);
}
if ($header !== null && $withDismissButton) {
$this->addDefaultDismissButton();
}
}

/**
Expand Down Expand Up @@ -187,6 +199,41 @@ public function getContentAsString()
return (string) $modalDialog;
}

/**
* @return $this
*/
public function addDefaultDismissButton()
{
$this->getHeader()->prependContent(self::$defaultDismiss);

return $this;
}

/**
* @param GenericTag|string $dismissButton
* @return $this
*/
public function addDismissButton($dismissButton)
{
$this->getHeader()->prependContent($dismissButton);

return $this;
}

/**
* @param string $title
* @param string $level
* @return $this
*/
public function addTitle($title, $level='h4')
{
$titleWrapper = new GenericTag($level, $title);
$titleWrapper->addClass('modal-title');
$this->getHeader()->appendContent($titleWrapper);

return $this;
}

/**
* @param bool $replaceBodyOnly
* @return $this
Expand Down
25 changes: 23 additions & 2 deletions src/MyCLabs/MUIH/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,23 @@ public function __construct($content=null, $header=null, $footer=null, $type=se
}
$this->addClass('panel-' . $type);

$this->header = new GenericTag('div', $header);
$this->header = new GenericTag('div');
$this->header->addClass('panel-header');

$this->footer = new GenericTag('div', $footer);
$this->footer = new GenericTag('div');
$this->footer->addClass('panel-footer');

$this->content= new GenericTag('div');
$this->content->addClass('panel-body');

parent::__construct('div', $content);

if ($header !== null) {
$this->addTitle($header);
}
if ($footer !== null) {
$this->setFooterContent($footer);
}
}

/**
Expand Down Expand Up @@ -168,4 +175,18 @@ public function getContentAsString()

return $content;
}

/**
* @param string $title
* @param string $level
* @return $this
*/
public function addTitle($title, $level='h3')
{
$titleWrapper = new GenericTag($level, $title);
$titleWrapper->addClass('panel-title');
$this->getHeader()->appendContent($titleWrapper);

return $this;
}
}
117 changes: 116 additions & 1 deletion tests/MUIHTest/MUIH/ModalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,27 @@ public function testConstructor()
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">bar</div>'.
'<div class="modal-header">'.
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'.
'<h4 class="modal-title">bar</h4>'.
'</div>'.
'<div class="modal-body">foo</div>'.
'<div class="modal-footer">baz</div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);

$tag = new Modal('foo', 'bar', 'baz', false);

$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'<h4 class="modal-title">bar</h4>'.
'</div>'.
'<div class="modal-body">foo</div>'.
'<div class="modal-footer">baz</div>'.
'</div>'.
Expand Down Expand Up @@ -202,6 +222,101 @@ public function testAppendContent()
);
}

public function testAddDefaultDismissButton()
{
$tag = new Modal();

$tag->addDefaultDismissButton();
$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'.
'</div>'.
'<div class="modal-body"></div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);
}

public function testAddDismissButton()
{
$tag = new Modal();

$tag->addDismissButton('foo');
$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'foo'.
'</div>'.
'<div class="modal-body"></div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);

$tag = new Modal('foo', 'bar', null, false);

$tag->addDismissButton('fu');
$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'fu'.
'<h4 class="modal-title">bar</h4>'.
'</div>'.
'<div class="modal-body">foo</div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);
}

public function testAddTitle()
{
$tag = new Modal();

$tag->addTitle('foo');
$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'<h4 class="modal-title">foo</h4>'.
'</div>'.
'<div class="modal-body"></div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);

$tag = new Modal();

$tag->addTitle('foo', 'bar');
$this->assertEquals(
'<div class="modal">'.
'<div class="modal-dialog">'.
'<div class="modal-content">'.
'<div class="modal-header">'.
'<bar class="modal-title">foo</bar>'.
'</div>'.
'<div class="modal-body"></div>'.
'</div>'.
'</div>'.
'</div>',
$tag->getHTML()
);
}

public function testAjax()
{
$tag = new Modal();
Expand Down
33 changes: 32 additions & 1 deletion tests/MUIHTest/MUIH/PanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function testConstructor()

$this->assertEquals(
'<div class="panel panel-info">'.
'<div class="panel-header">bar</div>'.
'<div class="panel-header">'.
'<h3 class="panel-title">bar</h3>'.
'</div>'.
'<div class="panel-body">foo</div>'.
'<div class="panel-footer">baz</div>'.
'</div>',
Expand Down Expand Up @@ -141,4 +143,33 @@ public function testAppendContent()
$tag->getHTML()
);
}

public function testAddTitle()
{
$tag = new Panel();

$tag->addTitle('foo');
$this->assertEquals(
'<div class="panel panel-default">'.
'<div class="panel-header">'.
'<h3 class="panel-title">foo</h3>'.
'</div>'.
'<div class="panel-body"></div>'.
'</div>',
$tag->getHTML()
);

$tag = new Panel();

$tag->addTitle('foo', 'bar');
$this->assertEquals(
'<div class="panel panel-default">'.
'<div class="panel-header">'.
'<bar class="panel-title">foo</bar>'.
'</div>'.
'<div class="panel-body"></div>'.
'</div>',
$tag->getHTML()
);
}
}

0 comments on commit 6952d0a

Please sign in to comment.