<?xml version="1.0" encoding="UTF-8"?>
<!--
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>macro-pdfviewer</artifactId>
    <groupId>com.xwiki.pdfviewer</groupId>
    <version>2.7.3-SNAPSHOT</version>
  </parent>
  <artifactId>macro-pdfviewer-webjar</artifactId>
  <name>PDF Viewer Macro - Webjar</name>
  <description>Resources needed by the PDFViewer.</description>
  <packaging>webjar</packaging>
  <properties>
    <!-- Name to display by the Extension Manager -->
    <xwiki.extension.name>PDF Viewer WebJar (Pro)</xwiki.extension.name>
    <webjar.contentDirectory>
      ${project.build.outputDirectory}/META-INF/resources/webjars/${project.artifactId}/${project.version}
    </webjar.contentDirectory>
    <pdfjs.version>4.2.67</pdfjs.version>
  </properties>
  <build>
    <plugins>
      <!-- Unpack the pdf.js webjar -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.webjars</groupId>
                  <artifactId>pdf-js</artifactId>
                  <version>${pdfjs.version}</version>
                  <type>jar</type>
                  <includes>META-INF/resources/webjars/pdf-js/${pdfjs.version}/**</includes>
                  <fileMappers>
                    <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
                      <pattern>^\QMETA-INF/resources/webjars/pdf-js/${pdfjs.version}/\E</pattern>
                      <replacement>./</replacement>
                    </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
                  </fileMappers>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${webjar.contentDirectory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- Add XWiki customizations to the PDF.JS -->
      <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.3</version>
        <executions>
          <execution>
            <id>fix-viewer-js</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>replace</goal>
            </goals>
            <configuration>
              <file>${webjar.contentDirectory}/web/viewer.mjs</file>
              <replacements>
                <!-- Issue #15: External PDFs are not displayed -->
                <replacement>
                  <token>const HOSTED_VIEWER_ORIGINS[^;]*</token>
                  <value>$0;
  const queryString = document.location.search.substring(1);
  const params = (0, _ui_utils_js__WEBPACK_IMPORTED_MODULE_0__.parseQueryString)(queryString);
  const trustedOrigins = (params.get &amp;&amp; params.get('trustedorigins')) || params.trustedorigins;
  if (trustedOrigins !== undefined &amp;&amp; trustedOrigins !== '') {
    HOSTED_VIEWER_ORIGINS.push.apply(HOSTED_VIEWER_ORIGINS, trustedOrigins.split(',')) }</value>
                </replacement>
                <replacement>
                  <token>PDFViewerApplication.open\(\{</token>
                  <value>
    let withCredentials = /withCredentials=1(&amp;|\$)/.test(window.location.search);
    PDFViewerApplication.open({
      withCredentials,
                  </value>
                </replacement>
              </replacements>
            </configuration>
          </execution>
          <execution>
            <id>fix-viewer-html</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>replace</goal>
            </goals>
            <configuration>
              <file>${webjar.contentDirectory}/web/viewer.html</file>
              <replacements>
                <!-- Load our JavaScript customizations before the PDF viewer code. -->
                <replacement>
                  <token>&lt;script src="viewer\.mjs"</token>
                  <value>&lt;script src="../pdfviewer-customizations.js"&gt;&lt;/script&gt;
  $0</value>
                </replacement>
              </replacements>
            </configuration>
          </execution>
          <execution>
            <id>fix-viewer-css</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>replace</goal>
            </goals>
            <configuration>
              <file>${webjar.contentDirectory}/web/viewer.css</file>
              <replacements>
                <replacement>
                  <token>mask-image:var\(--toolbarButton-download-icon\);\n}</token>
                  <value>$0

.toolbarButton.permalink::before,
.secondaryToolbarButton.permalink::before {
  -webkit-mask-image: url(images/toolbarButton-permalink.svg);
  mask-image: url(images/toolbarButton-permalink.svg);
}</value>
                </replacement>
              </replacements>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <!-- Cancel custom MANIFEST file (since it's not generated for "webjar" packaging) -->
            <manifestFile combine.self="override" />
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.github.searls</groupId>
        <artifactId>jasmine-maven-plugin</artifactId>
        <!-- Skip the execution of the Jasmine tests. -->
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
