Skip to content

Commit

Permalink
v4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pakastin committed Feb 3, 2024
1 parent e2f4a47 commit 67a4de9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 69 deletions.
73 changes: 40 additions & 33 deletions redom.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function hooksAreEmpty (hooks) {

/* global Node, ShadowRoot */


var hookNames = ['onmount', 'onremount', 'onunmount'];
var shadowRootAvailable = typeof window !== 'undefined' && 'ShadowRoot' in window;

Expand Down Expand Up @@ -134,7 +135,7 @@ function mount (parent, child, before, replace) {
var beforeEl = getEl(before);

if (beforeEl.__redom_mounted) {
trigger(before.el, 'onunmount');
trigger(beforeEl, 'onunmount');
}

parentEl.replaceChild(childEl, beforeEl);
Expand Down Expand Up @@ -261,6 +262,7 @@ function setStyleValue (el, key, value) {

/* global SVGElement */


var xlinkns = 'http://www.w3.org/1999/xlink';

function setAttr (view, arg1, arg2) {
Expand Down Expand Up @@ -373,8 +375,6 @@ function isNode (arg) {
return arg && arg.nodeType;
}

var htmlCache = {};

function html (query) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
Expand All @@ -384,9 +384,7 @@ function html (query) {
var type = typeof query;

if (type === 'string') {
element = memoizeHTML(query).cloneNode(false);
} else if (isNode(query)) {
element = query.cloneNode(false);
element = createElement(query);
} else if (type === 'function') {
var Query = query;
element = new (Function.prototype.bind.apply( Query, [ null ].concat( args) ));
Expand All @@ -402,19 +400,13 @@ function html (query) {
var el = html;
var h = html;

html.extend = function extendHtml (query) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
html.extend = function extendHtml () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];

var clone = memoizeHTML(query);

return html.bind.apply(html, [ this, clone ].concat( args ));
return html.bind.apply(html, [ this ].concat( args ));
};

function memoizeHTML (query) {
return htmlCache[query] || (htmlCache[query] = createElement(query));
}

function setChildren (parent) {
var children = [], len = arguments.length - 1;
while ( len-- > 0 ) children[ len ] = arguments[ len + 1 ];
Expand Down Expand Up @@ -600,6 +592,7 @@ list.extend = List.extend;

/* global Node */


function place (View, initData) {
return new Place(View, initData);
}
Expand Down Expand Up @@ -669,20 +662,22 @@ Place.prototype.update = function update (visible, data) {

/* global Node */

function router (parent, Views, initData) {
return new Router(parent, Views, initData);

function router (parent, views, initData) {
return new Router(parent, views, initData);
}

var Router = function Router (parent, Views, initData) {
var Router = function Router (parent, views, initData) {
this.el = ensureEl(parent);
this.Views = Views;
this.views = views;
this.Views = views; // backwards compatibility
this.initData = initData;
};

Router.prototype.update = function update (route, data) {
if (route !== this.route) {
var Views = this.Views;
var View = Views[route];
var views = this.views;
var View = views[route];

this.route = route;

Expand All @@ -699,8 +694,6 @@ Router.prototype.update = function update (route, data) {

var ns = 'http://www.w3.org/2000/svg';

var svgCache = {};

function svg (query) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
Expand All @@ -710,9 +703,7 @@ function svg (query) {
var type = typeof query;

if (type === 'string') {
element = memoizeSVG(query).cloneNode(false);
} else if (isNode(query)) {
element = query.cloneNode(false);
element = createElement(query, ns);
} else if (type === 'function') {
var Query = query;
element = new (Function.prototype.bind.apply( Query, [ null ].concat( args) ));
Expand All @@ -727,16 +718,32 @@ function svg (query) {

var s = svg;

svg.extend = function extendSvg (query) {
var clone = memoizeSVG(query);
svg.extend = function extendSvg () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];

return svg.bind(this, clone);
return svg.bind.apply(svg, [ this ].concat( args ));
};

svg.ns = ns;

function memoizeSVG (query) {
return svgCache[query] || (svgCache[query] = createElement(query, ns));
function viewFactory (views, key) {
if (!views || typeof views !== 'object') {
throw new Error('views must be an object');
}
if (!key || typeof key !== 'string') {
throw new Error('key must be a string');
}
return function (initData, item, i, data) {
var viewKey = item[key];
var View = views[viewKey];

if (View) {
return new View(initData, item, i, data);
} else {
throw new Error(("view " + viewKey + " not found"));
}
};
}

export { List, ListPool, Place, Router, el, h, html, list, listPool, mount, place, router, s, setAttr, setChildren, setData, setStyle, setXlink, svg, text, unmount };
export { List, ListPool, Place, Router, el, h, html, list, listPool, mount, place, router, s, setAttr, setChildren, setData, setStyle, setXlink, svg, text, unmount, viewFactory };
Loading

0 comments on commit 67a4de9

Please sign in to comment.