| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface HC3_Ui_Element_ICollapse |
| 3 |
{ |
| 4 |
public function setContent( $content ); |
| 5 |
public function border( $border = TRUE ); |
| 6 |
public function arrow( $arrow ); |
| 7 |
public function expand( $expand = TRUE ); |
| 8 |
public function render(); |
| 9 |
public function hideToggle( $hide = TRUE ); |
| 10 |
} |
| 11 |
|
| 12 |
class HC3_Ui_Element_Collapse extends HC3_Ui_Abstract_Collection implements HC3_Ui_Element_ICollapse |
| 13 |
{ |
| 14 |
protected $uiType = 'collapse'; |
| 15 |
protected $html = NULL; |
| 16 |
protected $label = NULL; |
| 17 |
protected $content = NULL; |
| 18 |
protected $expand = FALSE; |
| 19 |
protected $hideToggle = FALSE; |
| 20 |
|
| 21 |
protected $border = FALSE; |
| 22 |
protected $arrow = '↓'; |
| 23 |
|
| 24 |
public function __construct( HC3_Ui $html, $label, $content, $expand = FALSE ) |
| 25 |
{ |
| 26 |
$this->html = $html; |
| 27 |
$this->label = $label; |
| 28 |
$this->content = $content; |
| 29 |
|
| 30 |
$this->add( $this->label ); |
| 31 |
$this->add( $this->content ); |
| 32 |
|
| 33 |
if( $expand ){ |
| 34 |
$this->expand(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function setContent( $content ) |
| 39 |
{ |
| 40 |
$this->content = $content; |
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function border( $border = TRUE ) |
| 45 |
{ |
| 46 |
$this->border = $border; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
public function arrow( $arrow ) |
| 51 |
{ |
| 52 |
$this->arrow = $arrow; |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function expand( $expand = TRUE ) |
| 57 |
{ |
| 58 |
$this->expand = $expand; |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function hideToggle( $hideToggle = TRUE ) |
| 63 |
{ |
| 64 |
$this->hideToggle = $hideToggle; |
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
public function render() |
| 69 |
{ |
| 70 |
if( NULL === $this->content ){ |
| 71 |
return $this->label; |
| 72 |
} |
| 73 |
|
| 74 |
$this_id = 'hc3_' . mt_rand( 100000, 999999 ); |
| 75 |
|
| 76 |
$checkbox = $this->html->makeElement('input') |
| 77 |
->addAttr('id', $this_id) |
| 78 |
->addAttr('type', 'checkbox') |
| 79 |
->addAttr('class', 'hc-collapse-toggler') |
| 80 |
->addAttr('class', 'hc-hide') |
| 81 |
; |
| 82 |
if( $this->expand ){ |
| 83 |
$checkbox->addAttr('checked', 'checked'); |
| 84 |
} |
| 85 |
|
| 86 |
$trigger = $this->html->makeElement('label', $this->label) |
| 87 |
->addAttr('for', $this_id) |
| 88 |
->addAttr('class', 'hc-block') |
| 89 |
// ->addAttr('class', 'hc-py1') |
| 90 |
->addAttr('class', 'hc-collapse-burger') |
| 91 |
->addAttr('class', 'hc-regular') |
| 92 |
; |
| 93 |
|
| 94 |
if( ! is_object($this->label) ){ |
| 95 |
$trigger |
| 96 |
->addAttr('title', strip_tags($this->label) ) |
| 97 |
; |
| 98 |
} |
| 99 |
|
| 100 |
if( $this->border ){ |
| 101 |
$trigger |
| 102 |
->addAttr('title', strip_tags($this->label) ) |
| 103 |
->addAttr('class', 'hc-border-bottom-dotted') |
| 104 |
->addAttr('class', 'hc-border-gray') |
| 105 |
; |
| 106 |
} |
| 107 |
|
| 108 |
if( $this->arrow ){ |
| 109 |
$trigger = $this->html->makeListInline( array($this->arrow, $trigger) )->gutter(1) |
| 110 |
; |
| 111 |
$trigger = $this->html->makeBlock( $trigger ) |
| 112 |
->addAttr('class', 'hc-nowrap') |
| 113 |
; |
| 114 |
} |
| 115 |
$content = $this->html->makeBlock( $this->content ) |
| 116 |
->addAttr('class', 'hc-collapse-content') |
| 117 |
->addAttr('class', 'hc-mt1') |
| 118 |
; |
| 119 |
|
| 120 |
$out = $this->html->makeCollection( array($checkbox, $trigger, $content) ); |
| 121 |
$out = $this->html->makeBlock( $out ) |
| 122 |
->addAttr('class', 'hc-collapse-container') |
| 123 |
; |
| 124 |
|
| 125 |
if( $this->hideToggle ){ |
| 126 |
$out |
| 127 |
->addAttr('class', 'hc-collapse-container-hidetoggle') |
| 128 |
; |
| 129 |
} |
| 130 |
|
| 131 |
return $out; |
| 132 |
} |
| 133 |
} |