-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMarkupSimpleNavigation.module
More file actions
392 lines (321 loc) · 13.3 KB
/
MarkupSimpleNavigation.module
File metadata and controls
392 lines (321 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
*
* Simple Tree Menu Module
*
* @author Soma, with code based by Apeisa, Ryan.
* https://github.com/somatonic/MarkupSimpleNavigation
*
* @date_created 08/03/2012
*
* ProcessWire 2.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class MarkupSimpleNavigation extends WireData implements Module {
public static $defaultOptions = array(
'parent_class' => 'parent', // string (default 'parent') overwrite class name for current parent levels
'current_class' => 'current', // string (default 'current') overwrite current class
'has_children_class' => 'has_children', // string (default 'has_children') overwrite class name for entries with children
'levels' => false, // bool (default false) wether to output "level-1, level-2, ..." as css class in links
'levels_prefix' => 'level-', // bool (default 'level-') prefix string that will be used for level class
'max_levels' => null, // int (default null) set the max level rendered
'firstlast' => false, // bool (default false) puts last,first class to link items
'collapsed' => false, // bool (default false) if you want to auto-collapse the tree you set this to true
'show_root' => false, // bool (default false) set this to true if you want to rootPage to get prepended to the menu
'selector' => '', // string (default '') define custom PW selector, you may sanitize values from user input
'selector_field' => 'nav_selector', // string (default 'nav_selector') define custom PW selector by using a property or field on a page. Use this setting if you want to overwrite the default nav_selector
'outer_tpl' => '<ul>||</ul>', // template string for the outer most wrapper. || will contain entries
'inner_tpl' => '<ul>||</ul>', // template string for inner wrappers. || will contain entries
'list_tpl' => '<li%s>||</li>', // template string for the items. || will contain entries, %s will replaced with class="..." string
'list_field_class' => '', // string (default '') add custom classes to each list_tpl using tags like {field} i.e. {template} p_{id}
'item_tpl' => '<a href="{url}">{title}</a>', // template string for the inner items. Use {anyfield} and {url}, i.e. {headline|title}, if field is of type image it will return url to image (first image if multiple)
'item_current_tpl' => '<a href="{url}">{title}</a>', // template string for the current active inner items.
'xtemplates' => '', // specify one or more templates separated with a pipe | to use the xitem_tpl and xitem_current_tpl markup
'xitem_tpl' => '<a href="{url}">{title}</a>', // same as 'item_tpl' but for xtemplates pages, can be used to define placholders
'xitem_current_tpl' => '<span>{title}</span>', // same as 'item_current_tpl' but for xtemplates pages
'date_format' => 'Y/m/d', // default date formatting for Datetime fields and native created/modified
'code_formatting' => false, // enable or disable code indentations and newslines in markup output
'debug' => false, // show some inline information about rendertime and selectors used as html comments
);
public static function getModuleInfo() {
return array(
'title' => "Markup Simple Navigation",
'summary' => 'Module to render simple nested ul tree of the site structure. See readme for usage and options avaiable.',
'version' => 136,
'href' => 'http://processwire.com/talk/topic/1036-markupsimplenavigation/',
'autoload' => false,
'singular' => false
);
}
public function init() {
//$this->fuel->set('treeMenu', $this );
}
/**
*
* Init method to render nested menu tree
* init tree rendering, merge options with defaults
*
*/
public function render( array $options = null, Page $page = null, $rootPage = null) {
if($options != null && is_array($options)){
$options = array_merge(self::$defaultOptions, $options);
} else {
$options = self::$defaultOptions;
}
if($options['debug']) $this->timer = Debug::timer();
$this->set('iteration', 0);
if(is_null($page)) $page = wire('page');
if(is_null($rootPage)) {
$rootPage = wire('pages')->get('/');
// clear navigationEntries in case it's set in a previous render
$rootPage->navigationEntries = null;
} else if($rootPage instanceof PageArray) {
// if we have a PageArray save it in a temporary property for use in the _renderTree method
$entries = $rootPage;
$rootPage = wire('pages')->get('/');
$rootPage->navigationEntries = $entries;
}
// level offset of root page
if($rootPage != null) $offset = count($rootPage->parents);
else $offset = 0;
$this->outer_tpl = explode('||',$options['outer_tpl']);
$this->inner_tpl = explode('||',$options['inner_tpl']);
$this->list_tpl = explode('||',$options['list_tpl']);
$this->options = $options;
$out = $this->_renderTree( $options, $page, $rootPage, $offset);
if(!$options["code_formatting"]){
$out = str_replace(array("\n", "\t"), array("",""), $out);
}
if($options['debug']) {
$out .= "<!-- [MarkupSimpleNavigation Rendertime: " . Debug::timer($this->timer) . "] -->";
}
return $out;
}
/**
*
* Render the tree method
*
*/
private function _renderTree( $options, Page $page, Page $rootPage, $offset = 0){
$this->iteration++;
$selector = '';
// check for if this page is listable,
// can be used for custom hooks on Page::listable
if(!$rootPage->listable()) return;
$parents = $page->parents;
if($rootPage->{$options['selector_field']}) {
$selector = $rootPage->{$options['selector_field']};
} else if(isset($options['selector_level' . $this->iteration])) {
$selector = $options['selector_level' . $this->iteration];
} else {
$selector = $options['selector'];
}
if($options['show_root'] && $this->iteration == 1) {
$children = new PageArray();
$children->add($rootPage->children( $selector ));
$children->prepend( $rootPage );
} else {
if($rootPage->navigationEntries && $rootPage->navigationEntries instanceof PageArray){
$children = $rootPage->navigationEntries;
} else {
$children = $rootPage->children( $selector );
}
}
// filter out entries may exluded on runtime using Page::listable
// TODO: not sure how it behaves with page hidden status
foreach($children as $key => $child) {
if(!$child->listable()) $children->remove($child);
}
// if no children there's no need to go any further
if( !count($children) ) {
// save the has_children information temporary to the page
// so we can ask for it later when adding class string to childs
// this is to prevent adding the "has_children" class to list items
// when there's no children found using custom selector,selector_leveln
$rootPage->has_valid_children = false;
return;
}
$out = '';
// output outer tpl wrapper only on first iteration
if($this->iteration == 1) {
$out .= "\n{$this->outer_tpl[0]}";
} else if( count($children) ) {
$out .= "\n" . $this->getInnerStringOpen($this->inner_tpl[0], $rootPage);
}
foreach($children as $key => $child) {
// check for if this page is listable,
// can be used for custom hooks on Page::listable
//if(!$child->listable()) continue;
if($child->{$options['selector_field']}) {
$selector = $child->{$options['selector_field']};
} else if(isset($options['selector_level' . ($this->iteration + 1)])) {
$selector = $options['selector_level' . ($this->iteration + 1)];
} else {
$selector = $options['selector'];
}
$s = '';
$class = '';
$is_root = ($child === $rootPage) ? true : false;
$level = $is_root ? 1 : count($child->parents) - $offset;
$stopit = ($options['max_levels'] != null && $level >= $options['max_levels']) ? true : false;
// we save the information to the page as it would get overwritten is using a local variable
// note: counting here is also a performance "bottleneck", but don't see how it could be done better
$child->has_valid_children = ($child->child( $selector )->id && !$stopit) ? true : false;
$is_parent = $parents->has($child);
$is_current = $child === $page;
if($options['levels']) $class = " " . $options['levels_prefix'] . $level;
else $class = '';
if($options['firstlast']) {
if($key == 0) $class .= " first";
if($key == (count($children) - 1 )) $class .= " last";
}
// generate an "all open" tree structure
if(!$options['collapsed']) {
if($is_parent && !$is_root) {
$class .= " " . $options['parent_class'];
}
else if($is_current){
$class .= " " . $options['current_class'];
}
if($child->has_valid_children && !$is_root && !$stopit){
$s = str_replace("\n", "\n\t\t", $this->_renderTree($options, $page, $child, $offset));
// to maintain interation count for correct outer_tpl
$this->iteration--;
}
}
// if auto collapse enabled only open current branch
else
{
if($child->has_valid_children && $is_parent && !$is_root) {
$class .= " " . $options['parent_class'];
if(!$stopit) {
$s = str_replace("\n", "\n\t\t", $this->_renderTree($options, $page, $child, $offset));
// to maintain interation count for correct outer_tpl
$this->iteration--;
}
}
else if($is_parent && !$is_root) {
// make sure parent class gets added to current parents
$class .= " " . $options['parent_class'];
}
else if($is_current) {
$class .= " " . $options['current_class'];
if($child->has_valid_children && !$is_root && !$stopit) {
$s = str_replace("\n", "\n\t\t", $this->_renderTree($options, $page, $child, $offset));
// to maintain interation count for correct outer_tpl
$this->iteration--;
}
}
}
// append has_children only if there's a need to ( if exluded throu selector or max_levels)
// still not sure how to behave, if one wants to still output "has_children" even if max_levels on
if($child->has_valid_children && !$is_root) {
$class .= " " . $options['has_children_class'];
}
if($options['list_field_class']){
$class .= " " . $this->getListString($options['list_field_class'], $child);
}
// if current page, get current item tpl else we use the non-current
if($is_current) {
if($child->is("template=".$options['xtemplates']))
$link_tpl = $options['xitem_current_tpl'];
else $link_tpl = $options['item_current_tpl'];
} else {
if($child->is("template=".$options['xtemplates']))
$link_tpl = $options['xitem_tpl'];
else $link_tpl = $options['item_tpl'];
}
// template field tags matching
$link_tpl = $this->getItemString($link_tpl, $child);
$class = $this->getListClass($class, $child);
$class = trim($class);
if(strlen($class) > 0) {
$class = ' class="' . $class . '"';
}
$list_tpl_open = str_replace("%s", $class, $this->list_tpl[0]);
if($options['debug']) {
$link_tpl .= "<!-- [Selector Level$this->iteration: $selector] -->";
}
$out .= "\n\t{$list_tpl_open}{$link_tpl}{$s}{$this->list_tpl[1]}";
// clear the has_children temporary value
if($child->has_valid_children) unset($child->has_valid_children);
}
// output closing outer tpl wrapper only on first iteration
if($this->iteration == 1) {
$out .= "\n{$this->outer_tpl[1]}";
} else if( count($children) ) {
$out .= "\n{$this->inner_tpl[1]}\n";
}
return $out;
}
protected function parsePlaceholders($tpl, Page $page){
// template field tags matching
preg_match_all('#\{(.+?)\}#i', $tpl, $matches, PREG_SET_ORDER);
if(count($matches)){
foreach($matches as $match) {
$v = $page->get($match[1]);
$v_unformatted = $page->getUnformatted($match[1]);
$field = $this->fields->get($match[1]);
// check if it's an image file field and output url
if($v instanceof Pageimage) {
$field_value = $v->url;
} else if($v instanceof Pageimages) {
if(count($v)) $field_value = $v->first()->url;
else $field_value = '';
}
// if page object from a page field get its url
else if($v instanceof Page){
$field_value = $v->url;
} else if($match[1] == 'created' || $match[1] == 'modified') {
$field_value = date($this->options['date_format'], $v);
// } else if(isset($field->type) && $field->type == "FieldtypeDatetime") {
// $field_value = date($this->options['date_format'], $v_unformatted);
} else if($match[1] == "createdUsersID" || $match[1] == "modifiedUsersID") {
$field_value = $this->users->get($v)->name;
} else {
$field_value = $v;
}
$tpl = str_replace($match[0], $field_value, $tpl);
}
}
return $tpl;
}
/**
* hookable method to add custom classes to list items
*/
protected function ___getListClass($class, Page $page) {
return $class;
}
/**
* deprecated, to be removed
*/
public function ___getTagsString($tpl, Page $page){
return $this->parsePlaceholders($tpl, $page);
}
/**
* hookable method for items,
* same as MarkupSimpleNavigation::getTagsString to have a better suited name but
* keep backward compatibility
*/
public function ___getItemString($tpl, Page $page){
return $this->getTagsString($tpl, $page);
}
/**
* hookable method for lists
*/
public function ___getListString($tpl, Page $page){
return $this->parsePlaceholders($tpl, $page);
}
/**
* hookable method for inner template, only for open tag <ul>||</ul>)
* $page refers to the current rootPage for the container of it's inner childs
*/
public function ___getInnerStringOpen($tpl, Page $page){
return $this->parsePlaceholders($tpl, $page);
}
}