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 , [ ] ) ;
@@ -22,29 +23,27 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
2223 this . addButton = this . createAddButton ( ) ;
2324
2425 if ( ! this . fromTemplate ( "templateElement" , "accepts" , "initialItems" ) ) {
25- this . accepts = this . templateElement . getAttribute ( "mv-accepts" ) ;
26+ this . accepts = this . element . getAttribute ( "mv-accepts" ) ;
2627 this . accepts = new Set ( this . accepts ?. split ( / \s + / ) ) ;
2728
28- this . initialItems = + ( this . templateElement . getAttribute ( "mv-initial-items" ) || 1 ) ;
29+ this . initialItems = + ( this . element . getAttribute ( "mv-initial-items" ) || 1 ) ;
2930
3031 // Must clone because otherwise once expressions are parsed on the template element
3132 // we will not be able to pick them up from subsequent items
33+
3234 this . templateElement = this . templateElement . cloneNode ( true ) ;
3335 }
3436
35- if ( this . initialItems > 0 || ! this . template ) {
36- var item = this . createItem ( this . element ) ;
37- this . add ( item , undefined , { silent : true } ) ;
38- }
37+ let item = this . add ( this . firstItemElement , undefined , { silent : true } ) ;
3938
4039 this . mavo . treeBuilt . then ( ( ) => {
41- if ( ! this . initialItems ) {
40+ if ( this . initialItems === 0 ) {
4241 if ( item ) {
4342 this . delete ( item , { silent : true } ) ;
4443 }
4544 else {
4645 // No item to delete
47- this . element . remove ( ) ;
46+ this . firstItemElement . remove ( ) ;
4847 }
4948 }
5049 else if ( this . initialItems > 1 ) {
@@ -66,7 +65,7 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
6665 var group = this . parentGroup . element ;
6766
6867 var button = $$ ( selector , group ) . filter ( button => {
69- return ! this . templateElement . contains ( button ) // is outside the template element
68+ return ! this . element . contains ( button ) // is outside the list element
7069 && ! Mavo . data ( button , "collection" ) ; // and does not belong to another collection
7170 } ) [ 0 ] ;
7271
@@ -339,12 +338,9 @@ var _ = Mavo.Collection = class Collection extends Mavo.Node {
339338
340339 // Insert the add button if it's not already in the DOM
341340 if ( ! this . addButton . parentNode ) {
342- var tag = this . element . tagName . toLowerCase ( ) ;
343-
344- if ( tag in Mavo . selectors . container ) {
345- var rel = this . marker . parentNode . closest ( Mavo . selectors . container [ tag ] ) ;
346- }
347- else if ( this . bottomUp && this . children [ 0 ] ) {
341+ // In bottom up collections, button goes before first item
342+ // otherwise, it goes after the marker
343+ if ( this . bottomUp && this . children [ 0 ] ) {
348344 var rel = this . children [ 0 ] . element ;
349345 }
350346
@@ -595,7 +591,7 @@ $.Class(_, {
595591 * Add new items at the top or bottom?
596592 */
597593
598- return / ^ d e s c \b / i. test ( this . templateElement . getAttribute ( "mv-order" ) ) ;
594+ return / ^ d e s c \b / i. test ( this . element . getAttribute ( "mv-order" ) ) ;
599595 }
600596 } ,
601597
0 commit comments