2 Commits f7a0fe37a8 ... 6433b35cf5

Author SHA1 Message Date
  Mathieu Lirzin 6433b35cf5 Replace IIFE usage with function properties 6 years ago
  Mathieu Lirzin b57bdc6760 utils: Add 'href_hash' function 6 years ago
4 changed files with 57 additions and 42 deletions
  1. 3 7
      js/src/iframe.js
  2. 18 18
      js/src/index.js
  3. 8 2
      js/src/toc.js
  4. 28 15
      js/src/utils.js

+ 3 - 7
js/src/iframe.js

@@ -17,7 +17,7 @@
    along with GNU Texinfo.  If not, see <http://www.gnu.org/licenses/>.  */
 
 import * as actions from "./actions";
-import { basename, navigation_links } from "./utils";
+import { basename, href_hash, navigation_links } from "./utils";
 import config from "./config";
 import { fix_links } from "./toc";
 import { iframe_dispatch } from "./store";
@@ -67,12 +67,8 @@ on_message (event)
   switch (data.message_kind)
     {
     case "scroll-to":
-      {
-        let url = data.url;
-        window.location.hash = (url.includes ("#")) ?
-          url.replace (/.*#/, "") : "";
-        break;
-      }
+      window.location.hash = href_hash (data.url);
+      break;
     default:
       break;
     }

+ 18 - 18
js/src/index.js

@@ -32,7 +32,7 @@ import "./polyfill";
 import * as main from "./main";
 import * as pages from "./iframe";
 import * as sidebar from "./sidebar";
-import { absolute_url_p } from "./utils";
+import { absolute_url_p, href_hash } from "./utils";
 import config from "./config";
 import { with_sidebar_query } from "./toc";
 
@@ -50,7 +50,7 @@ on_click (event)
           let href = target.getAttribute ("href");
           if (!absolute_url_p (href))
             {
-              let url = href.replace (/.*#/, "") || config.INDEX_ID;
+              let url = href_hash (href) || config.INDEX_ID;
               if (url.includes ("."))
                 url = url.replace (/[.]/, ".xhtml#");
               else
@@ -76,22 +76,22 @@ on_unload ()
 }
 
 /* Handle Keyboard 'keypress' events.  */
-var on_keypress = (function () {
-  /* Dictionary associating an Event 'key' property to its navigation id.  */
-  let dict = {
-    n: "next",
-    p: "prev",
-    u: "up",
-    "]": "forward",
-    "[": "backward"
-  };
-
-  return function (event) {
-    let nav = dict[event.key];
-    if (nav)
-      top.postMessage ({ message_kind: "load-page", nav }, "*");
-  };
-} ());
+function
+on_keypress (event)
+{
+  let nav = on_keypress.dict[event.key];
+  if (nav)
+    top.postMessage ({ message_kind: "load-page", nav }, "*");
+}
+
+/* Dictionary associating an Event 'key' property to its navigation id.  */
+on_keypress.dict = {
+  n: "next",
+  p: "prev",
+  u: "up",
+  "]": "forward",
+  "[": "backward"
+};
 
 /*--------------------
 | Context dispatch.  |

+ 8 - 2
js/src/toc.js

@@ -16,7 +16,13 @@
    You should have received a copy of the GNU General Public License
    along with GNU Texinfo.  If not, see <http://www.gnu.org/licenses/>.  */
 
-import { absolute_url_p, basename, depth_first_walk } from "./utils";
+import {
+  absolute_url_p,
+  basename,
+  depth_first_walk,
+  href_hash
+} from "./utils";
+
 import config from "./config";
 
 /** Return a relative URL corresponding to HREF, which refers to an anchor of
@@ -141,7 +147,7 @@ create_link_dict (nav)
   {
     if (elem.matches ("a") && elem.hasAttribute ("href"))
     {
-      let id = elem.getAttribute ("href").replace (/.*#/, "");
+      let id = href_hash (elem.getAttribute ("href"));
       links[prev_id] = Object.assign ({}, links[prev_id], { forward: id });
       links[id] = Object.assign ({}, links[id], { backward: prev_id });
       prev_id = id;

+ 28 - 15
js/src/utils.js

@@ -54,22 +54,35 @@ depth_first_walk (node, func, node_type)
     depth_first_walk (child, func, node_type);
 }
 
+/** Return the hash part of HREF without the '#' prefix.  HREF must be
+    a string.  If there is no hash part in HREF then return the empty
+    string.  */
+export function
+href_hash (href)
+{
+  if (typeof href !== "string")
+    throw new TypeError (href + " is not a string");
+
+  return href.replace (/.*#/, "");
+}
+
 /** Retrieve PREV, NEXT, and UP links and Return a object containing
     references to those links.  */
-export var navigation_links = (function () {
-  /* Dictionary associating an 'accesskey' property to its navigation id.  */
-  let dict = { n: "next", p: "prev", u: "up" };
+export function
+navigation_links (content)
+{
+  let links = Array.from (content.querySelectorAll ("footer a"));
+
+  /* links have the form MAIN_FILE.html#FRAME-ID.  For convenience
+     we only store FRAME-ID.  */
+  return links.reduce ((acc, link) => {
+    let nav_id = navigation_links.dict[link.getAttribute ("accesskey")];
+    if (nav_id)
+      acc[nav_id] = href_hash (link.getAttribute ("href"));
+    return acc;
+  }, {});
+}
 
-  return function (content) {
-    let links = Array.from (content.querySelectorAll ("footer a"));
+/* Dictionary associating an 'accesskey' property to its navigation id.  */
+navigation_links.dict = { n: "next", p: "prev", u: "up" };
 
-    /* links have the form MAIN_FILE.html#FRAME-ID.  For convenience
-       we only store FRAME-ID.  */
-    return links.reduce ((acc, link) => {
-      let nav_id = dict[link.getAttribute ("accesskey")];
-      if (nav_id)
-        acc[nav_id] = link.getAttribute ("href").replace (/.*#/, "");
-      return acc;
-    }, {});
-  };
-} ());