get parent content in popupnested()

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

get parent content in popupnested()

Post by SkyFrontier »

Is there a way I'm missing so popupnested() could return both data AND category (data or caption) it belongs to at the same time?

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
...so if I pick 'item 5' I can also retrieve 'DATA 3' ?
Thanks in advance!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

rur54
Posts: 46
Joined: 12 Feb 2015 19:52

popupnested - get all menu names as path?

Post by rur54 »

Hi,
I came across "popupnested" and have been trying it out but can not figure out how to get entire manu path.

For example if I click on Cb2, I would like to get entire path (all parents and child) like A1\B3\Cb2.
Any way to get that path?

Thank you all.

from help..

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);

highend
Posts: 9547
Joined: 06 Feb 2011 00:33

Re: popupnested request

Post by highend »

Merged. Closely related
update: edited title & menu text in codeview - ss
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

SammaySarkar
Posts: 4235
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: get parent content in popupnested()

Post by SammaySarkar »

Made a wrapper function to popupnested(). It's only a rough draft a few versions above a rough draft.

CODE

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);
  }
}

[/size]

DEMO
  • save function to <xyscripts>\_inc\popupnestedpath.xyi.
  • code:

    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*/ '|');
  • returns:

    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
NOTES
  • can return incorrect path if source indentation is mixed or not strictly valid. Even if the menu looks okay. For example:

    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
    This may also happen when $start is defined such that the menu starts from a sublevel but still has higher level items (and one of those highlevel items are selected).
edit: removed an unnecessarily duplicated gettoken() call. added logic to handle cancelled return.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

rur54
Posts: 46
Joined: 12 Feb 2015 19:52

Re: get parent content in popupnested()

Post by rur54 »

@SammaySarkar
Thank you! Works well so far.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: get parent content in popupnested()

Post by SkyFrontier »

...kool, Sammay.
:blackstorm:

Pretty kool.
:maf:

Now I'll have to rewrite my entire beta 3, fully functional, error-handling-only-pending super-script I've waited for, what?, 5 years now?, so I can have a better interface for copyto\moveto\bkpto ops.
:evil:

...err.
Are you willing to HTML-something so bkpto ops get a better user end input? Input() multiline is ultimate lame, and it's the best I could cope with so far... :roll:
:mrgreen:

Thanks, man! Ultra again. Integration, step 1 is looking sharp & pretty.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SammaySarkar
Posts: 4235
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: get parent content in popupnested()

Post by SammaySarkar »

Thanks mate.
HTML control panel? Sure.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: get parent content in popupnested()

Post by SkyFrontier »

You rock, Sammay!
Fully integrated.
I'll mail you the base code, then. You know the scheme... :wink:

Many thanks!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: get parent content in popupnested()

Post by SkyFrontier »

extra-thanks: this is allowing me to work towards a multi-level rating system, which will ultimately give me freedom to develop a custom text-based XY categorization (thus portable, like smartFavs already does) method to plug it with.

Superb, Sammay!

:tup:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SammaySarkar
Posts: 4235
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: get parent content in popupnested()

Post by SammaySarkar »

:D Thank you! Glad to help.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Post Reply