1 | <?php |
||
2 | |||
3 | namespace Ajax\bootstrap\html; |
||
4 | |||
5 | use Ajax\JsUtils; |
||
6 | use Ajax\bootstrap\components\Scrollspy; |
||
7 | use Ajax\bootstrap\html\content\HtmlNavzone; |
||
8 | use Ajax\bootstrap\html\base\CssNavbar; |
||
9 | use Ajax\common\html\BaseHtml; |
||
10 | use Ajax\common\html\html5\HtmlImg; |
||
11 | /** |
||
12 | * Twitter Bootstrap HTML Navbar component |
||
13 | * @author jc |
||
14 | * @version 1.001 |
||
15 | */ |
||
16 | class HtmlNavbar extends BaseHtml { |
||
17 | protected $navZones; |
||
18 | protected $class="navbar-default"; |
||
19 | protected $brand="Brand"; |
||
20 | protected $brandHref="#"; |
||
21 | protected $brandTarget="_self"; |
||
22 | protected $brandImage=""; |
||
23 | protected $scrollspy; |
||
24 | protected $hasScrollspy=false; |
||
25 | protected $scrollspyId="body"; |
||
26 | protected $fluid="container-fluid"; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @param string $identifier the id |
||
31 | */ |
||
32 | public function __construct($identifier, $brand="Brand", $brandHref="#") { |
||
33 | parent::__construct($identifier); |
||
34 | $this->_template=include 'templates/tplNavbar.php'; |
||
35 | $this->navZones=array (); |
||
36 | $this->class="navbar-default"; |
||
37 | $this->brand=$brand; |
||
38 | $this->brandHref=$brandHref; |
||
39 | |||
40 | } |
||
41 | |||
42 | public function setClass($class) { |
||
43 | $this->class=$class; |
||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | public function setBrand($brand) { |
||
48 | $this->brand=$brand; |
||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | public function setBrandHref($brandHref) { |
||
53 | $this->brandHref=$brandHref; |
||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | public function setBrandTarget($brandTarget) { |
||
58 | $this->brandTarget=$brandTarget; |
||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function setBrandImage($imageSrc) { |
||
63 | $this->brandImage=new HtmlImg("brand-img-".$this->_identifier,$imageSrc,$this->brand); |
||
64 | $this->brand=""; |
||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * adds a new zone of type $type |
||
70 | * @param string $type one of nav, form, btn, right, left |
||
71 | * @param string $identifier |
||
72 | * @return HtmlNavzone |
||
73 | */ |
||
74 | public function addZone($type="nav", $identifier=NULL) { |
||
75 | if (!isset($identifier)) { |
||
76 | $nb=sizeof($this->navZones)+1; |
||
77 | $identifier=$this->identifier."-navzone-".$nb; |
||
78 | } |
||
79 | $zone=HtmlNavzone::$type($identifier); |
||
80 | $this->navZones []=$zone; |
||
81 | return $zone; |
||
82 | } |
||
83 | |||
84 | public function addElement($element, HtmlNavzone $zone=NULL) { |
||
85 | $zone=$this->getZoneToInsertIn($zone); |
||
86 | if ($element instanceof HtmlDropdown) |
||
87 | $element->setMTagName("li"); |
||
88 | $zone->addElement($element); |
||
89 | } |
||
90 | |||
91 | public function addElements($elements, HtmlNavzone $zone=NULL) { |
||
92 | $zone=$this->getZoneToInsertIn($zone); |
||
93 | $zone->addElements($elements); |
||
94 | return $zone; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * /* (non-PHPdoc) |
||
99 | * @see BaseHtml::addProperties() |
||
100 | */ |
||
101 | public function fromArray($array) { |
||
102 | return parent::fromArray($array); |
||
103 | } |
||
104 | |||
105 | public function setNavZones($navZones) { |
||
106 | if (\is_array($navZones)) { |
||
107 | foreach ( $navZones as $zoneType => $zoneArray ) { |
||
108 | if (is_string($zoneType)) { |
||
109 | $zone=$this->addZone($zoneType); |
||
110 | $zone->fromArray($zoneArray); |
||
111 | } else if (is_string($zoneArray)) |
||
112 | $this->addElement($zoneArray); |
||
113 | else |
||
114 | $this->addElements($zoneArray); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * @param HtmlNavzone $zone |
||
122 | * @return HtmlNavzone |
||
123 | */ |
||
124 | public function getZoneToInsertIn($zone=NULL) { |
||
125 | if (!isset($zone)) { |
||
126 | $nb=sizeof($this->navZones); |
||
127 | if ($nb<1) |
||
128 | $zone=$this->addZone(); |
||
129 | else |
||
130 | $zone=$this->navZones [$nb-1]; |
||
131 | } |
||
132 | return $zone; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * |
||
137 | * @param int $index |
||
138 | * @return HtmlNavzone |
||
139 | */ |
||
140 | public function getZone($index) { |
||
141 | $zone=null; |
||
142 | $nb=sizeof($this->navZones); |
||
143 | if (is_int($index)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
144 | if ($index<$nb) |
||
145 | $zone=$this->navZones [$index]; |
||
146 | } else { |
||
147 | for($i=0; $i<$nb; $i++) { |
||
148 | if ($this->navZones [$i]->getIdentifier()===$index) { |
||
149 | $zone=$this->navZones [$i]; |
||
150 | break; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | return $zone; |
||
155 | } |
||
156 | |||
157 | public function run(JsUtils $js) { |
||
158 | foreach ( $this->navZones as $zone ) { |
||
159 | $zone->run($js); |
||
160 | } |
||
161 | if ($this->hasScrollspy) { |
||
162 | $this->scrollspy=new Scrollspy($js); |
||
163 | $this->scrollspy->attach($this->scrollspyId); |
||
164 | $this->scrollspy->setTarget("#".$this->identifier); |
||
165 | $this->scrollspy->compile($js); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public function cssInverse() { |
||
170 | $this->addToMember($this->class, CssNavbar::NAVBAR_INVERSE); |
||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | public function scrollspy($attachTo="body") { |
||
175 | $this->hasScrollspy=true; |
||
176 | $this->scrollspyId=$attachTo; |
||
177 | } |
||
178 | |||
179 | public function setFluid($fluid) { |
||
180 | if($fluid===true){ |
||
181 | $this->fluid="container-fluid"; |
||
182 | }else{ |
||
183 | $this->fluid="container"; |
||
184 | } |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | /* (non-PHPdoc) |
||
189 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
190 | */ |
||
191 | public function fromDatabaseObject($object, $function) { |
||
192 | $this->addElement($function($object)); |
||
193 | } |
||
194 | |||
195 | } |
||
196 |