rowCollection.js
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Holds all of our row models.
*
* @package Ninja Forms Layouts
* @subpackage Fields
* @copyright (c) 2016 WP Ninjas
* @since 3.0
*/
define( ['models/rowModel'], function( rowModel ) {
var collection = Backbone.Collection.extend( {
model: rowModel,
comparator: 'order',
initialize: function( models ) {
this.updateMaxCols( models );
this.on( 'add:cell', this.updateMaxCols, this );
this.on( 'destroy:cell', this.updateMaxCols, this );
this.on( 'remove:cell', this.updateMaxCols, this );
this.on( 'destroy', this.updateMaxCols, this );
this.on( 'add:field', this.addField, this );
this.on( 'append:field', this.appendField, this );
this.on( 'remove:field', this.removeField, this );
},
updateMaxCols: function( models ) {
var maxCols = 1;
if ( true === models instanceof Backbone.Model ) {
models = this.models
}
_.each( models, function( row ) {
if ( 'undefined' != typeof row.cells ) {
if ( maxCols < row.cells.length ) {
maxCols = row.cells.length;
}
} else if ( true === row instanceof Backbone.Model ) {
if ( maxCols < row.get( 'cells' ).length ) {
maxCols = row.get( 'cells' ).length;
}
}
} );
nfRadio.channel( 'layouts' ).request( 'update:colClass', maxCols );
},
addField: function( fieldModel ) {
if ( ! fieldModel.get( 'oldCellcid' ) ) {
this.appendField( fieldModel );
return false;
}
var cellModel = false;
this.every( function( rowModel ) {
if ( rowModel.get( 'cells' ).get( { cid: fieldModel.get( 'oldCellcid' ) } ) ) {
cellModel = rowModel.get( 'cells' ).get( { cid: fieldModel.get( 'oldCellcid' ) } );
return false;
}
return true;
} );
if ( cellModel ) {
cellModel.get( 'fields' ).add( fieldModel );
cellModel.collection.sort();
} else {
this.appendField( fieldModel );
}
fieldModel.set( 'oldCellcid', false );
},
removeField: function( fieldModel ) {
if ( ! fieldModel.get( 'oldCellcid' ) ) {
fieldModel.set( 'oldCellcid', fieldModel.get( 'cellcid' ) );
}
nfRadio.channel( 'layouts-cell' ).trigger( 'remove:field', fieldModel.get( 'id' ) );
},
appendField: function( fieldModel ) {
nfRadio.channel( 'layouts' ).request( 'add:row', this, { field: fieldModel.get( 'key' ) } );
}
} );
return collection;
} );