refactoring url handling + cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-10-10 19:45:16 +03:00
parent 1785447fd4
commit 1da5cb65df
2 changed files with 42 additions and 14 deletions

View File

@ -71,31 +71,30 @@ $(function(){requirejs(['pwiki'], function(pwiki){
client.page = page
client.dom = $('.wiki')
// XXX get and load page location...
client.location('WikiHome')
// get the base url...
var url = window.location.hash.slice(1)
url = url != '' ? url
// XXX get and load page location...
// XXX
: 'WikiHome'
// go to base location...
client.location(url)
// handle hash change...
// XXX move to client setup...
// XXX need to resolve relative hashes...
$(window).on('hashchange', function(evt){
evt.preventDefault()
var path = location.hash.slice(1)
var hash = path.split('#')
path = hash.shift()
hash = hash.pop() || ''
// expand the path...
if(page.get(path).path() != path){
location.hash = page.get(path).path()
location.hash = client.get(path).path()
// open page...
} else {
client.location(path)
if(hash.length > 0){
// XXX focus anchor...
}
}
})
})})

View File

@ -1398,6 +1398,8 @@ var pWikiUIActions = actions.Actions({
},
},
// XXX add support for anchors -- #Wiki/Path#anchor...
// ...not working yet...
location: ['',
function(path){
var page = this.page
@ -1407,17 +1409,40 @@ var pWikiUIActions = actions.Actions({
return page.path()
}
path = path.trim()
path = path.trim().split('#')
var hash = path[1]
path = path[0]
// special paths...
if(path in this.config['special-paths']){
this[this.config['special-paths'][path]]()
}
var orig = this.location()
page.location(path)
this.reload()
// reset scroll location...
orig != this.location()
&& this.dom
.scrollParent()
.scrollLeft(0)
.scrollTop(0)
// focus hash..
// XXX not working yet...
hash != null && hash != ''
&& this.dom
.scrollParent()
.scrollLeft(0)
.scrollTop(
(this.dom
.find('#'+hash+', a[name="'+hash+'"]').first()
.offset() || {}).top || 0)
&& console.log('HASH:', hash)
}],
reload: ['',
function(){
@ -1459,6 +1484,10 @@ var pWikiUIActions = actions.Actions({
filters[pattern].call(that, dom.find(pattern)) })
}],
// shorthand...
get: ['',
function(){ return this.page.get.apply(this.page, arguments) }]
/*
// XXX url?
// - couch url
@ -1493,7 +1522,7 @@ var pWikiClient =
module.pWikiClient = object.makeConstructor('pWikiClient',
actions.mix(
// XXX not sure if we need this here...
//actions.MetaActions,
actions.MetaActions,
pWikiUIActions))