使用 Javascript 确定 JSF 库资源

Mel*_*are 5 javascript jsf primefaces omnifaces primefaces-extensions

我是 Primefaces Extensions 项目的开发人员,我使用核心 Primefaces JS 函数来获取 DocumentViewer 和 CKEditor 组件的 Javascript 资源位置。getFacesResource ()函数位于 PrimeFaces core.js 中,如下所示:

   /**
     * Builds a resource URL for given parameters.
     *
     * @param {string} name The name of the resource. For example: primefaces.js
     * @param {string} library The library of the resource. For example: primefaces
     * @param {string} version The version of the library. For example: 5.1
     * @returns {string} The resource URL.
     */
getFacesResource : function(name, library, version) {
    // just get sure - name shoudln't start with a slash
    if (name.indexOf('/') === 0)
    {
        name = name.substring(1, name.length);
    }

    var scriptURI = $('script[src*="/' + PrimeFaces.RESOURCE_IDENTIFIER + '/core.js"]').attr('src');
    // portlet
    if (!scriptURI) {
        scriptURI = $('script[src*="' + PrimeFaces.RESOURCE_IDENTIFIER + '=core.js"]').attr('src');
    }

    scriptURI = scriptURI.replace('core.js', name);
Run Code Online (Sandbox Code Playgroud)

它的作用是在 DOM 中找到“core.js”脚本,然后删除 core.js,以便它为您构建的用于查找资源的新 URL 提供正确的位置。

为什么我们需要这样做?

CKEDitor 和 DocumentViewer 组件是复杂的组件,会加载大量插件、语言文件等。我们无法修改这些插件使用的核心 JS 文件,否则每次升级时我们都必须编辑它们的核心源代码。因此,对于 DocumentViewer 加载语言包,库值具有“documentviewer/locale/en-GB.locale.txt”,用于加载 DocumentViewer 使用的 PDF.js 的英语语言包。然而,这需要成为一个真正的资源,因此PrimeFaces.getFacesResource('documentviewer/locale/en-GB.locale.txt');将 URL 转换为 PDF.JS 代码可以找到的适当资源,例如:

https://www.primefaces.org/showcase-ext/javax.faces.resource/documentviewer/locale/en-GB.locale.txt.jsf?ln=primefaces-extensions&v=6.2.5
Run Code Online (Sandbox Code Playgroud)

它负责了解完整的 URL、附加库版本以及了解当前服务器是否提供 .jsf 或 .xhtml 扩展名等。

问题:

我正在使用OmniFaces CombinedResourceHandler,它获取所有 JS 文件并从它找到的所有 JS 文件创建 1 个脚本。所以 10 个不同的 JS 文件现在变成了 1 个资源来实现这样的性能......

/javax.faces.resource/XXX.js.xhtml?ln=omnifaces.combined&v=1532916484000
Run Code Online (Sandbox Code Playgroud)

现在,这破坏了核心 PrimeFaces 代码,因为它无法查找 core.js,因为 core.js 不再存在于页面上。

我尝试使用此开关排除 core.js 的组合。

<context-param>
    <param-name>org.omnifaces.COMBINED_RESOURCE_HANDLER_EXCLUDED_RESOURCES</param-name>
    <param-value>primefaces:core.js</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

这会在omnifaces.combined.js之后加载PrimeFaces core.js,这会破坏整个页面。为了确认输出是这样的......

<script src="/primeext-showcase/javax.faces.resource/XXX.js.jsf?ln=omnifaces.combined&amp;v=1533319992000"></script>
<script src="/primeext-showcase/javax.faces.resource/core.js.jsf?ln=primefaces&amp;v=6.2">
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,无论 OmniFaces 是否组合了脚本,如何使用纯 JavaScript 替换/修复 PrimeFaces.getFacesResource() JS 函数以使其工作?

Mel*_*are 1

我能够重写该方法,以不假设使用正则表达式在页面上找到 core.js。现在,当 PrimeFaces 处于正常模式时,它可以工作,并且如果使用 OmniFaces CombinedResourceHandler,它也可以工作。我将把它作为补丁提交给 PrimeFaces。

这是最终的工作方法:

/**
    * Builds a resource URL for given parameters.
    * 
    * @param {string}
    *        name The name of the resource. For example: primefaces.js
    * @param {string}
    *        library The library of the resource. For example: primefaces
    * @param {string}
    *        version The version of the library. For example: 5.1
    * @returns {string} The resource URL.
    */
   getFacesResource : function(name, library, version) {
      // just get sure - name shoudln't start with a slash
      if (name.indexOf('/') === 0) {
         name = name.substring(1, name.length);
      }

      // find any JS served JSF resource
      var scriptURI = $('script[src*="/' + PrimeFaces.RESOURCE_IDENTIFIER + '/"]').first().attr('src');
      // portlet
      if (!scriptURI) {
        scriptURI = $('script[src*="' + PrimeFaces.RESOURCE_IDENTIFIER + '="]').first().attr('src');
      }

      // find script...normal is '/core.js' and portlets are '=core.js'
      var scriptRegex = new RegExp('\\/' + PrimeFaces.RESOURCE_IDENTIFIER + '(\\/|=)(.*?)\\.js');

      // find script to replace e.g. 'core.js'
      var currentScriptName = scriptRegex.exec(scriptURI)[2] + '.js';

      // replace core.js with our custom name
      scriptURI = scriptURI.replace(currentScriptName, name);

      // find the library like ln=primefaces
      var libraryRegex = new RegExp('[?&]([^&=]*)ln=(.*?)(&|$)');

      // find library to replace e.g. 'ln=primefaces'
      var currentLibraryName = 'ln=' + libraryRegex.exec(scriptURI)[2];

      // In a portlet environment, url parameters may be namespaced.
      var namespace = '';
      var urlParametersAreNamespaced = !(scriptURI.indexOf('?' + currentLibraryName) > -1 || scriptURI.indexOf('&'
            + currentLibraryName) > -1);

      if (urlParametersAreNamespaced) {
         namespace = new RegExp('[?&]([^&=]+)' + currentLibraryName + '($|&)').exec(scriptURI)[1];
      }

      // If the parameters are namespaced, the namespace must be included
      // when replacing parameters.
      scriptURI = scriptURI.replace(namespace + currentLibraryName, namespace + 'ln=' + library);

      if (version) {
         var extractedVersion = new RegExp('[?&]' + namespace + 'v=([^&]*)').exec(scriptURI)[1];
         scriptURI = scriptURI.replace(namespace + 'v=' + extractedVersion, namespace + 'v=' + version);
      }

      var prefix = window.location.protocol + '//' + window.location.host;
      return scriptURI.indexOf(prefix) >= 0 ? scriptURI : prefix + scriptURI;
   },
Run Code Online (Sandbox Code Playgroud)