Skip to content

Commit

Permalink
Item9457:
Browse files Browse the repository at this point in the history
   * putting the button metadata into the class instead of a non-standard data attribute
   * encoding the js code to cope with different quotings and the like
   * don't generate an id for the button if not needed, leftover from the time all buttons were init'ed via their id



git-svn-id: http://svn.foswiki.org/trunk@8616 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Aug 20, 2010
1 parent d1fbcd8 commit 050e182
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
12 changes: 6 additions & 6 deletions JQueryPlugin/data/System/JQueryButton.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="save topic" date="1258378459" format="1.1" reprev="1.2" version="1.2"}%
%META:TOPICINFO{author="micha" comment="save topic" date="1282309264" format="1.1" reprev="2" version="3"}%
%META:TOPICPARENT{name="JQueryPlugin"}%
---+ %TOPIC%
%JQPLUGINS{"button"
Expand All @@ -24,9 +24,9 @@ theme roller. This is independent.
---++ Examples

%BUTTON{"%MAKETEXT{"OK"}%" icon="tick" onclick="alert($(this).text());"}%
%BUTTON{"%MAKETEXT{"Cancel"}%" icon="cross" onclick="return false"}%
%BUTTON{"%MAKETEXT{"Info"}%" icon="information" onclick="return false"}%
%BUTTON{"%MAKETEXT{"Add"}%" icon="add" onclick="return false"}%
%BUTTON{"%MAKETEXT{"Delete"}%" icon="delete" onclick="return false"}%
%BUTTON{"%MAKETEXT{"Globe"}%" icon="world_key" onclick="return false"}%
%BUTTON{"%MAKETEXT{"Cancel"}%" icon="cross" onclick="alert($(this).text());"}%
%BUTTON{"%MAKETEXT{"Info"}%" icon="information" onclick="alert($(this).text());"}%
%BUTTON{"%MAKETEXT{"Add"}%" icon="add" onclick="alert($(this).text());"}%
%BUTTON{"%MAKETEXT{"Delete"}%" icon="delete" onclick="alert($(this).text());"}%
%BUTTON{"%MAKETEXT{"Globe"}%" icon="world_key" onclick="alert('foo')"}%
%CLEAR%
35 changes: 22 additions & 13 deletions JQueryPlugin/lib/Foswiki/Plugins/JQueryPlugin/BUTTON.pm
Expand Up @@ -29,7 +29,7 @@ sub new {
$class->SUPER::new(
$session,
name => 'Button',
version => '1.1',
version => '1.2',
author => 'Michael Daum',
homepage => 'http://foswiki.org/Extensions/JQueryPlugin',
tags => 'BUTTON',
Expand Down Expand Up @@ -66,14 +66,13 @@ sub handleButton {
my $theTitle = $params->{title};
my $theIconName = $params->{icon} || '';
my $theAccessKey = $params->{accesskey};
my $theId = $params->{id};
my $theId = $params->{id} || '';
my $theClass = $params->{class} || '';
my $theStyle = $params->{style} || '';
my $theTarget = $params->{target};
my $theType = $params->{type} || 'button';

$theId = 'jqButton' . Foswiki::Plugins::JQueryPlugin::Plugins::getRandom()
unless defined $theId;
$theId = "id='$theId'" if $theId;

my $theIcon;
$theIcon =
Expand All @@ -99,25 +98,29 @@ sub handleButton {
Foswiki::Func::normalizeWebTopicName( $theWeb, $theTarget );
$url = Foswiki::Func::getViewUrl( $web, $topic );
}
$theOnClick .= ";window.location.href='$url';";
$theOnClick .= ";window.location.href=\"$url\";";
}

if ( $theType eq 'submit' ) {
$theOnClick .= ";jQuery(this).parents('form:first').submit();";
$theOnClick .= ';jQuery(this).parents("form:first").submit();';
}
if ( $theType eq 'save' ) {
$theOnClick .=
";var form = jQuery(this).parents('form:first'); if(typeof(foswikiStrikeOne) == 'function') foswikiStrikeOne(form[0]); form.submit();";
';var form = jQuery(this).parents("form:first"); if(typeof(foswikiStrikeOne) == "function") foswikiStrikeOne(form[0]); form.submit();';
}
if ( $theType eq 'reset' ) {
$theOnClick .= ";jQuery(this).parents('form:first').resetForm();";
$theOnClick .= ';jQuery(this).parents("form:first").resetForm();';
Foswiki::Plugins::JQueryPlugin::Plugins::createPlugin('Form');
}
if ( $theType eq 'clear' ) {
$theOnClick .= ";jQuery(this).parents('form:first').clearForm();";
$theOnClick .= ';jQuery(this).parents("form:first").clearForm();';
Foswiki::Plugins::JQueryPlugin::Plugins::createPlugin('Form');
}

my @class = ();
push @class, 'jqButton';
push @class, $theClass if $theClass;

my @callbacks = ();
if ($theOnClick) {
$theOnClick =~ s/;$//;
Expand All @@ -128,11 +131,17 @@ sub handleButton {
if $theOnMouseOver;
push @callbacks, "onmouseout:function(){$theOnMouseOut}" if $theOnMouseOut;

my $callbacks = join( ', ', @callbacks );
$callbacks = 'data="{' . $callbacks . '}"' if $callbacks;
if (@callbacks) {
my $callbacks = '{' . join( ', ', @callbacks ) . '}';

# entity encode
$callbacks =~
s/([[\x01-\x09\x0b\x0c\x0e-\x1f"%&'*<=>@[_])/'&#'.ord($1).';'/ge;
push @class, $callbacks;
}
my $class = join( ' ', @class );

my $result =
"<a id='$theId' class='jqButton $theClass' $callbacks href='$theHref'";
my $result = "<a $theId class='$class' href='$theHref'";
$result .= " accesskey='$theAccessKey' " if $theAccessKey;
$result .= " title='$theTitle' " if $theTitle;
$result .= " style='$theStyle' " if $theStyle;
Expand Down
Expand Up @@ -2,7 +2,7 @@ jQuery(function($) {
$(".jqButton:not(.jqInitedButton)").livequery(function() {
var $this = $(this), options;
$this.addClass("jqInitedButton");
options = $.extend({}, $this.metadata({type:'attr', name:'data'}));
options = $.extend({}, $this.metadata());
if (options.onclick) {
$this.click(function() {
return options.onclick.call(this);
Expand Down

0 comments on commit 050e182

Please sign in to comment.