001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.causeway.extensions.pdfjs.wkt.integration.components;
020
021import org.apache.wicket.markup.head.IHeaderResponse;
022import org.apache.wicket.markup.html.WebComponent;
023import org.apache.wicket.markup.html.panel.Panel;
024
025import org.apache.causeway.extensions.pdfjs.applib.config.PdfJsConfig;
026import org.apache.causeway.extensions.pdfjs.wkt.integration.res.PdfJsCmapsReference;
027import org.apache.causeway.extensions.pdfjs.wkt.integration.res.PdfJsIntegrationReference;
028import org.apache.causeway.extensions.pdfjs.wkt.integration.res.PdfJsReference;
029import org.apache.causeway.extensions.pdfjs.wkt.integration.res.PdfJsWorkerReference;
030import org.apache.causeway.viewer.wicket.ui.util.Wkt;
031
032import org.jspecify.annotations.NonNull;
033
034/**
035 * A panel for rendering PDF documents inline in the page
036 */
037public class PdfJsPanel extends Panel {
038
039    private static final long serialVersionUID = 1L;
040
041    private final static String ID_PDFJSCANVAS = "pdfJsCanvas";
042
043    private PdfJsConfig config;
044
045    /**
046     * Constructor.
047     *
048     * @param id The component id
049     */
050    public PdfJsPanel(final String id, final @NonNull PdfJsConfig config) {
051        super(id);
052
053        var pdfJsCanvas = Wkt.add(this, Wkt.ajaxEnable(new WebComponent(ID_PDFJSCANVAS)));
054
055        this.config = config
056                .withWorkerUrl(PdfJsWorkerReference.workerUrl())
057                .withCmapsUrl(PdfJsCmapsReference.cmapsUrl())
058                .withCanvasId(pdfJsCanvas.getMarkupId());
059    }
060
061    @Override
062    public void renderHead(final IHeaderResponse response) {
063        super.renderHead(response);
064
065        response.render(PdfJsReference.asHeaderItem());
066        response.render(PdfJsIntegrationReference.asHeaderItem());
067        response.render(PdfJsIntegrationReference.domReadyScript(config));
068    }
069
070    public CharSequence getCanvasId() {
071        return config.getCanvasId();
072    }
073
074}