added basic aquisition mechanics + started templates...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-07-05 16:58:04 +03:00
parent 0a583db1e4
commit 5ed51c559d

80
wiki.js
View File

@ -22,11 +22,8 @@
// } // }
// } // }
var data = { var data = {
'/': { 'TemplatePages/DefaultPage': {
text: 'base page...' text: 'This page is empty.<br><br>WikiHome',
},
'dir/page': {
text: 'some text...'
}, },
} }
@ -46,9 +43,14 @@ var normalizePath = path =>
var Wiki = { var Wiki = {
__wiki_data: data, __wiki_data: data,
__home_page__: 'WikiHome',
__default_page__: 'DefaultPage',
__templates__: 'TemplatePages',
// current location... // current location...
get location(){ get location(){
return this.__location || '/' }, return this.__location || this.__home_page__ },
set location(value){ set location(value){
this.__location = normalizePath(value) }, this.__location = normalizePath(value) },
@ -89,8 +91,13 @@ var Wiki = {
// page content... // page content...
//
// NOTE: if this page has not text, this will get the DefaultPage...
// XXX should this get the DefaultPage or the EmptyPage???
get text(){ get text(){
return (this.__wiki_data[this.location] || {}).text || '' return (this.__wiki_data[this.location] || {}).text
|| (this.acquire(this.__default_page__) || {}).text
|| ''
}, },
set text(value){ set text(value){
var l = this.location var l = this.location
@ -112,22 +119,63 @@ var Wiki = {
}, },
exists: function(path){
return normalizePath(path) in this.__wiki_data },
// get title from dir and then go up the tree...
acquire: function(title){
title = title || this.__default_page__
var templates = this.__templates__
var data = this.__wiki_data
var that = this
// serialization... var path = path2lst(this.dir)
get json(){
var _res = function(p){
return { return {
path: this.path, dir: normalizePath(p.slice(0, -1)),
text: this.text, title: title,
text: that.__wiki_data[normalizePath(p)].text
}
}
while(true){
// get title from path...
var p = path.concat([title])
if(this.exists(p)){
return _res(p)
}
// get title from templates in path...
var p = path.concat([templates, title])
if(this.exists(p)){
return _res(p)
}
if(path.length == 0){
return
}
path.pop()
} }
}, },
set json(value){
if(value.title){
this.title = value.title // serialization...
json: function(path){
return path == null ? JSON.parse(JSON.stringify(this.__wiki_data))
: path == '.' ? {
path: this.location,
text: this.text,
} }
if(value.text){ : {
this.text = value.text path: path,
text: (this.__wiki_data[path] || {}).text,
} }
}, },
// XXX should we inherit from the default???
load: function(json){
this.__wiki_data = json
},
} }