Skip to content

Commit

Permalink
Update to v2.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisPower1 committed Jan 20, 2024
1 parent 0ecb19a commit 55137bb
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 206 deletions.
228 changes: 127 additions & 101 deletions inter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Interjs
* Version - 2.2.0
* Version - 2.2.1
* MIT LICENSED BY - Denis Power
* Repo - https://github.com/interjs/inter
* 2021 - 2024
Expand Down Expand Up @@ -71,6 +71,10 @@
consW(prop.startsWith("on") ? event : styleProp);
}

function runInvalidStyleValue(name, value) {
ParserWarning(`"${value}" is an invalid value for the "${name}" style.`);
}

function runCanNotDefineReactivePropWarning() {
consW(`Inter failed to define reactivity
in a plain Javascript object, because it is not configurable.`);
Expand Down Expand Up @@ -1719,6 +1723,7 @@
if (isDefined(styleValue)) {
container.style[name] = styleValue;
container.template.styles[name] = styleValue;
if (!container.style[name]) runInvalidStyleValue(name, styleValue);
}
} else runInvalidStyleWarning(name);
});
Expand Down Expand Up @@ -2672,6 +2677,98 @@
return text;
}

function runNestedListDiffing(reactor, target, newChildren, oldChildren) {
const {
mutationInfo: { method, start, deleteCount, itemsLength },
} = reactor;

function addByPush() {
let i = itemsLength;

for (; i > 0; i--) {
const child = newChildren[newChildren.length - i];

target.appendChild(toDOM(child, true, child.index));
oldChildren.push(child);
}
}

function AddByUnShiftOrSplice(mutationMethod) {
function insertBehind(start, itemsLength) {
for (let i = itemsLength - 1; i > -1; i--) {
const child = target.children[start];
const virtualChild = newChildren[i];
const newChild = toDOM(virtualChild, true, virtualChild.index);

if (child) target.insertBefore(newChild, child);
else target.appendChild(newChild);

addedtems.unshift(virtualChild);
}
}

const addedtems = new Array();

if (mutationMethod == "splice" && deleteCount == 0 && itemsLength > 0) {
insertBehind(start, itemsLength);
oldChildren.splice(start, deleteCount, ...addedtems);
} else if (mutationMethod == "splice" && deleteCount > 0) {
for (let i = 0; i < deleteCount; i++) {
const child = target.children[start];

if (child) target.removeChild(child);
}

insertBehind(start, itemsLength);

oldChildren.splice(start, deleteCount, addedtems);
} else if (mutationMethod == "unshift") {
insertBehind(0, itemsLength);

oldChildren.unshift(...addedtems);
}
}

function deleteBySplice() {
let i = start;
for (; newChildren.length < target.children.length; i++) {
const elToRemove = target.children[start];
if (elToRemove) target.removeChild(elToRemove);
}

oldChildren.splice(start, deleteCount);
}

const lastNodeElement = target.children[target.children.length - 1];
const firstNodeElement = target.children[0];
if (method == "pop" && lastNodeElement) {
target.removeChild(lastNodeElement);
oldChildren.pop();
} else if (method == "shift" && firstNodeElement) {
target.removeChild(firstNodeElement);
oldChildren.shift();
} else if (method == "push") addByPush();
else if (method == "unshift") AddByUnShiftOrSplice(method);
else if (method == "splice") {
if (
typeof start == "number" &&
typeof deleteCount == "number" &&
itemsLength == 0
)
deleteBySplice();
else if (itemsLength > 0) AddByUnShiftOrSplice(method);
else if (deleteCount == void 0) {
const data = {
source: {
values: newChildren,
},
};

synchronizeRootChildrenLengthAndSourceLength(target, data);
}
}
}

function runContainerDiffing(newContainer, oldContainer, diff) {
const {
attrs: newAttrs = {},
Expand All @@ -2688,6 +2785,11 @@
target,
} = oldContainer;

const { reactor } = newChildren;

if (reactor != void 0)
runNestedListDiffing(reactor, target, newChildren, oldChildren);

const rootEL = target.parentNode;
const newText = getValue(newContainer.text);
const oldText = getValue(oldContainer.text);
Expand Down Expand Up @@ -2808,10 +2910,8 @@
if (validStyleName(newStyleName)) {
target.style[newStyleName] = newStyleValue;

if (target.style[newStyleName] !== newStyleValue)
ParserWarning(
`"${newStyleValue}" is an invalid value for the "${newStyleName}" style.`
);
if (!target.style[newStyleName])
runInvalidStyleValue(newStyleName, newStyleValue);
} else runInvalidStyleWarning(newStyleName);
}
}
Expand Down Expand Up @@ -2909,101 +3009,9 @@
if (newChildren.length !== oldChildren.length) {
const { reactor } = newChildren;

if (reactor != void 0) {
const {
mutationInfo: { method, start, deleteCount, itemsLength },
} = reactor;

function addByPush() {
let i = itemsLength;

for (; i > 0; i--) {
const child = newChildren[newChildren.length - i];

target.appendChild(toDOM(child, true, child.index));
oldChildren.push(child);
}
}

function AddByShiftOrSplice(mutationMethod) {
function insertBehind(start, itemsLength) {
for (let i = itemsLength - 1; i > -1; i--) {
const child = target.children[start];
const virtualChild = newChildren[i];
const newChild = toDOM(virtualChild, true, virtualChild.index);

if (child) target.insertBefore(newChild, child);
else target.appendChild(newChild);

addedtems.unshift(virtualChild);
}
}

const addedtems = new Array();

if (
mutationMethod == "splice" &&
deleteCount == 0 &&
itemsLength > 0
) {
insertBehind(start, itemsLength);
oldChildren.splice(start, deleteCount, ...addedtems);
} else if (mutationMethod == "splice" && deleteCount > 0) {
for (let i = 0; i < deleteCount; i++) {
const child = target.children[start];

if (child) target.removeChild(child);
}

insertBehind(start, itemsLength);

oldChildren.splice(start, deleteCount, addedtems);
} else if (mutationMethod == "unshift") {
insertBehind(0, itemsLength);

oldChildren.unshift(...addedtems);
}
}

function deleteBySplice() {
let i = start;
for (; newChildren.length < target.children.length; i++) {
const elToRemove = target.children[start];
if (elToRemove) target.removeChild(elToRemove);
}

oldChildren.splice(start, deleteCount);
}

const lastNodeElement = target.children[target.children.length - 1];
const firstNodeElement = target.children[0];
if (method == "pop" && lastNodeElement) {
target.removeChild(lastNodeElement);
oldChildren.pop();
} else if (method == "shift" && firstNodeElement) {
target.removeChild(firstNodeElement);
oldChildren.shift();
} else if (method == "push") addByPush();
else if (method == "unshift") AddByShiftOrSplice(method);
else if (method == "splice") {
if (
typeof start == "number" &&
typeof deleteCount == "number" &&
itemsLength == 0
)
deleteBySplice();
else if (itemsLength > 0) AddByShiftOrSplice(method);
else if (deleteCount == void 0) {
const data = {
source: {
values: newChildren,
},
};

synchronizeRootChildrenLengthAndSourceLength(target, data);
}
}
} else if (target && target.parentNode != null) {
if (reactor != void 0)
runNestedListDiffing(reactor, target, newChildren, oldChildren);
else if (target && target.parentNode != null) {
const newElement = toDOM(newChild, true, index);

realParent.replaceChild(newElement, target);
Expand Down Expand Up @@ -3073,6 +3081,24 @@
}
}

/**
* <div>
* <p>Olá</p>
* <p>Olá</p>
* <p>Olá</p>
* <!--Added dynamically -->
* <p>Olá</p>
* </div>
*
*<div>
* <p>Olá</p>
* <p>Olá</p>
* <p>Olá</p>
*
* </div>
*
*/

function toObj(obj) {
if (obj !== void 0) {
try {
Expand Down Expand Up @@ -3316,5 +3342,5 @@
window.template = template;
window.toAttrs = toAttrs;
window.Backend = Backend;
console.log("The global version 2.2.0 of Inter was loaded successfully.");
console.log("The global version 2.2.1 of Inter was loaded successfully.");
})();
Loading

0 comments on commit 55137bb

Please sign in to comment.