diff options
author | Andrew Dolgov <[email protected]> | 2020-09-14 12:12:44 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2020-09-14 12:12:44 +0300 |
commit | 3dd691939c9c8755b47a4761ec36660b7ec2c140 (patch) | |
tree | 0e47bc17c22be4d62a5d76fc3a39caab016b0f6b /js/reader.js | |
parent | 4e343773dd6fd4ecb3f3382719ca363f93ef372b (diff) |
add helpers to flatten epub TOC
Diffstat (limited to 'js/reader.js')
-rw-r--r-- | js/reader.js | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/js/reader.js b/js/reader.js index a54bea0..e271762 100644 --- a/js/reader.js +++ b/js/reader.js @@ -563,40 +563,16 @@ const Reader = { const cur_href = book.canonical(location.start.href); let toc_entry = false; - /* eslint-disable no-inner-declarations */ - function iterate_sublist(row, nest) { - if (nest == 2) return false; + $.each(Reader.flattenToc(book), function(i, r) { - if (row.subitems) { - $.each(row.subitems, function (i, r) { - - if (book.spine.get(r.href).canonical == cur_href) { - toc_entry = r; - return true; - } - - if (iterate_sublist(r, nest + 0)) - return true; - }); - } - - return false; - } - - /* eslint-enable no-inner-declarations */ - - $.each(book.navigation.toc, function(i, a) { - if (book.spine.get(a.href).canonical == cur_href) { - toc_entry = a; + if (book.spine.get(r.href).canonical == cur_href) { + toc_entry = r; return; } - - if (iterate_sublist(a, 0)) return; - }); - if (toc_entry && toc_entry.label.trim()) - $(".chapter").append(" " + toc_entry.label + " | "); + if (toc_entry && toc_entry.label) + $(".chapter").append(" " + toc_entry.label.trim() + " | "); } } catch (e) { @@ -657,6 +633,30 @@ const Reader = { } }); }, + flattenTocSubItems: function(entry, nest) { + const rv = []; + + if (nest == 2) return false; + + if (entry.subitems) { + $.each(entry.subitems, function (i, r) { + rv.push(r); + rv.concat(Reader.flattenTocSubItems(r, nest+1)); + }); + } + + return rv; + }, + flattenToc: function(book) { + const rv = []; + + $.each(book.navigation.toc, function(i, a) { + rv.push(a); + rv.concat(Reader.flattenTocSubItems(a), 0); + }); + + return rv; + }, applyStyles: function(default_only) { Promise.all([ localforage.getItem("epube.fontSize"), |