Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote' (or anywhere)

by tye (Sage)
on Dec 17, 2006 at 22:14 UTC ( [id://590356]=note: print w/replies, xml ) Need Help??


in reply to Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote'
in thread Have parent 'Node ID' as link in 'Nodes You Wrote' node

I think that if you avoid overgeneralizing and just hard code "redir to root" and "redir to parent", then the code can become much simpler, you don't need to depend on AJAX components (so it is easier to make work more places), and you avoid waiting for one round-trip to the server.

Actually, the amount of hard-coding you need to do would be minimal, just a case statement on the fields that make sense, with the Free Nodelet templatung filling in values for you.

Just check if "redir" parameter was specified, then switch on the parameter's value to redirect to the appropriate next field (not including the "redir" parameter, of course). And then add an on-load function to use the DOM to transform links of type ?node_id=... to be three links, the original plus one that includes ;redir=parent and one that includes ;redir=root. It seems like this strategy even makes it easy to provide the feedback that you note you didn't include.

Okay, here is a working redirector that adds links to your Free Nodelet that will send you to the grandparent of a reply or to the author of the node that this node was in reply to:

<script type="text/javascript"><!-- var params= window.location.search; if( 0 <= params.indexOf( ";_redir=root" ) ) { window.location.search= "?node_id=`root_id`"; } else if( 0 <= params.indexOf( ";_redir=parent" ) ) { window.location.search= "?node_id=`parent_id`"; } else if( 0 <= params.indexOf( ";_redir=author" ) ) { window.location.search= "?node_id=`author_id`"; } --></script> <a href="?node_id=`parent_id`;_redir=author">Who replied to</a> <a href="?node_id=`parent_id`;_redir=parent">Grandparent</a>

The on-load code to transforms node_id= links is left as an exercise.

It might even make sense to have PM directly support redir=parent (and perhaps redir=root and redir=author) to allow both Free Nodelet hackers and pmdev members to construct "efficient" "indirect" links... Note that I've used _redir= in my javascript code above in order to future-proof against such a redir= feature being provided by PM.

- tye        

  • Comment on Re: Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote' (or anywhere)
  • Download Code

Replies are listed 'Best First'.
Re^2: Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote' (or anywhere)
by ikegami (Patriarch) on Dec 17, 2006 at 23:17 UTC

    Your solution (redir argument solution) occured to me, but I wanted some experience with XMLHttpRequest and XPath. I intended to implement the redir argument solution at a later point.

    There are downside to the redir argument solution (until it's handled by PM natively). It still loads two pages. Its intermediary page is slower to generate, transmit and parse. And the user sees its intermediary pages.

Re^2: Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote' (or anywhere)
by jdporter (Paladin) on Dec 22, 2006 at 14:19 UTC

    I haven't tried modifying your code above, but I point out that parameter names with leading underscores are already handled for you by the Free Nodelet templating. So instead of

    if ( params.indexOf( ";_redir=root" ) > 0 )
    you should be able to write
    if ( `_redir` == "root" )
    We're building the house of the future together.
      if ( `_redir` == "root" )

      No, that'd turn into one of the following, either:

      if ( root == "root" )

      or

      if ( `_redir` == "root" )

      neither of which looks like valid javascript to me. So you'd want something more like:

      if ( "`_redir`" == "root" )

      which would induce a syntax error if somehow you got a quote (or perhaps a trailing backslash) in your parameter value.

      There needs to be an escape specifier for use in javascript strings. Perhaps:

      if ( "`_redir\`" == "root" )

      where that trailing \ tells the Free Nodelet templating to apply quotemeta to the parameter value (not implemented yet). I haven't checked exactly how escaping works in javascript strings, so quotemeta might not be appropriate. Perhaps `_redir"` would surround it in quotes and escape as appropriate.

      - tye        

        root is a URI component, so it could be escaped.

        if ( decodeURIComponent(`_redir"`) == "root" )

        I've discussed the need for a variables returned as JavaScript literals before. I even provided a workaround.

        if ( decodeURIComponent(decodeURIComponent('`_redir%`')) == "root" )
        if ( decodeURIComponent( '`_redir%`' ) == "root" )

Re^2: Free Nodelet Hack: Add parent & root links to nodes in 'Nodes You Wrote' (or anywhere)
by ikegami (Patriarch) on Dec 20, 2006 at 04:40 UTC
    I just found a very good reason to use the redir argument solution: The ability to open the link in a different tab or window.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590356]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found