aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-10-11 11:56:43 +0000
committers-ol <s-ol@users.noreply.github.com>2019-10-11 11:56:43 +0000
commit8cdf5d4a363ba99a6356e7e1dfe0dfb39e6fb13e (patch)
treeb04d0832d1eddb92910d9eed46610649c059975d
parentfix about section links (diff)
downloadmmm-8cdf5d4a363ba99a6356e7e1dfe0dfb39e6fb13e.tar.gz
mmm-8cdf5d4a363ba99a6356e7e1dfe0dfb39e6fb13e.zip
change ?interactive pseudofacet to text/html+interactive pseudo-type
fix browser push/popstate
-rw-r--r--build/server.moon53
-rw-r--r--mmm/mmmfs/browser.moon47
-rw-r--r--mmm/mmmfs/fileder.moon5
-rw-r--r--mmm/mmmfs/stores/web.moon4
-rw-r--r--spec/key_spec.moon10
5 files changed, 70 insertions, 49 deletions
diff --git a/build/server.moon b/build/server.moon
index 89cf1a6..ff138ee 100644
--- a/build/server.moon
+++ b/build/server.moon
@@ -47,28 +47,6 @@ class Server
switch method
when 'GET', 'HEAD'
val = switch facet.name
- when '?interactive'
- export BROWSER
-
- root = Fileder @store
- BROWSER = Browser root, path
- render BROWSER\todom!, fileder, noview: true, scripts: "
- <script type=\"application/lua\">
- on_load = on_load or {}
- table.insert(on_load, function()
- local path = #{string.format '%q', path}
- local browser = require 'mmm.mmmfs.browser'
- local fileder = require 'mmm.mmmfs.fileder'
- local web = require 'mmm.mmmfs.stores.web'
-
- local store = web.WebStore({ verbose = true })
- local index = store:get_index(path, -1)
- local root = fileder.Fileder(store, index)
-
- BROWSER = browser.Browser(root, path, true)
- end)
- </script>"
-
when '?index', '?tree'
-- serve fileder index
-- '?index': one level deep
@@ -81,7 +59,30 @@ class Server
if not fileder\has_facet facet.name
return 404, "facet '#{facet.name}' not found in fileder '#{path}'"
- fileder\get facet
+ if facet.type == 'text/html+interactive'
+ export BROWSER
+
+ root = Fileder @store
+ BROWSER = Browser root, path, facet.name
+ render BROWSER\todom!, fileder, noview: true, scripts: "
+ <script type=\"application/lua\">
+ on_load = on_load or {}
+ table.insert(on_load, function()
+ local path = #{string.format '%q', path}
+ local facet = #{string.format '%q', facet.name}
+ local browser = require 'mmm.mmmfs.browser'
+ local fileder = require 'mmm.mmmfs.fileder'
+ local web = require 'mmm.mmmfs.stores.web'
+
+ local store = web.WebStore({ verbose = true })
+ local index = store:get_index(path, -1)
+ local root = fileder.Fileder(store, index)
+
+ BROWSER = browser.Browser(root, path, facet, true)
+ end)
+ </script>"
+ else
+ fileder\get facet
if val
200, val
@@ -100,7 +101,7 @@ class Server
path_facet or= path
path, facet = path_facet\match '(.*)/([^/]*)'
- type or= 'text/html'
+ type or= 'text/html+interactive'
type = type\match '%s*(.*)'
facet = Key facet, type
@@ -112,8 +113,8 @@ class Server
res = headers.new!
response_type = if status > 299 then 'text/plain'
- else if facet then facet.type
- else 'text/json'
+ else if facet.type == 'text/html+interactive' then 'text/html'
+ else facet.type
res\append ':status', tostring status
res\append 'content-type', response_type
diff --git a/mmm/mmmfs/browser.moon b/mmm/mmmfs/browser.moon
index 247e36a..a0c5541 100644
--- a/mmm/mmmfs/browser.moon
+++ b/mmm/mmmfs/browser.moon
@@ -40,13 +40,26 @@ for convert in *converts
table.insert casts, convert
class Browser
- new: (@root, path, rehydrate=false) =>
+ new: (@root, path, facet, rehydrate=false) =>
-- root fileder
assert @root, 'root fileder is nil'
-- active path
@path = ReactiveVar path or ''
+ -- active fileder
+ -- (re)set every time @path changes
+ @active = @path\map @root\walk
+
+ -- currently active facet
+ -- (re)set to default when @active changes
+ @facet = ReactiveVar Key facet, 'mmm/dom'
+ if MODE == 'CLIENT'
+ @active\subscribe (fileder) ->
+ return unless fileder
+ last = @facet and @facet\get!
+ @facet\set Key if last then last.type else 'mmm/dom'
+
-- update URL bar
if MODE == 'CLIENT'
logo = document\querySelector 'header h1 > a > svg'
@@ -54,31 +67,29 @@ class Browser
logo.classList\add 'spin'
logo.parentElement.offsetWidth
logo.classList\remove 'spin'
- @path\subscribe (path) ->
+
+ @facet\subscribe (facet) ->
document.body.classList\add 'loading'
spin!
return if @skip
- vis_path = path .. (if '/' == path\sub -1 then '' else '/')
- window.history\pushState path, '', vis_path
+
+ path = @path\get!
+ state = js.global\eval 'new Object()'
+ state.path = path
+ state.name = facet.name
+ state.type = facet.type
+
+ window.history\pushState state, '', "#{path}/#{(Key facet.name, 'text/html+interactive')\tostring true}"
window.onpopstate = (_, event) ->
- if event.state and not event.state == js.null
+ state = event.state
+ if state != js.null
@skip = true
- @path\set event.state
+ @path\set state.path
+ @facet\set Key state.name, state.type
@skip = nil
- -- active fileder
- -- (re)set every time @path changes
- @active = @path\map @root\walk
-
- -- currently active facet
- -- (re)set to default when @active changes
- @facet = @active\map (fileder) ->
- return unless fileder
- last = @facet and @facet\get!
- Key if last then last.type else 'mmm/dom'
-
-- whether inspect tab is active
@inspect = ReactiveVar (MODE == 'CLIENT' and window.location.search\match '[?&]inspect')
@@ -121,7 +132,7 @@ class Browser
current = @facet\get!
current = current and current.name
- with select :onchange, disabled: not fileder
+ with select :onchange, disabled: not fileder, value: @facet\map (f) -> f and f.name
has_main = fileder and fileder\has_facet ''
\append option '(main)', value: '', disabled: not has_main, selected: current == ''
if fileder
diff --git a/mmm/mmmfs/fileder.moon b/mmm/mmmfs/fileder.moon
index 8c89083..174f8b0 100644
--- a/mmm/mmmfs/fileder.moon
+++ b/mmm/mmmfs/fileder.moon
@@ -34,8 +34,9 @@ class Key
assert ('string' == type @type), "type is not a string: '#{@type}'"
-- format as a string (see constructor)
- tostring: =>
- if @name == ''
+ -- in strict mode never omit name
+ tostring: (strict=false) =>
+ if not strict and @name == ''
@type
else
"#{@name}: #{@type}"
diff --git a/mmm/mmmfs/stores/web.moon b/mmm/mmmfs/stores/web.moon
index 082fea8..2eea45c 100644
--- a/mmm/mmmfs/stores/web.moon
+++ b/mmm/mmmfs/stores/web.moon
@@ -29,9 +29,7 @@ parse_json = do
else
val
- (string) ->
- print fix JSON\parse string
- fix JSON\parse string
+ (string) -> fix JSON\parse string
class WebStore extends Store
new: (opts = {}) =>
diff --git a/spec/key_spec.moon b/spec/key_spec.moon
index 9b7dde4..edd5ccf 100644
--- a/spec/key_spec.moon
+++ b/spec/key_spec.moon
@@ -56,3 +56,13 @@ describe "Key", ->
assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
assert.is.equal 'facet: and -> long -> type', tostring Key 'facet', 'and -> long -> type'
assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
+
+ it ":tostring formats the key", ->
+ assert.is.equal 'type/only', (Key 'type/only')\tostring!
+ assert.is.equal 'type/only', (Key '', 'type/only')\tostring!
+ assert.is.equal 'type/only', (Key ": type/only")\tostring!
+
+ it ":tostring supports strict mode", ->
+ assert.is.equal ': type/only', (Key 'type/only')\tostring true
+ assert.is.equal ': type/only', (Key '', 'type/only')\tostring true
+ assert.is.equal ': type/only', (Key ": type/only")\tostring true