add option 'scripts' to mapper which allows to load javascripts in header

Closes #6184
This commit is contained in:
Jérôme Schneider 2014-12-11 14:42:43 +01:00
parent 6a43276c45
commit d7e0fffb12
2 changed files with 31 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from urlparse import urlparse
from importlib import import_module
from mandaye import config
from mandaye.filters.default import MandayeFilter
from mandaye.http import HTTPRequest, HTTPResponse
from mandaye.log import logger
from mandaye.mappers import default
@ -66,6 +67,14 @@ class Dispatcher(object):
"""
if not mapper.has_key('method') or \
mapper['method'] == self.env['REQUEST_METHOD']:
if mapper.has_key('scripts'):
req_mapping["on_response"].append(
{
'filter': MandayeFilter.add_js_header,
'values': {'scripts': mapper['scripts']},
'content-types': ['text/html']
}
)
for hookname in req_mapping:
if mapper.has_key(hookname):
if isinstance(req_mapping[hookname], list):

View File

@ -58,6 +58,28 @@ class MandayeFilter(object):
'%s://%s' % (env["mandaye.scheme"], env["HTTP_HOST"]))
return response
@staticmethod
def add_js_header(env, values, request, response):
if response.msg:
if not "mjQuery = jQuery.noConflict" in response.msg:
response.msg = re.sub(
r'<head(.*?)>',
r"""<head\1>
<script src="%s/js/jquery-1.11.1.min.js" type="text/javascript"></script>
<script>
mjQuery = jQuery.noConflict( true );
</script>""" % (config.static_url),
response.msg, 1, re.IGNORECASE)
scripts_html = ""
for script in values["scripts"]:
scripts_html += '<script src="%s/%s" charset="UTF-8" type="text/javascript"></script>' % \
(config.static_url, script)
response.msg = re.sub(
'</head>',
'%s</head>' % scripts_html,
response.msg, 1, re.IGNORECASE)
return response
@staticmethod
def addtoolbar(env, values, request, response):
if config.mandaye_offline_toolbar or \