This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking changes ⚠: - Move SSR implementation into separate package (`@isotope/server`). - Change `map()` behavior to only rerender when new array is provided. Other changes: - Improve performance of classes configurator on Node creation. - Emit lifecycle events only when they're listened to.
- Loading branch information
Showing
45 changed files
with
477 additions
and
1,054 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/*! | ||
* @isotope/core v0.1.4 | ||
* @isotope/core v0.2.0 | ||
* (c) Arek Nawo <[email protected]> (areknawo.com) | ||
* Released under the MIT License. | ||
*/ | ||
|
@@ -124,7 +124,9 @@ | |
* @returns - IsotopeNode. | ||
*/ | ||
emit(event, data = {}) { | ||
this.element.dispatchEvent(Object.assign(this.customDOM ? this.customDOM.createEvent(event) : new Event(event), { node: this }, data)); | ||
if (this.listenedEvents && this.listenedEvents.includes(event)) { | ||
this.element.dispatchEvent(Object.assign(this.customDOM ? this.customDOM.createEvent(event) : new Event(event), data)); | ||
} | ||
return this; | ||
} | ||
/** | ||
|
@@ -196,6 +198,12 @@ | |
/** @private */ | ||
on(event, handler, options) { | ||
this.element.addEventListener(event, handler, options); | ||
if (this.listenedEvents) { | ||
this.listenedEvents.push(event); | ||
} | ||
else { | ||
this.listenedEvents = [event]; | ||
} | ||
return this; | ||
} | ||
/** | ||
|
@@ -299,157 +307,6 @@ | |
return new IsotopeNode(element, config); | ||
}; | ||
|
||
/** | ||
* Class implementing Isotope CustomElement API, allowing for easy server-side stringification. | ||
*/ | ||
class StringElement { | ||
/** | ||
* Creates a new ServerElement instance. | ||
* | ||
* @param tag - Tag to be used for the element. | ||
*/ | ||
constructor(tag) { | ||
this.children = []; | ||
this.classList = { | ||
add: (...tokens) => { | ||
this.classes.push(...tokens); | ||
}, | ||
contains: (token) => { | ||
return this.classes.includes(token); | ||
}, | ||
remove: (...tokens) => { | ||
tokens.forEach((token) => { | ||
this.classes.splice(this.classes.indexOf(token), 1); | ||
}); | ||
} | ||
}; | ||
this.parentElement = null; | ||
this.style = {}; | ||
this.attributes = {}; | ||
this.classes = []; | ||
this.$textContent = ""; | ||
this.events = {}; | ||
this.tagName = tag; | ||
} | ||
/** @private */ | ||
addEventListener(type, listener) { | ||
(this.events[type] || (this.events[type] = [])).push(listener); | ||
} | ||
/** @private */ | ||
appendChild(newChild) { | ||
this.children.push(newChild); | ||
newChild.parentElement = this; | ||
return newChild; | ||
} | ||
/** @private */ | ||
dispatchEvent(event) { | ||
(this.events[event.type] || []).slice().forEach((handler) => { | ||
if (typeof handler === "function") { | ||
handler(event); | ||
} | ||
else { | ||
handler.handleEvent(event); | ||
} | ||
}); | ||
return true; | ||
} | ||
/** @private */ | ||
getAttribute(qualifiedName) { | ||
return this.attributes[qualifiedName]; | ||
} | ||
/** @private */ | ||
insertBefore(newChild, refChild) { | ||
newChild.parentElement = this; | ||
if (refChild) { | ||
const index = this.children.indexOf(refChild); | ||
if (index >= 0) { | ||
this.children.splice(index + 1, 0, newChild); | ||
return newChild; | ||
} | ||
} | ||
this.children.push(newChild); | ||
return newChild; | ||
} | ||
/** @private */ | ||
removeAttribute(qualifiedName) { | ||
this.attributes[qualifiedName] = null; | ||
} | ||
/** @private */ | ||
removeChild(oldChild) { | ||
oldChild.parentElement = null; | ||
this.children.splice(this.children.indexOf(oldChild), 1); | ||
return oldChild; | ||
} | ||
/** @private */ | ||
removeEventListener(type, listener) { | ||
if (this.events[type]) { | ||
this.events[type].splice(this.events[type].indexOf(listener) >>> 0, 1); | ||
} | ||
} | ||
/** @private */ | ||
setAttribute(qualifiedName, value) { | ||
this.attributes[qualifiedName] = value; | ||
} | ||
/** @private */ | ||
set textContent(textContent) { | ||
this.children = []; | ||
this.$textContent = textContent || ""; | ||
} | ||
/** @private */ | ||
get textContent() { | ||
return this.$textContent; | ||
} | ||
/** | ||
* Stringifies the ServerElement. | ||
* | ||
* @returns - Stringified ServerElement. | ||
*/ | ||
toString() { | ||
const tag = this.tagName; | ||
const content = this.textContent || | ||
this.children | ||
.map((child) => { | ||
return `${child}`; | ||
}) | ||
.join(""); | ||
const classes = this.classes.join(" "); | ||
const styles = Object.entries(this.style) | ||
.map(([property, value]) => { | ||
const propertyKebabCase = property.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`); | ||
return `${propertyKebabCase}:${value}`; | ||
}) | ||
.join(";"); | ||
const attributes = Object.entries(this.attributes) | ||
.map(([name, value]) => { | ||
return `${name}="${value}"`; | ||
}) | ||
.join(" "); | ||
return `<${tag}${classes ? ` class="${classes}"` : ""}${styles ? `style="${styles}"` : ""}${attributes}>${content}</${tag}>`; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a String View. | ||
* | ||
* @param tag - Tag for the top-level Node. | ||
* @returns - The created top-level Node. | ||
*/ | ||
const createStringView = (tag) => { | ||
if (!IsotopeNode.prototype.customDOM) { | ||
IsotopeNode.prototype.customDOM = { | ||
/** @private */ | ||
createElement(tag) { | ||
return new StringElement(tag); | ||
}, | ||
/** @private */ | ||
createEvent(type) { | ||
return { type }; | ||
} | ||
}; | ||
} | ||
return new IsotopeNode(tag); | ||
}; | ||
|
||
if (!IsotopeNode.prototype.setAttribs) { | ||
IsotopeNode.prototype.onCreate.push((node, config) => { | ||
if (config.attribs) { | ||
|
@@ -495,11 +352,21 @@ | |
if (!IsotopeNode.prototype.setClasses) { | ||
IsotopeNode.prototype.onCreate.push((node, config) => { | ||
if (config.classes) { | ||
let classes = ""; | ||
if (typeof config.classes === "function") { | ||
node.classes = config.classes; | ||
} | ||
else if (Array.isArray(config.classes)) { | ||
classes = config.classes.join(" "); | ||
} | ||
else { | ||
node.setClasses(config.classes); | ||
classes = Object.entries(config.classes) | ||
.filter(([, apply]) => apply) | ||
.map(([name]) => name) | ||
.join(" "); | ||
} | ||
if (classes) { | ||
node.element.setAttribute("class", classes); | ||
} | ||
} | ||
}); | ||
|
@@ -867,12 +734,19 @@ | |
if (node.mapData) { | ||
const data = node.mapData; | ||
const items = typeof data.items === "function" ? data.items(node) : node.getState(data.items); | ||
if (node.linked) { | ||
handleMapUpdate(node, items); | ||
} | ||
else { | ||
handleMapCreation(node, items); | ||
if (!items.isotopeMapped) { | ||
if (items.length === 0) { | ||
node.linked = []; | ||
node.element.textContent = ""; | ||
} | ||
else if (node.linked) { | ||
handleMapUpdate(node, items); | ||
} | ||
else { | ||
handleMapCreation(node, items); | ||
} | ||
} | ||
items.isotopeMapped = true; | ||
} | ||
}; | ||
IsotopeNode.prototype.onClean.push((node) => { | ||
|
@@ -1103,7 +977,6 @@ | |
|
||
exports.IsotopeNode = IsotopeNode; | ||
exports.createDOMView = createDOMView; | ||
exports.createStringView = createStringView; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { createDOMView, createStringView } from "./views"; | ||
import { IsotopeNode } from "./node"; | ||
import { createDOMView } from "./view"; | ||
import "./configurators"; | ||
import "./nodes"; | ||
export { IsotopeNode, createDOMView, createStringView }; | ||
export { IsotopeNode, createDOMView }; |
Oops, something went wrong.