From 617dac9ce31a781198545b0e7a3cb1b637b6cfc7 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 6 Aug 2022 11:09:25 +0300 Subject: [PATCH] reworked .paths(), now it will search subpaths too... Signed-off-by: Alex A. Naanou --- browser.js | 2 ++ pwiki/page.js | 16 ++++++++++++++++ pwiki/path.js | 37 ++++++++++++++++++++++++++----------- pwiki2-test.js | 1 + pwiki2.html | 30 ++++++++++++++++++++++++++++-- pwiki2.js | 9 ++++++++- 6 files changed, 81 insertions(+), 14 deletions(-) diff --git a/browser.js b/browser.js index 1f7c2a6..82f505d 100755 --- a/browser.js +++ b/browser.js @@ -34,6 +34,8 @@ store.update('System', var pwiki = module.pwiki = + // XXX + //page.DOMPage('/', '/', store) page.Page('/', '/', store) diff --git a/pwiki/page.js b/pwiki/page.js index 613baf7..e12890a 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -1087,6 +1087,21 @@ object.Constructor('Page', BasePage, { +//--------------------------------------------------------------------- + +// XXX do we actually need this??? +var DOMPage = +module.DOMPage = +object.Constructor('DOMPage', Page, { + dom: undefined, + + // XXX might be a good idea to move this up to Page and trigger when + // done updating... + onLoad: types.event.Event('onLoad'), +}) + + + //--------------------------------------------------------------------- // System pages/actions... @@ -1190,5 +1205,6 @@ module.System = { + /********************************************************************** * vim:set ts=4 sw=4 nowrap : */ return module }) diff --git a/pwiki/path.js b/pwiki/path.js index f5451f2..d0e1ffe 100755 --- a/pwiki/path.js +++ b/pwiki/path.js @@ -124,7 +124,15 @@ module = { // // NOTE: if seen is given (when called recursively) this will not // search for .ALTERNATIVE_PAGES... - // XXX should we search for each path element or just the last one (current)??? + // NOTE: this will search for basename and each subpath, e.g: + // a/b/c + // -> a/b/c/d + // -> a/c/d + // -> c/d + // -> d + // // now search for 'c/d'... + // -> a/c/d + // -> ... // XXX should we keep the trailing '/'??? paths: function*(path='/', strict=false){ if(path === true || path === false){ @@ -159,16 +167,23 @@ module = { var page = path.pop() for(var tpl of ['.', ...this.SEARCH_PATHS]){ // search for page up the path... - var p = path.slice() - while(p.length > 0){ - var cur = this.relative(p, tpl +'/'+ page, 'string') - if(!seen.has(cur)){ - seen.add(cur) - yield cur } - // special case: non-relative template/page path... - if(tpl[0] == '/'){ - break } - p.pop() } } + var pg = page + var base = path.slice() + while(base.length > 0){ + var p = base.slice() + while(p.length > 0){ + var cur = this.relative(p, tpl +'/'+ pg, 'string') + if(!seen.has(cur)){ + seen.add(cur) + yield cur } + // special case: non-relative template/page path... + if(tpl[0] == '/'){ + break } + p.pop() } + // next search for tail sub-path... + // for a/b/c + // c in a/b -> b/c in a + pg = base.pop() +'/'+ pg } } // alternative pages... if(alt_pages){ for(var page of [...this.ALTERNATIVE_PAGES]){ diff --git a/pwiki2-test.js b/pwiki2-test.js index b446326..3d8de9b 100755 --- a/pwiki2-test.js +++ b/pwiki2-test.js @@ -10,6 +10,7 @@ var object = require('ig-object') var pwiki = require('./pwiki2') +module.path = pwiki.path // XXX for some reason this does not run quietly in browser var pouchdbstore = require('./pwiki/store/pouchdb') diff --git a/pwiki2.html b/pwiki2.html index d14a598..3af78ad 100755 --- a/pwiki2.html +++ b/pwiki2.html @@ -83,10 +83,18 @@ require.config({ ] }) + +document.pwikiloaded = new Event('pwikiloaded') + + // start loading pWiki... require(['./browser'], function(m){ window.pwiki = m.pwiki + // XXX make a pWikiDom page to manage this... + pwiki.dom = document.querySelector('#pWiki') + + // handle location.hash (both directions) var _debounceHashChange = false pwiki.onNavigate(async function(){ @@ -96,7 +104,13 @@ require(['./browser'], function(m){ setTimeout(function(){ _debounceHashChange = false }, 0) // render... - document.querySelector('#pWiki').innerHTML = await this.text }) + pwiki.dom.innerHTML = await this.text + // pwiki page loaded event... + // XXX do we need to use a MutationObserver here to trigger this + // after the above is done loading??? + // (see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) + pwiki.dom.dispatchEvent(document.pwikiloaded) }) + window.addEventListener('hashchange', function(evt){ evt.preventDefault() if(_debounceHashChange){ @@ -105,7 +119,19 @@ require(['./browser'], function(m){ path = path.trim() == '' ? '/' : path - pwiki.path = path }) + pwiki.path = path + + // XXX when loaded us .scrollIntoView() to get to hash... + // ...would also be nice to keep hash within location.hash... + }) + + + // XXX use a pwiki.onLoad event... + pwiki.dom.addEventListener('pwikiloaded', function(evt){ + console.log('pWiki loaded') + + // XXX scroll + }) // show current page... pwiki.path = location.hash.slice(1) diff --git a/pwiki2.js b/pwiki2.js index 61b7d0a..a45ec02 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -1,6 +1,11 @@ /********************************************************************** * * +* XXX shoul Doc/About be found from both / and /WikiHome??? +* ...currently / + Doc/About reolves correctly while +* /WikiHome + Doc/About is not found... +* ...this is a questions of subpath search, i.e. when we do not +* find "About" should we search for Doc/About and so on... * XXX wikiword filter seems to act up on / * XXX BUG? /test/wikiword -- produces nested links... * @@ -120,7 +125,9 @@ var object = require('ig-object') var types = require('ig-types') -var pwpath = require('./pwiki/path') +var pwpath = +module.path = + require('./pwiki/path') var page = require('./pwiki/page') var basestore = require('./pwiki/store/base')