From 67a4de98e95e1975a462afe9ab65885b42186219 Mon Sep 17 00:00:00 2001 From: Juha Lindstedt Date: Sat, 3 Feb 2024 21:24:12 +0200 Subject: [PATCH] v4.0.0 --- redom.es.js | 73 ++++++++++++++++++++++++++---------------------- redom.es.min.js | 2 +- redom.js | 74 ++++++++++++++++++++++++++----------------------- redom.min.js | 2 +- 4 files changed, 82 insertions(+), 69 deletions(-) diff --git a/redom.es.js b/redom.es.js index 3e08fc5..7ddcfb5 100644 --- a/redom.es.js +++ b/redom.es.js @@ -106,6 +106,7 @@ function hooksAreEmpty (hooks) { /* global Node, ShadowRoot */ + var hookNames = ['onmount', 'onremount', 'onunmount']; var shadowRootAvailable = typeof window !== 'undefined' && 'ShadowRoot' in window; @@ -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); @@ -261,6 +262,7 @@ function setStyleValue (el, key, value) { /* global SVGElement */ + var xlinkns = 'http://www.w3.org/1999/xlink'; function setAttr (view, arg1, arg2) { @@ -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 ]; @@ -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) )); @@ -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 ]; @@ -600,6 +592,7 @@ list.extend = List.extend; /* global Node */ + function place (View, initData) { return new Place(View, initData); } @@ -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; @@ -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 ]; @@ -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) )); @@ -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 }; diff --git a/redom.es.min.js b/redom.es.min.js index 89f7ce9..44759b3 100644 --- a/redom.es.min.js +++ b/redom.es.min.js @@ -1 +1 @@ -function createElement(query,ns){var ref=parse(query);var tag=ref.tag;var id=ref.id;var className=ref.className;var element=ns?document.createElementNS(ns,tag):document.createElement(tag);if(id){element.id=id}if(className){if(ns){element.setAttribute("class",className)}else{element.className=className}}return element}function parse(query){var chunks=query.split(/([.#])/);var className="";var id="";for(var i=1;i0)args[len]=arguments[len+1];var element;var type=typeof query;if(type==="string"){element=memoizeHTML(query).cloneNode(false)}else if(isNode(query)){element=query.cloneNode(false)}else if(type==="function"){var Query=query;element=new(Function.prototype.bind.apply(Query,[null].concat(args)))}else{throw new Error("At least one argument required")}parseArgumentsInternal(getEl(element),args,true);return element}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];var clone=memoizeHTML(query);return html.bind.apply(html,[this,clone].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];var parentEl=getEl(parent);var current=traverse(parent,children,parentEl.firstChild);while(current){var next=current.nextSibling;unmount(parent,current);current=next}}function traverse(parent,children,_current){var current=_current;var childEls=Array(children.length);for(var i=0;i0)args[len]=arguments[len+1];var element;var type=typeof query;if(type==="string"){element=memoizeSVG(query).cloneNode(false)}else if(isNode(query)){element=query.cloneNode(false)}else if(type==="function"){var Query=query;element=new(Function.prototype.bind.apply(Query,[null].concat(args)))}else{throw new Error("At least one argument required")}parseArgumentsInternal(getEl(element),args,true);return element}var s=svg;svg.extend=function extendSvg(query){var clone=memoizeSVG(query);return svg.bind(this,clone)};svg.ns=ns;function memoizeSVG(query){return svgCache[query]||(svgCache[query]=createElement(query,ns))}export{List,ListPool,Place,Router,el,h,html,list,listPool,mount,place,router,s,setAttr,setChildren,setData,setStyle,setXlink,svg,text,unmount}; \ No newline at end of file +function createElement(query,ns){var ref=parse(query);var tag=ref.tag;var id=ref.id;var className=ref.className;var element=ns?document.createElementNS(ns,tag):document.createElement(tag);if(id){element.id=id}if(className){if(ns){element.setAttribute("class",className)}else{element.className=className}}return element}function parse(query){var chunks=query.split(/([.#])/);var className="";var id="";for(var i=1;i0)args[len]=arguments[len+1];var element;var type=typeof query;if(type==="string"){element=createElement(query)}else if(type==="function"){var Query=query;element=new(Function.prototype.bind.apply(Query,[null].concat(args)))}else{throw new Error("At least one argument required")}parseArgumentsInternal(getEl(element),args,true);return element}var el=html;var h=html;html.extend=function extendHtml(){var args=[],len=arguments.length;while(len--)args[len]=arguments[len];return html.bind.apply(html,[this].concat(args))};function setChildren(parent){var children=[],len=arguments.length-1;while(len-- >0)children[len]=arguments[len+1];var parentEl=getEl(parent);var current=traverse(parent,children,parentEl.firstChild);while(current){var next=current.nextSibling;unmount(parent,current);current=next}}function traverse(parent,children,_current){var current=_current;var childEls=Array(children.length);for(var i=0;i0)args[len]=arguments[len+1];var element;var type=typeof query;if(type==="string"){element=createElement(query,ns)}else if(type==="function"){var Query=query;element=new(Function.prototype.bind.apply(Query,[null].concat(args)))}else{throw new Error("At least one argument required")}parseArgumentsInternal(getEl(element),args,true);return element}var s=svg;svg.extend=function extendSvg(){var args=[],len=arguments.length;while(len--)args[len]=arguments[len];return svg.bind.apply(svg,[this].concat(args))};svg.ns=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,viewFactory}; \ No newline at end of file diff --git a/redom.js b/redom.js index 9982892..49ef45f 100644 --- a/redom.js +++ b/redom.js @@ -112,6 +112,7 @@ /* global Node, ShadowRoot */ + var hookNames = ['onmount', 'onremount', 'onunmount']; var shadowRootAvailable = typeof window !== 'undefined' && 'ShadowRoot' in window; @@ -140,7 +141,7 @@ var beforeEl = getEl(before); if (beforeEl.__redom_mounted) { - trigger(before.el, 'onunmount'); + trigger(beforeEl, 'onunmount'); } parentEl.replaceChild(childEl, beforeEl); @@ -267,6 +268,7 @@ /* global SVGElement */ + var xlinkns = 'http://www.w3.org/1999/xlink'; function setAttr (view, arg1, arg2) { @@ -379,8 +381,6 @@ return arg && arg.nodeType; } - var htmlCache = {}; - function html (query) { var args = [], len = arguments.length - 1; while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ]; @@ -390,9 +390,7 @@ 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) )); @@ -408,19 +406,13 @@ 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 ]; - - var clone = memoizeHTML(query); + html.extend = function extendHtml () { + var args = [], len = arguments.length; + while ( len-- ) args[ len ] = arguments[ len ]; - 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 ]; @@ -606,6 +598,7 @@ /* global Node */ + function place (View, initData) { return new Place(View, initData); } @@ -675,20 +668,22 @@ /* 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; @@ -705,8 +700,6 @@ 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 ]; @@ -716,9 +709,7 @@ 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) )); @@ -733,16 +724,32 @@ 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")); + } + }; } exports.List = List; @@ -766,7 +773,6 @@ exports.svg = svg; exports.text = text; exports.unmount = unmount; - - Object.defineProperty(exports, '__esModule', { value: true }); + exports.viewFactory = viewFactory; })); diff --git a/redom.min.js b/redom.min.js index ba506ac..b659da7 100644 --- a/redom.min.js +++ b/redom.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).redom={})}(this,(function(e){"use strict";function t(e,t){var i=function(e){for(var t=e.split(/([.#])/),i="",n="",o=1;o0;)i[n]=arguments[n+1];var o=typeof e;if("string"===o)t=k(e).cloneNode(!1);else if(y(e))t=e.cloneNode(!1);else{if("function"!==o)throw new Error("At least one argument required");var r=e;t=new(Function.prototype.bind.apply(r,[null].concat(i)))}return p(w(t),i,!0),t}var N=b,x=b;function k(e){return g[e]||(g[e]=t(e))}function S(e){for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];for(var o=w(e),r=A(e,t,o.firstChild);r;){var l=r.nextSibling;i(e,r),r=l}}function A(e,t,i){for(var n=i,o=Array(t.length),r=0;r0;)t[i]=arguments[i+1];var n=k(e);return b.bind.apply(b,[this,n].concat(t))};var D=function(e,t,i){this.View=e,this.initData=i,this.oldLookup={},this.lookup={},this.oldViews=[],this.views=[],null!=t&&(this.key="function"==typeof t?t:function(e){return function(t){return t[e]}}(t))};function V(e,t,i,n){return new j(e,t,i,n)}D.prototype.update=function(e,t){for(var i=this,n=i.View,o=i.key,r=i.initData,l=null!=o,s=this.lookup,u={},a=Array(e.length),f=this.views,d=0;d0;)i[n]=arguments[n+1];var o=typeof e;if("string"===o)t=R(e).cloneNode(!1);else if(y(e))t=e.cloneNode(!1);else{if("function"!==o)throw new Error("At least one argument required");var r=e;t=new(Function.prototype.bind.apply(r,[null].concat(i)))}return p(w(t),i,!0),t}var O=P;function R(e){return L[e]||(L[e]=t(e,T))}P.extend=function(e){var t=R(e);return P.bind(this,t)},P.ns=T,e.List=j,e.ListPool=D,e.Place=C,e.Router=E,e.el=N,e.h=x,e.html=b,e.list=V,e.listPool=function(e,t,i){return new D(e,t,i)},e.mount=s,e.place=function(e,t){return new C(e,t)},e.router=function(e,t,i){return new E(e,t,i)},e.s=O,e.setAttr=function(e,t,i){c(e,t,i)},e.setChildren=S,e.setData=v,e.setStyle=a,e.setXlink=h,e.svg=P,e.text=_,e.unmount=i,Object.defineProperty(e,"__esModule",{value:!0})})); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).redom={})}(this,(function(e){"use strict";function t(e,t){var i=function(e){for(var t=e.split(/([.#])/),i="",n="",o=1;o0;)n[o]=arguments[o+1];var r=typeof e;if("string"===r)i=t(e);else{if("function"!==r)throw new Error("At least one argument required");var l=e;i=new(Function.prototype.bind.apply(l,[null].concat(n)))}return p(m(i),n,!0),i}var b=g,N=g;function x(e){for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];for(var o=k(e,t,m(e).firstChild);o;){var r=o.nextSibling;i(e,o),o=r}}function k(e,t,i){for(var n=i,o=Array(t.length),r=0;r0;)n[o]=arguments[o+1];var r=typeof e;if("string"===r)i=t(e,V);else{if("function"!==r)throw new Error("At least one argument required");var l=e;i=new(Function.prototype.bind.apply(l,[null].concat(n)))}return p(m(i),n,!0),i}var T=C;C.extend=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.bind.apply(C,[this].concat(e))},C.ns=V,e.List=D,e.ListPool=S,e.Place=E,e.Router=j,e.el=b,e.h=N,e.html=g,e.list=A,e.listPool=function(e,t,i){return new S(e,t,i)},e.mount=s,e.place=function(e,t){return new E(e,t)},e.router=function(e,t,i){return new j(e,t,i)},e.s=T,e.setAttr=function(e,t,i){h(e,t,i)},e.setChildren=x,e.setData=c,e.setStyle=a,e.setXlink=v,e.svg=C,e.text=_,e.unmount=i,e.viewFactory=function(e,t){if(!e||"object"!=typeof e)throw new Error("views must be an object");if(!t||"string"!=typeof t)throw new Error("key must be a string");return function(i,n,o,r){var l=n[t],s=e[l];if(s)return new s(i,n,o,r);throw new Error("view "+l+" not found")}}})); \ No newline at end of file