【问题标题】:How to apply badges in the sidebar counts using Mirage2 as theme如何使用 Mirage2 作为主题在侧边栏计数中应用徽章
【发布时间】:2018-10-16 09:41:53
【问题描述】:

我想将徽章应用于侧边栏分面中的数字。

来自:

至此:

我似乎找不到要编辑的正确文件。我尝试在第 248 行和第 260 行编辑SidebarFacetsTransformer.java

if (i < shownFacets - 1) {
    String displayedValue = value.getDisplayedValue();
    String filterQuery = value.getAsFilterQuery();
    String filterType = value.getFilterType();
    if (fqs.contains(getSearchService().toFilterQuery(context, field.getIndexFieldName(), value.getFilterType(), value.getAsFilterQuery()).getFilterQuery())) {
        filterValsList.addItem(Math.random() + "", "selected").addContent(displayedValue + " <span class=\"badge\">" + value.getCount() + "</span>");
        } else {
            String paramsQuery = retrieveParameters(request);

            filterValsList.addItem().addXref(
                    contextPath +
                            (dso == null ? "" : "/handle/" + dso.getHandle()) +
                            "/discover?" +
                            paramsQuery +
                            "filtertype=" + field.getIndexFieldName() +
                                                "&filter_relational_operator="+ filterType  +
                            "&filter=" + encodeForURL(filterQuery),
                            displayedValue + " <span class=\"badge\">" + value.getCount() + "</span>"
                    );
            }
    }

但是重建DSpace后,标签&lt;span class="badge"&gt;也显示出来了:

那么我应该编辑什么文件才能将徽章应用于侧边栏计数?我见过一个使用 Mirage2 作为其基本主题并在侧边栏中应用徽章的存储库:University of Waikato Research Commons

提前致谢!

更新

应用@schweerelos 回答中的代码后,除了Has Files(s) 的侧边栏外,我得到了想要的结果。它只是显示No,而不是数字。

【问题讨论】:

    标签: dspace


    【解决方案1】:

    对于 Research Commons 徽章,我们修改了dspace-xmlui-mirage2/src/main/webapp/xsl/preprocess.xsl,并截取了对应元素的渲染,即

    <!-- render frequency counts in sidebar facets as badges -->
    <xsl:template match="dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list/dri:item/dri:xref">
      <xref>
        <xsl:call-template name="copy-attributes"/>
        <xsl:choose>
          <xsl:when test="contains(text(), ' (') and contains(substring-after(text(), ' ('), ')')">
            <xsl:variable name="title">
              <xsl:call-template name="substring-before-last">
                <xsl:with-param name="string" select="text()"/>
                <xsl:with-param name="separator"><xsl:text> (</xsl:text></xsl:with-param>
              </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="count">
              <xsl:call-template name="remove-parens">
                <xsl:with-param name="string" select="normalize-space(substring-after(text(), $title))"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:value-of select="$title"/>
            <xsl:text> </xsl:text>
            <hi rend="badge">
              <xsl:value-of select="$count"/>
            </hi>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
      </xref>
    </xsl:template>
    

    还有一个用于当前选择的构面值的类似模板(IIRC 不是链接):

    <!-- better highlight active co-author/subject etc in facet list, incl show count as badge -->
    <xsl:template match="dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list/dri:item[@rend='selected']">
      <item>
        <xsl:call-template name="copy-attributes"/>
        <xsl:attribute name="rend"><xsl:value-of select="@rend"/> disabled</xsl:attribute>
        <xsl:choose>
          <xsl:when test="contains(text(), ' (') and contains(substring-after(text(), ' ('), ')')">
            <xsl:variable name="title">
              <xsl:call-template name="substring-before-last">
                <xsl:with-param name="string" select="text()"/>
                <xsl:with-param name="separator"><xsl:text> (</xsl:text></xsl:with-param>
              </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="count">
              <xsl:call-template name="remove-parens">
                <xsl:with-param name="string" select="normalize-space(substring-after(text(), $title))"/>
              </xsl:call-template>
            </xsl:variable>
           <xsl:value-of select="$title"/>
            <xsl:text> </xsl:text>
            <hi rend="badge">
             <xsl:value-of select="$count"/>
            </hi>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
      </item>
    </xsl:template>
    

    您会看到,涉及到一些 XSL/T 技巧来处理构面值本身包含括号的情况,并带有几个实用程序模板:

    <xsl:template name="substring-before-last">
      <xsl:param name="string" select="''" />
      <xsl:param name="separator" select="''" />
    
      <xsl:if test="$string != '' and $separator != ''">
        <xsl:variable name="head" select="substring-before($string, $separator)" />
        <xsl:variable name="tail" select="substring-after($string, $separator)" />
        <xsl:value-of select="$head" />
        <xsl:if test="contains($tail, $separator)">
          <xsl:value-of select="$separator" />
          <xsl:call-template name="substring-before-last">
            <xsl:with-param name="string" select="$tail" />
            <xsl:with-param name="separator" select="$separator" />
          </xsl:call-template>
        </xsl:if>
      </xsl:if>
    </xsl:template>
    
    <xsl:template name="remove-parens">
      <xsl:param name="string" select="''" />
      <xsl:if test="starts-with($string, '(') and substring($string, string-length($string))=')'">
        <xsl:value-of select="substring($string, 2, string-length($string) - 2)"/>
      </xsl:if>
    </xsl:template>
    

    只是要补充一点,我们在本地偏好修改主题 XSL 文件而不是修改 Java 代码。作为替代方案,您可以在 Java 代码中执行此操作,但您需要在 DRI API 中工作,例如,将 .addHighlight("badge") Java 方法调用与 addXref 一起调用(DRI hi 转换为 HTML span )。请参阅https://wiki.duraspace.org/display/DSDOC5x/DRI+Schema+Reference 上的 DRI 文档(对于 5.x,但对于 6.x 保持不变)。

    Cocoon 管道是 Java 生成 DRI (XML) -> preprocess.xsl 和相关文件修改 DRI 并且输出仍然是 DRI -> theme.xsl 和相关文件将 DRI 转换为 HTML。您正在尝试在管道中“过早”创建 HTML。您的原始 HTML 被进一步转义,这就是您在屏幕上看到标签的原因。

    【讨论】:

    • 嗨,安德里亚,非常感谢您!但我有个问题。在preprocess.xsl 中应用上面的代码后,我得到了我想要的输出,除了 Has Fiels(s)。它没有显示数字,而是显示 No 但应用了徽章。我相信这个侧边栏方面只在版本 6x 中引入。我认为上面的代码覆盖了core/utils.xsl:utils.xsl 中的代码。有任何想法吗?谢谢!
    • 嗨 euler,您应该能够通过更改我的前 2 个模板中的匹配以排除“有文件”方面的 DRI 列表元素,例如 @ 987654333@ - 是的,看起来这个方面在某处得到了特殊处理 - 所以你可能必须将排除与进一步修改 utils.xsl 结合起来。我们使用 5.x,所以没有那个方面。
    【解决方案2】:

    此时,我认为您只需要设置 span.badge 元素的样式即可。

    查看怀卡托大学研究共享资源,我看到以下 CSS。

    list-group-item>.badge {
        float: right;
    }
    .badge {
        display: inline-block;
        min-width: 10px;
        padding: 3px 7px;
        font-size: 12px;
        font-weight: bold;
        color: #fff;
        line-height: 1;
        vertical-align: baseline;
        white-space: nowrap;
        text-align: center;
        background-color: #777;
        border-radius: 10px;
    }
    

    【讨论】:

      【解决方案3】:

      感谢@schweerelos 的回答,我设法应用她的代码并进行了一些修改,以解决 DSpace 版本 6.x 中引入的 Has Files(s) 方面的问题。

      我在utils.xsl中取消了这段代码的注释:

      <xsl:template match="//dri:list[@id='aspect.discovery.SidebarFacetsTransformer.list.has_content_in_original_bundle']/dri:item//text()">
          <xsl:choose>
              <xsl:when test="contains(.,'true')">
                  <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_true</i18n:text>
              </xsl:when>
              <xsl:otherwise>
                  <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_false</i18n:text>
              </xsl:otherwise>
          </xsl:choose>
          <xsl:text> </xsl:text>
          <xsl:value-of select="substring-after(.,' ')"/>
      </xsl:template>
      

      然后我修改了 Andrea 的代码以显示 NoYes 用于具有 ORIGINAL 捆绑包的记录:

      <!-- render frequency counts in sidebar facets as badges -->
      <xsl:template match="dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list/dri:item/dri:xref" priority="9">
          <xref>
              <xsl:call-template name="copy-attributes"/>
              <xsl:choose>
                  <xsl:when test="contains(text(), ' (') and contains(substring-after(text(), ' ('), ')')">
                      <xsl:variable name="title">
                          <xsl:call-template name="substring-before-last">
                              <xsl:with-param name="string" select="text()"/>
                              <xsl:with-param name="separator"><xsl:text> (</xsl:text></xsl:with-param>
                          </xsl:call-template>
                      </xsl:variable>
                      <xsl:variable name="count">
                          <xsl:call-template name="remove-parens">
                              <xsl:with-param name="string" select="normalize-space(substring-after(text(), $title))"/>
                          </xsl:call-template>
                      </xsl:variable>
                      <xsl:choose>
                          <xsl:when test="contains(.,'true')">
                              <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_true</i18n:text>
                          </xsl:when>
                          <xsl:when test="contains(.,'false')">
                              <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_false</i18n:text>
                          </xsl:when>
                          <xsl:otherwise>
                              <xsl:value-of select="$title"/>
                          </xsl:otherwise>
                      </xsl:choose>
                      <xsl:text> </xsl:text>
                      <hi rend="badge">
                          <xsl:value-of select="$count"/>
                      </hi>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates/>
                  </xsl:otherwise>
              </xsl:choose>
          </xref>
      </xsl:template>
      

      现在它显示了我想要的输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-13
        • 2016-04-12
        • 1970-01-01
        • 2014-02-07
        • 2013-12-13
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        相关资源
        最近更新 更多