How to use jQuery and jQuery UI in my skin ? For versions 3.0 to 3.1.x The jar file Call the JS Use the JS For versions 3.2+ How can I link a basic html text input with the search engine ? Use auto-completion (for versions 3.2+) How to get the required level title from inputdata filters on contents ? How to display tranlations ?
How to use jQuery and jQuery UI in my skin ? jQuery and jQuery UI are major JavaScript libraries for skin effects.
For versions 3.0 to 3.1.x Plugins (as Multimedia) needs it, so using different version may conflict. For that reason do not put the jQuery files in your skin (or your plugin) directly.
The jar file The first step is to see if you have a jar named jquery-*.jar in your WEB-INF/lib directory. This is the case when depending of a plugin that already use jQuery.
If not you have to add it :
if you use ivy, add this to your ivy
Oops ! Copy to clipboard failed. Open the code and copy it manually. <dependency org="jquery" name="jquery" rev="1.4.2" conf="default"/>
<dependency org="jquery" name="jquery-ui" rev="1.8.5" conf="default"/
<dependency org="jquery" name="jquery" rev="1.4.2" conf="default"/>
<dependency org="jquery" name="jquery-ui" rev="1.8.5" conf="default"/
<dependency org="jquery" name="jquery" rev="1.4.2" conf="default"/>
<dependency org="jquery" name="jquery-ui" rev="1.8.5" conf="default"/
if not, download the jar here http://viewvc.ametys.org/viewvc/ivyrep/external/jquery/jquery/ and put it in the WEB-INF/lib Call the JS In your skin you have to use the scripts. There are 2 scripts, the expansed and the minified one. For jQuery the relative urls are :
/plugins/jquery/resources/js/jquery.js /plugins/jquery/resources/js/jquery.min.js For jQuery UI the relative urls are :
/plugins/jquery-ui/resources/js/jquery-ui.js /plugins/jquery-ui/resources/js/jquery-ui.min.js Here is a sample in a skin :
Oops ! Copy to clipboard failed. Open the code and copy it manually.<script type="text/javascript" src="{$contextPath}/plugins/jquery/resources/js/jquery.min.js"></script>
<script type="text/javascript" src="{$contextPath}/plugins/jquery-ui/resources/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="{$contextPath}/plugins/jquery/resources/js/jquery.min.js"></script>
<script type="text/javascript" src="{$contextPath}/plugins/jquery-ui/resources/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="{$contextPath}/plugins/jquery/resources/js/jquery.min.js"></script>
<script type="text/javascript" src="{$contextPath}/plugins/jquery-ui/resources/js/jquery-ui.min.js"></script>
Use the JS Declare an unique variable in your XSL :
Oops ! Copy to clipboard failed. Open the code and copy it manually. <xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
<xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
<xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
Use the unique variable to avoid jQuery conflict like following :
Oops ! Copy to clipboard failed. Open the code and copy it manually. // For no conflict
var $j<xsl:value-of select="$uniqueId"/> = jQuery.noConflict();
// Sample for use
$j<xsl:value-of select="$uniqueId"/>("#myDiv").html('');
// For no conflict
var $j<xsl:value-of select="$uniqueId"/> = jQuery.noConflict();
// Sample for use
$j<xsl:value-of select="$uniqueId"/>("#myDiv").html('');
// For no conflict
var $j<xsl:value-of select="$uniqueId"/> = jQuery.noConflict();
// Sample for use
$j<xsl:value-of select="$uniqueId"/>("#myDiv").html('');
For versions 3.2+ jQuery and jQuery UI libraries are brought by web, so they are directly availables in your skin : you do not need to add jars and you do not need to import js.
You can directly use jQuery functions by using variable $j :
Oops ! Copy to clipboard failed. Open the code and copy it manually. $j().ready(function() {
// ...
$j(#myInput).autocomplete (...);
$j.ajax({
url: url,
dataType: "xml",
success: function(xml) {
// ...
}
});
}
$j().ready(function() {
// ...
$j(#myInput).autocomplete (...);
$j.ajax({
url: url,
dataType: "xml",
success: function(xml) {
// ...
}
});
}
Show code
$j().ready(function() {
// ...
$j(#myInput).autocomplete (...);
$j.ajax({
url: url,
dataType: "xml",
success: function(xml) {
// ...
}
});
}
How can I link a basic html text input with the search engine ? Two actions are possible : redirect to the search page with or without launching the search. The search field will automatically be filled in.
In order to do this, you must follow these steps :
The action of the form must point to the page containing the search service. Give "textfield" as name for you text input Add a hidden field named "submit-form" if you want to launch automatically the search
Oops ! Copy to clipboard failed. Open the code and copy it manually.<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="submit" class="submit" value="OK"/>
</form>
<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="submit" class="submit" value="OK"/>
</form>
<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="submit" class="submit" value="OK"/>
</form>
You can restrict search to some content types like following :
Oops ! Copy to clipboard failed. Open the code and copy it manually.<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="hidden" name="content-types" value="org.ametys.web.default.Content.article"/>
<input type="hidden" name="content-types" value="org.ametys.plugins.news.Content.news"/>
<input type="hidden" name="content-types" value="resource"/>
<input type="submit" class="submit" value="OK"/>
</form>
<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="hidden" name="content-types" value="org.ametys.web.default.Content.article"/>
<input type="hidden" name="content-types" value="org.ametys.plugins.news.Content.news"/>
<input type="hidden" name="content-types" value="resource"/>
<input type="submit" class="submit" value="OK"/>
</form>
<form action="{$cms-context}/{$lang}/search.html#nav" method="post">
<input type="hidden" name="submit-form"/>
<input type="text" name="textfield" value=""/>
<input type="hidden" name="content-types" value="org.ametys.web.default.Content.article"/>
<input type="hidden" name="content-types" value="org.ametys.plugins.news.Content.news"/>
<input type="hidden" name="content-types" value="resource"/>
<input type="submit" class="submit" value="OK"/>
</form>
Use auto-completion (for versions 3.2+) To use the auto-completion on your search input (available only for version 3.2+) you must follow these steps :
Add xmlns:math="java.lang.Math" namespace to your xsl :
Oops ! Copy to clipboard failed. Open the code and copy it manually. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
xmlns:math="java.lang.Math"
xmlns:sitemap="http://www.ametys.org/inputdata/sitemap/3.0"
exclude-result-prefixes="sitemap math">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
xmlns:math="java.lang.Math"
xmlns:sitemap="http://www.ametys.org/inputdata/sitemap/3.0"
exclude-result-prefixes="sitemap math">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
xmlns:math="java.lang.Math"
xmlns:sitemap="http://www.ametys.org/inputdata/sitemap/3.0"
exclude-result-prefixes="sitemap math">
Declare a global variable uniqueId with unique identifier
Oops ! Copy to clipboard failed. Open the code and copy it manually.<xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
<xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
<xsl:variable name="uniqueId" select="substring-after(math:random(), '.')"/>
Add the following script on the section <head> :
Oops ! Copy to clipboard failed. Open the code and copy it manually.<script type="text/javascript">
$j().ready(function()
{
function parseXML(data)
{
var results = [];
var branches = $j(data).find('item');
$(branches).each(function() {
var text = $j.trim($(this).text());
results[results.length] = {'data': [text, text], 'result': text, 'value': text};
});
return results;
}
var url = "<xsl:value-of select="$contextPath"/>/plugins/web/service/search/auto-completion/<xsl:value-of select="$site"/>/<xsl:value-of select="$lang"/>.xml";
$j("#search-input-<xsl:value-of select="$uniqueId"/>").autocomplete({
source: function( request, response ) {
$j.ajax({
url: url + "?q=" + request.term,
dataType: "xml",
success: function( xmlResponse ) {
response( parseXML (xmlResponse));
}
});},
minLength: 2,
delay: 500
});
});
</script>
<script type="text/javascript">
$j().ready(function()
{
function parseXML(data)
{
var results = [];
var branches = $j(data).find('item');
$(branches).each(function() {
var text = $j.trim($(this).text());
results[results.length] = {'data': [text, text], 'result': text, 'value': text};
});
return results;
}
var url = "<xsl:value-of select="$contextPath"/>/plugins/web/service/search/auto-completion/<xsl:value-of select="$site"/>/<xsl:value-of select="$lang"/>.xml";
$j("#search-input-<xsl:value-of select="$uniqueId"/>").autocomplete({
source: function( request, response ) {
$j.ajax({
url: url + "?q=" + request.term,
dataType: "xml",
success: function( xmlResponse ) {
response( parseXML (xmlResponse));
}
});},
minLength: 2,
delay: 500
});
});
</script>
Show code
<script type="text/javascript">
$j().ready(function()
{
function parseXML(data)
{
var results = [];
var branches = $j(data).find('item');
$(branches).each(function() {
var text = $j.trim($(this).text());
results[results.length] = {'data': [text, text], 'result': text, 'value': text};
});
return results;
}
var url = "<xsl:value-of select="$contextPath"/>/plugins/web/service/search/auto-completion/<xsl:value-of select="$site"/>/<xsl:value-of select="$lang"/>.xml";
$j("#search-input-<xsl:value-of select="$uniqueId"/>").autocomplete({
source: function( request, response ) {
$j.ajax({
url: url + "?q=" + request.term,
dataType: "xml",
success: function( xmlResponse ) {
response( parseXML (xmlResponse));
}
});},
minLength: 2,
delay: 500
});
});
</script>
How to get the required level title from inputdata filters on contents ? The inputdata of filter content (cf Filters on contents ) returns the data in this form:
Oops ! Copy to clipboard failed. Open the code and copy it manually.<news-section>
<content id="defaultWebContent://ef594708-c4f7-4c61-b46a-ed888acec9bc" name="new-new" title="New" lastModifiedAt="2010-08-11T14:18:07.092+02:00">
<html>
<head>
<title>New</title>
<meta content="2010-08-11T14:18:07.092+02:00" name="lastModificationRaw"/>
</head>
<body>
<a href="/enit/preview/default/en/new.html">
<img alt="" src="/enit/preview/default/_contents-images/illustration/image_max100x100"/>
</a>
<span class="date">04.08.10 - 19.08.10</span>
<h1>New</h1>
<p>Pellentesque convallis diam. Consectetuer adipiscing elit. Etiam blandit euismod urna. Aenean condimentum consectetuer adipiscing elit odio et pede.
<a href="/enit/preview/default/fr/l-ecole/une-actualite.html">Read more</a>
</p>
</body>
</html>
</content>
</news-section>
<news-section>
<content id="defaultWebContent://ef594708-c4f7-4c61-b46a-ed888acec9bc" name="new-new" title="New" lastModifiedAt="2010-08-11T14:18:07.092+02:00">
<html>
<head>
<title>New</title>
<meta content="2010-08-11T14:18:07.092+02:00" name="lastModificationRaw"/>
</head>
<body>
<a href="/enit/preview/default/en/new.html">
<img alt="" src="/enit/preview/default/_contents-images/illustration/image_max100x100"/>
</a>
<span class="date">04.08.10 - 19.08.10</span>
<h1>New</h1>
<p>Pellentesque convallis diam. Consectetuer adipiscing elit. Etiam blandit euismod urna. Aenean condimentum consectetuer adipiscing elit odio et pede.
<a href="/enit/preview/default/fr/l-ecole/une-actualite.html">Read more</a>
</p>
</body>
</html>
</content>
</news-section>
Show code
<news-section>
<content id="defaultWebContent://ef594708-c4f7-4c61-b46a-ed888acec9bc" name="new-new" title="New" lastModifiedAt="2010-08-11T14:18:07.092+02:00">
<html>
<head>
<title>New</title>
<meta content="2010-08-11T14:18:07.092+02:00" name="lastModificationRaw"/>
</head>
<body>
<a href="/enit/preview/default/en/new.html">
<img alt="" src="/enit/preview/default/_contents-images/illustration/image_max100x100"/>
</a>
<span class="date">04.08.10 - 19.08.10</span>
<h1>New</h1>
<p>Pellentesque convallis diam. Consectetuer adipiscing elit. Etiam blandit euismod urna. Aenean condimentum consectetuer adipiscing elit odio et pede.
<a href="/enit/preview/default/fr/l-ecole/une-actualite.html">Read more</a>
</p>
</body>
</html>
</content>
</news-section>
In the template.xsl we display the results of the filter with this template :
Oops ! Copy to clipboard failed. Open the code and copy it manually. <xsl:copy-of select="html/body/node()"/>
<xsl:copy-of select="html/body/node()"/>
<xsl:copy-of select="html/body/node()"/>
But we want to display different levels of titles that h1 is used.
We begin by importing this xsl :
Oops ! Copy to clipboard failed. Open the code and copy it manually. <xsl:import href="workspace:web://stylesheets/helper/html-hierarchy.xsl"/>
<xsl:import href="workspace:web://stylesheets/helper/html-hierarchy.xsl"/>
<xsl:import href="workspace:web://stylesheets/helper/html-hierarchy.xsl"/>
Then we used this template :
Oops ! Copy to clipboard failed. Open the code and copy it manually. <xsl:apply-templates select="html/body/node()" mode="move-hierarchy">
<xsl:with-param name="level" select="4"/>
</xsl:apply-templates>
<xsl:apply-templates select="html/body/node()" mode="move-hierarchy">
<xsl:with-param name="level" select="4"/>
</xsl:apply-templates>
<xsl:apply-templates select="html/body/node()" mode="move-hierarchy">
<xsl:with-param name="level" select="4"/>
</xsl:apply-templates>
How to display tranlations ? With plugin translation flagging, you have translation meta : but the meta can be positionned on non existing pages (deleted, not validated...)
Here a code to do so :
Oops ! Copy to clipboard failed. Open the code and copy it manually.
<xsl:variable name="raw-translations">
<xsl:for-each select="/cms/page/metadata/translations/*">
<xsl:variable name="hrefResolved" select="resolver:resolve('page', .)"/>
<xsl:if test="$hrefResolved != ''">
<xsl:element name="{local-name()}">
<xsl:value-of select="$hrefResolved"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="translations" select="exsl:node-set($raw-translations)"/>
<xsl:if test="$translation/*">
<ul>
<xsl:for-each select="$translation/*">
<li>
<a href="{.}"><xsl:value-of select="local-name()"/></a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
<xsl:variable name="raw-translations">
<xsl:for-each select="/cms/page/metadata/translations/*">
<xsl:variable name="hrefResolved" select="resolver:resolve('page', .)"/>
<xsl:if test="$hrefResolved != ''">
<xsl:element name="{local-name()}">
<xsl:value-of select="$hrefResolved"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="translations" select="exsl:node-set($raw-translations)"/>
<xsl:if test="$translation/*">
<ul>
<xsl:for-each select="$translation/*">
<li>
<a href="{.}"><xsl:value-of select="local-name()"/></a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
Show code
<xsl:variable name="raw-translations">
<xsl:for-each select="/cms/page/metadata/translations/*">
<xsl:variable name="hrefResolved" select="resolver:resolve('page', .)"/>
<xsl:if test="$hrefResolved != ''">
<xsl:element name="{local-name()}">
<xsl:value-of select="$hrefResolved"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="translations" select="exsl:node-set($raw-translations)"/>
<xsl:if test="$translation/*">
<ul>
<xsl:for-each select="$translation/*">
<li>
<a href="{.}"><xsl:value-of select="local-name()"/></a>
</li>
</xsl:for-each>
</ul>
</xsl:if>