Skip to content

Commit

Permalink
Item9815: Restore randomness for non-remembering twisties, and append…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
OlivierRaginel authored and OlivierRaginel committed Oct 24, 2010
1 parent f6d8fb5 commit 8d2a173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
18 changes: 12 additions & 6 deletions TwistyPlugin/data/System/TwistyPlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<verbatim class="tml">
%TWISTY{
showlink="Show..."
Expand Down Expand Up @@ -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.


<verbatim class="tml">
%TWISTY{
Expand Down Expand Up @@ -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}%
Expand Down Expand Up @@ -482,7 +488,7 @@ You can override some default settings in the plugin by setting the following [[
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 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. |
Expand Down
23 changes: 11 additions & 12 deletions TwistyPlugin/lib/Foswiki/Plugins/TwistyPlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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");
<meta name="foswiki.TWISTYANIMATIONSPEED" content="$pref" />
HERE

Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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";
}

Expand Down

0 comments on commit 8d2a173

Please sign in to comment.