11( function ( $ , $$ ) {
22
3- Mavo . attributes . push ( "mv-multiple " , "mv-order" , "mv-accepts" , "mv-initial-items" ) ;
3+ Mavo . attributes . push ( "mv-list" , "mv-list-item ", "mv-order" , "mv-accepts" , "mv-initial-items" ) ;
44
55var _ = Mavo . Collection = class Collection extends Mavo . Node {
66 constructor ( element , mavo , o ) {
@@ -9,7 +9,8 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
99 /*
1010 * Create the template, remove it from the DOM and store it
1111 */
12- this . templateElement = this . element ;
12+
13+ this . firstItemElement = this . templateElement = $ ( Mavo . selectors . multiple , this . element ) ;
1314
1415 this . children = [ ] ;
1516 this . liveData = new Mavo . Data ( this , [ ] ) ;
@@ -26,29 +27,27 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
2627 }
2728
2829 if ( ! this . fromTemplate ( "templateElement" , "accepts" , "initialItems" ) ) {
29- this . accepts = this . templateElement . getAttribute ( "mv-accepts" ) ;
30+ this . accepts = this . element . getAttribute ( "mv-accepts" ) ;
3031 this . accepts = new Set ( this . accepts ?. split ( / \s + / ) ) ;
3132
32- this . initialItems = + ( this . templateElement . getAttribute ( "mv-initial-items" ) || 1 ) ;
33+ this . initialItems = + ( this . element . getAttribute ( "mv-initial-items" ) || 1 ) ;
3334
3435 // Must clone because otherwise once expressions are parsed on the template element
3536 // we will not be able to pick them up from subsequent items
37+
3638 this . templateElement = this . templateElement . cloneNode ( true ) ;
3739 }
3840
39- if ( this . initialItems > 0 || ! this . template ) {
40- var item = this . createItem ( this . element ) ;
41- this . add ( item , undefined , { silent : true } ) ;
42- }
41+ let item = this . add ( this . firstItemElement , undefined , { silent : true } ) ;
4342
4443 this . mavo . treeBuilt . then ( ( ) => {
45- if ( ! this . initialItems ) {
44+ if ( this . initialItems === 0 ) {
4645 if ( item ) {
4746 this . delete ( item , { silent : true } ) ;
4847 }
4948 else {
5049 // No item to delete
51- this . element . remove ( ) ;
50+ this . firstItemElement . remove ( ) ;
5251 }
5352 }
5453 else if ( this . initialItems > 1 ) {
@@ -70,7 +69,7 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
7069 var group = this . parentGroup . element ;
7170
7271 var button = $$ ( selector , group ) . filter ( button => {
73- return ! this . templateElement . contains ( button ) // is outside the template element
72+ return ! this . element . contains ( button ) // is outside the list element
7473 && ! Mavo . data ( button , "collection" ) ; // and does not belong to another collection
7574 } ) [ 0 ] ;
7675
@@ -343,12 +342,9 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
343342
344343 // Insert the add button if it's not already in the DOM
345344 if ( ! this . addButton . parentNode ) {
346- var tag = this . element . tagName . toLowerCase ( ) ;
347-
348- if ( tag in Mavo . selectors . container ) {
349- var rel = this . marker . parentNode . closest ( Mavo . selectors . container [ tag ] ) ;
350- }
351- else if ( this . bottomUp && this . children [ 0 ] ) {
345+ // In bottom up collections, button goes before first item
346+ // otherwise, it goes after the marker
347+ if ( this . bottomUp && this . children [ 0 ] ) {
352348 var rel = this . children [ 0 ] . element ;
353349 }
354350
@@ -599,7 +595,7 @@ $.Class(_, {
599595 * Add new items at the top or bottom?
600596 */
601597
602- return / ^ d e s c \b / i. test ( this . templateElement . getAttribute ( "mv-order" ) ) ;
598+ return / ^ d e s c \b / i. test ( this . element . getAttribute ( "mv-order" ) ) ;
603599 }
604600 } ,
605601
0 commit comments