Code: Select all
category 1; data 1
item 1
item 2
category 2; data 2
item 3
item 4
category 3; data 3
item 5
item 6
Thanks in advance!
Code: Select all
category 1; data 1
item 1
item 2
category 2; data 2
item 3
item 4
category 3; data 3
item 5
item 6
Code: Select all
$menu = <<<MENU
A1
B1;;:paper
B2
Ca1
B3
Cb1;;:paper;2
Cb2;;;4
-
Cb3;Cb3Data;;3
-
B4
A2;;:minitree
-
A3
MENU;
text popupnested($menu);
Code: Select all
/*popupnestedpath.xyi|v0.9.7*/
FUNCTION popupnestedpath($itemlist, $x=-1, $y=-1, $start=1, $count=-1, $flags=0, $sep_itemlist="|", $sep_item=";", $on_cancel="", $sep_path="\", $data=0) {
// A wrapper to popupnested() that returns menu path to the picked menuitem
// $* all params are equivalent to popupmenu(), except $flags
// $flags is hardcoded to 1 in function body because we cannot work without its indexed return.
// $sep_path separator in returned path. Default is "\"
// $data 0 default behavior - that is, return data, if not available then return caption
// 1 return caption
// 2 return data
// -1 return both
$return = ""; // holds returned path
$pattern = "^(\s|\t)*"; // submenu indentation pattern
$index = popupnested($itemlist, $x, $y, $start, $count, 1, // run popupnested() with provided params and get picked index
$sep_itemlist, $sep_item, $on_cancel);
if ($index == $on_cancel) { return $on_cancel; } // popupnested() returned $on_cancel, assume nothing picked
$index = $index + ($start>1 ? $start-1 : 0); // adjust returned index if $start defined ($start is 1-based)
$first = strlen(regexmatches(gettoken($itemlist, 1, $sep_itemlist), $pattern)); // record root indentation
$pathstr = gettoken($itemlist, $index, $sep_itemlist); // get picked item from $itemlist
$indent = strlen(regexmatches($pathstr, $pattern)); // record picked item's indentation
$pathstr = popupnestedpath_extractdatacaption($pathstr, $sep_item, $data);
$return = substr($pathstr, $indent); // start path construction with picked item's text
if ($indent == $first) { // shortcut logic when picked item is at root level
$pathstr = substr(gettoken($itemlist, $index, $sep_itemlist), $indent); // in that case, simply (process and) return the picked item
$return = popupnestedpath_extractdatacaption($pathstr, $sep_item, $data);
} else { // else continue with part construction
$line = $index; // line counter for the while() loop
while ($line >= $start && $indent > $first) { // loop thru $itemlist lines upwards from picked item till start index set by $start
$loopline = gettoken($itemlist, $line, $sep_itemlist); // record current line in loop
$loopindent = strlen(regexmatches($loopline, $pattern)); // record indent of current line
if ($loopindent < $indent) { // IF (this logic) THEN current line is parent to previously recorded path item
$indent = $loopindent; // set indent to current indent - this skips siblings with same indent as current
$pathstr = substr($loopline, $indent); // strip indent from current line
$pathstr = popupnestedpath_extractdatacaption($pathstr, $sep_item, $data);
$return = $pathstr . $sep_path . $return; // prepend parent text to return path
}
$line--; // move line index up so next loop processes the line above current in $itemlist
}
}
return $return;
}
// helper function to get data and/or caption from menuitem definition
FUNCTION popupnestedpath_extractdatacaption($str, $sep, $mode) {
if ($mode == -1) {
return $str;
} elseif ($mode == 0) {
return (strpos($str, $sep) > -1) ? gettoken($str, 2, $sep) : $str;
} else {
return gettoken($str, $mode, $sep);
}
}
Code: Select all
INCLUDE "_inc\popupnestedpath.xyi";
// define a variable with the menu structure
$itemlist = <<<#MENUSTRUCT
category 1;data 1
item 1
item 2
category 2;data 2
item 3
item 4
category 3;data 3
item 5
item 6
category 3.1;data 3.1
item 7
item 8
item 9
item 10#MENUSTRUCT;
text popupnestedpath($itemlist,,, /*start*/,,, /*sep_itemlist*/ <crlf>,,, /*sep_path*/ '|');
Code: Select all
pick | return
---------|------------------------------------------------
item 8 category 3;data 3|category 3.1;data 3.1|item 8
item 4 category 2;data 2|item 4
item 10 item 10
Code: Select all
category 3;data 3
item 5
item 6
category 3.1;data 3.1 < bad indent
item 7 < bad indent
item 8 < bad indent