get paper list

This commit is contained in:
Anton Romanov 2018-09-05 11:27:22 +04:00
parent 2fda487555
commit 43e446e871
3 changed files with 288 additions and 44 deletions

View File

@ -0,0 +1,249 @@
// from config.js
/* global urlVersions */
/* exported MessageTypesEnum */
var MessageTypesEnum = {
INFO: "info",
SUCCESS: "success",
WARNING: "warning",
DANGER: "danger"
};
Object.freeze(MessageTypesEnum);
function isEmpty(value) {
if (typeof value === "function") {
return false;
}
return (value == null || value.length === 0);
}
/* exported showFeedbackMessage */
function showFeedbackMessage(message, type) {
var drawMessage = function (message, type) {
var li = $(
"<li class=\"alert alert-" + type + "\">" +
"<span>" + message + "</span>" +
"</li>"
);
li.click(function () {
$(this).remove();
});
messageDiv.append(li);
};
var liveTimeMs = 15000;
var messageDiv = $("#messages");
if (isEmpty(message)) {
messageDiv.html("");
return;
}
drawMessage(message, (isEmpty(type) || isEmpty(MessageTypesEnum[type.toUpperCase()]) ?
MessageTypesEnum.INFO : type));
setTimeout(function () {
messageDiv.find("li").first().remove();
}, liveTimeMs);
}
/* exported errorHandler */
function errorHandler(response, callBack, errorCallBack) {
if (isEmpty(response)) {
showFeedbackMessage("Empty response", MessageTypesEnum.DANGER);
return;
}
if (!isEmpty(response.error)) {
// TODO: add l10n
// showFeedbackMessage(response.error.code + ": " + response.error.message, MessageTypesEnum.DANGER);
if (!isEmpty(errorCallBack)) {
errorCallBack(response.data);
}
throw response.error.code + ": " + response.error.message +
" / Details: " + response.error.data;
}
if (!isEmpty(callBack)) {
if (isLoginPageResponse(response)) {
window.location.href = "/";
} else {
callBack(response.data);
}
}
}
function isLoginPageResponse(response) {
return (typeof response) === 'string' && response.indexOf("<!DOCTYPE html>") >= 0;
}
/* exported getFromRest */
function getFromRest(url, callBack, errorCallBack) {
$.ajax({
url: url,
cache: false,
success: function (response) {
errorHandler(response, callBack, errorCallBack);
}
});
}
/* exported getFromRestWithVersion */
function getFromRestWithVersion(url, callBack, errorCallBack) {
getFromRestWithVersionAndParams(url, "", callBack, errorCallBack);
}
/* exported getFromRestWithVersionAndParams */
function getFromRestWithVersionAndParams(url, params, callBack, errorCallBack) {
getCurrentVersion(function (version) {
$.ajax({
url: url + "?versionId=" + version + params,
cache: false,
success: function (response) {
errorHandler(response, callBack, errorCallBack);
}
});
});
}
/* exported postToRest */
function postToRest(url, postData, callBack, completeCallback, errorCallBack) {
$.ajax({
url: url,
cache: false,
dataType: "json",
data: postData,
contentType: "application/json; charset=utf-8",
processData: false,
method: "POST",
success: function (response) {
errorHandler(response, callBack, errorCallBack);
},
complete: function () {
if (isEmpty(completeCallback)) {
return;
}
completeCallback();
}
});
}
/* exported postToRestWithVersion */
function postToRestWithVersion(url, postData, callBack, errorCallBack) {
postToRestWithVersionAndParams(url, postData, "", callBack, errorCallBack);
}
/* exported postToRestWithVersionAndParams */
function postToRestWithVersionAndParams(url, postData, params, callBack, errorCallBack) {
getCurrentVersion(function (version) {
$.ajax({
url: url + "?versionId=" + version + params,
cache: false,
dataType: "json",
data: postData,
contentType: "application/json; charset=utf-8",
processData: false,
method: "POST",
success: function (response) {
errorHandler(response, callBack, errorCallBack);
}
});
});
}
/* exported putToRestWithVersion */
function putToRestWithVersion(url, putData, callBack, errorCallBack) {
putToRestWithVersionAndParams(url, putData, "", callBack, errorCallBack);
}
/* exported putToRestWithVersionAndParams */
function putToRestWithVersionAndParams(url, putData, params, callBack, errorCallBack) {
getCurrentVersion(function (version) {
$.ajax({
url: url + "?versionId=" + version + params,
cache: false,
dataType: "json",
data: putData,
contentType: "application/json; charset=utf-8",
processData: false,
method: "PUT",
success: function (response) {
errorHandler(response, callBack, errorCallBack);
}
});
});
}
/* exported deleteFromRest */
function deleteFromRest(url, callBack, completeCallback, errorCallBack) {
$.ajax({
url: url,
cache: false,
method: "DELETE",
success: function (response) {
errorHandler(response, callBack, errorCallBack);
},
complete: function () {
if (isEmpty(completeCallback)) {
return;
}
completeCallback();
}
});
}
/* exported getCurrentVersion */
function getCurrentVersion(callBack, errorCallBack) {
var version = localStorage.getItem("currentVersion");
if (($("#select-versions")[0].options.length > 0)
&& ($("#select-versions option[value='" + version + "']").length === 0)) {
localStorage.removeItem("currentVersion");
version = "";
}
if (isEmpty(version)) {
getFromRest(urlVersions,
function (versions) {
if (isEmpty(versions) || versions.count === 0) {
return;
}
var currentVersion = versions.items[0];
localStorage.setItem("currentVersion", currentVersion.id);
callBack(currentVersion.id);
},
errorCallBack);
} else {
callBack(version);
}
}
/* exported fillSelect */
function fillSelect(selectElement, values) {
$(selectElement).html("");
$.each(values, function (key, value) {
$(selectElement).append(
"<option value=\"" + value.id + "\">" + value.name + "</option>"
);
});
}
/* exported isUrlVarExists */
function isUrlVarExists(key) {
var URL = window.location.href;
if (URL.indexOf("?" + key + "=") !== -1) {
return true;
} else if (URL.indexOf("&" + key + "=") !== -1) {
return true;
} else if (URL.indexOf("?" + key + "&") !== -1) {
return true;
} else if (URL.indexOf("&" + key + "&") !== -1) {
return true;
} else if (URL.endsWith("?" + key)) {
return true;
} else if (URL.endsWith("&" + key)) {
return true;
}
return false;
}
/* exported getUrlVar */
function getUrlVar(key) {
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && decodeURIComponent(result[1]) || "";
}

View File

@ -0,0 +1,17 @@
var urlPaperList = "https://localhost:8443/api/1.0/papers";
function showPapers(papersElement) {
getFromRest(urlPaperList, function (paperList) {
paperList.forEach(function (paper, index) {
$(papersElement).parent().append("<div class='row text-left'>" +
" <div class='col-md-12'>" +
" <span class='fa-stack fa-1x'>\n" +
" <i class='fa fa-circle fa-stack-2x text-warning'></i>" +
" <i class='fa fa-file-text-o fa-stack-1x fa-inverse'></i>" +
" </span>" +
" <a href='paper.html?id=" + paper.id + "" +
"'><span>" + paper.title + "</span></a>" +
" </div></div>");
});
});
}

View File

@ -7,56 +7,34 @@
<body>
<div class="container" layout:fragment="content">
<section id="services">
<section id="papers">
<div class="container">
<div class="row">
<div class="row" id="paper-list">
<div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase">Статьи</h2>
</div>
</div>
<div class="row text-left">
<div class="col-md-12">
<span class="fa-stack fa-1x">
<i class="fa fa-circle fa-stack-2x text-warning"></i>
<i class="fa fa-file-text-o fa-stack-1x fa-inverse"></i>
</span>
<a href="paper.html?id=1"><span>А.А. Романов, И.А. Тимина. Обзор методов оценки трудоемкости производства</span></a>
</div>
</div>
<div class="row text-left">
<div class="col-md-12">
<span class="fa-stack fa-1x">
<i class="fa fa-circle fa-stack-2x text-primary"></i>
<i class="fa fa-file-text-o fa-stack-1x fa-inverse"></i>
</span>
<a href="paper.html?id=2"><span>А.А. Филиппов, В.С.Мошкин, Н.Г. Ярушкина. Построение онтологии предметной ...</span></a>
</div>
</div>
<div class="row text-left">
<div class="col-md-12">
<span class="fa-stack fa-1x">
<i class="fa fa-circle fa-stack-2x text-success"></i>
<i class="fa fa-file-text-o fa-stack-1x fa-inverse"></i>
</span>
<a href="paper.html?id=2"><span>А.А. Филиппов, В.С.Мошкин, Н.Г. Ярушкина. Построение онтологии предметной ...</span></a>
</div>
</div>
</div>
</section>
</section>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Plugin JavaScript -->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Plugin JavaScript -->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Contact form JavaScript -->
<script src="js/jqBootstrapValidation.js"></script>
<script src="js/contact_me.js"></script>
<!-- Contact form JavaScript -->
<script src="js/jqBootstrapValidation.js"></script>
<script src="js/contact_me.js"></script>
<!-- Custom scripts for this template -->
<script src="js/agency.js"></script>
<!-- Custom scripts for this template -->
<script src="js/agency.js"></script>
<script src="js/core.js"></script>
<script src="js/papers.js"></script>
<script>
showPapers($("#paper-list"));
</script>
</div>
</body>