misc: add support for select2.js in included_js_libraries (#53647) #1038

Merged
fpeters merged 1 commits from wip/53647-included-js-support-select2 into main 2024-01-26 08:46:34 +01:00
2 changed files with 54 additions and 9 deletions

View File

@ -4936,7 +4936,10 @@ def test_create_formdata_locked_prefill_parent(create_formdata):
def test_js_libraries(pub):
create_formdef()
formdef = create_formdef()
formdef.enable_tracking_codes = True # will force gadjo.js -> jquery-ui
formdef.store()
resp = get_app(pub).get('/test/', status=200)
assert 'jquery.js' not in resp.text
assert 'jquery.min.js' in resp.text
@ -4947,6 +4950,8 @@ def test_js_libraries(pub):
resp = get_app(pub).get('/test/', status=200)
assert 'jquery.js' in resp.text
assert 'jquery.min.js' not in resp.text
assert 'jquery-ui.js' in resp.text
assert 'jquery-ui.min.js' not in resp.text
assert 'qommon.forms.js' in resp.text
pub.cfg['branding'] = {'included_js_libraries': ['jquery.js']}
@ -4956,6 +4961,44 @@ def test_js_libraries(pub):
assert 'jquery.min.js' not in resp.text
assert 'qommon.forms.js' in resp.text
pub.cfg['branding'] = {'included_js_libraries': ['jquery.js', 'jquery-ui.js']}
pub.write_cfg()
resp = get_app(pub).get('/test/', status=200)
assert 'jquery.js' not in resp.text
assert 'jquery.min.js' not in resp.text
assert 'qommon.forms.js' in resp.text
pub.cfg['branding'] = {'included_js_libraries': ['jquery.js']}
pub.write_cfg()
formdef.enable_tracking_codes = False # no popup, no jquery-ui (and no i18n.js)
formdef.store()
resp = get_app(pub).get('/test/', status=200)
assert 'jquery-ui.js' not in resp.text
assert 'jquery-ui.min.js' not in resp.text
assert 'select2.js' not in resp.text
assert 'i18n.js' not in resp.text
# add autocomplete field
formdef.fields = [
fields.ItemField(
id='1',
label='string',
data_source={'type': 'jsonp', 'value': 'http://remote.example.net/jsonp'},
),
]
formdef.store()
resp = get_app(pub).get('/test/', status=200)
assert 'select2.js' in resp.text
assert 'select2.css' in resp.text
assert 'i18n.js' in resp.text
pub.cfg['branding'] = {'included_js_libraries': ['jquery.js', 'select2.js']}
pub.write_cfg()
resp = get_app(pub).get('/test/', status=200)
assert 'select2.js' not in resp.text
assert 'select2.css' not in resp.text
assert 'i18n.js' in resp.text
def test_after_submit_location(pub):
create_user(pub)

View File

@ -56,14 +56,13 @@ class HTTPResponse(quixote.http_response.HTTPResponse):
mappings['jquery.js'] = '../xstatic/jquery.js'
mappings['jquery-ui.js'] = '../xstatic/jquery-ui.js'
mappings['select2.js'] = '../xstatic/select2.js'
if not get_request().is_in_backoffice():
if get_request().is_in_backoffice():
included_js_libraries = []
else:
branding_cfg = get_publisher().cfg.get('branding') or {}
for included_js_library in branding_cfg.get('included_js_libraries') or []:
mappings[included_js_library] = ''
included_js_libraries = branding_cfg.get('included_js_libraries') or []
for script_name in script_names:
mapped_script_name = mappings.get(script_name, script_name)
if not mapped_script_name:
continue
if mapped_script_name not in self.javascript_scripts:
if script_name == 'qommon.map.js':
self.add_javascript(['jquery.js'])
@ -92,7 +91,8 @@ class HTTPResponse(quixote.http_response.HTTPResponse):
'jquery.fileupload.js',
]
)
self.javascript_scripts.append(str(mapped_script_name))
if script_name not in included_js_libraries:
self.javascript_scripts.append(str(mapped_script_name))
if script_name == 'afterjob.js':
self.add_javascript_code(
'var QOMMON_ROOT_URL = "%s";\n'
@ -117,8 +117,10 @@ class HTTPResponse(quixote.http_response.HTTPResponse):
if script_name == 'qommon.admin.js':
self.add_javascript(['../../i18n.js', 'jquery.js', 'qommon.slugify.js'])
if script_name == 'select2.js':
self.add_javascript(['jquery.js', '../../i18n.js', 'qommon.forms.js', 'select2.js'])
self.add_css_include('select2.css')
self.add_javascript(['jquery.js', '../../i18n.js', 'qommon.forms.js'])
if script_name not in included_js_libraries:
# assume a theme embedding select2.js will also include the css parts
self.add_css_include('select2.css')
def add_javascript_code(self, code):
if not self.javascript_code_parts: