Search in sources :

Example 1 with AsyncBuffer

use of org.structr.web.common.AsyncBuffer in project structr by structr.

the class Content method renderContent.

/*
	@Override
	public String getIdHash() {

		final DOMNode _parent = getProperty(DOMNode.parent);
		if (_parent != null) {

			String dataHash = _parent.getProperty(DOMNode.dataHashProperty);
			if (dataHash == null) {
				dataHash = _parent.getIdHash();
			}

			return dataHash + "Content" + treeGetChildPosition(this);
		}

		return super.getIdHash();
	}

	*/
public static void renderContent(final Content thisNode, final RenderContext renderContext, final int depth) throws FrameworkException {
    final SecurityContext securityContext = thisNode.getSecurityContext();
    try {
        final EditMode edit = renderContext.getEditMode(securityContext.getUser(false));
        if (EditMode.DEPLOYMENT.equals(edit)) {
            final AsyncBuffer buf = renderContext.getBuffer();
            // output ownership comments
            DOMNode.renderDeploymentExportComments(thisNode, buf, true);
            // EditMode "deployment" means "output raw content, do not interpret in any way
            buf.append(escapeForHtml(thisNode.getContent()));
            return;
        }
        if (thisNode.isDeleted() || thisNode.isHidden() || !thisNode.displayForLocale(renderContext) || !thisNode.displayForConditions(renderContext)) {
            return;
        }
        final String id = thisNode.getUuid();
        final boolean inBody = renderContext.inBody();
        final AsyncBuffer out = renderContext.getBuffer();
        final String _contentType = thisNode.getContentType();
        // apply configuration for shared component if present
        final String _sharedComponentConfiguration = thisNode.getSharedComponentConfiguration();
        if (StringUtils.isNotBlank(_sharedComponentConfiguration)) {
            Scripting.evaluate(renderContext, thisNode, "${" + _sharedComponentConfiguration + "}", "shared component configuration");
        }
        // fetch content with variable replacement
        String _content = thisNode.getPropertyWithVariableReplacement(renderContext, StructrApp.key(Content.class, "content"));
        if (!(EditMode.RAW.equals(edit) || EditMode.WIDGET.equals(edit)) && (_contentType == null || ("text/plain".equals(_contentType)))) {
            _content = escapeForHtml(_content);
        }
        if (EditMode.CONTENT.equals(edit) && inBody && thisNode.isGranted(Permission.write, securityContext)) {
            if ("text/javascript".equals(_contentType)) {
                // Javascript will only be given some local vars
                out.append("// data-structr-type='").append(thisNode.getType()).append("'\n// data-structr-id='").append(id).append("'\n");
            } else if ("text/css".equals(_contentType)) {
                // CSS will only be given some local vars
                out.append("/* data-structr-type='").append(thisNode.getType()).append("'*/\n/* data-structr-id='").append(id).append("'*/\n");
            } else {
                // In edit mode, add an artificial comment tag around content nodes within body to make them editable
                final String cleanedContent = StringUtils.remove(StringUtils.remove(org.apache.commons.lang3.StringUtils.replace(thisNode.getContent(), "\n", "\\\\n"), "<!--"), "-->");
                out.append("<!--data-structr-id=\"".concat(id).concat("\" data-structr-raw-value=\"").concat(escapeForHtmlAttributes(cleanedContent)).concat("\"-->"));
            }
        }
        // examine content type and apply converter
        if (_contentType != null) {
            final Adapter<String, String> converter = ContentConverters.getConverterForType(_contentType);
            if (converter != null) {
                try {
                    // apply adapter
                    _content = converter.adapt(_content);
                } catch (FrameworkException fex) {
                    logger.warn("Unable to convert content: {}", fex.getMessage());
                }
            }
        }
        // replace newlines with <br /> for rendering
        if (((_contentType == null) || _contentType.equals("text/plain")) && (_content != null) && !_content.isEmpty()) {
            final DOMNode _parent = thisNode.getParent();
            if (_parent == null || !(_parent instanceof Textarea)) {
                _content = _content.replaceAll("[\\n]{1}", "<br>");
            }
        }
        if (_content != null) {
            // insert whitespace to make element clickable
            if (EditMode.CONTENT.equals(edit) && _content.length() == 0) {
                _content = "--- empty ---";
            }
            out.append(_content);
        }
        if (EditMode.CONTENT.equals(edit) && inBody && !("text/javascript".equals(_contentType) && !("text/css".equals(_contentType)))) {
            out.append("<!---->");
        }
    } catch (Throwable t) {
        // catch exception to prevent ugly status 500 error pages in frontend.
        logger.error("", t);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) Textarea(org.structr.web.entity.html.Textarea) SecurityContext(org.structr.common.SecurityContext) EditMode(org.structr.web.common.RenderContext.EditMode) AsyncBuffer(org.structr.web.common.AsyncBuffer)

Example 2 with AsyncBuffer

use of org.structr.web.common.AsyncBuffer in project structr by structr.

the class Template method renderContent.

/*
	public static final org.structr.common.View uiView                                   = new org.structr.common.View(Content.class, PropertyView.Ui,
		children, childrenIds, content, contentType, parent, pageId, hideOnDetail, hideOnIndex, sharedComponent, syncedNodes, dataKey, restQuery, cypherQuery, xpathQuery, functionQuery,
		showForLocales, hideForLocales, showConditions, hideConditions, isContent
	);

	public static final org.structr.common.View publicView                               = new org.structr.common.View(Content.class, PropertyView.Public,
		children, childrenIds, content, contentType, parent, pageId, hideOnDetail, hideOnIndex, sharedComponent, syncedNodes, dataKey, restQuery, cypherQuery, xpathQuery, functionQuery,
		showForLocales, hideForLocales, showConditions, hideConditions, isContent
	);
	*/
public static void renderContent(final Template thisTemplate, final RenderContext renderContext, final int depth) throws FrameworkException {
    final SecurityContext securityContext = thisTemplate.getSecurityContext();
    final EditMode editMode = renderContext.getEditMode(securityContext.getUser(false));
    if (EditMode.DEPLOYMENT.equals(editMode)) {
        final DOMNode _syncedNode = thisTemplate.getSharedComponent();
        final AsyncBuffer out = renderContext.getBuffer();
        if (depth > 0) {
            out.append(DOMNode.indent(depth, renderContext));
        }
        DOMNode.renderDeploymentExportComments(thisTemplate, out, true);
        out.append("<structr:template src=\"");
        if (_syncedNode != null) {
            // use name of synced node
            final String _name = _syncedNode.getProperty(AbstractNode.name);
            out.append(_name != null ? _name.concat("-").concat(_syncedNode.getUuid()) : _syncedNode.getUuid());
        } else {
            // use name of local template
            final String _name = thisTemplate.getProperty(AbstractNode.name);
            out.append(_name != null ? _name.concat("-").concat(thisTemplate.getUuid()) : thisTemplate.getUuid());
        }
        out.append("\"");
        DOMNode.renderSharedComponentConfiguration(thisTemplate, out, editMode);
        // include custom attributes in templates as well!
        DOMNode.renderCustomAttributes(thisTemplate, out, securityContext, renderContext);
        out.append(">");
        // fetch children
        final List<RelationshipInterface> rels = thisTemplate.getChildRelationships();
        if (rels.isEmpty()) {
            // No child relationships, maybe this node is in sync with another node
            if (_syncedNode != null) {
                rels.addAll(_syncedNode.getChildRelationships());
            }
        }
        for (final RelationshipInterface rel : rels) {
            final DOMNode subNode = (DOMNode) rel.getTargetNode();
            subNode.render(renderContext, depth + 1);
        }
        out.append(DOMNode.indent(depth, renderContext));
        out.append("</structr:template>");
        out.append(DOMNode.indent(depth - 1, renderContext));
    } else {
        // "super" call using static method..
        Content.renderContent(thisTemplate, renderContext, depth);
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) RelationshipInterface(org.structr.core.graph.RelationshipInterface) EditMode(org.structr.web.common.RenderContext.EditMode) AsyncBuffer(org.structr.web.common.AsyncBuffer)

Example 3 with AsyncBuffer

use of org.structr.web.common.AsyncBuffer in project structr by structr.

the class DOMElement method renderContent.

static void renderContent(final DOMElement thisElement, final RenderContext renderContext, final int depth) throws FrameworkException {
    if (thisElement.isDeleted() || thisElement.isHidden() || !thisElement.displayForLocale(renderContext) || !thisElement.displayForConditions(renderContext)) {
        return;
    }
    // final variables
    final SecurityContext securityContext = renderContext.getSecurityContext();
    final AsyncBuffer out = renderContext.getBuffer();
    final EditMode editMode = renderContext.getEditMode(securityContext.getUser(false));
    final boolean isVoid = thisElement.isVoidElement();
    final String _tag = thisElement.getTag();
    // non-final variables
    Result localResult = renderContext.getResult();
    boolean anyChildNodeCreatesNewLine = false;
    thisElement.renderStructrAppLib(out, securityContext, renderContext, depth);
    if (depth > 0 && !thisElement.avoidWhitespace()) {
        out.append(DOMNode.indent(depth, renderContext));
    }
    if (StringUtils.isNotBlank(_tag)) {
        if (EditMode.DEPLOYMENT.equals(editMode)) {
            // comment accordingly.
            if (DOMNode.renderDeploymentExportComments(thisElement, out, false)) {
                // restore indentation
                if (depth > 0 && !thisElement.avoidWhitespace()) {
                    out.append(DOMNode.indent(depth, renderContext));
                }
            }
        }
        thisElement.openingTag(out, _tag, editMode, renderContext, depth);
        try {
            // in body?
            if (lowercaseBodyName.equals(thisElement.getTagName())) {
                renderContext.setInBody(true);
            }
            // only render children if we are not in a shared component scenario and not in deployment mode
            if (thisElement.getSharedComponent() == null || !EditMode.DEPLOYMENT.equals(editMode)) {
                // fetch children
                final List<RelationshipInterface> rels = thisElement.getChildRelationships();
                if (rels.isEmpty()) {
                    // No child relationships, maybe this node is in sync with another node
                    final DOMElement _syncedNode = (DOMElement) thisElement.getSharedComponent();
                    if (_syncedNode != null) {
                        rels.addAll(_syncedNode.getChildRelationships());
                    }
                }
                // apply configuration for shared component if present
                final String _sharedComponentConfiguration = thisElement.getProperty(StructrApp.key(DOMElement.class, "sharedComponentConfiguration"));
                if (StringUtils.isNotBlank(_sharedComponentConfiguration)) {
                    Scripting.evaluate(renderContext, thisElement, "${" + _sharedComponentConfiguration + "}", "shared component configuration");
                }
                for (final RelationshipInterface rel : rels) {
                    final DOMNode subNode = (DOMNode) rel.getTargetNode();
                    if (subNode instanceof DOMElement) {
                        anyChildNodeCreatesNewLine = (anyChildNodeCreatesNewLine || !(subNode.avoidWhitespace()));
                    }
                    subNode.render(renderContext, depth + 1);
                }
            }
        } catch (Throwable t) {
            out.append("Error while rendering node ").append(thisElement.getUuid()).append(": ").append(t.getMessage());
            logger.warn("", t);
        }
        // render end tag, if needed (= if not singleton tags)
        if (StringUtils.isNotBlank(_tag) && (!isVoid)) {
            // only insert a newline + indentation before the closing tag if any child-element used a newline
            final DOMElement _syncedNode = (DOMElement) thisElement.getSharedComponent();
            final boolean isTemplate = _syncedNode != null && EditMode.DEPLOYMENT.equals(editMode);
            if (anyChildNodeCreatesNewLine || isTemplate) {
                out.append(DOMNode.indent(depth, renderContext));
            }
            if (_syncedNode != null && EditMode.DEPLOYMENT.equals(editMode)) {
                out.append("</structr:component>");
            } else if (isTemplate) {
                out.append("</structr:template>");
            } else {
                out.append("</").append(_tag).append(">");
            }
        }
    }
    // Set result for this level again, if there was any
    if (localResult != null) {
        renderContext.setResult(localResult);
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) RelationshipInterface(org.structr.core.graph.RelationshipInterface) EditMode(org.structr.web.common.RenderContext.EditMode) AsyncBuffer(org.structr.web.common.AsyncBuffer) Result(org.structr.core.Result)

Aggregations

SecurityContext (org.structr.common.SecurityContext)3 AsyncBuffer (org.structr.web.common.AsyncBuffer)3 EditMode (org.structr.web.common.RenderContext.EditMode)3 RelationshipInterface (org.structr.core.graph.RelationshipInterface)2 FrameworkException (org.structr.common.error.FrameworkException)1 Result (org.structr.core.Result)1 Textarea (org.structr.web.entity.html.Textarea)1