Adam Kennedy - Display.Swap-0.09

Documentation | Source

NAME

Display.Swap - Display only one of two sets of elements, and swap between them

SYNOPSIS

  // Some things to show
  <p id='first'>First section</p>
  <p id='second'>Second section</p>
  <p id='foo'>Also in Second section</p>
  
  // Create the swap control
  <script language="JavaScript" type="text/JavaScript">
  var stuff = new Display.Swap( [ "first" ], [ "second", "foo" ] );
  
  // This would be equivalent
  // var stuff = new Display.Swap( "first", [ "second", "foo" ] );
  </script>
  
  // Alternate display whenever this is clicked
  <p><a href="javascript:stuff.swap();">Swap visibility</a></p>

DESCRIPTION

Display.Swap is used to control groups of elements, where you only ever want one of two sets to be show at once. Most of the time, you probably only want one element in each set, but doing it as a set means that we can swap not just one big area, but headers and control links and other ancillary items as well.

METHODS

*/

// FIXME - We're actually completely screwed if we don't have the // Display primitives, so doing this won't help one bit. if ( typeof Display == "undefined" ) Display = {};

/*

new Display.Swap( [ "show", ... ], [ "hide", ... ] );

The Display.Swap constructor takes two arguments. Each argument should be an Array containing zero or more element ids.

An additional shorthand if provided that allows you to specify a single id name as a string instead of an Array, and it will Do What You Mean.

When the object is created it will automatically show all elements in the first list, and hide all those in the second list.

refresh

The refresh method does the mechanics of showing and hiding the appropriate elements. You can call this yourself if a problem has occured and you need to force show/hide of the elements that currently should be.

swap

The swap method will change the set currently showing. The shown list will be hidden, and the hidden list will be shown.

Note that the hiding and showing is done based on the objects understanding of what should be hidden or show, not based on the actually state of the elements.

*/

Display.Swap.prototype.swap = function () { var tmp = this.hiding; this.hiding = this.showing; this.showing = tmp; this.refresh(); };

/*

SUPPORT

For non-support issues or questions, contact the author.

AUTHOR

Adam Kennedy <jsan@ali.as>, http://ali.as/

ACKNOWLEDGEMENTS

Thank you to my employer Phase N http://phase-n.com/ for donating the time to write and maintain this package.

COPYRIGHT

Copyright (c) 2005 Adam Kennedy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the the terms of the Perl dual GPL/Artistic license.

The full text of the license can be found in the LICENSE file included with this package

*/

// JSAN::Concat-compatible dependency header
JSAN.use("Display");
// __CODE__

/*

=head1 NAME

Display.Swap - Display only one of two sets of elements, and swap between them

=head1 SYNOPSIS

  // Some things to show
  <p id='first'>First section</p>
  <p id='second'>Second section</p>
  <p id='foo'>Also in Second section</p>
  
  // Create the swap control
  <script language="JavaScript" type="text/JavaScript">
  var stuff = new Display.Swap( [ "first" ], [ "second", "foo" ] );
  
  // This would be equivalent
  // var stuff = new Display.Swap( "first", [ "second", "foo" ] );
  </script>
  
  // Alternate display whenever this is clicked
  <p><a href="javascript:stuff.swap();">Swap visibility</a></p>

=head1 DESCRIPTION

C<Display.Swap> is used to control groups of elements, where you only ever want
one of two sets to be show at once. Most of the time, you probably only want one
element in each set, but doing it as a set means that we can swap not just one
big area, but headers and control links and other ancillary items as well.

=head1 METHODS

*/

// FIXME - We're actually completely screwed if we don't have the
//         Display primitives, so doing this won't help one bit.
if ( typeof Display == "undefined" ) Display = {};

/*

=head2 new Display.Swap( [ "show", ... ], [ "hide", ... ] );

The C<Display.Swap> constructor takes two arguments. Each argument should
be an C<Array> containing zero or more element ids.

An additional shorthand if provided that allows you to specify a single id
name as a string instead of an C<Array>, and it will Do What You Mean.

When the object is created it will automatically show all elements in the first list,
and hide all those in the second list.

=cut

*/

Display.Swap = function (show, hide) {
	// Allow a single element id
	if ( typeof show == "string" ) show = [ show ];
	if ( typeof hide == "string" ) hide = [ hide ];

	// Initialise the lists
	this.showing = show;
	this.hiding  = hide;

	// Do an initial show/hide
	this.refresh();
}

Display.Swap.VERSION = 0.09;

/*

=head2 refresh

The C<refresh> method does the mechanics of showing and hiding the appropriate
elements. You can call this yourself if a problem has occured and you need to
force show/hide of the elements that currently should be.

=cut

*/

Display.Swap.prototype.refresh = function () {
	// Hide everything that needs hiding
	for ( i in this.hiding ) {
		Display.hideElementById(this.hiding[i]);
	}

	// Show everything that needs showing
	for ( i in this.showing ) {
		Display.showElementById(this.showing[i]);
	}
};

/*

=head2 swap

The C<swap> method will change the set currently showing. The shown list will be
hidden, and the hidden list will be shown.

Note that the hiding and showing is done based on the objects understanding of what
should be hidden or show, B<not> based on the actually state of the elements.

*/

Display.Swap.prototype.swap = function () {
	var tmp      = this.hiding;
	this.hiding  = this.showing;
	this.showing = tmp;
	this.refresh();
};

/*

=head1 SUPPORT


For B<non-support> issues or questions, contact the author.

=head1 AUTHOR

Adam Kennedy <jsan@ali.as>, L<http://ali.as/>

=head1 ACKNOWLEDGEMENTS

Thank you to my employer Phase N L<http://phase-n.com/> for donating the
time to write and maintain this package.

=head1 COPYRIGHT

Copyright (c) 2005 Adam Kennedy. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the the terms of the Perl dual GPL/Artistic license.

The full text of the license can be found in the
LICENSE file included with this package

*/