|
@@ -13,6 +13,7 @@
|
|
|
|
|
|
import * as actions from "./actions";
|
|
|
import * as sidebar from "./sidebar";
|
|
|
+import { Store, iframe_dispatch } from "./store";
|
|
|
|
|
|
import {
|
|
|
absolute_url_p,
|
|
@@ -23,70 +24,37 @@ import {
|
|
|
|
|
|
import {
|
|
|
clear_toc_styles,
|
|
|
- fix_link,
|
|
|
- main_filename,
|
|
|
+ fix_links,
|
|
|
scan_toc
|
|
|
} from "./toc";
|
|
|
|
|
|
-import Store from "./store";
|
|
|
+import { Main_component } from "./component";
|
|
|
import config from "./config";
|
|
|
import { global_reducer } from "./reducers";
|
|
|
import polyfill from "./polyfill";
|
|
|
|
|
|
/* Global state manager. */
|
|
|
let store;
|
|
|
-
|
|
|
-let selected_div = {
|
|
|
- id: null,
|
|
|
- element: null,
|
|
|
-
|
|
|
- render (new_id)
|
|
|
- {
|
|
|
- if (new_id === this.id)
|
|
|
- return;
|
|
|
-
|
|
|
- if (this.element)
|
|
|
- this.element.setAttribute ("hidden", "true");
|
|
|
- var div = document.getElementById (new_id);
|
|
|
- div.removeAttribute ("hidden");
|
|
|
-
|
|
|
- this.id = new_id;
|
|
|
- this.element = div;
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-let components = {
|
|
|
- /* Instance of a Sidebar object. */
|
|
|
- sidebar: null,
|
|
|
- /* Currently visible page. */
|
|
|
- selected_div
|
|
|
-};
|
|
|
+/* Main stateful view. */
|
|
|
+let components;
|
|
|
|
|
|
/* Initialize the top level 'config.INDEX_NAME' DOM. */
|
|
|
function
|
|
|
on_index_load (_event)
|
|
|
{
|
|
|
- main_filename.val = basename (window.location.pathname);
|
|
|
+ fix_links (document.links);
|
|
|
+ document.body.setAttribute ("class", "mainbar");
|
|
|
|
|
|
- /* Move contents of <body> into a a fresh <div>. */
|
|
|
- var body = document.body;
|
|
|
- var div = document.createElement ("div");
|
|
|
- div.setAttribute ("id", config.INDEX_ID);
|
|
|
- div.setAttribute ("node", config.INDEX_ID);
|
|
|
- for (let ch = body.firstChild; ch !== null; ch = body.firstChild)
|
|
|
- div.appendChild (ch);
|
|
|
- body.appendChild (div);
|
|
|
+ /* Move contents of <body> into a a fresh <div> to let the components treat
|
|
|
+ the index page like other iframe page. */
|
|
|
+ let index_div = document.createElement ("div");
|
|
|
+ for (let ch = document.body.firstChild; ch; ch = document.body.firstChild)
|
|
|
+ index_div.appendChild (ch);
|
|
|
|
|
|
- if (sidebar.use_sidebar (window.location.hash))
|
|
|
- {
|
|
|
- let sbi = new sidebar.Sidebar ();
|
|
|
- sbi.render ({ current: config.INDEX_ID, visible: true });
|
|
|
- document.body.insertBefore (sbi.element, document.body.firstChild);
|
|
|
- document.body.setAttribute ("class", "mainbar");
|
|
|
- components.sidebar = sbi;
|
|
|
- }
|
|
|
+ /* Instanciate the components. */
|
|
|
+ components = new Main_component (document.body, index_div);
|
|
|
|
|
|
- fix_links (document.links);
|
|
|
+ /* Retrieve NEXT link. */
|
|
|
let links = {};
|
|
|
links[config.INDEX_ID] = navigation_links (document);
|
|
|
store.dispatch (actions.cache_links (links));
|
|
@@ -97,28 +65,11 @@ on_index_load (_event)
|
|
|
function
|
|
|
on_iframe_load (_event)
|
|
|
{
|
|
|
- main_filename.val = basename (window.name, /#.*/);
|
|
|
fix_links (document.links);
|
|
|
let links = {};
|
|
|
let url = basename (window.location.pathname, /[.]x?html$/);
|
|
|
links[url] = navigation_links (document);
|
|
|
- let action = actions.cache_links (links);
|
|
|
- top.postMessage ({ message_kind: "action", action }, "*");
|
|
|
-}
|
|
|
-
|
|
|
-/* Modify LINKS to handle the iframe based navigation properly.
|
|
|
- relative links will be opened inside the corresponding iframe and
|
|
|
- absolute links will be opened in a new tab. LINKS must be an array
|
|
|
- or a collection of nodes. */
|
|
|
-function
|
|
|
-fix_links (links)
|
|
|
-{
|
|
|
- for (let i = 0; i < links.length; i += 1)
|
|
|
- {
|
|
|
- let href = links[i].getAttribute ("href");
|
|
|
- if (href)
|
|
|
- fix_link (links[i], href);
|
|
|
- }
|
|
|
+ iframe_dispatch (actions.cache_links (links));
|
|
|
}
|
|
|
|
|
|
/* Retrieve PREV, NEXT, and UP links and Return a object containing references
|
|
@@ -181,7 +132,7 @@ load_page (url, hash)
|
|
|
}
|
|
|
|
|
|
let msg = { message_kind: "update-sidebar", selected: node_name };
|
|
|
- components.sidebar.element.contentWindow.postMessage (msg, "*");
|
|
|
+ components.sidebar.get_iframe_window ().postMessage (msg, "*");
|
|
|
window.history.pushState ("", document.title, path);
|
|
|
store.dispatch (actions.set_current_url (node_name));
|
|
|
}
|
|
@@ -197,16 +148,17 @@ receive_message (event)
|
|
|
break;
|
|
|
case "node-list": /* from sidebar to top frame */
|
|
|
{
|
|
|
- for (var i = 0; i < data.length; i += 1)
|
|
|
+ let nodes = Object.keys (store.state.loaded_nodes);
|
|
|
+ for (var i = 0; i < nodes.length; i += 1)
|
|
|
{
|
|
|
- let name = data[i];
|
|
|
+ let name = nodes[i];
|
|
|
if (name == config.INDEX_ID)
|
|
|
continue;
|
|
|
let div = document.createElement ("div");
|
|
|
div.setAttribute ("id", name);
|
|
|
div.setAttribute ("node", name);
|
|
|
div.setAttribute ("hidden", "true");
|
|
|
- document.body.appendChild (div);
|
|
|
+ document.querySelector ("#sub-pages").appendChild (div);
|
|
|
}
|
|
|
if (window.location.hash)
|
|
|
{
|
|
@@ -313,7 +265,7 @@ if (inside_iframe_p () || inside_index_page_p (window.location.pathname))
|
|
|
|
|
|
if (!inside_iframe_p ())
|
|
|
{
|
|
|
- window.addEventListener ("load", on_index_load, false);
|
|
|
+ window.addEventListener ("DOMContentLoaded", on_index_load, false);
|
|
|
window.addEventListener ("message", receive_message, false);
|
|
|
|
|
|
let initial_state = {
|
|
@@ -326,18 +278,16 @@ if (inside_iframe_p () || inside_index_page_p (window.location.pathname))
|
|
|
|
|
|
store = new Store (global_reducer, initial_state);
|
|
|
store.subscribe (() => console.log ("state: ", store.state));
|
|
|
- store.subscribe (() => {
|
|
|
- components.selected_div.render (store.state.current);
|
|
|
- });
|
|
|
+ store.subscribe (() => components.render (store.state));
|
|
|
}
|
|
|
else if (window.name == "slider")
|
|
|
{
|
|
|
- window.addEventListener ("load", sidebar.on_load, false);
|
|
|
+ window.addEventListener ("DOMContentLoaded", sidebar.on_load, false);
|
|
|
window.addEventListener ("message", sidebar.on_message, false);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- window.addEventListener ("load", on_iframe_load, false);
|
|
|
+ window.addEventListener ("DOMContentLoaded", on_iframe_load, false);
|
|
|
window.addEventListener ("message", receive_message, false);
|
|
|
}
|
|
|
|