Skip to content

Commit

Permalink
Correct order of property loading for simple pages
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed Aug 21, 2023
1 parent 533209d commit 81a20c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?jelly escape-by-default='false'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<script id="theme-manager-theme" type="application/json">{ "id": "${it.themeKey}" }</script>
<st:adjunct includes="io.jenkins.plugins.thememanager.header.main" />
<script id="theme-manager-theme" type="application/json">{ "id": "${it.themeKey}", "respect_system_appearance": ${it.respectSystemAppearance} }</script>
${it.getHeaderHtml()}
<st:adjunct includes="io.jenkins.plugins.thememanager.header.main" />
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?jelly escape-by-default='false'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<script id="theme-manager-theme" type="application/json">{ "id": "${it.themeKey}" }</script>
<st:adjunct includes="io.jenkins.plugins.thememanager.header.main" />
<j:new var="h" className="hudson.Functions"/>
<!-- Load the default styles -->
<st:include it="${h.simplePageDecorators.get(h.simplePageDecorators.size() - 1)}"
<st:include it="${h.simplePageDecorators.get(h.simplePageDecorators.size() - 1)}"
page="simple-head.jelly"
optional="true"/>

<script id="theme-manager-theme" type="application/json">{ "id": "${it.themeKey}", "respect_system_appearance": ${it.respectSystemAppearance} }</script>
<st:adjunct includes="io.jenkins.plugins.thememanager.header.main" />
${it.getHeaderHtml()}
</j:jelly>
20 changes: 11 additions & 9 deletions src/main/resources/io/jenkins/plugins/thememanager/header/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
}
window.isSystemRespectingTheme = theme.respect_system_appearance

const propertiesJson = document.getElementById('theme-manager-properties').text
const parsedProperties = JSON.parse(propertiesJson);
const propertiesJson = document.getElementById('theme-manager-properties')
// may not be present, e.g. on BlueOcean where the theme is not marked as BlueOcean compatible
if (propertiesJson) {
const parsedProperties = JSON.parse(propertiesJson.text);

window.getThemeManagerProperty = function (plugin, propertyName) {
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches

window.getThemeManagerProperty = function (plugin, propertyName) {
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches

let propertyNameNormalised = propertyName
if (isSystemRespectingTheme) {
propertyNameNormalised = isDark ? `${propertyName}-dark` : `${propertyName}-light`
let propertyNameNormalised = propertyName
if (isSystemRespectingTheme) {
propertyNameNormalised = isDark ? `${propertyName}-dark` : `${propertyName}-light`
}
return parsedProperties[`${plugin}:${propertyNameNormalised}`]
}
return parsedProperties[`${plugin}:${propertyNameNormalised}`]
}
})()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.jenkins.plugins.thememanager;

import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

public class PageDecoratorTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testJavaScriptLoadsOnSimpleAndRegularPages() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy().
grant(Jenkins.READ).everywhere().to("alice");
j.jenkins.setAuthorizationStrategy(auth);

JenkinsRule.WebClient wc = j.createWebClient();

wc.login("alice");
wc.goTo("");
}
}

0 comments on commit 81a20c6

Please sign in to comment.