some refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-09-05 00:57:42 +03:00
parent b4c319585e
commit 07e1e3a346
3 changed files with 138 additions and 60 deletions

View File

@ -64,7 +64,6 @@ object.Constructor('BasePage', {
'title', 'title',
'resolved', 'resolved',
'rootpath', 'rootpath',
'renderer',
'length', 'length',
'type', 'type',
'ctime', 'ctime',
@ -106,7 +105,6 @@ object.Constructor('BasePage', {
return context.index }, return context.index },
//*/ //*/
}, },
resolvePathVars: function(path, context={}){ resolvePathVars: function(path, context={}){
var that = this var that = this
return Object.entries(this.path_vars) return Object.entries(this.path_vars)
@ -663,10 +661,11 @@ object.Constructor('Page', BasePage, {
// //
// NOTE: for manual rendering (.parse(..), ... etc.) this has to be // NOTE: for manual rendering (.parse(..), ... etc.) this has to be
// setup manually. // setup manually.
render_root: undefined, //renderer: undefined,
get renderer(){ get renderer(){
return (this.render_root || {}).path }, return this.__render_root ?? this },
set renderer(value){
this.__render_root = value },
// //
// <filter>(<source>) // <filter>(<source>)
@ -735,11 +734,13 @@ object.Constructor('Page', BasePage, {
&& this.root && this.root
&& this.root.args[args.name]) && this.root.args[args.name])
|| args.default }), || args.default }),
// alias to @arg(..)...
'': Macro( '': Macro(
['name', 'default', ['local']], ['name', 'default', ['local']],
function(args){ function(args){
return this.macros.arg.call(this, args) }), return this.macros.arg.call(this, args) }),
// XXX do we need this???
'args': function(){
return pwpath.obj2args(this.args) },
// //
// @filter(<filter-spec>) // @filter(<filter-spec>)
// <filter <filter-spec>/> // <filter <filter-spec>/>
@ -1240,6 +1241,10 @@ object.Constructor('Page', BasePage, {
...module.BasePage.prototype.actions, ...module.BasePage.prototype.actions,
'!', '!',
// XXX DEBUG -- remove these...
'testDirect',
'testDirect!',
]), ]),
'!': Object.assign( '!': Object.assign(
@ -1247,6 +1252,24 @@ object.Constructor('Page', BasePage, {
return this.get('.', {energetic: true}).raw }, return this.get('.', {energetic: true}).raw },
{energetic: true}), {energetic: true}),
// XXX DEBUG -- remove these...
testDirect: function(){
console.log('testDirect:', this.location,
this.args,
(this.renderer ?? {}).args,
(this.root ?? {}).args)
console.log(' :', this, this.renderer)
return this.location },
'testDirect!': Object.assign(
function(){
console.log('testDirect!:', this.location,
this.args,
(this.renderer ?? {}).args,
(this.root ?? {}).args)
console.log(' :', this, this.renderer)
return this.location },
{energetic: true}),
// events... // events...
// //
// NOTE: textUpdate event will not get triggered if text is updated // NOTE: textUpdate event will not get triggered if text is updated
@ -1273,7 +1296,6 @@ object.Constructor('Page', BasePage, {
state = text state = text
text = null } text = null }
state = state ?? {} state = state ?? {}
return this.__parser__.parse(this, text, state) }, return this.__parser__.parse(this, text, state) },
// true if page has an array value but is not a pattern page... // true if page has an array value but is not a pattern page...
@ -1389,14 +1411,14 @@ object.Constructor('Page', BasePage, {
// strict mode -- break on non-existing pages... // strict mode -- break on non-existing pages...
if(this.strict if(this.strict
&& !await this.resolve(true)){ && !await this.resolve(true)){
throw new Error('NOT FOUND ERROR: '+ this.path) } throw new Error('NOT FOUND ERROR: '+ this.location) }
var path = pwpath.split(this.path) var path = pwpath.split(this.path)
path.at(-1)[0] == '_' path.at(-1)[0] == '_'
|| path.push(this.PAGE_TEMPLATE) || path.push(this.PAGE_TEMPLATE)
var tpl = pwpath.join(path) var tpl = pwpath.join(path)
var tpl_name = path.pop() var tpl_name = path.pop()
path = pwpath.join(path) path = pwpath.joinArgs(path, this.args)
// get the template relative to the top most pattern... // get the template relative to the top most pattern...
tpl = await this.get(tpl).find(true) tpl = await this.get(tpl).find(true)
@ -1410,7 +1432,7 @@ object.Constructor('Page', BasePage, {
// this is here for debugging and introspection... // this is here for debugging and introspection...
'__debug_last_render_state' in this '__debug_last_render_state' in this
&& (this.__debug_last_render_state = state) && (this.__debug_last_render_state = state)
var data = { render_root: this } var data = { renderer: this }
return this.get(path, data) return this.get(path, data)
.parse( .parse(
this.get('/'+tpl, data).raw, this.get('/'+tpl, data).raw,
@ -1419,10 +1441,10 @@ object.Constructor('Page', BasePage, {
this.__update__({text: value}) }, this.__update__({text: value}) },
//this.onTextUpdate(value) }, //this.onTextUpdate(value) },
// pass on .render_root to clones... // pass on .renderer to clones...
clone: function(data={}, ...args){ clone: function(data={}, ...args){
this.render_root this.renderer
&& (data = {render_root: this.render_root, ...data}) && (data = {renderer: this.renderer, ...data})
return object.parentCall(Page.prototype.clone, this, data, ...args) }, return object.parentCall(Page.prototype.clone, this, data, ...args) },
}) })
@ -1635,15 +1657,15 @@ object.Constructor('pWikiPageElement', Page, {
this.dom.dispatchEvent(this.__pWikiLoadedDOMEvent) }), this.dom.dispatchEvent(this.__pWikiLoadedDOMEvent) }),
// XXX CACHE... // XXX CACHE...
__last_refresh_path: undefined, __last_refresh_location: undefined,
refresh: async function(full=false){ refresh: async function(full=false){
// drop cache if re-refreshing or when full refresh requested... // drop cache if re-refreshing or when full refresh requested...
// XXX CACHE... // XXX CACHE...
;(full ;(full
|| this.__last_refresh_path == this.path) || this.__last_refresh_location == this.location)
&& this.cache && this.cache
&& (this.cache = null) && (this.cache = null)
this.__last_refresh_path = this.path this.__last_refresh_location = this.location
var dom = this.dom var dom = this.dom
dom.innerHTML = await this.text dom.innerHTML = await this.text
for(var filter of Object.values(this.domFilters)){ for(var filter of Object.values(this.domFilters)){
@ -1741,6 +1763,12 @@ module.System = {
'@source(./path)' '@source(./path)'
+'<hr>' +'<hr>'
+'<macro src="." join="@source(file-separator)">' +'<macro src="." join="@source(file-separator)">'
+'<h1 '
+'contenteditable '
// XXX need to make this savable...
+'oninput="saveContent(\'@source(./path)/name\')">'
+'@source(./name)'
+'</h1>'
+'<pre class="editor" ' +'<pre class="editor" '
+'wikiwords="no" ' +'wikiwords="no" '
+'contenteditable ' +'contenteditable '
@ -1755,6 +1783,11 @@ module.System = {
<slot name="header">@source(../path)</slot> <slot name="header">@source(../path)</slot>
<slot name="content"> <slot name="content">
<macro src=".." join="@source(file-separator)"> <macro src=".." join="@source(file-separator)">
<h1 class="title-editor"
contenteditable
oninput="saveContent(\'@source(./path)/name\')">
@source(./name)
</h1>
<pre class="editor" <pre class="editor"
wikiwords="no" wikiwords="no"
contenteditable contenteditable
@ -1763,17 +1796,6 @@ module.System = {
</macro> </macro>
</slot>`}, </slot>`},
// XXX this does not yet work...
// XXX "_test" breaks differently than "test"
//_test: {
test: {
text: object.doc`
@source(_view)
<slot name="header">HEADER</slot>
<slot name="content">CONTENT</slot>
<slot name="footer">FOOTER</slot> `},
// XXX debug... // XXX debug...
_path: {text: '@source(./path join=" ")'}, _path: {text: '@source(./path join=" ")'},
@ -1824,8 +1846,6 @@ module.System = {
(<a href="#@source(../resolved)/edit">edit</a>)<br> (<a href="#@source(../resolved)/edit">edit</a>)<br>
Referrer: @source(../referrer) Referrer: @source(../referrer)
(<a href="#@source(../referrer)/edit">edit</a>)<br> (<a href="#@source(../referrer)/edit">edit</a>)<br>
Renderer: @source(../renderer)
(<a href="#@source(../renderer)/edit">edit</a>)<br>
type: @source(../type)<br> type: @source(../type)<br>
@ -1872,6 +1892,7 @@ module.System = {
// page actions... // page actions...
// //
/* XXX broken...
// XXX this does not work as energetic... // XXX this does not work as energetic...
// XXX for some reason this is called twice... // XXX for some reason this is called twice...
time: async function(){ time: async function(){
@ -1885,6 +1906,7 @@ module.System = {
Time to render: ${time}ms <br> Time to render: ${time}ms <br>
<ht> <ht>
${text}`}, ${text}`},
//*/
// XXX EXPERIMENTAL -- page types... // XXX EXPERIMENTAL -- page types...
isAction: async function(){ isAction: async function(){
@ -1897,20 +1919,29 @@ module.System = {
: undefined }, : undefined },
// utils... // XXX DEBUG -- remove these...
// testPage: {
// XXX System/subpaths text: object.doc`<pre>
/*/ XXX location: @source(./location)
links: function(){ path: @source(./path)
// XXX args: <args/>
return '' }, </pre>`},
// XXX links to pages... testAction: function(){
LinksTo: function(){ console.log('testAction:', this.location,
return (this.get('..').data || {}).to ?? [] }, this.args,
// XXX pages linking to us... (this.renderer ?? {}).args,
LinksFrom: function(){ (this.root ?? {}).args)
return (this.get('..').data || {})['from'] ?? [] }, console.log(' :', this, this.renderer)
//*/ return this.location },
'testAction!': Object.assign(
function(){
console.log('testAction!:', this.location,
this.args,
(this.renderer ?? {}).args,
(this.root ?? {}).args)
console.log(' :', this, this.renderer)
return this.location },
{energetic: true}),
// actions... // actions...
@ -1923,20 +1954,37 @@ module.System = {
target.delete() target.delete()
// redirect... // redirect...
this.render_root this.renderer
&& (this.render_root.location = this.referrer) && (this.renderer.location = this.referrer)
// show info about the delete operation... // show info about the delete operation...
return target.get('DeletingPage/_text').text }, return target.get('DeletingPage/_text').text },
// XXX copy/move/... // XXX copy/move/...
// ...need arguments // XXX do we need this as a page action???
move: function(){
var from = this.get('..')
// XXX this is ugly...
// ...need to standardize how we get arguments when rendering....
var to = this.args.to
|| (this.renderer || {args:{}}).args.to
console.log('MOVE:', from.path, to)
// XXX
if(to){
// XXX move...
}
// redirect...
this.renderer
&& (this.renderer.location = this.referrer)
// XXX if we return undefined here this will not fully redirect,
// keeping the move page open but setting the url to the
// redirected page...
return '' },
// XXX System/back // XXX System/back
// XXX System/forward // XXX System/forward
// XXX System/sort // XXX System/sort
// XXX System/reverse // XXX System/reverse
} }
var Test = var Test =

View File

@ -219,17 +219,19 @@ require(['./browser'], function(browser){
// XXX versioning??? // XXX versioning???
var SAVE_TIMEOUT = 5000 var SAVE_TIMEOUT = 5000
var SAVE_QUEUE = {} var SAVE_QUEUE = {}
var saveContent = function(path, text){ var saveContent =
SAVE_QUEUE[path] = text function(path, text){
// clear editor page cache... SAVE_QUEUE[path] = text
pwiki.cache = null } // clear editor page cache...
var saveNow = function(){ pwiki.cache = null }
var queue = Object.entries(SAVE_QUEUE) var saveNow =
SAVE_QUEUE = {} function(){
queue var queue = Object.entries(SAVE_QUEUE)
.forEach(function([path, text]){ SAVE_QUEUE = {}
console.log('saving changes to:', path) queue
pwiki.get(path).raw = text }) } .forEach(function([path, text]){
console.log('saving changes to:', path)
pwiki.get(path).raw = text }) }
setInterval(saveNow, 5000) setInterval(saveNow, 5000)

View File

@ -1,6 +1,34 @@
/********************************************************************** /**********************************************************************
* *
* *
* XXX ARGS: arg passing is a mess...
* - need a consistent way to access args
* - global
* .root.args?
* - render root
* - local
* use:
* testDirect - direct action
* testDirect! - energetic direct action
* testPage - page
* testAction - page action
* testAction! - energetic page action
* examples:
* // root path: /System/testAction:a:b:c
* await pwiki.get('/path/testDirect:x:y:z').raw
* - x y z -- not seen at all (XXX)
* - a b c -- seen .renderer and .root
* await pwiki.get('/path/testAction:x:y:z').raw
* - x y z -- seen local
* - a b c -- seen .renderer and .root
* await pwiki.get('/path/testDirect:x:y:z').text
* - x y z -- seen .renderer
* - a b c -- seen .root
* await pwiki.get('/path/testAction:x:y:z').text
* (same as for testDirect+text)
* - x y z -- seen .renderer
* - a b c -- seen .root
* XXX CACHE need to explicitly prevent caching of some actions/pages...
* XXX FEATURE tags and accompanying API... * XXX FEATURE tags and accompanying API...
* - add tags to page -- macro/filter * - add tags to page -- macro/filter
* - <page>.text -> <page>.tags (cached on .update(..)) * - <page>.text -> <page>.tags (cached on .update(..))