js.template.xhtml.CssClassOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of js-xhtml-template Show documentation
Show all versions of js-xhtml-template Show documentation
Reference implementation for j(s)-lib template API, declarative, natural and based on X(HT)ML language.
The newest version!
package js.template.xhtml;
import js.dom.Element;
import js.lang.Pair;
import js.lang.PairsList;
import js.template.TemplateException;
/**
* Add or remove element CSS class considering conditional expression value. In sample code there are two class expressions
* evaluated in natural order. If conditional expression is true add related class otherwise remove it. In example, if
* type
value is DIRECTORY
add CSS class directory
to div
element otherwise
* add file
class.
*
*
*
* <div data-css-class="type=DIRECTORY:directory;!type=DIRECTORY:file;" />
*
*
* Operand is a list of semicolon separated CSS class expressions, see syntax below. A class expression has a conditional
* expression as described by {@link ConditionalExpression} class description and a CSS class name separated by colon.
*
*
*
* operand = class-expression *( ';' class-expression ) [ ';' ]
* class-expression = conditional-expression ':' class-name
*
* ; conditional-expression = see conditional expression class description
* ; class-name = CSS class name
*
*
* @author Iulian Rotaru
*/
final class CssClassOperator extends Operator {
/** Dynamic content reference. */
private Content content;
/**
* Construct CSS_CLASS operator instance.
*
* @param serializer parent serializer,
* @param content dynamic content.
*/
CssClassOperator(Serializer serializer, Content content) {
this.content = content;
}
@Override
protected Object doExec(Element element, Object scope, String expression, Object... arguments) throws TemplateException {
if (expression.isEmpty()) {
throw new TemplateException("Invalid CSS_CLASS operand. Expression is empty.");
}
CssClass cssClass = new CssClass(element);
PairsList pairs = new PairsList(expression);
for (Pair pair : pairs) {
// accordingly CSS_CLASS operator syntax first pair value is a conditional expression and the second is the CSS
// class name
ConditionalExpression conditionalExpression = new ConditionalExpression(content, scope, pair.first());
String className = pair.second();
if (conditionalExpression.value()) {
log.debug("True conditional expression |%s|. Add CSS class |%s| to element |%s|.", pair.first(), className, element.trace());
cssClass.add(className);
} else {
log.debug("False conditional expression |%s|. Remove CSS class |%s| from element |%s|.", pair.first(), className, element.trace());
cssClass.remove(className);
}
}
return cssClass;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy