overleaf/services/web/app/coffee/infrastructure/Modules.coffee

39 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-09-08 09:23:47 -04:00
fs = require "fs"
Path = require "path"
2014-09-08 10:40:46 -04:00
jade = require "jade"
2014-09-08 09:23:47 -04:00
MODULE_BASE_PATH = Path.resolve(__dirname + "/../../../modules")
module.exports = Modules =
2014-09-08 10:40:46 -04:00
modules: []
loadModules: () ->
for moduleName in fs.readdirSync(MODULE_BASE_PATH)
if fs.existsSync(Path.join(MODULE_BASE_PATH, moduleName, "index.js"))
loadedModule = require(Path.join(MODULE_BASE_PATH, moduleName, "index"))
loadedModule.name = moduleName
@modules.push loadedModule
applyRouter: (webRouter, apiRouter) ->
2014-09-08 10:40:46 -04:00
for module in @modules
module.router?.apply(webRouter, apiRouter)
2014-09-08 10:40:46 -04:00
viewIncludes: {}
loadViewIncludes: (app) ->
@viewIncludes = {}
for module in @modules
for view, partial of module.viewIncludes or {}
@viewIncludes[view] ||= []
@viewIncludes[view].push fs.readFileSync(Path.join(MODULE_BASE_PATH, module.name, "app/views", partial + ".jade"))
moduleIncludes: (view, locals) ->
partials = Modules.viewIncludes[view] or []
html = ""
for partial in partials
compiler = jade.compile(partial, doctype: "html")
2014-09-08 10:40:46 -04:00
html += compiler(locals)
return html
moduleIncludesAvailable: (view) ->
return (Modules.viewIncludes[view] or []).length > 0
2014-09-08 10:40:46 -04:00
Modules.loadModules()