# all files / src/mixins/ DraggableGroupElement.js

35.62% Statements 26/73
20.59% Branches 7/34
16.67% Functions 2/12
35.62% Lines 26/73

    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
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254

    1×

    24×

    24×

    24×
    24×
    24×
    24×
    24×
    24×

    24×

    24×

    24×

    24×

    1×

    1×
    24×

    24×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * DraggableGroupElement is a mixin class used to create a group element to
     * contain draggable elements, which are items that can be clicked and dragged by a mouse.
     * The class is used with OO.ui.mixin.DraggableElement.
     *
     * @abstract
     * @class
     * @mixins OO.ui.mixin.GroupElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {string} [orientation] Item orientation: 'horizontal' or 'vertical'. The orientation
     *  should match the layout of the items. Items displayed in a single row
     *  or in several rows should use horizontal orientation. The vertical orientation should only be
     *  used when the items are displayed in a single column. Defaults to 'vertical'
     * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
     */
    OO.ui.mixin.DraggableGroupElement = function OoUiMixinDraggableGroupElement( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Parent constructor
    	OO.ui.mixin.GroupElement.call( this, config );

    	// Properties
    	this.orientation = config.orientation || 'vertical';
    	this.dragItem = null;
    	this.itemKeys = {};
    	this.dir = null;
    	this.itemsOrder = null;
    	this.draggable = config.draggable === undefined ? true : !!config.draggable;

    	// Events
    	this.aggregate( {
    		dragstart: 'itemDragStart',
    		dragend: 'itemDragEnd',
    		drop: 'itemDrop'
    	} );
    	this.connect( this, {
    		itemDragStart: 'onItemDragStart',
    		itemDrop: 'onItemDropOrDragEnd',
    		itemDragEnd: 'onItemDropOrDragEnd'
    	} );

    	// Initialize
    	Iif ( Array.isArray( config.items ) ) {
    		this.addItems( config.items );
    	}
    	this.$element
    		.addClass( 'oo-ui-draggableGroupElement' )
    		.append( this.$status )
    		.toggleClass( 'oo-ui-draggableGroupElement-horizontal', this.orientation === 'horizontal' );
    };

    /* Setup */
    OO.mixinClass( OO.ui.mixin.DraggableGroupElement, OO.ui.mixin.GroupElement );

    /* Events */

    /**
     * An item has been dragged to a new position, but not yet dropped.
     *
     * @event drag
     * @param {OO.ui.mixin.DraggableElement} item Dragged item
     * @param {number} [newIndex] New index for the item
     */

    /**
     * An item has been dropped at a new position.
     *
     * @event reorder
     * @param {OO.ui.mixin.DraggableElement} item Reordered item
     * @param {number} [newIndex] New index for the item
     */

    /**
     * Draggable state of this widget has changed.
     *
     * @event draggable
     * @param {boolean} [draggable] Widget is draggable
     */

    /* Methods */

    /**
     * Change the draggable state of this widget.
     * This allows users to temporarily halt the dragging operations.
     *
     * @param {boolean} isDraggable Widget supports draggable operations
     * @fires draggable
     */
    OO.ui.mixin.DraggableGroupElement.prototype.toggleDraggable = function ( isDraggable ) {
    	isDraggable = isDraggable !== undefined ? !!isDraggable : !this.draggable;

    	Iif ( this.draggable !== isDraggable ) {
    		this.draggable = isDraggable;

    		// Tell the items their draggable state changed
    		this.getItems().forEach( function ( item ) {
    			item.toggleDraggable( this.draggable );
    		}.bind( this ) );

    		// Emit event
    		this.emit( 'draggable', this.draggable );
    	}
    };

    /**
     * Check the draggable state of this widget
     *
     * @return {boolean} Widget supports draggable operations
     */
    OO.ui.mixin.DraggableGroupElement.prototype.isDraggable = function () {
    	return this.draggable;
    };

    /**
     * Respond to item drag start event
     *
     * @private
     * @param {OO.ui.mixin.DraggableElement} item Dragged item
     */
    OO.ui.mixin.DraggableGroupElement.prototype.onItemDragStart = function ( item ) {
    	if ( !this.isDraggable() ) {
    		return;
    	}
    	// Make a shallow copy of this.items so we can re-order it during previews
    	// without affecting the original array.
    	this.itemsOrder = this.items.slice();
    	this.updateIndexes();
    	if ( this.orientation === 'horizontal' ) {
    		// Calculate and cache directionality on drag start - it's a little
    		// expensive and it shouldn't change while dragging.
    		this.dir = this.$element.css( 'direction' );
    	}
    	this.setDragItem( item );
    };

    /**
     * Update the index properties of the items
     */
    OO.ui.mixin.DraggableGroupElement.prototype.updateIndexes = function () {
    	var i, len;

    	// Map the index of each object
    	for ( i = 0, len = this.itemsOrder.length; i < len; i++ ) {
    		this.itemsOrder[ i ].setIndex( i );
    	}
    };

    /**
     * Handle drop or dragend event and switch the order of the items accordingly
     *
     * @private
     * @param {OO.ui.mixin.DraggableElement} item Dropped item
     */
    OO.ui.mixin.DraggableGroupElement.prototype.onItemDropOrDragEnd = function () {
    	var targetIndex, originalIndex,
    		item = this.getDragItem();

    	// TODO: Figure out a way to configure a list of legally droppable
    	// elements even if they are not yet in the list
    	if ( item ) {
    		originalIndex = this.items.indexOf( item );
    		// If the item has moved forward, add one to the index to account for the left shift
    		targetIndex = item.getIndex() + ( item.getIndex() > originalIndex ? 1 : 0 );
    		if ( targetIndex !== originalIndex ) {
    			this.reorder( this.getDragItem(), targetIndex );
    			this.emit( 'reorder', this.getDragItem(), targetIndex );
    		}
    		this.updateIndexes();
    	}
    	this.unsetDragItem();
    	// Return false to prevent propogation
    	return false;
    };

    /**
     * Respond to dragover event
     *
     * @private
     * @param {jQuery.Event} e Dragover event
     * @fires reorder
     */
    OO.ui.mixin.DraggableGroupElement.prototype.onDragOver = function ( e ) {
    	var overIndex, targetIndex,
    		item = this.getDragItem(),
    		dragItemIndex = item.getIndex();

    	// Get the OptionWidget item we are dragging over
    	overIndex = $( e.target ).closest( '.oo-ui-draggableElement' ).data( 'index' );

    	if ( overIndex !== undefined && overIndex !== dragItemIndex ) {
    		targetIndex = overIndex + ( overIndex > dragItemIndex ? 1 : 0 );

    		if ( targetIndex > 0 ) {
    			this.$group.children().eq( targetIndex - 1 ).after( item.$element );
    		} else {
    			this.$group.prepend( item.$element );
    		}
    		// Move item in itemsOrder array
    		this.itemsOrder.splice( overIndex, 0,
    			this.itemsOrder.splice( dragItemIndex, 1 )[ 0 ]
    		);
    		this.updateIndexes();
    		this.emit( 'drag', item, targetIndex );
    	}
    	// Prevent default
    	e.preventDefault();
    };

    /**
     * Reorder the items in the group
     *
     * @param {OO.ui.mixin.DraggableElement} item Reordered item
     * @param {number} newIndex New index
     */
    OO.ui.mixin.DraggableGroupElement.prototype.reorder = function ( item, newIndex ) {
    	this.addItems( [ item ], newIndex );
    };

    /**
     * Set a dragged item
     *
     * @param {OO.ui.mixin.DraggableElement} item Dragged item
     */
    OO.ui.mixin.DraggableGroupElement.prototype.setDragItem = function ( item ) {
    	if ( this.dragItem !== item ) {
    		this.dragItem = item;
    		this.$element.on( 'dragover', this.onDragOver.bind( this ) );
    		this.$element.addClass( 'oo-ui-draggableGroupElement-dragging' );
    	}
    };

    /**
     * Unset the current dragged item
     */
    OO.ui.mixin.DraggableGroupElement.prototype.unsetDragItem = function () {
    	if ( this.dragItem ) {
    		this.dragItem = null;
    		this.$element.off( 'dragover' );
    		this.$element.removeClass( 'oo-ui-draggableGroupElement-dragging' );
    	}
    };

    /**
     * Get the item that is currently being dragged.
     *
     * @return {OO.ui.mixin.DraggableElement|null} The currently dragged item, or `null` if no item is being dragged
     */
    OO.ui.mixin.DraggableGroupElement.prototype.getDragItem = function () {
    	return this.dragItem;
    };