com.tcdng.unify.web.ui.widget.writer.container.DocumentWriter Maven / Gradle / Ivy
/*
* Copyright 2018-2023 The Code Department.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.tcdng.unify.web.ui.widget.writer.container;
import com.tcdng.unify.core.UnifyException;
import com.tcdng.unify.core.annotation.Component;
import com.tcdng.unify.core.annotation.Configurable;
import com.tcdng.unify.core.annotation.Writes;
import com.tcdng.unify.core.constant.MimeType;
import com.tcdng.unify.core.util.StringUtils;
import com.tcdng.unify.web.ControllerPathParts;
import com.tcdng.unify.web.data.WebStringWriter;
import com.tcdng.unify.web.ui.PagePathInfoRepository;
import com.tcdng.unify.web.ui.widget.DocumentLayout;
import com.tcdng.unify.web.ui.widget.Panel;
import com.tcdng.unify.web.ui.widget.ResponseWriter;
import com.tcdng.unify.web.ui.widget.Widget;
import com.tcdng.unify.web.ui.widget.container.BasicDocument;
import com.tcdng.unify.web.ui.widget.container.BasicDocumentResources;
import com.tcdng.unify.web.ui.widget.writer.AbstractPageWriter;
/**
* Basic document writer.
*
* @author The Code Department
* @since 1.0
*/
@Writes(BasicDocument.class)
@Component("document-writer")
public class DocumentWriter extends AbstractPageWriter {
@Configurable
private PagePathInfoRepository pathInfoRepository;
@Configurable
private BasicDocumentResources resources;
public void setPathInfoRepository(PagePathInfoRepository pathInfoRepository) {
this.pathInfoRepository = pathInfoRepository;
}
public void setResources(BasicDocumentResources resources) {
this.resources = resources;
}
@Override
protected void doWriteStructureAndContent(ResponseWriter writer, Widget widget) throws UnifyException {
BasicDocument document = (BasicDocument) widget;
writer.write("");
writer.write("");
// Head
writer.write("");
// Write title
writer.write("");
String title = document.getUplAttribute(String.class, "caption");
if (StringUtils.isNotBlank(title)) {
writer.write(title);
} else {
writer.write(getUnifyComponentContext().getInstanceName());
}
writer.write(" ");
// Write META
if (document.getMeta() != null) {
for (String oneMeta : document.getMeta()) {
writer.write("");
}
}
// Write favorite icon
writer.write("");
// Write style sheet links
writeStyleSheet(writer, "$t{css/unify-web.css}");
for (String styleSheet : getPageManager().getDocumentStyleSheets()) {
writeStyleSheet(writer, styleSheet);
}
String[] styleSheets = document.getUplAttribute(String[].class, "styleSheet");
if (styleSheets != null) {
for (String styleSheet : styleSheets) {
writeStyleSheet(writer, styleSheet);
}
}
writeResourcesStyleSheet(writer);
// Write font symbols
writeEmbeddedStyle(writer, document);
// Write javascript sources
writeJavascript(writer, "web/js/unify-web.js");
for (String script : getPageManager().getDocumentsScripts()) {
writeJavascript(writer, script);
}
String[] scripts = document.getUplAttribute(String[].class, "script");
if (scripts != null) {
for (String script : scripts) {
writeJavascript(writer, script);
}
}
writeResourcesScript(writer);
writer.write("");
// Body
writer.write("");
// Popup base
writer.write("");
writer.write("");
writer.write("");
writer.write("");
// Write document structure an content
DocumentLayout documentLayout = document.getUplAttribute(DocumentLayout.class, "layout");
writer.writeStructureAndContent(documentLayout, document);
writer.write("");
}
@Override
protected void doWriteBehavior(ResponseWriter writer, Widget widget) throws UnifyException {
BasicDocument document = (BasicDocument) widget;
writer.write("");
}
@Override
protected void doWriteInnerStructureAndContent(ResponseWriter writer, Panel panel) throws UnifyException {
}
private void writeEmbeddedStyle(ResponseWriter writer, BasicDocument document) throws UnifyException {
writer.write("");
}
private void writeImageBeforeCss(ResponseWriter writer, String className, String imgSrc) throws UnifyException {
writer.write(className).write(" {vertical-align:middle;display: inline-block !important;} ").write(className).write(
":before {content: \"\";vertical-align:middle;display: inline-block;width: 100%;height: 100%;background: url(");
writer.writeFileImageContextURL(imgSrc);
writer.write(")no-repeat center/100% 100%; }");
}
private void writeResourcesStyleSheet(ResponseWriter writer) throws UnifyException {
if (resources != null) {
for (String sheetLink : resources.getStyleSheetResourceLinks()) {
writer.write("");
}
}
}
private void writeResourcesScript(ResponseWriter writer) throws UnifyException {
if (resources != null) {
for (String scriptLink : resources.getScriptResourceLinks()) {
writer.write("");
}
}
}
private void writeStyleSheet(ResponseWriter writer, String styleSheet) throws UnifyException {
writer.write("");
}
private void writeJavascript(ResponseWriter writer, String script) throws UnifyException {
writer.write("");
}
private void writeNonce(ResponseWriter writer) throws UnifyException {
if (getRequestContextUtil().isWithNonce()) {
writer.write(" nonce=\"");
writer.write(getRequestContextUtil().getNonce());
writer.write("\"");
}
}
private void writeBehaviour(ResponseWriter writer, Panel panel) throws UnifyException {
if (panel != null) {
writer.writeBehavior(panel);
}
}
}