support search
This commit is contained in:
parent
8cf0a0c377
commit
8f40954f75
|
@ -125,7 +125,7 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate {
|
|||
tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
|
||||
tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
|
||||
tpl.binding(TemplateVariable.VERSION.getVariable(), now);
|
||||
tpl.binding(TemplateVariable.INDEX_ALIAS.getVariable(), apiDoc.getAlias());
|
||||
|
||||
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
|
||||
tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
|
||||
tpl.binding(TemplateVariable.REQUEST_EXAMPLE.getVariable(), config.isRequestExample());
|
||||
|
@ -136,6 +136,7 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate {
|
|||
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
||||
}
|
||||
if (Objects.nonNull(apiDoc)) {
|
||||
tpl.binding(TemplateVariable.INDEX_ALIAS.getVariable(), apiDoc.getAlias());
|
||||
tpl.binding(TemplateVariable.DESC.getVariable(), apiDoc.getDesc());
|
||||
tpl.binding(TemplateVariable.ORDER.getVariable(), apiDoc.order);
|
||||
tpl.binding(TemplateVariable.LIST.getVariable(), apiDoc.getList());//类名
|
||||
|
@ -184,6 +185,7 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate {
|
|||
errorTemplate.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
||||
}
|
||||
List<ApiDocDict> apiDocDictList = buildDictionary(config, javaProjectBuilder);
|
||||
errorTemplate.binding(TemplateVariable.VERSION.getVariable(), now);
|
||||
errorTemplate.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);
|
||||
errorTemplate.binding(TemplateVariable.INDEX_ALIAS.getVariable(), indexAlias);
|
||||
errorTemplate.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
||||
|
@ -217,6 +219,7 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate {
|
|||
} else {
|
||||
mapper.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
||||
}
|
||||
mapper.binding(TemplateVariable.VERSION.getVariable(), now);
|
||||
mapper.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
||||
mapper.binding(TemplateVariable.INDEX_ALIAS.getVariable(), indexAlias);
|
||||
mapper.binding(TemplateVariable.BACKGROUND.getVariable(), HighlightStyle.getBackgroundColor(style));
|
||||
|
|
|
@ -24,6 +24,7 @@ package com.power.doc.builder;
|
|||
|
||||
import com.power.common.util.DateTimeUtil;
|
||||
import com.power.common.util.FileUtil;
|
||||
import com.power.doc.constants.TemplateVariable;
|
||||
import com.power.doc.model.ApiConfig;
|
||||
import com.power.doc.model.ApiDoc;
|
||||
import com.power.doc.template.IDocBuildTemplate;
|
||||
|
@ -80,25 +81,31 @@ public class HtmlApiDocBuilder {
|
|||
INDEX_HTML = config.getAllInOneDocFileName();
|
||||
}
|
||||
if (config.isCreateDebugPage()) {
|
||||
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_TPL, DEBUG_PAGE_ALL_TPL);
|
||||
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_ALL_TPL, DEBUG_PAGE_ALL_TPL);
|
||||
Template mockJs = BeetlTemplateUtil.getByName("js/mock.js");
|
||||
FileUtil.nioWriteFile(mockJs.render(), config.getOutPath() + FILE_SEPARATOR + "mock.js");
|
||||
} else {
|
||||
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, ALL_IN_ONE_HTML_TPL, INDEX_HTML);
|
||||
}
|
||||
buildSearchJs(apiDocList, config,"js/search_all.js.btl");
|
||||
} else {
|
||||
String indexAlias;
|
||||
if (config.isCreateDebugPage()) {
|
||||
indexAlias = "mock";
|
||||
buildDoc(builderTemplate, apiDocList, config, javaProjectBuilder, DEBUG_PAGE_SINGLE_TPL, indexAlias);
|
||||
Template mockJs = BeetlTemplateUtil.getByName("js/mock.js");
|
||||
FileUtil.nioWriteFile(mockJs.render(), config.getOutPath() + FILE_SEPARATOR + "mock.js");
|
||||
} else {
|
||||
indexAlias = "api";
|
||||
buildDoc(builderTemplate, apiDocList, config, javaProjectBuilder, SINGLE_INDEX_HTML_TPL, indexAlias);
|
||||
}
|
||||
builderTemplate.buildErrorCodeDoc(config, javaProjectBuilder, apiDocList, SINGLE_ERROR_HTML_TPL,
|
||||
"error.html",indexAlias);
|
||||
"error.html", indexAlias);
|
||||
builderTemplate.buildDirectoryDataDoc(config, javaProjectBuilder, apiDocList,
|
||||
SINGLE_DICT_HTML_TPL, "dict.html",indexAlias);
|
||||
|
||||
SINGLE_DICT_HTML_TPL, "dict.html", indexAlias);
|
||||
buildSearchJs(apiDocList, config,"js/search.js.btl");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -125,4 +132,10 @@ public class HtmlApiDocBuilder {
|
|||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildSearchJs(List<ApiDoc> apiDocList, ApiConfig config, String template) {
|
||||
Template tpl = BeetlTemplateUtil.getByName(template);
|
||||
tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
||||
FileUtil.nioWriteFile(tpl.render(), config.getOutPath() + FILE_SEPARATOR + "search.js");
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -109,6 +109,7 @@
|
|||
href="https://github.com/smart-doc-group/smart-doc.git" target="_blank"> Github</a></span>
|
||||
</footer>
|
||||
</div>
|
||||
<script src="search.js?v=${version}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
|
|
|
@ -113,6 +113,7 @@
|
|||
href="https://github.com/smart-doc-group/smart-doc.git" target="_blank"> Github</a></span>
|
||||
</footer>
|
||||
</div>
|
||||
<script src="search.js?v=${version}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
|
|
|
@ -265,6 +265,7 @@
|
|||
href="https://github.com/smart-doc-group/smart-doc.git" target="_blank"> Github</a></span>
|
||||
</footer>
|
||||
</div>
|
||||
<script src="search.js?v=${version}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
|
|
|
@ -313,236 +313,7 @@
|
|||
href="https://github.com/smart-doc-group/smart-doc.git" target="_blank"> Github</a></span>
|
||||
</footer>
|
||||
</div>
|
||||
<script>
|
||||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
this.el = el || {};
|
||||
this.multiple = multiple || false;
|
||||
const links = this.el.find('.dd');
|
||||
links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
|
||||
};
|
||||
Accordion.prototype.dropdown = function (e) {
|
||||
const $el = e.data.el;
|
||||
const $this = $(this), $next = $this.next();
|
||||
$next.slideToggle();
|
||||
$this.parent().toggleClass('open');
|
||||
if (!e.data.multiple) {
|
||||
$el.find('.submenu').not($next).slideUp("20").parent().removeClass('open');
|
||||
}
|
||||
};
|
||||
new Accordion($('#accordion'), false);
|
||||
});
|
||||
$('textarea').each(function () {
|
||||
this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
|
||||
}).on('input', function () {
|
||||
this.style.height = '80px';
|
||||
this.style.height = (this.scrollHeight) + 'px';
|
||||
});
|
||||
$("button").on("click", function () {
|
||||
const $this = $(this);
|
||||
const id = $this.data("id");
|
||||
console.log("method-id=>" + id);
|
||||
|
||||
let body = $("#" + id + "-body").val();
|
||||
|
||||
// header
|
||||
const $headerElement = $("#" + id + "-header");
|
||||
const headersData = getInputData($headerElement);
|
||||
|
||||
// body param
|
||||
const $paramElement = $("#" + id + "-param");
|
||||
let bodyParamData = getInputData($paramElement)
|
||||
|
||||
// path param
|
||||
const $pathElement = $("#" + id + "-path-params")
|
||||
const pathParamData = getInputData($pathElement)
|
||||
|
||||
// query param
|
||||
const $queryElement = $("#" + id + "-query-params")
|
||||
|
||||
|
||||
const url = $("#" + id + "-url").data("url");
|
||||
const isDownload = $("#" + id + "-url").data("download");
|
||||
const method = $("#" + id + "-method").data("method");
|
||||
const contentType = $("#" + id + "-content-type").data("content-type");
|
||||
console.log("request-headers=>" + JSON.stringify(headersData))
|
||||
console.log("path-params=>" + JSON.stringify(pathParamData))
|
||||
|
||||
console.log("body-params=>" + JSON.stringify(bodyParamData))
|
||||
console.log("json-body=>" + body);
|
||||
|
||||
let queryParamData = "";
|
||||
if (isDownload) {
|
||||
queryParamData = getInputData($queryElement);
|
||||
download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType);
|
||||
return;
|
||||
}
|
||||
const ajaxOptions = {};
|
||||
|
||||
|
||||
let finalUrl = "";
|
||||
if ("multipart/form-data" == contentType) {
|
||||
finalUrl = castToGetUri(url, pathParamData);
|
||||
queryParamData = getInputData($queryElement, true)
|
||||
body = queryParamData;
|
||||
ajaxOptions.processData = false;
|
||||
ajaxOptions.contentType = false;
|
||||
} else {
|
||||
queryParamData = getInputData($queryElement)
|
||||
finalUrl = castToGetUri(url, pathParamData, queryParamData)
|
||||
ajaxOptions.contentType = contentType;
|
||||
}
|
||||
console.log("query-params=>" + JSON.stringify(queryParamData));
|
||||
console.log("url=>" + finalUrl)
|
||||
ajaxOptions.headers = headersData
|
||||
ajaxOptions.url = finalUrl
|
||||
ajaxOptions.type = method
|
||||
ajaxOptions.data = body;
|
||||
|
||||
const ajaxTime = new Date().getTime();
|
||||
$.ajax(ajaxOptions).done(function (result, textStatus, jqXHR) {
|
||||
$this.css("background", "#5cb85c");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(result, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).fail(function (jqXHR) {
|
||||
$this.css("background", "#D44B47");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(jqXHR.responseJSON, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).always(function () {
|
||||
|
||||
});
|
||||
})
|
||||
$(".check-all").on("click", function () {
|
||||
const checkboxName = $(this).prop("name");
|
||||
const checked = $(this).is(':checked');
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
$('input[name="' + checkboxName + '"]').each(function () {
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function castToGetUri(url, pathParams, params) {
|
||||
if (pathParams instanceof Object && !(pathParams instanceof Array)) {
|
||||
url = url.format(pathParams)
|
||||
}
|
||||
if (params instanceof Object && !(params instanceof Array)) {
|
||||
const pm = params || {};
|
||||
const arr = [];
|
||||
arr.push(url);
|
||||
let j = 0;
|
||||
for (const i in pm) {
|
||||
if (j === 0) {
|
||||
arr.push("?");
|
||||
arr.push(i + "=" + pm[i]);
|
||||
} else {
|
||||
arr.push("&" + i + "=" + pm[i]);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return arr.join("");
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
function getInputData(element, returnFormDate) {
|
||||
const formData = new FormData();
|
||||
$(element).find("tr").each(function (i) {
|
||||
const checked = $(this).find('td:eq(0)').children(".checkbox").children("input").is(':checked');
|
||||
if (checked) {
|
||||
const input = $(this).find('td:eq(2) input');
|
||||
console.log("input type:" + $(input).attr("type"))
|
||||
const name = $(input).attr("name");
|
||||
if ($(input).attr("type") == "file") {
|
||||
formData.append(name, $(input)[0].files[0]);
|
||||
} else {
|
||||
const val = $(input).val();
|
||||
formData.append(name, val);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (returnFormDate) {
|
||||
return formData;
|
||||
}
|
||||
const headersData = {};
|
||||
formData.forEach((value, key) => headersData[key] = value);
|
||||
return headersData;
|
||||
}
|
||||
|
||||
String.prototype.format = function (args) {
|
||||
let reg;
|
||||
if (arguments.length > 0) {
|
||||
let result = this;
|
||||
if (arguments.length == 1 && typeof (args) == "object") {
|
||||
for (const key in args) {
|
||||
reg = new RegExp("({" + key + "})", "g");
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (arguments[i] == undefined) {
|
||||
return "";
|
||||
} else {
|
||||
reg = new RegExp("({[" + i + "]})", "g");
|
||||
result = result.replace(reg, arguments[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
function download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType) {
|
||||
url = castToGetUri(url, pathParamData, queryParamData)
|
||||
const xmlRequest = new XMLHttpRequest();
|
||||
xmlRequest.open(method, url, true);
|
||||
xmlRequest.setRequestHeader("Content-type", contentType);
|
||||
for (let key in headersData) {
|
||||
xmlRequest.setRequestHeader(key, headersData[key])
|
||||
}
|
||||
xmlRequest.responseType = "blob";
|
||||
xmlRequest.onload = function () {
|
||||
if (this.status === 200) {
|
||||
let fileName = decodeURI(xmlRequest.getResponseHeader('filename'));
|
||||
console.log(fileName);
|
||||
const blob = this.response;
|
||||
if (navigator.msSaveBlob) {
|
||||
// IE10 can't do a[download], only Blobs:
|
||||
window.navigator.msSaveBlob(blob, fileName);
|
||||
return;
|
||||
}
|
||||
if (window.URL) { // simple fast and modern way using Blob and URL:
|
||||
const a = document.createElement("a");
|
||||
document.body.appendChild(a);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
console.log(fileName);
|
||||
} else {
|
||||
console.log("download failed");
|
||||
}
|
||||
};
|
||||
try {
|
||||
xmlRequest.send(bodyParamData);
|
||||
} catch (e) {
|
||||
console.error("Failed to send data", e);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script src="mock.js?v=${version}"></script>
|
||||
<script src="search.js?v=${version}"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -0,0 +1,229 @@
|
|||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
this.el = el || {};
|
||||
this.multiple = multiple || false;
|
||||
const links = this.el.find('.dd');
|
||||
links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
|
||||
};
|
||||
Accordion.prototype.dropdown = function (e) {
|
||||
const $el = e.data.el;
|
||||
const $this = $(this), $next = $this.next();
|
||||
$next.slideToggle();
|
||||
$this.parent().toggleClass('open');
|
||||
if (!e.data.multiple) {
|
||||
$el.find('.submenu').not($next).slideUp("20").parent().removeClass('open');
|
||||
}
|
||||
};
|
||||
new Accordion($('#accordion'), false);
|
||||
});
|
||||
$('textarea').each(function () {
|
||||
this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
|
||||
}).on('input', function () {
|
||||
this.style.height = '80px';
|
||||
this.style.height = (this.scrollHeight) + 'px';
|
||||
});
|
||||
$("button").on("click", function () {
|
||||
const $this = $(this);
|
||||
const id = $this.data("id");
|
||||
console.log("method-id=>" + id);
|
||||
|
||||
let body = $("#" + id + "-body").val();
|
||||
|
||||
// header
|
||||
const $headerElement = $("#" + id + "-header");
|
||||
const headersData = getInputData($headerElement);
|
||||
|
||||
// body param
|
||||
const $paramElement = $("#" + id + "-param");
|
||||
let bodyParamData = getInputData($paramElement)
|
||||
|
||||
// path param
|
||||
const $pathElement = $("#" + id + "-path-params")
|
||||
const pathParamData = getInputData($pathElement)
|
||||
|
||||
// query param
|
||||
const $queryElement = $("#" + id + "-query-params")
|
||||
|
||||
|
||||
const url = $("#" + id + "-url").data("url");
|
||||
const isDownload = $("#" + id + "-url").data("download");
|
||||
const method = $("#" + id + "-method").data("method");
|
||||
const contentType = $("#" + id + "-content-type").data("content-type");
|
||||
console.log("request-headers=>" + JSON.stringify(headersData))
|
||||
console.log("path-params=>" + JSON.stringify(pathParamData))
|
||||
|
||||
console.log("body-params=>" + JSON.stringify(bodyParamData))
|
||||
console.log("json-body=>" + body);
|
||||
|
||||
let queryParamData = "";
|
||||
if (isDownload) {
|
||||
queryParamData = getInputData($queryElement);
|
||||
download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType);
|
||||
return;
|
||||
}
|
||||
const ajaxOptions = {};
|
||||
|
||||
|
||||
let finalUrl = "";
|
||||
if ("multipart/form-data" == contentType) {
|
||||
finalUrl = castToGetUri(url, pathParamData);
|
||||
queryParamData = getInputData($queryElement, true)
|
||||
body = queryParamData;
|
||||
ajaxOptions.processData = false;
|
||||
ajaxOptions.contentType = false;
|
||||
} else {
|
||||
queryParamData = getInputData($queryElement)
|
||||
finalUrl = castToGetUri(url, pathParamData, queryParamData)
|
||||
ajaxOptions.contentType = contentType;
|
||||
}
|
||||
console.log("query-params=>" + JSON.stringify(queryParamData));
|
||||
console.log("url=>" + finalUrl)
|
||||
ajaxOptions.headers = headersData
|
||||
ajaxOptions.url = finalUrl
|
||||
ajaxOptions.type = method
|
||||
ajaxOptions.data = body;
|
||||
|
||||
const ajaxTime = new Date().getTime();
|
||||
$.ajax(ajaxOptions).done(function (result, textStatus, jqXHR) {
|
||||
$this.css("background", "#5cb85c");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(result, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).fail(function (jqXHR) {
|
||||
$this.css("background", "#D44B47");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(jqXHR.responseJSON, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).always(function () {
|
||||
|
||||
});
|
||||
})
|
||||
$(".check-all").on("click", function () {
|
||||
const checkboxName = $(this).prop("name");
|
||||
const checked = $(this).is(':checked');
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
$('input[name="' + checkboxName + '"]').each(function () {
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function castToGetUri(url, pathParams, params) {
|
||||
if (pathParams instanceof Object && !(pathParams instanceof Array)) {
|
||||
url = url.format(pathParams)
|
||||
}
|
||||
if (params instanceof Object && !(params instanceof Array)) {
|
||||
const pm = params || {};
|
||||
const arr = [];
|
||||
arr.push(url);
|
||||
let j = 0;
|
||||
for (const i in pm) {
|
||||
if (j === 0) {
|
||||
arr.push("?");
|
||||
arr.push(i + "=" + pm[i]);
|
||||
} else {
|
||||
arr.push("&" + i + "=" + pm[i]);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return arr.join("");
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
function getInputData(element, returnFormDate) {
|
||||
const formData = new FormData();
|
||||
$(element).find("tr").each(function (i) {
|
||||
const checked = $(this).find('td:eq(0)').children(".checkbox").children("input").is(':checked');
|
||||
if (checked) {
|
||||
const input = $(this).find('td:eq(2) input');
|
||||
console.log("input type:" + $(input).attr("type"))
|
||||
const name = $(input).attr("name");
|
||||
if ($(input).attr("type") == "file") {
|
||||
formData.append(name, $(input)[0].files[0]);
|
||||
} else {
|
||||
const val = $(input).val();
|
||||
formData.append(name, val);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (returnFormDate) {
|
||||
return formData;
|
||||
}
|
||||
const headersData = {};
|
||||
formData.forEach((value, key) => headersData[key] = value);
|
||||
return headersData;
|
||||
}
|
||||
|
||||
String.prototype.format = function (args) {
|
||||
let reg;
|
||||
if (arguments.length > 0) {
|
||||
let result = this;
|
||||
if (arguments.length == 1 && typeof (args) == "object") {
|
||||
for (const key in args) {
|
||||
reg = new RegExp("({" + key + "})", "g");
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (arguments[i] == undefined) {
|
||||
return "";
|
||||
} else {
|
||||
reg = new RegExp("({[" + i + "]})", "g");
|
||||
result = result.replace(reg, arguments[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
function download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType) {
|
||||
url = castToGetUri(url, pathParamData, queryParamData)
|
||||
const xmlRequest = new XMLHttpRequest();
|
||||
xmlRequest.open(method, url, true);
|
||||
xmlRequest.setRequestHeader("Content-type", contentType);
|
||||
for (let key in headersData) {
|
||||
xmlRequest.setRequestHeader(key, headersData[key])
|
||||
}
|
||||
xmlRequest.responseType = "blob";
|
||||
xmlRequest.onload = function () {
|
||||
if (this.status === 200) {
|
||||
let fileName = decodeURI(xmlRequest.getResponseHeader('filename'));
|
||||
console.log(fileName);
|
||||
const blob = this.response;
|
||||
if (navigator.msSaveBlob) {
|
||||
// IE10 can't do a[download], only Blobs:
|
||||
window.navigator.msSaveBlob(blob, fileName);
|
||||
return;
|
||||
}
|
||||
if (window.URL) { // simple fast and modern way using Blob and URL:
|
||||
const a = document.createElement("a");
|
||||
document.body.appendChild(a);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
console.log(fileName);
|
||||
} else {
|
||||
console.log("download failed");
|
||||
}
|
||||
};
|
||||
try {
|
||||
xmlRequest.send(bodyParamData);
|
||||
} catch (e) {
|
||||
console.error("Failed to send data", e);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
let api = [];
|
||||
document.onkeydown = keyDownSearch;
|
||||
|
||||
function keyDownSearch(e) {
|
||||
let j;
|
||||
let m;
|
||||
let doc;
|
||||
let html;
|
||||
const theEvent = e || window.event;
|
||||
const code = theEvent.keyCode || theEvent.which || theEvent.charCode;
|
||||
if (code == 13) {
|
||||
const search = document.getElementById('search');
|
||||
const searchArr = [];
|
||||
<%for(api in apiDocList){%>
|
||||
searchArr.push({
|
||||
alias: '${api.alias}',
|
||||
order: '${api.order}',
|
||||
desc: '${api.desc}',
|
||||
list: []
|
||||
})
|
||||
api.push({
|
||||
alias: '${api.alias}',
|
||||
order: '${api.order}',
|
||||
desc: '${api.desc}',
|
||||
list: []
|
||||
})
|
||||
|
||||
<%for (doc in api.list) {%>
|
||||
api[${apiLP.dataIndex}].list.push({
|
||||
order: '${doc.order}',
|
||||
desc: '${doc.desc}',
|
||||
});
|
||||
if ('${doc.desc}'.indexOf(search.value) > -1) {
|
||||
searchArr[${apiLP.dataIndex}].list.push({
|
||||
order: '${doc.order}',
|
||||
desc: '${doc.desc}',
|
||||
});
|
||||
}
|
||||
<%}%>
|
||||
<%}%>
|
||||
const searchResult = searchArr.filter(obj => obj.list.length > 0);
|
||||
if (searchResult.length > 0) {
|
||||
html = '';
|
||||
for (j = 0; j < searchResult.length; j++) {
|
||||
html += '<li class="open">';
|
||||
html += ' <a class="dd" href="' + searchResult[j].alias + '.html#header">' + searchResult[j].order + '. ' + searchResult[j].desc + '</a>';
|
||||
html += ' <ul class="sectlevel2" style="display: block">';
|
||||
doc = searchResult[j].list;
|
||||
for (m = 0; m < doc.length; m++) {
|
||||
html += '<li><a href="' + searchResult[j].alias + '.html#_' + searchResult[j].order + '_' + doc[m].order + '_' + doc[m].desc + '">' + searchResult[j].order + '.' + doc[m].order + '. ' + doc[m].desc + '</a> </li>';
|
||||
}
|
||||
html += '</ul>';
|
||||
html += '</li>';
|
||||
}
|
||||
document.getElementById('accordion').innerHTML = html;
|
||||
} else {
|
||||
if (search.value == '') {
|
||||
html = '';
|
||||
for (j = 0; j < api.length; j++) {
|
||||
html += '<li class="open">';
|
||||
html += ' <a class="dd" href="' + api[j].alias + '.html#header">' + api[j].order + '. ' + api[j].desc + '</a>';
|
||||
html += ' <ul class="sectlevel2" style="display: block">';
|
||||
doc = api[j].list;
|
||||
for (m = 0; m < doc.length; m++) {
|
||||
html += '<li><a href="' + api[j].alias + '.html#_' + searchResult[j].order + '_' + doc[m].order + '_' + doc[m].desc + '">' + searchResult[j].order + '.' + doc[m].order + '. ' + doc[m].desc + '</a> </li>';
|
||||
}
|
||||
html += '</ul>';
|
||||
html += '</li>';
|
||||
}
|
||||
document.getElementById('accordion').innerHTML = html;
|
||||
} else {
|
||||
document.getElementById('accordion').innerHTML = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
let api = [];
|
||||
document.onkeydown = keyDownSearch;
|
||||
|
||||
function keyDownSearch(e) {
|
||||
let j;
|
||||
let m;
|
||||
let doc;
|
||||
let html;
|
||||
const theEvent = e || window.event;
|
||||
const code = theEvent.keyCode || theEvent.which || theEvent.charCode;
|
||||
if (code == 13) {
|
||||
const search = document.getElementById('search');
|
||||
const searchArr = [];
|
||||
<%for(api in apiDocList){%>
|
||||
searchArr.push({
|
||||
alias: '${api.alias}',
|
||||
order: '${api.order}',
|
||||
desc: '${api.desc}',
|
||||
list: []
|
||||
})
|
||||
api.push({
|
||||
alias: '${api.alias}',
|
||||
order: '${api.order}',
|
||||
desc: '${api.desc}',
|
||||
list: []
|
||||
})
|
||||
|
||||
<%for (doc in api.list) {%>
|
||||
api[${apiLP.dataIndex}].list.push({
|
||||
order: '${doc.order}',
|
||||
desc: '${doc.desc}',
|
||||
});
|
||||
if ('${doc.desc}'.indexOf(search.value) > -1) {
|
||||
searchArr[${apiLP.dataIndex}].list.push({
|
||||
order: '${doc.order}',
|
||||
desc: '${doc.desc}',
|
||||
});
|
||||
}
|
||||
<%}%>
|
||||
<%}%>
|
||||
const searchResult = searchArr.filter(obj => obj.list.length > 0);
|
||||
if (searchResult.length > 0) {
|
||||
html = '';
|
||||
for (j = 0; j < searchResult.length; j++) {
|
||||
html += '<li class="open">';
|
||||
html += ' <a class="dd" href="#_' + searchResult[j].desc + '">' + searchResult[j].order + '. ' + searchResult[j].desc + '</a>';
|
||||
html += ' <ul class="sectlevel2" style="display: block">';
|
||||
doc = searchResult[j].list;
|
||||
for (m = 0; m < doc.length; m++) {
|
||||
html += '<li><a href="#_' + searchResult[j].order + '_' + doc[m].order + '_' + doc[m].desc + '">' + searchResult[j].order + '.' + doc[m].order + '. ' + doc[m].desc + '</a> </li>';
|
||||
}
|
||||
html += '</ul>';
|
||||
html += '</li>';
|
||||
}
|
||||
document.getElementById('accordion').innerHTML = html;
|
||||
} else {
|
||||
if (search.value == '') {
|
||||
html = '';
|
||||
for (j = 0; j < api.length; j++) {
|
||||
html += '<li class="open">';
|
||||
html += ' <a class="dd" href="#_' + api[j].desc + '">' + api[j].order + '. ' + api[j].desc + '</a>';
|
||||
html += ' <ul class="sectlevel2" style="display: block">';
|
||||
doc = api[j].list;
|
||||
for (m = 0; m < doc.length; m++) {
|
||||
html += '<li><a href="#_' + searchResult[j].order + '_' + doc[m].order + '_' + doc[m].desc + '">' + searchResult[j].order + '.' + doc[m].order + '. ' + doc[m].desc + '</a> </li>';
|
||||
}
|
||||
html += '</ul>';
|
||||
html += '</li>';
|
||||
}
|
||||
document.getElementById('accordion').innerHTML = html;
|
||||
} else {
|
||||
document.getElementById('accordion').innerHTML = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@
|
|||
<body class="book toc2 toc-left">
|
||||
<div id="header"><%if(isNotEmpty(projectName)){%><h1>${projectName}</h1><%}%>
|
||||
<div id="toc" class="toc2">
|
||||
<div id="book-search-input"><input id="search" type="text" placeholder="Type to search"></div>
|
||||
<div id="toctitle"><span>API Reference</span></div>
|
||||
<ul id="accordion" class="sectlevel1"><%for(api in apiDocList){%><%if(apiLP.first){%>
|
||||
<li class="open"><a class="dd" href="#_${api.desc}">${api.order}. ${api.desc}</a>
|
||||
|
@ -394,236 +395,7 @@
|
|||
href="https://github.com/smart-doc-group/smart-doc.git" target="_blank"> Github</a></span>
|
||||
</footer>
|
||||
</div>
|
||||
<script>
|
||||
$(function () {
|
||||
const Accordion = function (el, multiple) {
|
||||
this.el = el || {};
|
||||
this.multiple = multiple || false;
|
||||
const links = this.el.find('.dd');
|
||||
links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
|
||||
};
|
||||
Accordion.prototype.dropdown = function (e) {
|
||||
const $el = e.data.el;
|
||||
const $this = $(this), $next = $this.next();
|
||||
$next.slideToggle();
|
||||
$this.parent().toggleClass('open');
|
||||
if (!e.data.multiple) {
|
||||
$el.find('.submenu').not($next).slideUp("20").parent().removeClass('open');
|
||||
}
|
||||
};
|
||||
new Accordion($('#accordion'), false);
|
||||
});
|
||||
$('textarea').each(function () {
|
||||
this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
|
||||
}).on('input', function () {
|
||||
this.style.height = '80px';
|
||||
this.style.height = (this.scrollHeight) + 'px';
|
||||
});
|
||||
$("button").on("click", function () {
|
||||
const $this = $(this);
|
||||
const id = $this.data("id");
|
||||
console.log("method-id=>" + id);
|
||||
|
||||
let body = $("#" + id + "-body").val();
|
||||
|
||||
// header
|
||||
const $headerElement = $("#" + id + "-header");
|
||||
const headersData = getInputData($headerElement);
|
||||
|
||||
// body param
|
||||
const $paramElement = $("#" + id + "-param");
|
||||
let bodyParamData = getInputData($paramElement)
|
||||
|
||||
// path param
|
||||
const $pathElement = $("#" + id + "-path-params")
|
||||
const pathParamData = getInputData($pathElement)
|
||||
|
||||
// query param
|
||||
const $queryElement = $("#" + id + "-query-params")
|
||||
|
||||
|
||||
const url = $("#" + id + "-url").data("url");
|
||||
const isDownload = $("#" + id + "-url").data("download");
|
||||
const method = $("#" + id + "-method").data("method");
|
||||
const contentType = $("#" + id + "-content-type").data("content-type");
|
||||
console.log("request-headers=>" + JSON.stringify(headersData))
|
||||
console.log("path-params=>" + JSON.stringify(pathParamData))
|
||||
|
||||
console.log("body-params=>" + JSON.stringify(bodyParamData))
|
||||
console.log("json-body=>" + body);
|
||||
|
||||
let queryParamData = "";
|
||||
if (isDownload) {
|
||||
queryParamData = getInputData($queryElement);
|
||||
download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType);
|
||||
return;
|
||||
}
|
||||
const ajaxOptions = {};
|
||||
|
||||
|
||||
let finalUrl = "";
|
||||
if ("multipart/form-data" == contentType) {
|
||||
finalUrl = castToGetUri(url, pathParamData);
|
||||
queryParamData = getInputData($queryElement, true)
|
||||
body = queryParamData;
|
||||
ajaxOptions.processData = false;
|
||||
ajaxOptions.contentType = false;
|
||||
} else {
|
||||
queryParamData = getInputData($queryElement)
|
||||
finalUrl = castToGetUri(url, pathParamData, queryParamData)
|
||||
ajaxOptions.contentType = contentType;
|
||||
}
|
||||
console.log("query-params=>" + JSON.stringify(queryParamData));
|
||||
console.log("url=>" + finalUrl)
|
||||
ajaxOptions.headers = headersData
|
||||
ajaxOptions.url = finalUrl
|
||||
ajaxOptions.type = method
|
||||
ajaxOptions.data = body;
|
||||
|
||||
const ajaxTime = new Date().getTime();
|
||||
$.ajax(ajaxOptions).done(function (result, textStatus, jqXHR) {
|
||||
$this.css("background", "#5cb85c");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(result, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).fail(function (jqXHR) {
|
||||
$this.css("background", "#D44B47");
|
||||
$("#" + id + "-response").find("pre").text(JSON.stringify(jqXHR.responseJSON, null, 4));
|
||||
const totalTime = new Date().getTime() - ajaxTime;
|
||||
$("#" + id + "-resp-status").html(" Status: " + jqXHR.status + " " + jqXHR.statusText + " Time: " + totalTime + " ms")
|
||||
}).always(function () {
|
||||
|
||||
});
|
||||
})
|
||||
$(".check-all").on("click", function () {
|
||||
const checkboxName = $(this).prop("name");
|
||||
const checked = $(this).is(':checked');
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
$('input[name="' + checkboxName + '"]').each(function () {
|
||||
if (!checked) {
|
||||
$(this).removeAttr("checked");
|
||||
} else {
|
||||
$(this).prop("checked", true);
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function castToGetUri(url, pathParams, params) {
|
||||
if (pathParams instanceof Object && !(pathParams instanceof Array)) {
|
||||
url = url.format(pathParams)
|
||||
}
|
||||
if (params instanceof Object && !(params instanceof Array)) {
|
||||
const pm = params || {};
|
||||
const arr = [];
|
||||
arr.push(url);
|
||||
let j = 0;
|
||||
for (const i in pm) {
|
||||
if (j === 0) {
|
||||
arr.push("?");
|
||||
arr.push(i + "=" + pm[i]);
|
||||
} else {
|
||||
arr.push("&" + i + "=" + pm[i]);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return arr.join("");
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
function getInputData(element, returnFormDate) {
|
||||
const formData = new FormData();
|
||||
$(element).find("tr").each(function (i) {
|
||||
const checked = $(this).find('td:eq(0)').children(".checkbox").children("input").is(':checked');
|
||||
if (checked) {
|
||||
const input = $(this).find('td:eq(2) input');
|
||||
console.log("input type:" + $(input).attr("type"))
|
||||
const name = $(input).attr("name");
|
||||
if ($(input).attr("type") == "file") {
|
||||
formData.append(name, $(input)[0].files[0]);
|
||||
} else {
|
||||
const val = $(input).val();
|
||||
formData.append(name, val);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (returnFormDate) {
|
||||
return formData;
|
||||
}
|
||||
const headersData = {};
|
||||
formData.forEach((value, key) => headersData[key] = value);
|
||||
return headersData;
|
||||
}
|
||||
|
||||
String.prototype.format = function (args) {
|
||||
let reg;
|
||||
if (arguments.length > 0) {
|
||||
let result = this;
|
||||
if (arguments.length == 1 && typeof (args) == "object") {
|
||||
for (const key in args) {
|
||||
reg = new RegExp("({" + key + "})", "g");
|
||||
result = result.replace(reg, args[key]);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
if (arguments[i] == undefined) {
|
||||
return "";
|
||||
} else {
|
||||
reg = new RegExp("({[" + i + "]})", "g");
|
||||
result = result.replace(reg, arguments[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
function download(url, headersData, pathParamData, queryParamData, bodyParamData, method, contentType) {
|
||||
url = castToGetUri(url, pathParamData, queryParamData)
|
||||
const xmlRequest = new XMLHttpRequest();
|
||||
xmlRequest.open(method, url, true);
|
||||
xmlRequest.setRequestHeader("Content-type", contentType);
|
||||
for (let key in headersData) {
|
||||
xmlRequest.setRequestHeader(key, headersData[key])
|
||||
}
|
||||
xmlRequest.responseType = "blob";
|
||||
xmlRequest.onload = function () {
|
||||
if (this.status === 200) {
|
||||
let fileName = decodeURI(xmlRequest.getResponseHeader('filename'));
|
||||
console.log(fileName);
|
||||
const blob = this.response;
|
||||
if (navigator.msSaveBlob) {
|
||||
// IE10 can't do a[download], only Blobs:
|
||||
window.navigator.msSaveBlob(blob, fileName);
|
||||
return;
|
||||
}
|
||||
if (window.URL) { // simple fast and modern way using Blob and URL:
|
||||
const a = document.createElement("a");
|
||||
document.body.appendChild(a);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
console.log(fileName);
|
||||
} else {
|
||||
console.log("download failed");
|
||||
}
|
||||
};
|
||||
try {
|
||||
xmlRequest.send(bodyParamData);
|
||||
} catch (e) {
|
||||
console.error("Failed to send data", e);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script src="mock.js?v=${version}"></script>
|
||||
<script src="search.js?v=${version}"></script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue