1- let did = 1 ;
2- const buildData = ( count ) => {
3- const adjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
4- const colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
5- const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
6- const data = [ ] ;
7- for ( let i = 0 ; i < count ; i ++ ) {
8- data . push ( {
9- id : did ++ ,
10- label : adjectives [ _random ( adjectives . length ) ] + " " + colours [ _random ( colours . length ) ] + " " + nouns [ _random ( nouns . length ) ]
11- } ) ;
12- }
13- return data ;
14- } ;
15-
16- const _random = max => Math . round ( Math . random ( ) * 1000 ) % max ;
17-
18- export function Scope ( update ) {
19- const scope = {
20- add ( ) {
21- scope . data = scope . data . concat ( buildData ( 1000 ) ) ;
22- update ( scope ) ;
23- } ,
24- run ( ) {
25- scope . data = buildData ( 1000 ) ;
26- update ( scope ) ;
27- } ,
28- runLots ( ) {
29- scope . data = buildData ( 10000 ) ;
30- update ( scope ) ;
31- } ,
32- clear ( ) {
33- scope . data = [ ] ;
34- update ( scope ) ;
35- } ,
36- update ( ) {
37- const { data} = scope ;
38- for ( let i = 0 , { length} = data ; i < length ; i += 10 )
39- data [ i ] . label += ' !!!' ;
40- update ( scope ) ;
41- } ,
42- swapRows ( ) {
43- const { data} = scope ;
44- if ( data . length > 998 ) {
45- const tmp = data [ 1 ] ;
46- data [ 1 ] = data [ 998 ] ;
47- data [ 998 ] = tmp ;
48- }
49- update ( scope ) ;
50- } ,
51- delete ( id ) {
52- const { data} = scope ;
53- const idx = data . findIndex ( d => d . id === id ) ;
54- data . splice ( idx , 1 ) ;
55- update ( scope ) ;
56- } ,
57- select ( id ) {
58- scope . selected = id ;
59- update ( scope ) ;
60- } ,
61- selected : - 1 ,
62- data : [ ] ,
63- } ;
64- return scope ;
65- } ;
66-
671const template = document . createElement ( 'template' ) ;
682template . innerHTML = `
693<tr>
@@ -77,23 +11,29 @@ template.innerHTML = `
7711` . trim ( ) ;
7812const tr = template . content . firstChild ;
7913
80- const rows = new WeakMap ;
81- const createRow = ( scope , item ) => {
82- const { id, label} = item ;
83-
14+ const createRow = ( select , remove , id , label ) => {
8415 const row = tr . cloneNode ( true ) ;
8516 const td = row . querySelector ( 'td' ) ;
8617 td . textContent = ( row . id = id ) ;
8718
88- const [ select , remove ] = row . querySelectorAll ( 'a' ) ;
89- select . textContent = label ;
90- select . addEventListener ( 'click' , ( ) => scope . select ( id ) ) ;
91- remove . addEventListener ( 'click' , ( ) => scope . delete ( id ) ) ;
19+ const [ selector , remover ] = row . querySelectorAll ( 'a' ) ;
20+ selector . textContent = label ;
21+ selector . addEventListener ( 'click' , ( ) => select ( id ) ) ;
22+ remover . addEventListener ( 'click' , ( ) => remove ( id ) ) ;
23+
24+ return { danger : false , id, label, row, selector, td} ;
25+ } ;
26+
27+ const { create} = Object ;
28+ const rows = new WeakMap ;
9229
93- const info = { danger : false , id, label, row, select, td} ;
94- rows . set ( item , info ) ;
95- return info ;
30+ const setCache = data => {
31+ const cache = create ( null ) ;
32+ rows . set ( data , cache ) ;
33+ return cache ;
9634} ;
9735
98- export const getRow = ( scope , item ) =>
99- rows . get ( item ) || createRow ( scope , item ) ;
36+ export const getRow = ( data , select , remove , id , label ) => {
37+ const cache = rows . get ( data ) || setCache ( data ) ;
38+ return cache [ id ] || ( cache [ id ] = createRow ( select , remove , id , label ) ) ;
39+ } ;
0 commit comments