Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
453 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
84 changes: 84 additions & 0 deletions
84
cms/test_utils/project/pluginapp/plugins/dynamic_js_loading/cms_plugins.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
import itertools | ||
|
||
from cms.plugin_base import CMSPluginBase | ||
from cms.plugin_pool import plugin_pool | ||
|
||
from .models import DummyModel | ||
|
||
|
||
@plugin_pool.register_plugin | ||
class DynamicJsLoadingPlugin(CMSPluginBase): | ||
model = DummyModel | ||
name = "DynamicJsLoading" | ||
render_template = "dynamic_js_loading/dynamic_js_loading.html" | ||
|
||
def render(self, context, instance, placeholder): | ||
""" | ||
This generates a list of all 16 class usage permutations. | ||
originaly from https://stackoverflow.com/a/54059999 | ||
>>> import itertools | ||
>>> l=[False,True] | ||
>>> list(itertools.product(l,repeat=4)) | ||
[(False, False, False, False), | ||
(False, False, False, True), | ||
(False, False, True, False), | ||
(False, False, True, True), | ||
(False, True, False, False), | ||
(False, True, False, True), | ||
(False, True, True, False), | ||
(False, True, True, True), | ||
(True, False, False, False), | ||
(True, False, False, True), | ||
(True, False, True, False), | ||
(True, False, True, True), | ||
(True, True, False, False), | ||
(True, True, False, True), | ||
(True, True, True, False), | ||
(True, True, True, True)] | ||
""" | ||
binary_list = [False, True] | ||
case_list = list(itertools.product(binary_list, repeat=4)) | ||
context["js_test_classes"] = self.generate_class_string( | ||
*case_list[instance.testcase - 1] | ||
) | ||
|
||
return super(DynamicJsLoadingPlugin, self).render( | ||
context, instance, placeholder | ||
) | ||
|
||
def generate_class_string( | ||
self, execute_js, document_content, window_content, window_load | ||
): | ||
""" | ||
Generates the class string for the different test cases based on the truth values of the parameters | ||
Parameters | ||
---------- | ||
execute_js : bool | ||
Whether or not to use the class "cms-execute-js-to-render" | ||
document_content: bool | ||
Whether or not to use the class "cms-trigger-event-document-DOMContentLoaded" | ||
window_content: bool | ||
Whether or not to use the class "cms-trigger-event-window-DOMContentLoaded" | ||
window_load: bool | ||
Whether or not to use the class "cms-trigger-event-window-DOMContentLoaded" | ||
""" | ||
full_class_list = [ | ||
"cms-execute-js-to-render", | ||
"cms-trigger-event-document-DOMContentLoaded", | ||
"cms-trigger-event-window-DOMContentLoaded", | ||
"cms-trigger-event-window-load", | ||
] | ||
include_list = [execute_js, document_content, window_content, window_load] | ||
class_list = [] | ||
for include_bool, class_string in zip(include_list, full_class_list): | ||
if include_bool: | ||
class_list.append(class_string) | ||
return " ".join(class_list) | ||
|
17 changes: 17 additions & 0 deletions
17
cms/test_utils/project/pluginapp/plugins/dynamic_js_loading/models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models | ||
from django.utils.encoding import python_2_unicode_compatible | ||
|
||
from cms.models import CMSPlugin | ||
|
||
|
||
@python_2_unicode_compatible | ||
class DummyModel(CMSPlugin): | ||
testcase = models.IntegerField( | ||
verbose_name='Testcase', | ||
) | ||
|
||
def __str__(self): | ||
return str(self.testcase) or str(self.pk) |
22 changes: 22 additions & 0 deletions
22
.../project/pluginapp/plugins/dynamic_js_loading/static/dynamic_js_loading/js/test_script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function append_h2_element_from_src (id) { | ||
var h2_elem = document.createElement('h2'); | ||
|
||
h2_elem.id = id; | ||
h2_elem.textContent = 'id=' + id; | ||
document.body.appendChild(h2_elem); | ||
} | ||
|
||
append_h2_element_from_src('from_src_no_trigger'); | ||
|
||
document.addEventListener('DOMContentLoaded', function() { | ||
append_h2_element_from_src('from_src_needs_trigger_document_DOMContentLoaded'); | ||
}); | ||
|
||
window.addEventListener('DOMContentLoaded', function() { | ||
append_h2_element_from_src('from_src_needs_trigger_window_DOMContentLoaded'); | ||
}); | ||
|
||
window.addEventListener('load', function() { | ||
append_h2_element_from_src('from_src_needs_trigger_window_load'); | ||
}); | ||
console.log("SCRIPT LOADED") |
33 changes: 33 additions & 0 deletions
33
...pluginapp/plugins/dynamic_js_loading/templates/dynamic_js_loading/dynamic_js_loading.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{% load sekizai_tags static %} | ||
<h1 id="dynamic_js_loading_heading">Test dynamic loading of testcase {{instance.testcase}}</h1> | ||
|
||
|
||
{% addtoblock "js" %} | ||
<script class="{{js_test_classes}}"> | ||
function append_h2_element_inline(id) { | ||
var h2_elem = document.createElement('h2'); | ||
|
||
h2_elem.id = id; | ||
h2_elem.textContent = 'id=' + id; | ||
document.body.appendChild(h2_elem); | ||
} | ||
|
||
append_h2_element_inline('inline_no_trigger'); | ||
|
||
document.addEventListener('DOMContentLoaded', function() { | ||
append_h2_element_inline('inline_needs_trigger_document_DOMContentLoaded'); | ||
}); | ||
|
||
window.addEventListener('DOMContentLoaded', function() { | ||
append_h2_element_inline('inline_needs_trigger_window_DOMContentLoaded'); | ||
}); | ||
|
||
window.addEventListener('load', function() { | ||
append_h2_element_inline('inline_needs_trigger_window_load'); | ||
}); | ||
</script> | ||
{% endaddtoblock %} | ||
|
||
{% addtoblock "js" %} | ||
<script src="{% static '/dynamic_js_loading/js/test_script.js' %}" class="{{js_test_classes}}"></script> | ||
{% endaddtoblock %} |
Oops, something went wrong.