From d8f5e4a3747e419899869572c92191a7773da229 Mon Sep 17 00:00:00 2001 From: yifang Date: Wed, 12 Jan 2022 22:30:43 +0800 Subject: [PATCH] improvement:seperate javascript file from cpp code. --- article_maker.cc | 55 +++++------------------ resources.qrc | 1 + resources/gd_builtin.js | 99 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 resources/gd_builtin.js diff --git a/article_maker.cc b/article_maker.cc index 9d4cac7d..3b6dea22 100644 --- a/article_maker.cc +++ b/article_maker.cc @@ -82,7 +82,6 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word, // Add a css stylesheet { - QByteArray css; result += ""; if ( displayStyle.size() ) @@ -136,51 +135,17 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word, result += "\n"; result += ""; - + result+= ""; result += ""; return result; diff --git a/resources.qrc b/resources.qrc index 29f3431d..619863f2 100644 --- a/resources.qrc +++ b/resources.qrc @@ -86,5 +86,6 @@ icons/ontop.png resources/jquery-3.6.0.slim.min.js resources/gd_custom.js + resources/gd_builtin.js diff --git a/resources/gd_builtin.js b/resources/gd_builtin.js new file mode 100644 index 00000000..04cf6ed5 --- /dev/null +++ b/resources/gd_builtin.js @@ -0,0 +1,99 @@ +// seperate from cpp code. +var gdAudioLinks = { first: null, current: null }; + +function gdMakeArticleActive(newId) { + if (gdCurrentArticle != 'gdfrom-' + newId) { + el = document.getElementById(gdCurrentArticle); + el.className = el.className.replace(' gdactivearticle', ''); + el = document.getElementById('gdfrom-' + newId); + el.className = el.className + ' gdactivearticle'; + gdCurrentArticle = 'gdfrom-' + newId; + gdAudioLinks.current = newId; + articleview.onJsActiveArticleChanged(gdCurrentArticle); + } +} +var overIframeId = null; + +function gdSelectArticle(id) { + var selection = window.getSelection(); + var range = document.createRange(); + range.selectNodeContents(document.getElementById('gdfrom-' + id)); + selection.removeAllRanges(); + selection.addRange(range); +} + +function processIframeMouseOut() { + overIframeId = null; + top.focus(); +} + +function processIframeMouseOver(newId) { overIframeId = newId; } + +function processIframeClick() { + if (overIframeId != null) { + overIframeId = overIframeId.replace('gdexpandframe-', ''); + gdMakeArticleActive(overIframeId) + } +} + +function init() { window.addEventListener('blur', processIframeClick, false); } +window.addEventListener('load', init, false); + +function gdExpandOptPart(expanderId, optionalId) { + var d1 = document.getElementById(expanderId); + var i = 0; + if (d1.alt == '[+]') { + d1.alt = '[-]'; + d1.src = 'qrcx://localhost/icons/collapse_opt.png'; + for (i = 0; i < 1000; i++) { + var d2 = document.getElementById(optionalId + i); + if (!d2) break; + d2.style.display = 'inline'; + } + } else { + d1.alt = '[+]'; + d1.src = 'qrcx://localhost/icons/expand_opt.png'; + for (i = 0; i < 1000; i++) { + var d2 = document.getElementById(optionalId + i); + if (!d2) break; + d2.style.display = 'none'; + } + } +}; + +function gdExpandArticle(id) { + elem = document.getElementById('gdarticlefrom-' + id); + ico = document.getElementById('expandicon-' + id); + art = document.getElementById('gdfrom-' + id); + ev = window.event; + t = null; + if (ev) t = ev.target || ev.srcElement; + if (elem.style.display == 'inline' && t == ico) { + elem.style.display = 'none'; + ico.className = 'gdexpandicon'; + art.className = art.className + ' gdcollapsedarticle'; + nm = document.getElementById('gddictname-' + id); + nm.style.cursor = 'pointer'; + if (ev) ev.stopPropagation(); + ico.title = tr("Expand article"); + nm.title = ''; + } else if (elem.style.display == 'none') { + elem.style.display = 'inline'; + ico.className = 'gdcollapseicon'; + art.className = art.className.replace(' gdcollapsedarticle', ''); + nm = document.getElementById('gddictname-' + id); + nm.style.cursor = 'default'; + nm.title = ''; + ico.title = tr("Collapse article"); + } +} + +function gdCheckArticlesNumber() { + elems = document.getElementsByClassName('gddictname'); + if (elems.length == 1) { + el = elems.item(0); + s = el.id.replace('gddictname-', ''); + el = document.getElementById('gdfrom-' + s); + if (el && el.className.search('gdcollapsedarticle') > 0) gdExpandArticle(s); + } +} \ No newline at end of file