dynamic_nested
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -114,3 +114,3 @@ /** | ||
cloneSelectOption.selected | ||
option.selected = cloneSelectOption.selected | ||
} | ||
@@ -117,0 +117,0 @@ } else { |
{ | ||
"name": "dynamic_nested", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Add/Remove nested associations dynamically in forms that generates markup like Phoenix Framework.", | ||
@@ -5,0 +5,0 @@ "main": "./static/dynamic_nested.js", |
# Dynamic Nested | ||
 | ||
@@ -37,3 +39,3 @@ Add dynamic support to add and remove nested associations generated by `Phoenix.HTML.inputs_for`. | ||
```JS | ||
import DynamicNested from 'dynamic-nested' | ||
import DynamicNested from 'dynamic_nested' | ||
@@ -40,0 +42,0 @@ document.querySelectorAll('[dynamic-nested]').forEach(element => DynamicNested(element)) |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.dynamic_nested=t():e.dynamic_nested=t()}(window,(function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){(function(t){t.Phoenix||(t.Phoenix={}),e.exports=t.Phoenix.LiveView=n(2)}).call(this,n(1))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){"use strict";n.r(t);t.default=class{constructor(e,t={}){this.element=e,this.options=t,this.toggleRemoveButtonDisplay(),document.addEventListener("click",e=>{if(e.target.matches("[dynamic-nested-add]")){const e=this.element.querySelectorAll("[dynamic-nested-index]");this.add(e),this.toggleRemoveButtonDisplay()}if(e.target.matches("[dynamic-nested-remove]")){const t=e.target.closest("[dynamic-nested-index]");this.remove(t),this.toggleRemoveButtonDisplay()}},!1)}toggleRemoveButtonDisplay(){const e=this.element.querySelectorAll("[dynamic-nested-index]");if(e.length<=1){e[0].querySelector("[dynamic-nested-remove]").style.display="none"}else for(let e of this.element.querySelectorAll("[dynamic-nested-remove]"))e.style.display="block"}add(e){const t=e[e.length-1];this.options.beforeClone&&this.options.beforeClone(t);const n=t.cloneNode(!0);n.querySelectorAll("select").forEach((e,n)=>{const o=t.querySelectorAll("select")[n];if(e.multiple)for(let t of e.options){Array.from(o.options).find(e=>e.value==t.value).selected}else e.selectedIndex=o.selectedIndex});const o=n.querySelector("[dynamic-nested-field-id]");o&&n.removeChild(o);const r=+t.getAttribute("dynamic-nested-index")+1;this.replaceIndex(n,r),this.element.appendChild(n),this.options.afterAdd&&this.options.afterAdd(t,n)}remove(e){this.element.removeChild(e);const t=this.element.querySelectorAll("[dynamic-nested-index]");Array.from(t).forEach((e,t)=>{this.replaceIndex(e,t)}),this.options.afterRemove&&this.options.afterRemove(t)}replaceIndex(e,t){for(let n of["id","name","for"]){const o=e.querySelectorAll(`[${n}]`);Array.from(o).forEach(e=>{const o=this.newAttributeName(e,n,t);e.setAttribute(n,o)}),e.setAttribute("dynamic-nested-index",t)}}newAttributeName(e,t,n){return e.getAttribute(t).replace(/[0-9]/g,n)}}}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.dynamic_nested=t():e.dynamic_nested=t()}(window,(function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){(function(t){t.Phoenix||(t.Phoenix={}),e.exports=t.Phoenix.LiveView=n(2)}).call(this,n(1))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){"use strict";n.r(t);t.default=class{constructor(e,t={}){this.element=e,this.options=t,this.toggleRemoveButtonDisplay(),document.addEventListener("click",e=>{if(e.target.matches("[dynamic-nested-add]")){const e=this.element.querySelectorAll("[dynamic-nested-index]");this.add(e),this.toggleRemoveButtonDisplay()}if(e.target.matches("[dynamic-nested-remove]")){const t=e.target.closest("[dynamic-nested-index]");this.remove(t),this.toggleRemoveButtonDisplay()}},!1)}toggleRemoveButtonDisplay(){const e=this.element.querySelectorAll("[dynamic-nested-index]");if(e.length<=1){e[0].querySelector("[dynamic-nested-remove]").style.display="none"}else for(let e of this.element.querySelectorAll("[dynamic-nested-remove]"))e.style.display="block"}add(e){const t=e[e.length-1];this.options.beforeClone&&this.options.beforeClone(t);const n=t.cloneNode(!0);n.querySelectorAll("select").forEach((e,n)=>{const o=t.querySelectorAll("select")[n];if(e.multiple)for(let t of e.options){const e=Array.from(o.options).find(e=>e.value==t.value);t.selected=e.selected}else e.selectedIndex=o.selectedIndex});const o=n.querySelector("[dynamic-nested-field-id]");o&&n.removeChild(o);const r=+t.getAttribute("dynamic-nested-index")+1;this.replaceIndex(n,r),this.element.appendChild(n),this.options.afterAdd&&this.options.afterAdd(t,n)}remove(e){this.element.removeChild(e);const t=this.element.querySelectorAll("[dynamic-nested-index]");Array.from(t).forEach((e,t)=>{this.replaceIndex(e,t)}),this.options.afterRemove&&this.options.afterRemove(t)}replaceIndex(e,t){for(let n of["id","name","for"]){const o=e.querySelectorAll(`[${n}]`);Array.from(o).forEach(e=>{const o=this.newAttributeName(e,n,t);e.setAttribute(n,o)}),e.setAttribute("dynamic-nested-index",t)}}newAttributeName(e,t,n){return e.getAttribute(t).replace(/[0-9]/g,n)}}}])})); |
@@ -33,4 +33,5 @@ import DynamicNested from '../js/dynamic_nested' | ||
const element = document.querySelector('[dynamic-nested]') | ||
const dynamicNested = new DynamicNested(element) | ||
new DynamicNested(element) | ||
// Simulates a User clicks on add button. | ||
@@ -67,2 +68,23 @@ document.querySelector('[dynamic-nested-add]').click() | ||
test('copy selected values from multiple select after user interaction', () => { | ||
const element = document.querySelector('[dynamic-nested]') | ||
new DynamicNested(element) | ||
const multipleSelect = document.querySelector('#user_tags_0_id') | ||
// Simulates the first tag being selected. | ||
multipleSelect.options[0].selected = true | ||
// Simulates the second tag being unselected. | ||
multipleSelect.options[1].selected = false | ||
// Simulates a User clicks on add button. | ||
document.querySelector('[dynamic-nested-add]').click() | ||
expect( | ||
element.querySelector('#user_tags_1_id') | ||
).toHaveValue(['1', '3']) | ||
}) | ||
test('supports callbacks beforeClone and afterAdd', () => { | ||
@@ -69,0 +91,0 @@ const element = document.querySelector('[dynamic-nested]') |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
153931
10
342
74
0