Opened 4 years ago

# CollapsiblePlugin does not work from report

Reported by: Owned by: kenclary@… codingking normal CollapsiblePlugin normal 1.0

### Description

When writing a custom report, you can use the "description" column to display wikiformatted text (e.g. "description AS description"). This performs wiki formatting, including macro expansion.

However, this collapsible macro does not work in that context.

It would appear that the report page does not load folding.js, nor does it call $(".foldable").enableFolding(true, true). I suspect that is the source of the problem. (Having collapsable sections in report result tables would be very nice.) ### Attachments (0) ### Change History (13) ### comment:1 Changed 4 years ago by Ryan J Ollos There's a proposed solution in my GitHub branch. You can grab it through: svn co https://github.com/rjollos/trachacks/branches/traccollapsible/collapsibleplugin/0.12  Please let me know if it's working well for you. If it is working well, and codingking is okay with me pushing the changes, then I'll commit them to collapsibleplugin/0.12. Future improvements could include: • Allowing autofold and snap to be specified as arguments. • Make this work at heading levels other than h3. • Making this function also as a WikiProcessor. ### comment:2 Changed 4 years ago by codingking Hey, thanks for taking a look at this. I appreciate it. I should be able to test it out on Friday. In the future if you want to collaborate on developing the plugin on Github, the "official" repo is here (I'm manoyes on Github): ### comment:3 Changed 4 years ago by kenclary@… The prospective version works for my immediate needs. ### comment:4 Changed 4 years ago by Ryan J Ollos There's one small change we might want to make. I think we don't need the conditional enclosing add_script since the use of a set will prevent the script from being added to ant pages more than once. ### comment:5 Changed 4 years ago by kenclary@… upon further testing... The change appears to have fixed folding sections in reports, but caused them to freeze shut in wiki pages. (On both Chrome and Firefox, at least.) ### comment:6 follow-up: 7 Changed 4 years ago by kenclary@… I suspect the new issue is caused by enableFolding being called on the same object more than once. Which is probably a bug with enableFolding, though I'm sure there's a workaround. ### comment:7 in reply to: 6 Changed 4 years ago by Ryan J Ollos Replying to kenclary@…: I suspect the new issue is caused by enableFolding being called on the same object more than once. Which is probably a bug with enableFolding, though I'm sure there's a workaround. enableFolding adds an anchor to each link (no1, no2, ...) so it is clear that it wasn't designed to be called more than once on a page. I tried a few different ways to work around the issue that enableFolding can't be called more than once on a page, eventually setting on this change. If the other Trac developers agree, I might add the feature to Trac in a forthcoming release (albeit in a different form that CollapsiblePlugin, instead using WikiProcessor arguments). Last edited 4 years ago by Ryan J Ollos (previous) (diff) ### comment:8 follow-up: 9 Changed 4 years ago by kenclary@… I was able to fix my issues with the below patch. The particular problem I was having was that .enableFolding keeps repeatedly binding a click handler, so if called more than once, you have more than one function handling clicks. In this case, if you've called .enableFolding an even number of times, clicks appear to do nothing (by rapidly unfolding and refolding the section). As best I can tell, it manages to not re-add the anchor each time. So, if one were looking to just patch enableFolding, having it be "trigger.unbind("click").click(...)" should solve the problem. Index: traccollapsible/collapsible.py =================================================================== --- traccollapsible/collapsible.py (revision 109) +++ traccollapsible/collapsible.py (working copy) @@ -33,7 +33,7 @@ add_script(formatter.req, folding_chrome_path) add_script(formatter.req, 'collapsible/collapsible.js') - return '<div><h3 class="foldable">%s</h3><div>' % content + return '<div><h3 class="foldable collapsibleplugin">%s</h3><div>' % con tent def get_htdocs_dirs(self): from pkg_resources import resource_filename Index: traccollapsible/htdocs/collapsible.js =================================================================== --- traccollapsible/htdocs/collapsible.js (revision 109) +++ traccollapsible/htdocs/collapsible.js (working copy) @@ -1,3 +1,18 @@ +(function($){
+
+  $.fn.unbindFolding = function() { + return this.each(function() { + // Use first child <a> as a trigger + var trigger =$(this).children("a").eq(0);
+      if (trigger.length != 0) {
+        trigger.unbind("click");
+      }
+    });
+  }
+
+})(jQuery);
+
jQuery(document).ready(function($) { -$('div h3.foldable').enableFolding(true, false);
+    $('div h3.collapsibleplugin').unbindFolding(); +$('div h3.collapsibleplugin').enableFolding(true, false);
});


### comment:9 in reply to:  8 Changed 4 years ago by Ryan J Ollos

As best I can tell, it manages to not re-add the anchor each time.

The issue I saw was not with re-adding the anchor. The issue is when a second call to enableFolding adds a link element, it creates it with id=no1, restarting the numbering. Having duplicated id elements is violation of the HTML spec. This is in addition to the problem you noted about the folding behavior not functioning correctly. I mentioned the issue with the id attributes because it makes it clear that enableFolding was not designed to be called more than once on a page.

Anyway, your patch will mess-up the "snap-to" behavior on several pages. Whether you care about that or not I don't know.

Last edited 4 years ago by Ryan J Ollos (previous) (diff)

### comment:10 follow-up:  11 Changed 4 years ago by kenclary@…

Anyway, your patch will mess-up the "snap-to" behavior on several pages. Whether you care about that or not I don't know.

As it turns out, on the pages where I'm using the plugin, there are no pre-existing uses of folding (that use snap-to, at least). In the short term, I have a working solution.

However, I'm still interested in seeing a cleaner implementation of the feature --- both to fix my bug and to allow for use-by-use control of the starting folded state and snap-to. From what I can tell, that requires either a significant rewrite of trac's built-in folding feature, or the plugin rolling its own folding code.

### comment:11 in reply to:  10 Changed 4 years ago by Ryan J Ollos

As it turns out, on the pages where I'm using the plugin, there are no pre-existing uses of folding (that use snap-to, at least). In the short term, I have a working solution.

I think the changes I posted earlier today avoid the bug.

However, I'm still interested in seeing a cleaner implementation of the feature --- both to fix my bug and to allow for use-by-use control of the starting folded state and snap-to. From what I can tell, that requires either a significant rewrite of trac's built-in folding feature, or the plugin rolling its own folding code.

Setting the folding state should be possible with selective application of the collapsed class. You can see how its done on the ticket page. I think the best course would be to just integrate the feature in to Trac, so if you are interested in helping with that we can prepare the changes in trac:#11550.

Version 0, edited 4 years ago by Ryan J Ollos (next)

### comment:12 Changed 4 years ago by kenclary@…

As it turns out, on the pages where I'm using the plugin, there are no pre-existing uses of folding (that use snap-to, at least). In the short term, I have a working solution.

I think the changes I posted earlier today avoid the bug.

I just tested with your earlier changes. I note 2 problems:

1. If I use the macro in a report view for a report that also has dynamic variables (which cause the report to show a foldable section for the dynamic variable setting), my report-generated sections don't fold. (They work correctly on reports without dynamic variables.)
2. On regular wiki pages, which call .enableFolding(true, true) on every .foldable at document ready, the plugin works, but always has snap. (Whereas the version from my patch added them without snap, which I've decided I like better.)

Setting the folding state should be possible with selective application of the collapsed class.

I believe you also need to bind a click handler, yes?

I think the best course would be to just integrate the feature into Trac, so if you are interested in helping with that we can prepare the changes in trac:#11550.

I am interested in helping (though I may continue to use my less-than-ideal patch in the meantime).

### comment:13 Changed 4 years ago by kenclary@…

Of note: I was able to do a cleaner local modification. It is still not perfect (it relies on any .enableFolding calls having happened before, not afterwards, but that seems to be the case for instances where trac itself is adding them).

• ## traccollapsible/collapsible.py

 def expand_macro(self, formatter, name, content): # Make sure we don't call enableFolding more than once on a page. folding_chrome_path = 'common/js/folding.js' if folding_chrome_path not in formatter.req.chrome['scriptset']: add_script(formatter.req, folding_chrome_path) add_script(formatter.req, 'collapsible/collapsible.js') #folding_chrome_path = 'common/js/folding.js' #if folding_chrome_path not in formatter.req.chrome['scriptset']: #    add_script(formatter.req, folding_chrome_path) add_script(formatter.req, 'collapsible/collapsible.js') return '
• ## traccollapsible/htdocs/collapsible.js

 (function($){$.fn.enableCollapsiblePlugin = function() { return this.each(function() { var trigger = $(this).children("a").eq(0); var contents; if (trigger.length == 0) { contents =$(this).html(); $(this).text(""); } else { contents = trigger.html(); trigger.remove(); } trigger =$(""); trigger.html(contents); $(this).append(trigger); trigger.unbind("click").click(function() { var div =$(this.parentNode.parentNode).toggleClass("collapsed"); return !div.hasClass("collapsed"); }); trigger.parents().eq(1).addClass("collapsed"); }); } })(jQuery); jQuery(document).ready(function($) {$('div h3.foldable').enableFolding(true, false); \$('div h3.collapsibleplugin').enableCollapsiblePlugin(); });
Last edited 4 years ago by Ryan J Ollos (previous) (diff)

### Modify Ticket

Change Properties