Changeset 8039 for branches/3.19-stable


Ignore:
Timestamp:
May 30, 2022, 7:52:36 AM (10 months ago)
Author:
Nicklas Nordborg
Message:

References #2278: Improvements to login page for better extensions

It is now possible to track scripts and stylesheets that are added to the JspContext and only activate those that are added by the a given extension. This is used on the login page to so that only the scripts and stylesheets that are needed for currently active login form are included in the generated HTML.

Location:
branches/3.19-stable
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/3.19-stable/src/clients/web/net/sf/basedb/clients/web/extensions/JspContext.java

    r6875 r8039  
    2323
    2424import java.util.Collection;
     25import java.util.HashMap;
    2526import java.util.HashSet;
     27import java.util.Map;
    2628import java.util.Set;
    2729
     
    8991  private final GuiContext guiContext;
    9092 
    91   private Set<String> scripts;
    92   private Set<String> stylesheets;
     93  private boolean needResourcesPerExtension;
     94  private Map<String, Set<String>> scripts;
     95  private Map<String, Set<String>> stylesheets;
    9396 
    9497  JspContext(SessionControl sc, DbControl dc,
     
    100103  }
    101104
     105  /**
     106    Set a flag indicating that resources (eg. scripts and stylesheets)
     107    should be tracked per extension. If this is set, it is possible to
     108    use {@link #getScripts(String)} and {@link #getStylesheets(String)}
     109    to get the resources that was added by the given extension.
     110    @since 3.19.3
     111  */
     112  public void setNeedResourcesPerExtension(boolean nrpe)
     113  {
     114    this.needResourcesPerExtension = nrpe;
     115  }
     116 
    102117  /**
    103118    Get the JSP Page context object for the current request.
     
    176191  public void addScript(String absolutePath)
    177192  {
    178     if (scripts == null) scripts = new HashSet<String>();
    179     scripts.add(absolutePath);
     193    if (scripts == null) scripts = new HashMap<>();
     194    getSet(scripts, null).add(absolutePath);
     195    if (needResourcesPerExtension)
     196    {
     197      getSet(scripts, getCurrentExtension().getId()).add(absolutePath);
     198    }
     199  }
     200 
     201  /**
     202    Get the Set that is stored under the given key. If
     203    no Set exists it is created and stored in the map.
     204  */
     205  private Set<String> getSet(Map<String, Set<String>> map, String key)
     206  {
     207    Set<String> set = map.get(key);
     208    if (set == null)
     209    {
     210      set = new HashSet<>();
     211      map.put(key, set);
     212    }
     213    return set;
    180214  }
    181215 
     
    200234  public void addStylesheet(String absolutePath)
    201235  {
    202     if (stylesheets == null) stylesheets = new HashSet<String>();
    203     stylesheets.add(absolutePath);
     236    if (stylesheets == null) stylesheets = new HashMap<>();
     237    getSet(stylesheets, null).add(absolutePath);
     238    if (needResourcesPerExtension)
     239    {
     240      getSet(stylesheets, getCurrentExtension().getId()).add(absolutePath);
     241    }
    204242  }
    205243
     
    210248  public Collection<String> getScripts()
    211249  {
    212     return scripts;
     250    return scripts == null ? null : scripts.get(null);
     251  }
     252 
     253  /**
     254    Get all scripts that has been added to this context
     255    by the given extension
     256    @return A collection of scripts
     257    @since 3.19.3
     258  */
     259  public Collection<String> getScripts(String xtId)
     260  {
     261    return scripts == null ? null : scripts.get(xtId);
    213262  }
    214263 
     
    219268  public Collection<String> getStylesheets()
    220269  {
    221     return stylesheets;
     270    return stylesheets == null ? null : stylesheets.get(null);
     271  }
     272 
     273  /**
     274    Get all stylesheets that has been added to this context
     275    by the given extension.
     276    @return A collection of stylesheets
     277    @since 3.19.3
     278  */
     279  public Collection<String> getStylesheets(String xtId)
     280  {
     281    return stylesheets == null ? null : stylesheets.get(xtId);
    222282  }
    223283 
  • branches/3.19-stable/src/clients/web/net/sf/basedb/clients/web/taglib/extensions/Scripts.java

    r7703 r8039  
    6363    </td>
    6464  </tr>
     65  <tr>
     66    <td>extension</td>
     67    <td>-</td>
     68    <td>no</td>
     69    <td>
     70      If set, only scripts defined by the given extension are
     71      included in the output.
     72    </td>
     73  </tr>
    6574  </table>
    6675
     
    7685  // The JSP context
    7786  private JspContext context = null;
     87  // If set, only output scripts defined by the given extension
     88  private String extensionId;
    7889
    7990  public void setContext(JspContext context)
    8091  {
    8192    this.context = context;
     93  }
     94 
     95  /**
     96    @since 3.19.3
     97  */
     98  public void setExtension(String extensionId)
     99  {
     100    this.extensionId = extensionId;
    82101  }
    83102
     
    88107    if (context == null) return SKIP_BODY;
    89108
    90     Collection<String> scripts = context.getScripts();
     109    Collection<String> scripts = context.getScripts(extensionId);
    91110    if (scripts == null) return SKIP_BODY;
    92111   
  • branches/3.19-stable/src/clients/web/net/sf/basedb/clients/web/taglib/extensions/Stylesheets.java

    r7703 r8039  
    6464    </td>
    6565  </tr>
     66  <tr>
     67    <td>extension</td>
     68    <td>-</td>
     69    <td>no</td>
     70    <td>
     71      If set, only scripts defined by the given extension are
     72      included in the output.
     73    </td>
     74  </tr>
    6675  </table>
    6776
     
    7786  // The JSP context
    7887  private JspContext context = null;
     88  // If set, only output scripts defined by the given extension
     89  private String extensionId;
    7990
    8091  public void setContext(JspContext context)
    8192  {
    8293    this.context = context;
     94  }
     95 
     96  /**
     97    @since 3.19.3
     98  */
     99  public void setExtension(String extensionId)
     100  {
     101    this.extensionId = extensionId;
    83102  }
    84103
     
    89108    if (context == null) return SKIP_BODY;
    90109
    91     Collection<String> stylesheets = context.getStylesheets();
     110    Collection<String> stylesheets = context.getStylesheets(extensionId);
    92111    if (stylesheets == null) return SKIP_BODY;
    93112   
  • branches/3.19-stable/src/core/net/sf/basedb/util/extensions/ClientContext.java

    r7895 r8039  
    6969  private final SessionControl sc;
    7070  private final DbControl dc;
     71  private ExtensionPoint<?> currentXtPoint;
     72  private Extension<?> currentXt;
    7173  private Object item;
    7274  private Map<String, Object> attributes;
     
    173175  {
    174176    this.item = item;
     177  }
     178 
     179  /**
     180    Get the currently active extension point.
     181    @since 3.19.3
     182  */
     183  public ExtensionPoint<?> getCurrentExtensionPoint()
     184  {
     185    return currentXtPoint;
     186  }
     187  protected void setCurrentExtensionPoint(ExtensionPoint<?> xtPoint)
     188  {
     189    this.currentXtPoint = xtPoint;
     190  }
     191 
     192  /**
     193    Get the currently active extension point.
     194    @since 3.19.3
     195  */
     196  public Extension<?> getCurrentExtension()
     197  {
     198    return currentXt;
     199  }
     200  protected void setCurrentExtension(Extension<?> xt)
     201  {
     202    this.currentXt = xt;
    175203  }
    176204 
  • branches/3.19-stable/src/core/net/sf/basedb/util/extensions/Registry.java

    r7642 r8039  
    673673
    674674      // YES! ...
     675      if (clientContext != null) clientContext.setCurrentExtensionPoint(rep);
    675676      ErrorHandlerFactory ehf = rep.getErrorHandlerFactory();
    676677      ExtensionPointContext<Action> mainContext = new ExtensionPointContext<Action>(this,
     
    690691
    691692        // Create invokation context for the extension
     693        if (clientContext != null) clientContext.setCurrentExtension(ext);
    692694        ExtensionContext<A> context =
    693695          new ExtensionContext(mainContext, ext);
  • branches/3.19-stable/www/WEB-INF/extensions.tld

    r7604 r8039  
    4141      <rtexprvalue>true</rtexprvalue>
    4242    </attribute>
     43    <attribute>
     44      <name>extension</name>
     45      <required>false</required>
     46      <rtexprvalue>true</rtexprvalue>
     47    </attribute>
    4348  </tag>
    4449
     
    5055      <name>context</name>
    5156      <required>true</required>
     57      <rtexprvalue>true</rtexprvalue>
     58    </attribute>
     59    <attribute>
     60      <name>extension</name>
     61      <required>false</required>
    5262      <rtexprvalue>true</rtexprvalue>
    5363    </attribute>
  • branches/3.19-stable/www/main.jsp

    r8037 r8039  
    8888  ItemResultIterator<News> news = null;
    8989  JspContext jspContext = ExtensionsControl.createContext(dc, pageContext);
     90  jspContext.setNeedResourcesPerExtension(true);
    9091  ExtensionsInvoker<LoginFormAction> invoker = ExtensionsControl.useExtensions(jspContext, "net.sf.basedb.clients.web.login-form");
    9192
    9293  LoginFormAction loginAction = null;
    9394  String selectedLoginForm = null;
     95  String selectedExtension = null;
    9496  Map<String, String> allForms = new TreeMap<String, String>();
    9597 
     
    110112        loginAction = action;
    111113        selectedLoginForm = formId;
     114        selectedExtension = it.getExtension().getId();
    112115      }
    113116    }
     
    125128  <base:page type="default">
    126129  <base:head styles="login.css" scripts="~login.js">
    127     <ext:scripts context="<%=jspContext%>" />
    128     <ext:stylesheets context="<%=jspContext%>" />
     130    <ext:scripts context="<%=jspContext%>" extension="<%=selectedExtension%>" />
     131    <ext:stylesheets context="<%=jspContext%>" extension="<%=selectedExtension%>" />
    129132  </base:head>
    130133  <base:body style="padding-top: 5em;" data-login-form="<%=HTML.encodeTags(selectedLoginForm)%>" data-requested-form="<%=HTML.encodeTags(requestedLoginForm) %>">
  • branches/3.19-stable/www/switch.jsp

    r8037 r8039  
    6565{
    6666  JspContext jspContext = ExtensionsControl.createContext(dc, pageContext);
     67  jspContext.setNeedResourcesPerExtension(true);
    6768  ExtensionsInvoker<LoginFormAction> invoker = ExtensionsControl.useExtensions(jspContext, "net.sf.basedb.clients.web.login-form");
    6869
    6970  LoginFormAction loginAction = null;
    7071  String selectedLoginForm = null;
     72  String selectedExtension = null;
    7173  Map<String, String> allForms = new TreeMap<String, String>();
    7274
     
    8789        loginAction = action;
    8890        selectedLoginForm = formId;
     91        selectedExtension = it.getExtension().getId();
    8992      }
    9093    }
     
    101104  <base:page type="popup" title="Switch user">
    102105  <base:head styles="login.css" scripts="~login.js">
    103     <ext:scripts context="<%=jspContext%>" />
    104     <ext:stylesheets context="<%=jspContext%>" />
     106    <ext:scripts context="<%=jspContext%>" extension="<%=selectedExtension%>" />
     107    <ext:stylesheets context="<%=jspContext%>" extension="<%=selectedExtension%>" />
    105108  </base:head>
    106109  <base:body data-login-form="<%=HTML.encodeTags(selectedLoginForm)%>" data-requested-form="<%=HTML.encodeTags(requestedLoginForm) %>">
Note: See TracChangeset for help on using the changeset viewer.