diff --git a/sdkjs-plugins/content/zotero/scripts/code.js b/sdkjs-plugins/content/zotero/scripts/code.js
index 65fc5bb5..57eddef0 100644
--- a/sdkjs-plugins/content/zotero/scripts/code.js
+++ b/sdkjs-plugins/content/zotero/scripts/code.js
@@ -16,29 +16,32 @@
*
*/
+// @ts-check
+
+///
///
+///
///
///
///
///
+///
+///
+
+/**
+ * @typedef {Object} Scroller
+ * @property {Function} onscroll
+ */
(function () {
var counter = 0; // счетчик отправленных запросов (используется чтобы знать показывать "not found" или нет)
var displayNoneClass = "display-none";
var blurClass = "blur";
- //var formatter = null;
- var citPrefixNew = "ZOTERO_ITEM";
- var citSuffixNew = "CSL_CITATION";
- //var citPrefix = "ZOTERO_CITATION";
- var bibPrefixNew = "ZOTERO_BIBL";
- var bibSuffixNew = "CSL_BIBLIOGRAPHY";
- //var bibPrefix = "ZOTERO_BIBLIOGRAPHY";
- var loadTimeout = null;
+ /** @type {number} */
+ var loadTimeout;
//var loadingStyle = false;
//var loadingLocale = false;
var bNumFormat = false;
- // let bibPlaceholder = "Please insert some citation into the document.";
- //var bUserItemsUpdated = false;
// TODO добавить ещё обработку событий (удаление линков) их не нужно удалять
// из библиографии автоматически (это делать только при обновлении библиографии
@@ -65,74 +68,239 @@
};
/** @type {ZoteroSdk} */
- var sdk = null;
- var cslStylesManager = null;
- var localesManager = null;
- var citationService = null;
- var citationDocService = null;
+ var sdk;
+ /** @type {CslStylesManager} */
+ var cslStylesManager;
+ /** @type {LocalesManager} */
+ var localesManager;
+ /** @type {CitationService} */
+ var citationService;
+ /** @type {{text: string, obj: SearchResult | null, groups: Array
}}} */
var lastSearch = {
text: "",
obj: null,
groups: [],
};
- var elements = {
- loader: document.getElementById("loader"),
- libLoader: document.getElementById("libLoader"),
- error: document.getElementById("errorWrapper"),
- contentHolder: document.getElementById("content"),
- docsHolder: document.getElementById("docsHolder"),
- docsThumb: document.getElementById("docsThumb"),
+ /** @type {Object.} */
+ var elements = {};
+ function initElements() {
+ const loader = document.getElementById("loader");
+ if (!loader) {
+ throw new Error("loader not found");
+ }
+ const libLoader = document.getElementById("libLoader");
+ if (!libLoader) {
+ throw new Error("libLoader not found");
+ }
+ const error = document.getElementById("errorWrapper");
+ if (!error) {
+ throw new Error("errorWrapper not found");
+ }
+ const contentHolder = document.getElementById("content");
+ if (!contentHolder) {
+ throw new Error("contentHolder not found");
+ }
+ const docsHolder = document.getElementById("docsHolder");
+ if (!docsHolder) {
+ throw new Error("docsHolder not found");
+ }
+ const docsThumb = document.getElementById("docsThumb");
+ if (!docsThumb) {
+ throw new Error("docsThumb not found");
+ }
+ const configState = document.getElementById("configState");
+ if (!configState) {
+ throw new Error("configState not found");
+ }
+ const apiKeyConfigField = document.getElementById("apiKeyField");
+ if (!apiKeyConfigField) {
+ throw new Error("apiKeyConfigField not found");
+ }
+ const saveConfigBtn = document.getElementById("saveConfigBtn");
+ if (!saveConfigBtn) {
+ throw new Error("saveConfigBtn not found");
+ }
+ const mainState = document.getElementById("mainState");
+ if (!mainState) {
+ throw new Error("mainState not found");
+ }
+ const logoutLink = document.getElementById("logoutLink");
+ if (!logoutLink) {
+ throw new Error("logoutLink not found");
+ }
+ const selectedWrapper = document.getElementById("selectedWrapper");
+ if (!selectedWrapper) {
+ throw new Error("selectedWrapper not found");
+ }
+ const selectedHolder = document.getElementById("selectedHolder");
+ if (!selectedHolder) {
+ throw new Error("selectedHolder not found");
+ }
+ const selectedThumb = document.getElementById("selectedThumb");
+ if (!selectedThumb) {
+ throw new Error("selectedThumb not found");
+ }
+ const buttonsWrapper = document.getElementById("buttonsWrapper");
+ if (!buttonsWrapper) {
+ throw new Error("buttonsWrapper not found");
+ }
+ const locatorLabel = document.getElementById("locatorLabel");
+ if (!locatorLabel) {
+ throw new Error("locatorLabel not found");
+ }
+ const locatorLabelsList = document.getElementById("locatorLabelsList");
+ if (!locatorLabelsList) {
+ throw new Error("locatorLabelsList not found");
+ }
+ const library = document.getElementById("library");
+ if (!library) {
+ throw new Error("library not found");
+ }
+ const searchLibrary = document.getElementById("searchLibrary");
+ if (!searchLibrary) {
+ throw new Error("searchLibrary not found");
+ }
+ const searchLabel = document.getElementById("searchLabel");
+ if (!searchLabel) {
+ throw new Error("searchLabel not found");
+ }
+ const searchClear = document.getElementById("searchClear");
+ if (!searchClear) {
+ throw new Error("searchClear not found");
+ }
+ const searchField = document.getElementById("searchField");
+ if (!searchField) {
+ throw new Error("searchField not found");
+ }
+ const styleWrapper = document.getElementById("styleWrapper");
+ if (!styleWrapper) {
+ throw new Error("styleWrapper not found");
+ }
+ const styleSelectList = document.getElementById("styleSelectList");
+ if (!styleSelectList) {
+ throw new Error("styleSelectList not found");
+ }
+ const styleSelectListOther = document.getElementById(
+ "styleSelectedListOther"
+ );
+ if (!styleSelectListOther) {
+ throw new Error("styleSelectListOther not found");
+ }
+ const styleSelect = document.getElementById("styleSelect");
+ if (!styleSelect) {
+ throw new Error("styleSelect not found");
+ }
+ const styleLang = document.getElementById("styleLang");
+ if (!styleLang) {
+ throw new Error("styleLang not found");
+ }
+ const notesStyleWrapper = document.getElementById("notesStyle");
+ if (!notesStyleWrapper) {
+ throw new Error("notesStyleWrapper not found");
+ }
+ const footNotes = document.getElementById("footNotes");
+ if (!footNotes) {
+ throw new Error("footNotes not found");
+ }
+ const endNotes = document.getElementById("endNotes");
+ if (!endNotes) {
+ throw new Error("endNotes not found");
+ }
+ const insertBibBtn = document.getElementById("insertBibBtn");
+ if (!insertBibBtn) {
+ throw new Error("insertBibBtn not found");
+ }
+ const insertLinkBtn = document.getElementById("insertLinkBtn");
+ if (!insertLinkBtn) {
+ throw new Error("insertLinkBtn not found");
+ }
+ const cancelBtn = document.getElementById("cancelBtn");
+ if (!cancelBtn) {
+ throw new Error("cancelBtn not found");
+ }
+ const refreshBtn = document.getElementById("refreshBtn");
+ if (!refreshBtn) {
+ throw new Error("refreshBtn not found");
+ }
+ const saveAsTextBtn = document.getElementById("saveAsTextBtn");
+ if (!saveAsTextBtn) {
+ throw new Error("saveAsTextBtn not found");
+ }
+ const checkOmitAuthor = document.getElementById("omitAuthor");
+ if (!checkOmitAuthor) {
+ throw new Error("checkOmitAuthor not found");
+ }
+ const useDesktopApp = document.getElementById("useDesktopApp");
+ if (!useDesktopApp) {
+ throw new Error("useDesktopApp not found");
+ }
+ const cslFileInput = document.getElementById("cslFileInput");
+ if (!cslFileInput) {
+ throw new Error("cslFileInput not found");
+ }
+ elements = {
+ loader: loader,
+ libLoader: libLoader,
+ error: error,
- configState: document.getElementById("configState"),
- apiKeyConfigField: document.getElementById("apiKeyField"),
- saveConfigBtn: document.getElementById("saveConfigBtn"),
+ contentHolder: contentHolder,
+ docsHolder: docsHolder,
+ docsThumb: docsThumb,
- mainState: document.getElementById("mainState"),
- logoutLink: document.getElementById("logoutLink"),
+ configState: configState,
+ apiKeyConfigField: apiKeyConfigField,
+ saveConfigBtn: saveConfigBtn,
- selectedWrapper: document.getElementById("selectedWrapper"),
- selectedHolder: document.getElementById("selectedHolder"),
- selectedThumb: document.getElementById("selectedThumb"),
- buttonsWrapper: document.getElementById("buttonsWrapper"),
+ mainState: mainState,
+ logoutLink: logoutLink,
- locatorLabel: document.getElementById("locatorLabel"),
- locatorLabelsList: document.getElementById("locatorLabelsList"),
+ selectedWrapper: selectedWrapper,
+ selectedHolder: selectedHolder,
+ selectedThumb: selectedThumb,
+ buttonsWrapper: buttonsWrapper,
- library: document.getElementById("library"),
- searchLibrary: document.getElementById("searchLibrary"),
- searchLabel: document.getElementById("searchLabel"),
- searchClear: document.getElementById("searchClear"),
- searchField: document.getElementById("searchField"),
+ locatorLabel: locatorLabel,
+ locatorLabelsList: locatorLabelsList,
- styleWrapper: document.getElementById("styleWrapper"),
- styleSelectList: document.getElementById("styleSelectList"),
- styleSelectListOther: document.getElementById("styleSelectedListOther"),
- styleSelect: document.getElementById("styleSelect"),
- styleLang: document.getElementById("styleLang"),
- notesStyleWrapper: document.querySelector(".notesStyle"),
- notesStyleRadios: document.querySelectorAll(".notesStyle input[name=notesAs]"),
+ library: library,
+ searchLibrary: searchLibrary,
+ searchLabel: searchLabel,
+ searchClear: searchClear,
+ searchField: searchField,
- insertBibBtn: document.getElementById("insertBibBtn"),
- insertLinkBtn: document.getElementById("insertLinkBtn"),
- cancelBtn: document.getElementById("cancelBtn"),
- tempDiv: document.getElementById("div_temp"),
- refreshBtn: document.getElementById("refreshBtn"),
- saveAsTextBtn: document.getElementById("saveAsTextBtn"),
+ styleWrapper: styleWrapper,
+ styleSelectList: styleSelectList,
+ styleSelectListOther: styleSelectListOther,
+ styleSelect: styleSelect,
+ styleLang: styleLang,
+ notesStyleWrapper: notesStyleWrapper,
+ footNotes: footNotes,
+ endNotes: endNotes,
- checkOmitAuthor: document.getElementById("omitAuthor"),
- useDesktopApp: document.getElementById("useDesktopApp"),
- cslFileInput: document.getElementById("cslFileInput"),
- };
+ insertBibBtn: insertBibBtn,
+ insertLinkBtn: insertLinkBtn,
+ cancelBtn: cancelBtn,
+ refreshBtn: refreshBtn,
+ saveAsTextBtn: saveAsTextBtn,
+ checkOmitAuthor: checkOmitAuthor,
+ useDesktopApp: useDesktopApp,
+ cslFileInput: cslFileInput,
+ };
+ }
+
+ /** @type {Scroller} */
var selectedScroller;
+ /** @type {Scroller} */
var docsScroller;
window.Asc.plugin.init = function () {
+ initElements();
showLoader(true);
setTimeout(function () {
- searchField.focus();
+ if (elements.searchField) elements.searchField.focus();
}, 100);
sdk = new ZoteroSdk();
@@ -145,15 +313,11 @@
loadStyles();
loadGroups();
- citationDocService = new CitationDocService(
- citPrefixNew,
- citSuffixNew,
- bibPrefixNew,
- bibSuffixNew,
- cslStylesManager.getLastUsedFormat(),
- cslStylesManager.getLastUsedNotesStyle()
+ citationService = new CitationService(
+ localesManager,
+ cslStylesManager,
+ sdk
);
- citationService = new CitationService(citationDocService, localesManager, cslStylesManager, sdk);
addStylesEventListeners();
initSelectBoxes();
@@ -188,7 +352,7 @@
localesManager.setRestApiAvailable(apis.online);
if (!hasFirstAnswer) {
hasFirstAnswer = true;
- if (!apis.desktopVersion) {
+ if (!apis.desktopVersion && elements.useDesktopApp) {
elements.useDesktopApp.classList.add("display-none");
}
showLoader(false);
@@ -211,7 +375,8 @@
return;
} else if (apis.desktop && apis.hasPermission) {
sdk.setIsOnlineAvailable(false);
- elements.logoutLink.style.display = "none";
+ if (elements.logoutLink)
+ elements.logoutLink.style.display = "none";
switchAuthState("main");
showError(false);
resolve(apis);
@@ -226,19 +391,26 @@
const option = elements.locatorLabelsList.querySelector(
'[data-value="' + id + '"]'
);
- option && option.setAttribute("selected", "");
+ if (!option || !(elements.locatorLabel instanceof HTMLInputElement)) {
+ return;
+ }
+ option.setAttribute("selected", "");
const name = option.textContent;
- locatorLabel.value = option.textContent;
- locatorLabel.setAttribute("data-value", id);
- locatorLabel.setAttribute("title", name);
+
+ elements.locatorLabel.value = option.textContent;
+ elements.locatorLabel.setAttribute("data-value", id);
+ elements.locatorLabel.setAttribute("title", name);
}
+ /** @returns {Promise} */
function loadStyles() {
- cslStylesManager
+ return cslStylesManager
.getStylesInfo()
.then(function (stylesInfo) {
- var openOtherStyleList = function (list) {
- return function (ev) {
+ var openOtherStyleList = function (
+ /** @type {HTMLElement} */ list
+ ) {
+ return function (/** @type {MouseEvent} */ ev) {
elements.styleSelectListOther.style.width =
elements.styleWrapper.clientWidth - 2 + "px";
ev.stopPropagation();
@@ -273,111 +445,146 @@
});
}
+ /** @returns {Promise} */
function loadGroups() {
- return sdk.getUserGroups().then(function (groups) {
- const customGroups = [
- { id: "all", name: translate("Everywhere") },
- { id: "my_library", name: translate("My Library") },
- { id: "group_libraries", name: translate("Group Libraries") },
- ];
- library.value = customGroups[0].name;
- library.setAttribute("data-value", customGroups[0].id);
- library.setAttribute("title", customGroups[0].name);
-
- const selectedItem = localStorage.getItem("selectedGroup") || "all";
-
- const addGroupToSelectBox = function (id, name) {
- const el = document.createElement("span");
- el.setAttribute("data-value", id);
- el.textContent = name;
- elements.searchLibrary.appendChild(el);
- if (id === selectedItem) {
- el.setAttribute("selected", "");
- library.value = name;
- library.setAttribute("data-value", id);
- library.setAttribute("title", name);
+ return sdk
+ .getUserGroups()
+ .then(function (/** @type {Array} */ groups) {
+ const customGroups = [
+ { id: "all", name: translate("Everywhere") },
+ { id: "my_library", name: translate("My Library") },
+ {
+ id: "group_libraries",
+ name: translate("Group Libraries"),
+ },
+ ];
+ if (elements.library instanceof HTMLInputElement) {
+ elements.library.value = customGroups[0].name;
}
- };
+ elements.library.setAttribute("data-value", customGroups[0].id);
+ elements.library.setAttribute("title", customGroups[0].name);
- const addSeparator = function () {
- const el = document.createElement("hr");
- elements.searchLibrary.appendChild(el);
- };
+ const selectedItem =
+ localStorage.getItem("selectedGroup") || "all";
- elements.searchLibrary.addEventListener("click", function (e) {
- const target = e.target;
- let option;
- if (target && target instanceof HTMLSpanElement) {
- option = target;
- } else {
+ /**
+ * @param {string|number} id
+ * @param {string} name
+ */
+ const addGroupToSelectBox = function (id, name) {
+ if (typeof id === "number") {
+ id = id.toString();
+ }
+ const el = document.createElement("span");
+ el.setAttribute("data-value", id);
+ el.textContent = name;
+ elements.searchLibrary.appendChild(el);
+ if (
+ id === selectedItem &&
+ elements.library instanceof HTMLInputElement
+ ) {
+ el.setAttribute("selected", "");
+ elements.library.value = name;
+ elements.library.setAttribute("data-value", id);
+ elements.library.setAttribute("title", name);
+ }
+ };
+
+ const addSeparator = function () {
+ const el = document.createElement("hr");
+ elements.searchLibrary.appendChild(el);
+ };
+
+ elements.searchLibrary.addEventListener("click", function (e) {
+ const target = e.target;
+ let option;
+ if (target && target instanceof HTMLSpanElement) {
+ option = target;
+ } else {
+ return;
+ }
+ const selected =
+ elements.searchLibrary.querySelector("span[selected]");
+ selected && selected.attributes.removeNamedItem("selected");
+ option.setAttribute("selected", "");
+ const id = option.getAttribute("data-value");
+ const name = option.textContent;
+ if (
+ !(elements.library instanceof HTMLInputElement) ||
+ !(elements.searchField instanceof HTMLInputElement) ||
+ typeof id !== "string"
+ ) {
+ return;
+ }
+ elements.library.value = option.textContent;
+ elements.library.setAttribute("data-value", id);
+ elements.library.setAttribute("title", name);
+
+ localStorage.setItem("selectedGroup", id);
+
+ switchClass(elements.searchClear, displayNoneClass, true);
+ elements.searchField.value = "";
+ lastSearch.text = "";
+ clearLibrary();
+ });
+
+ if (groups.length === 0) {
return;
}
- const selected =
- elements.searchLibrary.querySelector("span[selected]");
- selected && selected.attributes.removeNamedItem("selected");
- option.setAttribute("selected", "");
- const id = option.getAttribute("data-value");
- const name = option.textContent;
- library.value = option.textContent;
- library.setAttribute("data-value", id);
- library.setAttribute("title", name);
- localStorage.setItem("selectedGroup", id);
-
- switchClass(elements.searchClear, displayNoneClass, true);
- elements.searchField.value = "";
- lastSearch.text = "";
- clearLibrary();
+ for (var i = 0; i < customGroups.length; i++) {
+ const id = customGroups[i].id;
+ const name = customGroups[i].name;
+ addGroupToSelectBox(id, name);
+ }
+ addSeparator();
+ for (var i = 0; i < groups.length; i++) {
+ const id = groups[i].id;
+ const name = groups[i].name;
+ addGroupToSelectBox(id, name);
+ }
});
-
- if (groups.length === 0) {
- return;
- }
- for (var i = 0; i < customGroups.length; i++) {
- const id = customGroups[i].id;
- const name = customGroups[i].name;
- addGroupToSelectBox(id, name);
- }
- addSeparator();
- for (var i = 0; i < groups.length; i++) {
- const id = groups[i].id;
- const name = groups[i].name;
- addGroupToSelectBox(id, name);
- }
- });
}
/**
* @return {number|"all"|"my_library"|"group_libraries"}
*/
- getSelectedGroup = function () {
+ function getSelectedGroup() {
for (var i = 0; i < elements.searchLibrary.children.length; i++) {
const option = elements.searchLibrary.children[i];
if (option.hasAttribute("selected")) {
const id = option.getAttribute("data-value");
- if (["my_library", "group_libraries", "all"].indexOf(id) >= 0)
+ if (
+ id === "my_library" ||
+ id === "group_libraries" ||
+ id === "all"
+ ) {
return id;
+ }
return Number(id);
}
}
return "all";
- };
+ }
/**
- * @param {object} stylesInfo
+ * @param {Array