From 8d2a1737cc91ea5b39a321ebb2ddd9ae1678e52d Mon Sep 17 00:00:00 2001 From: OlivierRaginel Date: Sun, 24 Oct 2010 17:23:35 +0000 Subject: [PATCH] Item9815: Restore randomness for non-remembering twisties, and append all the time, to try and ensure uniqueness as much as possible git-svn-id: http://svn.foswiki.org/branches/Release01x01@9710 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- TwistyPlugin/data/System/TwistyPlugin.txt | 18 ++++++++++----- .../lib/Foswiki/Plugins/TwistyPlugin.pm | 23 +++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/TwistyPlugin/data/System/TwistyPlugin.txt b/TwistyPlugin/data/System/TwistyPlugin.txt index a161deed25..67c31be1c5 100644 --- a/TwistyPlugin/data/System/TwistyPlugin.txt +++ b/TwistyPlugin/data/System/TwistyPlugin.txt @@ -124,6 +124,8 @@ hideimgright="%ICONURLPATH{toggleclose-small}%" To store the last state in a FOSWIKIPREF cookie, add the parameter =remember="on"=.%BR% To test this, reload the page after toggling. +%X% WARNING: If you really want it to be remembered, best to provide an id with +it, otherwise it might not work. %TWISTY{ showlink="Show..." @@ -162,7 +164,11 @@ my twisty content %GREEN% my twisty content %ENDCOLOR% %ENDTWISTY% -*Note:* Twisty ids are generated automatically based on web and topic names and a counter that increments for each twisty. If you need control over exactly _which_ Twisty should be remembered, add the parameter =id=. Application developers creating e.g. Ajax applications where the fetched content contains a Twisty should always provide an ID. +*Note:* Unless specified, Twisty ids are generated automatically based on web +and topic names. If remember option is set, a counter that increments for each +twisty is appended, otherwise a random value is, so AJAX calls will not kill +JS, as IDs have to be unique. + %TWISTY{ @@ -424,11 +430,11 @@ link="Count: ($percentCALC{$quot$dollarGET(infoCount)$quot}$percent)" ---++ Generation of the twisty ID -When you define the =id= parameter a twisty the actual ID is appended by a number that counts from 1 for each twisty to ensure that the ID is unique. - -If you do not define an =id=, the plugin autogenerates an ID based on web and topic names and a counter that counts from 1 for each twisty. +If you do not define an =id=, the plugin autogenerates an ID based on web and +topic names. -In ajax application you may need to ensure that the ID is unique for each call. For this always define the id parameter in your application to ensure a unique id for each call. +ID is then appended with either a counter that counts from 1 for each +twisty if remember option is set, or a random number (useful for AJAX calls). ---++ Syntax %INCLUDE{VarTWISTY}% @@ -482,7 +488,7 @@ You can override some default settings in the plugin by setting the following [[ | Version: | %$VERSION% | | Release: | %$RELEASE% | | Change History: |   | -| 24 Oct 2010 | 1.6.7 Foswikitask:Item9815: Changed random IDs back to predictable IDs to restore remember feature | +| 24 Oct 2010 | 1.6.7 Foswikitask:Item9815: Changed random IDs back to predictable IDs if remember option is set | | 11 Sep 2010 | 1.6.6 Foswikitask:Item9499: Recoded show/hide animation code for smooth twisties. | | 10 Sep 2010 | 1.6.5 Foswikitask:Item9515: Simplified code that shows/hides twisties. | | 05 Sep 2010 | 1.6.4 Foswikitask:Item9626: Put link class around link and image. | diff --git a/TwistyPlugin/lib/Foswiki/Plugins/TwistyPlugin.pm b/TwistyPlugin/lib/Foswiki/Plugins/TwistyPlugin.pm index 678d95c559..4f12f718b2 100644 --- a/TwistyPlugin/lib/Foswiki/Plugins/TwistyPlugin.pm +++ b/TwistyPlugin/lib/Foswiki/Plugins/TwistyPlugin.pm @@ -68,7 +68,7 @@ sub _exportAnimationSpeed { # add TWISTYANIMATIONSPEED to the html head so # that it may be used in the client JS with # foswiki.getPreference('TWISTYANIMATIONSPEED') - Foswiki::Func::addToZone("head", "TWISTYPLUGIN::META", <<"HERE"); + Foswiki::Func::addToZone( "head", "TWISTYPLUGIN::META", <<"HERE"); HERE @@ -162,11 +162,7 @@ sub _TWISTY { my ( $session, $params, $theTopic, $theWeb ) = @_; _addHeader(); - my $id = $params->{'id'}; - if ( !defined $id || $id eq '' ) { - $params->{'id'} = _createId( $params->{'id'}, $theWeb, $theTopic ); - } - $params->{'id'} .= ++$twistyCount; + $params->{'id'} = _createId( $params->{'id'}, $theWeb, $theTopic ); return _TWISTYBUTTON( $session, $params, $theTopic, $theWeb ) . _TWISTYTOGGLE( $session, $params, $theTopic, $theWeb ); } @@ -208,14 +204,17 @@ sub _ENDTWISTYTOGGLE { sub _createId { my ( $inRawId, $inWeb, $inTopic ) = @_; - my $id; - if ($inRawId) { - $id = $inRawId; + my $id = $inRawId ? $inRawId : "$inWeb$inTopic"; + $id =~ s/\//subweb/go; + + # Ensure uniqueness, or at least try to + my $remember = $params->{'remember'} || $prefRemember; + if ($remember) { + $id .= ++$twistyCount; # For remember } - else { - $id = "$inWeb$inTopic"; + else { # 100 is the number of remembered cookies to avoid clashes + $id .= int( rand(10000) ) + 100; # For AJAX } - $id =~ s/\//subweb/go; return "twistyId$id"; }