Skip to content

Commit 68eb3a5

Browse files
authored
Merge pull request #13 from sashafirsov/develop
1.1.8
2 parents 48e4abf + 9f291f4 commit 68eb3a5

File tree

5 files changed

+3729
-959
lines changed

5 files changed

+3729
-959
lines changed

CssChain.d.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ export interface CssChainCollection<T> extends Array<AnyElement&T>, AnyElement
2222
attr(name:string): CssChainCollection<T>;
2323
/** (alias for `setAttribute`) sets elements attribute, returns CssChain */
2424
attr(name:string, value:string): CssChainCollection<T>;
25+
/** (alias for `setAttribute`) sets elements attribute with value from callback, returns CssChain */
26+
attr(name:string, valueCallback:( (el:T, i:number, arr:CssChainCollection<T>)=>string) ): CssChainCollection<T>;
2527
/** (alias for `setAttribute`) sets `css`-defined sub-tree elements attribute, returns CssChain */
26-
attr(name:string, value:string, css:string): CssChainCollection<T>;
28+
attr(name:string, valueOrCallback:string | ( (el:T, i:number, arrCss:CssChainCollection<T>, arrThis:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
2729
/** returns 1st element property value or `undefined` for empty collection */
2830
prop(name:string): any;
29-
/** sets elements attribute, returns CssChain */
30-
prop(name:string, value:any): CssChainCollection<T>;
31-
/** sets `css`-defined sub-tree elements attribute, returns CssChain */
32-
prop(name:string, value:any, css:string): CssChainCollection<T>;
31+
/** sets elements property, returns CssChain */
32+
prop(name:string, valueOrCallback:any | ( (el:T, i:number, arr:CssChainCollection<T>)=>string)): CssChainCollection<T>;
33+
/** sets `css`-defined sub-tree elements property, returns CssChain */
34+
prop(name:string, valueOrCallback:any | ( (el:T, i:number, arrCss:CssChainCollection<T>, arrThis:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
3335
/** selects 1st elements by @param css string from each collection element, returns CssChain */
3436
querySelector(css: string): CssChainT;
3537
/** selects child elements by @param css string, returns CssChain */
@@ -52,8 +54,14 @@ export interface CssChainCollection<T> extends Array<AnyElement&T>, AnyElement
5254
erase(): CssChainCollection<T>;
5355
/** returns text of whole collection */
5456
txt(): string;
55-
/** sets text for each element from `val` or callback */
56-
txt(val: string | ((el:T,i:number,arr:CssChainCollection<T>)=>string), css: string|CssChainCollection<T>): CssChainCollection<T>;
57+
/** sets text for each element from `val` */
58+
txt(val: string): CssChainCollection<T>;
59+
/** sets text for each element from callback */
60+
txt( valCb: (el:T,i:number,arr:CssChainCollection<T>)=>string): CssChainCollection<T>;
61+
/** sets text for each element from `val` */
62+
txt(val: string, css: string|CssChainCollection<T>): CssChainCollection<T>;
63+
/** sets text for each element from callback */
64+
txt( valCb: (el:T,i:number,arrCss:CssChainCollection<T>,arrThis:CssChainCollection<T>)=>string, css: string|CssChainCollection<T>): CssChainCollection<T>;
5765
/** sets text for children elements defined by css, returns original collection */
5866
txt(val: any, css: string|CssChainCollection<T>): CssChainCollection<T>;
5967

CssChain.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,34 @@ function assignedNodesLight(f)
8989
class
9090
CssChainT extends Array
9191
{
92-
attr(...args){ return args.length>1 ? (( args[2] ? this.$(args[2]) : this ).setAttribute(...args),this) : this.getAttribute(...args) }
93-
prop(...args){ return args.length>1 ? (( args[2] ? this.$(args[2]) : this ).forEach( el=>el[args[0]]=args[1]),this ): this[0][args[0]] }
92+
attr(...args)
93+
{ if( args.length < 2 )
94+
return this.getAttribute(...args);
95+
let [k,v,s] = args
96+
, $s = this.$(s);
97+
if(isFn(v))
98+
$s.map((n,i,arr)=>v(n,i,arr,this)).forEach((V,i)=>$s[i].setAttribute(k,V))
99+
else
100+
$s.setAttribute(...args);
101+
return this
102+
}
103+
prop(...args)
104+
{ if( args.length < 2 )
105+
return this[0][args[0]];
106+
let [k,v,s] = args
107+
, $s = this.$(s);
108+
if(isFn(v))
109+
$s.map((n,i,arr)=>v(n,i,arr,this)).forEach((V,i)=>$s[i][k]=V)
110+
else
111+
$s.forEach( el=>el[k]=v);
112+
return this
113+
}
94114
forEach( ...args){ Array.prototype.forEach.apply(this,args); return this }
95115
map( ...args){ return map(this,...args) }
96116
push(...args){ Array.prototype.push.apply(this,args); return this; }
97117
querySelector(css){ return new CssChainT().push( this.querySelectorAll(css)[0] ) }
98118
querySelectorAll(css){ return this.reduce( ($,el)=> $.push(...(el.shadowRoot||el).querySelectorAll(css) ), new CssChainT()) }
99-
$(...args){ return args.length ? this.querySelectorAll(...args) : this; }
119+
$(...args){ return args.length && args[0] ? this.querySelectorAll(...args) : this; }
100120
parent(css)
101121
{ const s = new Set()
102122
, add = n=> s.has(n) ? 0 : (s.add(n), n)
@@ -186,11 +206,11 @@ CssChainT extends Array
186206
get innerText(){ return this.txt() }
187207
set innerText( val ){ return this.txt( val ) }
188208
txt( val, css=undefined )
189-
{ const arr = css? this.$(css): this;
209+
{ const $ = this.$(css);
190210
if( val === undefined )
191-
return collectionText( arr );
192-
arr.forEach( isFn(val)
193-
? (n,i)=>setNodeText(n,val(n,i,arr))
211+
return collectionText( $ );
212+
$.forEach( isFn(val)
213+
? (n,i)=>setNodeText(n,val(n,i,$,this))
194214
: n=>setNodeText(n,val) );
195215
return this
196216
}

0 commit comments

Comments
 (0)