From 5551a36b8f059aa03a371fb5a851c116d305fccb Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 13 Mar 2013 10:47:15 +0100 Subject: [PATCH] ClientJS: jQuery-Funktionen addClass, removeClass, toggleClass; Utility-Funktion "action_if"; Doku --- SL/ClientJS.pm | 44 +++++++++++++++++++++++++++++++++++++++++++- js/client_js.js | 5 +++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/SL/ClientJS.pm b/SL/ClientJS.pm index 51f663fa5..1218e4cc5 100644 --- a/SL/ClientJS.pm +++ b/SL/ClientJS.pm @@ -58,6 +58,11 @@ my %supported_methods = ( removeProp => 2, val => 2, + # Class attribute + addClass => 2, + removeClass => 2, + toggleClass => 2, + # Data storage data => 3, removeData => 2, @@ -123,6 +128,12 @@ sub action { return $self; } +sub action_if { + my ($self, $condition, @args) = @_; + + return $condition ? $self->action(@args) : $self; +} + sub init__actions { return []; } @@ -338,7 +349,38 @@ Instead of: The first variation is obviously better suited for chaining. -Additional functions: +=over 4 + +=item C + +Call the function with the name C<$method> on C<$self> with arguments +C<@args>. Returns the return value of the actual function +called. Useful for chaining (see above). + +=item C + +Call the function with the name C<$method> on C<$self> with arguments +C<@args> if C<$condition> is trueish. Does nothing otherwise. + +Returns the return value of the actual function called if +C<$condition> is trueish and C<$self> otherwise. Useful for chaining +(see above). + +This function is equivalent to the following: + + if ($condition) { + $obj->$method(@args); + } + +But it is easier to integrate into a method call chain, e.g.: + + $js->html('#content', $html) + ->action_if($item->is_flagged, 'toggleClass', '#marker', 'flagged') + ->render($self); + +=back + +=head2 ADDITIONAL FUNCTIONS =over 4 diff --git a/js/client_js.js b/js/client_js.js index 031b5c8f4..b961f6591 100644 --- a/js/client_js.js +++ b/js/client_js.js @@ -71,6 +71,11 @@ function eval_json_result(data) { else if (action[0] == 'removeProp') $(action[1]).removeProp(action[2]); else if (action[0] == 'val') $(action[1]).val(action[2]); + // Class attribute + else if (action[0] == 'addClass') $(action[1]).addClass(action[2]); + else if (action[0] == 'removeClass') $(action[1]).removeClass(action[2]); + else if (action[0] == 'toggleClass') $(action[1]).toggleClass(action[2]); + // Data storage else if (action[0] == 'data') $(action[1]).data(action[2], action[3]); else if (action[0] == 'removeData') $(action[1]).removeData(action[2]); -- 2.20.1