Permalink
Browse files

JENKINS-45399 fix link to view referenced config file

  • Loading branch information...
imod committed Aug 4, 2017
1 parent 5731938 commit e76f2e587a1f2f19114bc073d7041317fa712805
@@ -28,16 +28,17 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.Util;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigFile;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.jenkinsci.plugins.configfiles.utils.ConfigFileDetailLinkDescription;
import org.kohsuke.stapler.*;

import java.io.Serializable;

@@ -50,7 +51,6 @@ of this software and associated documentation files (the "Software"), to deal

/**
* @param fileId the id of the file to be provided
*
* @since 2.12
*/
@DataBoundConstructor
@@ -115,6 +115,22 @@ public ListBoxModel doFillFileIdItems(@AncestorInPath ItemGroup context) {
return items;
}

/**
* validate that an existing config was chosen
*
* @param context the context this file is configured in
* @param fileId the id of the config file
* @return a validation result / description
*/
public HttpResponse doCheckFileId(StaplerRequest req, @AncestorInPath Item context, @QueryParameter String fileId) {
final Config config = ConfigFiles.getByIdOrNull(context, fileId);
if (config != null) {
return ConfigFileDetailLinkDescription.getDescription(req, context, fileId);
} else {
return FormValidation.error("you must select a valid file");
}
}

}
}

@@ -0,0 +1,30 @@
package org.jenkinsci.plugins.configfiles.utils;

import hudson.model.Item;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

public class ConfigFileDetailLinkDescription extends DescriptionResponse {
private ConfigFileDetailLinkDescription(String linkHtml) {
super(linkHtml);
}

public static ConfigFileDetailLinkDescription getDescription(StaplerRequest req, Item context, String fileId) {
return new ConfigFileDetailLinkDescription(getDetailsLink(req, context, fileId));
}

private static String getDetailsLink(StaplerRequest req, Item context, String fileId) {
String link = req.getContextPath();
link = StringUtils.isNotBlank(context.getUrl()) ? link + "/" + context.getUrl() : link;
link = link + "configfiles/show?id=" + fileId;
String linkHtml = "<a target=\"_blank\" href=\"" + link + "\">view selected file</a>";

// 1x16 spacer needed for IE since it doesn't support min-height
return "<div class='ok'><img src='" +
req.getContextPath() + Jenkins.RESOURCE_PATH + "/images/none.gif' height=16 width=1>" +
linkHtml + "</div>";

}

}
@@ -0,0 +1,31 @@
package org.jenkinsci.plugins.configfiles.utils;

import hudson.util.FormValidation;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.IOException;

/**
* Can be used instead of a {@link FormValidation#ok(String)} to give the user some information about
* the item to checked (e.g. a selection in a dropdown)
*/
public class DescriptionResponse implements HttpResponse {

private String html = "div/>";

public DescriptionResponse(String html) {
if (StringUtils.isNotBlank(html)) {
this.html = html;
}
}

@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
rsp.setContentType("text/html;charset=UTF-8");
rsp.getWriter().print(html);
}
}
@@ -9,20 +9,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<st:once>
<script type="text/javascript" src="${rootURL}/plugin/config-file-provider/js/fileprovider.js" />
</st:once>

<f:entry title="File" field="fileId">
<f:select />
</f:entry>

<tr><td></td>
<td></td>
<td><a target="_blank" name="showFileDetailLink" style="display:none;" href="" onclick="window.open(this.href,'window','width=900,height=640,resizable,scrollbars,toolbar,menubar') ;return false;">view selected file</a></td>
<td></td>
</tr>

<f:entry title="Target" field="targetLocation">
<f:textbox />
</f:entry>
@@ -42,25 +32,4 @@
</div>
</f:entry>

<st:once>
<script type="text/javascript">

function fp_updateDetailLink(event) {
fp_initDetailLink('<j:out value="${rootURL}/${it.url}" />', event.currentTarget, '_.fileId');
}

function fp_initAllDetailLinks() {
var all = new Array();
all = document.getElementsByName('_.fileId');
for(var i = 0; i &lt; all.length; i++) {
fp_initDetailLink('<j:out value="${rootURL}/${it.url}" />', all.item(i), '_.fileId');
Event.stopObserving(all.item(i), 'change', fp_updateDetailLink);
Event.observe(all.item(i), 'change', fp_updateDetailLink);
}
}

Event.observe(window, 'load', fp_initAllDetailLinks);
</script>
</st:once>

</j:jelly>

This file was deleted.

Oops, something went wrong.

0 comments on commit e76f2e5

Please sign in to comment.