adt-js-components 1.6.0 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -107,16 +107,36 @@ function run(options) {
107
107
  this.$originalElement.select2('destroy');
108
108
  };
109
109
 
110
- AjaxSelect.init = function ($container) {
111
- $container.find('select[data-adt-ajax-select]').each(function () {
112
- new AjaxSelect($(this));
110
+ function applyEventHandlers(el) {
111
+ new AjaxSelect($(el));
112
+ }
113
+
114
+ const observer = new MutationObserver(mutations => {
115
+ mutations.forEach(mutation => {
116
+ if (mutation.type === "childList") {
117
+ mutation.addedNodes.forEach(node => {
118
+ if (node.nodeType === 1) {
119
+ if (node.hasAttribute("data-adt-ajax-select")) {
120
+ applyEventHandlers(node);
121
+ }
122
+
123
+ node.querySelectorAll('[data-adt-ajax-select]').forEach(child => {
124
+ applyEventHandlers(child);
125
+ });
126
+ }
127
+ });
128
+ }
113
129
  });
114
- };
130
+ });
115
131
 
116
- $.nette.ext('live').after(function ($element) {
117
- AjaxSelect.init($element);
132
+ observer.observe(document.body, {
133
+ childList: true,
134
+ subtree: true,
118
135
  });
119
136
 
137
+ document.querySelectorAll('[data-adt-ajax-select]').forEach(function(el) {
138
+ applyEventHandlers(el);
139
+ });
120
140
  }
121
141
 
122
142
  export default { run }
@@ -1,7 +1,5 @@
1
1
  async function run(options) {
2
- console.log('xxx');
3
2
  function applyEventHandlers(el) {
4
- console.log('yyy');
5
3
  $(el).formReplicator({
6
4
  template: $.parseHTML($(el).attr('data-adt-replicator')),
7
5
  addStaticButton: $(el).find('[data-adt-replicator-add]'),
@@ -13,8 +11,14 @@ async function run(options) {
13
11
  mutations.forEach(mutation => {
14
12
  if (mutation.type === "childList") {
15
13
  mutation.addedNodes.forEach(node => {
16
- if (node.nodeType === 1 && node.hasAttribute("data-adt-form-replicator")) {
17
- applyEventHandlers(node);
14
+ if (node.nodeType === 1) {
15
+ if (node.hasAttribute("data-adt-replicator")) {
16
+ applyEventHandlers(node);
17
+ }
18
+
19
+ node.querySelectorAll('[data-adt-replicator]').forEach(child => {
20
+ applyEventHandlers(child);
21
+ });
18
22
  }
19
23
  });
20
24
  }
@@ -6,7 +6,7 @@ import 'select2/dist/js/i18n/cs'
6
6
  function run(options) {
7
7
  function applyEventHandlers(el) {
8
8
  $(el)
9
- .select2($.extend({theme: 'bootstrap-5', language: "en"}, $(this).data('adt-select2') || {}))
9
+ .select2($.extend({theme: 'bootstrap-5', language: "en"}, $(el).data('adt-select2') || {}))
10
10
  .on('change', function(e) {
11
11
  Nette.toggleForm($(e.currentTarget).closest('form')[0], e.currentTarget);
12
12
  });