|
|
||||
JQueryAjaxHelperThis page contains a set of example JQueryPlugin helpers that can be used in wiki applications. The examples demonstrate how you can dynamically populate autocomplete enabled input boxes using JQueryUIAutocomplete. Examples are in two parts; a Client section and a Server section. The Client section is designed to be included into your topics using%INCLUDE. The Server section is expanded in
response to an AJAX request from a jQuery plugin.
This page also acts as a library of some commonly-used form inputs. For
example, the Topic selector example can be used
in your own topics to define an HTML form input with the name newtopic
by putting this in your topic:
%INCLUDE{
"%SYSTEMWEB%.JQueryAjaxHelper"
section="topicselector"
INPUT_NAME="newtopic"
EXCLUDE="Web*"
}%
On this page:
Note that the examples below are implemented
using base Foswiki functionality. There are a range of plugins, such as
Foswiki:Extensions.RenderPlugin, Foswiki:Extensions.FlexWebListPlugin and
Foswiki:Extensions.DBCachePlugin that can be used to simplify or improve
these sections when building up your own library of input types. Some
examples of the use of these plugins are given at the end of this topic.
Topic selectorSelect a topic in the current web. JQueryUIAutocomplete is given a url that loads thetopic section of this topic, which expands to the topic list.
Client sectionINCLUDE Parameters
%STARTSECTION{"topicselector"}%<!-- topicselector -->
<input type="text" class="foswikiInputField" name="%INPUT_NAME%" id="%INPUT_NAME%" autocomplete="%SCRIPTURL{"view"}%/%SYSTEMWEB%/JQueryAjaxHelper?section=topic;skin=text;contenttype=application/json;baseweb=%BASEWEB%;%IF{"defined EXCLUDE" then="exclude=%EXCLUDE%"}%" size="60" value="%IF{"defined INPUT_VALUE" then="%INPUT_VALUE%"}%" />
<!-- //topicselector -->%ENDSECTION{"topicselector"}%
Server sectionURL parameters
%STARTSECTION{"topic"}%[%SEARCH{
"^%URLPARAM{"term" default="does not exist"}%.*"
type="regex"
scope="topic"
format="<nop>%URLPARAM{"format" encode="quote" default="{\\"label\\":\\"$topic\\", \\"webtopic\\":\\"$web.$topic\\"}"}%"
separator=","
limit="%URLPARAM{"limit" default="10"}%"
nonoise="on"
excludetopic="%URLPARAM{"exclude"}%"
web="%URLPARAM{"baseweb" default="%BASEWEB%"}%"
}%]%ENDSECTION{"topic"}%
ExampleWeb selectorSelect from a list of webs, using autocompleteClient sectionINCLUDE Parameters
%STARTSECTION{"webselector"}%<!-- webselector -->
<input type="text" class="foswikiInputField" name="%INPUT_NAME%" id="%INPUT_NAME%" autocomplete="%SCRIPTURL{"view"}%/%SYSTEMWEB%/JQueryAjaxHelper?section=web;skin=text;contenttype=application/json;%IF{"defined EXCLUDE" then="exclude=%EXCLUDE%"}%" size="60" value="%IF{"defined INPUT_VALUE" then="%INPUT_VALUE%"}%" />
<!-- //webselector -->%ENDSECTION{"webselector"}%
Server sectionURL parameters
%STARTSECTION{"web"}%%FLEXWEBLIST{
include="^%URLPARAM{"term" default="does not exist"}%.*"
ignorecase="on"
limit="%URLPARAM{"limit" default="10"}%"
header="["
format="\"<nop>$web\""
footer="]"
separator=", "
subheader=", "
subfooter=" "
}%%ENDSECTION{"web"}%
ExampleJump boxJump to specific topics.Client sectionINCLUDE Parameters
%STARTSECTION{"jumpbox"}%<!-- jumpbox -->
<input type="text" class="foswikiInputField jqJumpBox {%IF{
"$GLOBAL = 'on'"
then="global: true, baseweb:'all, -%TRASHWEB%', format:'"$web.$topic"'"
else="global: false, baseweb:'%BASEWEB%', format:'"$topic"'"
}%}" name="topic" autocomplete="off" size="60" />
%ADDTOZONE{"script"
tag="jumpbox::js"
section="jumpbox::js"
requires="JQUERYPLUGIN::UI::AUTOCOMPLETE"
}%
<!-- //jumpbox -->%ENDSECTION{"jumpbox"}%
The following section is used by the section above; it adds a special Javascript function to the page.
%STARTSECTION{"jumpbox::js"}%<literal>
<script type="text/javascript">
jQuery(function($){
$(".jqJumpBox").each(function() {
var $this = $(this),
opts = $.extend({}, $this.metadata());
$this.autocomplete({
source: foswiki.getPreference('SCRIPTURL') + '/view/' + foswiki.getPreference('SYSTEMWEB') + "/JQueryAjaxHelper?section=topic;skin=text;contenttype=application/json;baseweb="+opts.baseweb+";format="+opts.format,
select: function(event, ui) {
window.location.href=foswiki.getPreference('SCRIPTURL') + '/view/'+(opts.global?'':opts.baseweb+'/')+ui.item.value;
}
});
});
});
</script>
</literal>%ENDSECTION{"jumpbox::js"}%
Server sectionUses thetopic server section, defined above.
ExamplesJump to a topic in the current webJump to a topic in any webUser selector$Foswiki::cfg{AntiSpam}{HideUserDetails}), then this example will not work unless you are an administrator.Client sectionINCLUDE Parameters
%STARTSECTION{"userselector"}%<!-- userselector -->
<input type="text" class="foswikiInputField jqUserSelector" name="%INPUT_NAME%" id="%INPUT_NAME%" autocomplete="%SCRIPTURL{"view"}%/%SYSTEMWEB%/JQueryAjaxHelper?section=user;skin=text;contenttype=application/json" size="60" value="%IF{"defined INPUT_VALUE" then="%INPUT_VALUE%"}%" />
%ADDTOZONE{"script"
tag="userselector::js"
section="userselector::js"
requires="JQUERYPLUGIN::UI::AUTOCOMPLETE"
}%
<!-- //userselector -->%ENDSECTION{"userselector"}%
The following section is used by the section above; it adds a special Javascript function to the page.
%STARTSECTION{"userselector::js"}%<literal>
<script type="text/javascript">
jQuery(function($){
$(".jqUserSelector").data("autocomplete")._renderItem = function(ul, item) {
if (typeof(item.label) !== 'undefined') {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a><table width='100%'><tr><td width='60px'><img width='50' src='"+item.img+"' /></td><td>"+item.label+"<br />"+item.email+"</td></tr></table></a>")
.appendTo(ul);
}
};
});
</script>
</literal>%ENDSECTION{"userselector::js"}%
Server sectionURL parameters
%STARTSECTION{"user"}%<literal>%FORMAT{
"%SEARCH{
"^%URLPARAM{"term" default="does not exist"}%.*"
type="regex"
scope="topic"
format="$percntIF{\"'$percntGROUPINFO{\"$topic\" format=\"$dollarname, \" limit=\"1\"}$percnt'=''\"
then=\"$percntUSERINFO{\"$topic\" format=\"$dollarwikiname, \"}$percnt\"
else=\"$percntGROUPINFO{\"$topic\" format=\"$dollarname, \" limit=\"1\"}$percnt\"
}$percnt"
separator=""
limit="%URLPARAM{"limit" default="10"}%"
nonoise="on"
web="%USERSWEB%"
}%"
type="string"
header="[$n"
format=" {\"label\":\"<nop>$item\",
\"email\":\"$percntUSERINFO{\"$item\" format=\"$emails\"}$percnt\",
\"img\":\"$percntFORMFIELD{\"Photo\" topic=\"%USERSWEB%.$item\" default=\"%PUBURLPATH%/%SYSTEMWEB%/JQueryPlugin/images/nobody.gif\" alttext=\"%PUBURLPATH%/%SYSTEMWEB%/JQueryPlugin/images/nobody.gif\" }$percnt\"
}"
separator=",$n"
footer="$n]"
}%
</literal>%ENDSECTION{"user"}%
ExampleQuery fetcherPerform a%QUERY asynchronously (requires Foswiki 1.1
or later).
Client sectionINCLUDE Parameters
%STARTSECTION{"queryfetcher"}%%ADDTOZONE{"script"
tag="query::js"
requires="JQUERYPLUGIN"
text ="<literal>
<script type=\"text/javascript\">
jQuery(function($){
$(\"#%ID%\").click(function() {
$.get(foswiki.getPreference('SCRIPTURL') + '/view/' + foswiki.getPreference('SYSTEMWEB') + '/JQueryAjaxHelper?section=query;source=%SOURCE%;query=%QUERY%;skin=text',
function(data) {
// This is the function that will be executed when the
// data is fetched
alert(\"%QUERY% is '\" + data + \"'\");
});
});
});
</script></literal>
"}%%ENDSECTION{"queryfetcher"}%
Server sectionURL parameters
%STARTSECTION{"query"}%<noautolink>%QUERY{
"%URLPARAM{"query" default="query missing"}%"
topic="%URLPARAM{"source" default="source missing"
style="json"}%"
}%</noautolink>%ENDSECTION{"query"}%
Exampleto get the parent of this topic asynchronously (should be 'JQueryPlugin')Other selectorsServer sections for some other selectors that are common in wiki applications.FormfieldFetch a formfield from a topic asynchronously. Uses FORMFIELD, which is deprecated in Foswiki 1.1. Foswiki 1.1 and later should use QUERY URL Parameters
Server section
%STARTSECTION{"formfield"}%%FORMFIELD{
"%URLPARAM{"formfield" default="does not exist"}%"
topic="%URLPARAM{"source" default="does not exist"}%"
}%%ENDSECTION{"formfield"}%
User photoThis one returns an<img...> to a user's photo. See the User selector example above.
URL Parameters
Server section
%STARTSECTION{"userphoto"}%<img src='%FORMFIELD{"Photo" topic="%USERINFO{"%URLPARAM{"name" default="does not exist"}%" format="$wikiusername"}%" default="%PUBURLPATH%/%SYSTEMWEB%/JQueryPlugin/images/nobody.gif" alttext="%PUBURLPATH%/%SYSTEMWEB%/JQueryPlugin/images/nobody.gif"}%' alt='%URLPARAM{"name"}%' height='%URLPARAM{"height" default="50"}%' />%ENDSECTION{"userphoto"}%
Expand TMLexpand a TML expression in the given scopeServer sectionURL parameters
%STARTSECTION{"expand"}%%EXPAND{"$percnt%URLPARAM{"expression"}%$percnt" scope="%URLPARAM{"scope" default="%BASEWEB%.%BASETOPIC%"}%"}%%ENDSECTION{"expand"}%
r1 - 09 Nov 2011 - 20:49:39 - ProjectContributor
Copyright © by the contributing authors. All material on this site is the property of the contributing authors. Ideas, requests, problems regarding Wiki? Send feedback | ||||