rowItem.js
5.79 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Single row view
*/
define( ['views/cellItem', 'models/cellFieldCollection'], function( CellItemView, CellFieldCollection ) {
var view = Marionette.CollectionView.extend( {
tagname: 'div',
className: 'layouts-row',
childView: CellItemView,
reorderOnSort: true,
initialize: function() {
// Set our collection to our cells.
this.collection = this.model.get( 'cells' );
/*
* Set our childViewOptions.
* As the variable name suggests, this will be accessible within our child views.
*/
this.childViewOptions = {
cellCollection: this.collection
};
// Respond to requests to update our gutter/divider positions
nfRadio.channel( 'layouts' ).reply( 'update:gutters', this.updateGutters, this );
/*
* Bind listeners to row model events.
*/
// this.model.on( 'destroy:cell', this.render, this );
this.collection.on( 'sort', this.render, this );
/*
* Bind listeners to our nf-builder so that we can track when the user is dragging rather than just mouse over.
* Because our gutter/divider is a droppable that is very close to a sortable, sometimes the "drop" event will fire when we are just mousing over.
* Tracking that state of the mouse lets us prevent this later.
*
*/
jQuery( '#nf-builder' ).on( 'mousedown', function() {
jQuery( this ).data( 'mousedown', true );
} );
jQuery( '#nf-builder' ).on( 'mouseup', function() {
jQuery( this ).data( 'mousedown', false );
} );
},
/**
* Before we destroy this view, unbind our model change listeners.
* If we don't do this, we'll get JS errors.
*
* @since 3.0
* @return void
*/
onBeforeDestroy: function() {
// this.model.off( 'add:cell', this.render );
// this.model.off( 'destroy:cell', this.render );
this.collection.off( 'sort', this.maybeRender );
},
maybeRender: function() {
if ( 1 < this.collection.models.length ) {
this.render();
}
},
/**
* When we render:
* 1) Set our el id to the model cid
* 2) Add a class based upon the number of cells in the row
* 3) Remove any old gutters
* 4) Update our gutters/dividers
* 5) Init our gutters/dividers as droppables
*
* @since version
* @return {[type]} [description]
*/
onRender: function() {
// Set el ID
jQuery( this.el ).prop( 'id', this.model.cid );
// Add class based upon number of cells
if ( this.collection.models.length == 1 ) {
jQuery( this.el ).addClass( 'single-cell' );
jQuery( this.el ).removeClass( 'multi-cell' );
} else {
jQuery( this.el ).addClass( 'multi-cell' );
jQuery( this.el ).removeClass( 'single-cell' );
}
// Remove any gutters. This prevents extra HTML markup from appearing.
jQuery( this.el ).find( '.gutter' ).remove();
// Update our gutters/dividers
this.updateGutters();
// We want to access our rowView object later
var rowView = this;
// Init our droppables.
jQuery( this.el ).find( '.gutter' ).droppable( {
// Activate by pointer
tolerance: 'pointer',
// Class added when we're dragging over
hoverClass: 'nf-fields-sortable-placeholder',
// Which elements do we want to accept?
accept: '.nf-field-type-draggable, .nf-field-wrap, .nf-stage',
/**
* When we drag over this droppable, trigger a radio event.
*
* @since 3.0
* @param object e event
* @param object ui jQuery UI element
* @return void
*/
over: function( e, ui ) {
nfRadio.channel( 'layouts' ).trigger( 'over:gutterDroppable', e, ui, rowView, this );
},
/**
* When we drag out of this droppable, trigger a radio event.
*
* @since 3.0
* @param object e event
* @param object ui jQuery UI element
* @return void
*/
out: function( e, ui ) {
nfRadio.channel( 'layouts' ).trigger( 'out:gutterDroppable', e, ui, rowView, this );
},
/**
* When we drop on this droppable, trigger a radio event.
*
* @since 3.0
* @param object e event
* @param object ui jQuery UI element
* @return void
*/
drop: function( e, ui ) {
nfRadio.channel( 'layouts' ).trigger( 'drop:gutterDroppable', e, ui, rowView, this );
}
} );
},
/**
* Check for gutters in our row and activate them as sliders.
*
* @since 3.0
* @return void
*/
updateGutters: function() {
// Get our gutter elements
var elements = jQuery( this.el ).find( '.layouts-cell' );
var that = this;
// Call split.js to create resizable cells.
Split( elements, {
minSize: 50,
cellCollection: that.collection,
// When we start resizing our cell, trigger a radio event.
onDragStart: function( data ) {
nfRadio.channel( 'layouts' ).trigger( 'dragStart:gutterSlider', data, that.collection );
},
// When we drag/resize our cell, trigger a radio event.
onDrag: function( data ) {
nfRadio.channel( 'layouts' ).trigger( 'drag:gutterSlider', data, that.collection );
},
// When we stop resizing our cell, trigger a radio event.
onDragEnd: function( data ) {
nfRadio.channel( 'layouts' ).trigger( 'dragEnd:gutterSlider', data, that.collection );
}
} );
// Set the css width on our gutters
_.each( jQuery( elements ), function( cell, index ) {
var width = jQuery( cell ).data( 'width' );
var gutterWidth = 10;
if ( 0 == index || index == jQuery( elements ).length - 1 ) {
// gutterWidth = 5;
}
jQuery( cell ).css( 'width', 'calc(' + width + '% - ' + gutterWidth + 'px)' );
} );
// Add a gutter/divider before our first cell and after our last cell.
var html = '<div class="gutter" style="width: 10px; cursor: ew-resize;"></div>';
jQuery( this.el ).find( '.layouts-cell:first' ).before( html );
jQuery( this.el ).find( '.layouts-cell:last' ).after( html );
}
} );
return view;
} );