Implemented dispatching tree modifiers
[project/luci.git] / libs / web / luasrc / dispatcher.lua
index 8d79d7e..b48b584 100644 (file)
@@ -300,28 +300,35 @@ end
 -- @param path         Controller base directory
 -- @param suffix       Controller file suffix
 function createindex_plain(path, suffix)
+       local controllers = util.combine(
+               luci.fs.glob(path .. "*" .. suffix) or {},
+               luci.fs.glob(path .. "*/*" .. suffix) or {}
+       )
+
        if indexcache then
                local cachedate = fs.mtime(indexcache)
-               if cachedate and cachedate > fs.mtime(path) then
+               if cachedate then
+                       local realdate = 0
+                       for _, obj in ipairs(controllers) do
+                               local omtime = fs.mtime(path .. "/" .. obj)
+                               realdate = (omtime and omtime > realdate) and omtime or realdate
+                       end
 
-                       assert(
-                               sys.process.info("uid") == fs.stat(indexcache, "uid")
-                               and fs.stat(indexcache, "mode") == "rw-------",
-                               "Fatal: Indexcache is not sane!"
-                       )
+                       if cachedate > realdate then
+                               assert(
+                                       sys.process.info("uid") == fs.stat(indexcache, "uid")
+                                       and fs.stat(indexcache, "mode") == "rw-------",
+                                       "Fatal: Indexcache is not sane!"
+                               )
 
-                       index = loadfile(indexcache)()
-                       return index
+                               index = loadfile(indexcache)()
+                               return index
+                       end
                end
        end
 
        index = {}
 
-       local controllers = util.combine(
-               luci.fs.glob(path .. "*" .. suffix) or {},
-               luci.fs.glob(path .. "*/*" .. suffix) or {}
-       )
-
        for i,c in ipairs(controllers) do
                local module = "luci.controller." .. c:sub(#path+1, #c-#suffix):gsub("/", ".")
                local mod = require(module)
@@ -347,9 +354,11 @@ function createtree()
 
        local ctx  = context
        local tree = {nodes={}}
+       local modi = {}
 
        ctx.treecache = setmetatable({}, {__mode="v"})
        ctx.tree = tree
+       ctx.modifiers = modi
 
        -- Load default translation
        require "luci.i18n".loadc("default")
@@ -362,9 +371,30 @@ function createtree()
                v()
        end
 
+       local function modisort(a,b)
+               return modi[a].order < modi[b].order
+       end
+
+       for _, v in util.spairs(modi, modisort) do
+               scope._NAME = v.module
+               setfenv(v.func, scope)
+               v.func()
+       end
+
        return tree
 end
 
+--- Register a tree modifier.
+-- @param      func    Modifier function
+-- @param      order   Modifier order value (optional)
+function modifier(func, order)
+       context.modifiers[#context.modifiers+1] = {
+               func = func,
+               order = order or 0,
+               module = getfenv(2)._NAME
+       }
+end
+
 --- Clone a node of the dispatching tree to another position.
 -- @param      path    Virtual path destination
 -- @param      clone   Virtual path source
@@ -522,6 +552,16 @@ function cbi(model, config)
                        end
                end
 
+               if config.on_valid_to and state and state > 0 and state < 2 then
+                       luci.http.redirect(config.on_valid_to)
+                       return
+               end
+
+               if config.on_changed_to and state and state > 1 then
+                       luci.http.redirect(config.on_changed_to)
+                       return
+               end
+
                if config.on_success_to and state and state > 0 then
                        luci.http.redirect(config.on_success_to)
                        return