# all files / src/mixins/ DraggableElement.js

48.98% Statements 24/49
30% Branches 6/20
18.18% Functions 2/11
48.98% Lines 24/49

    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

    1×
    48×

    48×
    48×
    48×

    48×

    48×
    48×

    1×

    1×

    1×
    48×

    48×
    48×

    48×

    48×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * DraggableElement is a mixin class used to create elements that can be clicked
     * and dragged by a mouse to a new position within a group. This class must be used
     * in conjunction with OO.ui.mixin.DraggableGroupElement, which provides a container for
     * the draggable elements.
     *
     * @abstract
     * @class
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {jQuery} [$handle] The part of the element which can be used for dragging, defaults to the whole element
     * @cfg {boolean} [draggable] The items are draggable. This can change with #toggleDraggable
     *  but the draggable state should be called from the DraggableGroupElement, which updates
     *  the whole group
     */
    OO.ui.mixin.DraggableElement = function OoUiMixinDraggableElement( config ) {
    	config = config || {};

    	// Properties
    	this.index = null;
    	this.$handle = config.$handle || this.$element;
    	this.wasHandleUsed = null;

    	// Initialize and events
    	this.$element
    		.addClass( 'oo-ui-draggableElement' )
    		.on( {
    			mousedown: this.onDragMouseDown.bind( this ),
    			dragstart: this.onDragStart.bind( this ),
    			dragover: this.onDragOver.bind( this ),
    			dragend: this.onDragEnd.bind( this ),
    			drop: this.onDrop.bind( this )
    		} );
    	this.$handle.addClass( 'oo-ui-draggableElement-handle' );
    	this.toggleDraggable( config.draggable === undefined ? true : !!config.draggable );
    };

    OO.initClass( OO.ui.mixin.DraggableElement );

    /* Events */

    /**
     * @event dragstart
     *
     * A dragstart event is emitted when the user clicks and begins dragging an item.
     * @param {OO.ui.mixin.DraggableElement} item The item the user has clicked and is dragging with the mouse.
     */

    /**
     * @event dragend
     * A dragend event is emitted when the user drags an item and releases the mouse,
     * thus terminating the drag operation.
     */

    /**
     * @event drop
     * A drop event is emitted when the user drags an item and then releases the mouse button
     * over a valid target.
     */

    /* Static Properties */

    /**
     * @inheritdoc OO.ui.mixin.ButtonElement
     */
    OO.ui.mixin.DraggableElement.static.cancelButtonMouseDownEvents = false;

    /* 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.DraggableElement.prototype.toggleDraggable = function ( isDraggable ) {
    	isDraggable = isDraggable !== undefined ? !!isDraggable : !this.draggable;

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

    		this.$handle.toggleClass( 'oo-ui-draggableElement-undraggable', !this.draggable );

    		// We make the entire element draggable, not just the handle, so that
    		// the whole element appears to move. wasHandleUsed prevents drags from
    		// starting outside the handle
    		this.$element.prop( 'draggable', this.draggable );
    	}
    };

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

    /**
     * Respond to mousedown event.
     *
     * @private
     * @param {jQuery.Event} e Drag event
     */
    OO.ui.mixin.DraggableElement.prototype.onDragMouseDown = function ( e ) {
    	if ( !this.isDraggable() ) {
    		return;
    	}

    	this.wasHandleUsed =
    		// Optimization: if the handle is the whole element this is always true
    		this.$handle[ 0 ] === this.$element[ 0 ] ||
    		// Check the mousedown occurred inside the handle
    		OO.ui.contains( this.$handle[ 0 ], e.target, true );
    };

    /**
     * Respond to dragstart event.
     *
     * @private
     * @param {jQuery.Event} e Drag event
     * @return {boolean} False if the event is cancelled
     * @fires dragstart
     */
    OO.ui.mixin.DraggableElement.prototype.onDragStart = function ( e ) {
    	var element = this,
    		dataTransfer = e.originalEvent.dataTransfer;

    	if ( !this.wasHandleUsed || !this.isDraggable() ) {
    		return false;
    	}

    	// Define drop effect
    	dataTransfer.dropEffect = 'none';
    	dataTransfer.effectAllowed = 'move';
    	// Support: Firefox
    	// We must set up a dataTransfer data property or Firefox seems to
    	// ignore the fact the element is draggable.
    	try {
    		dataTransfer.setData( 'application-x/OOUI-draggable', this.getIndex() );
    	} catch ( err ) {
    		// The above is only for Firefox. Move on if it fails.
    	}
    	// Briefly add a 'clone' class to style the browser's native drag image
    	this.$element.addClass( 'oo-ui-draggableElement-clone' );
    	// Add placeholder class after the browser has rendered the clone
    	setTimeout( function () {
    		element.$element
    			.removeClass( 'oo-ui-draggableElement-clone' )
    			.addClass( 'oo-ui-draggableElement-placeholder' );
    	} );
    	// Emit event
    	this.emit( 'dragstart', this );
    	return true;
    };

    /**
     * Respond to dragend event.
     *
     * @private
     * @fires dragend
     */
    OO.ui.mixin.DraggableElement.prototype.onDragEnd = function () {
    	this.$element.removeClass( 'oo-ui-draggableElement-placeholder' );
    	this.emit( 'dragend' );
    };

    /**
     * Handle drop event.
     *
     * @private
     * @param {jQuery.Event} e Drop event
     * @fires drop
     */
    OO.ui.mixin.DraggableElement.prototype.onDrop = function ( e ) {
    	e.preventDefault();
    	this.emit( 'drop', e );
    };

    /**
     * In order for drag/drop to work, the dragover event must
     * return false and stop propogation.
     *
     * @param {jQuery.Event} e Drag event
     * @private
     */
    OO.ui.mixin.DraggableElement.prototype.onDragOver = function ( e ) {
    	e.preventDefault();
    };

    /**
     * Set item index.
     * Store it in the DOM so we can access from the widget drag event
     *
     * @private
     * @param {number} index Item index
     */
    OO.ui.mixin.DraggableElement.prototype.setIndex = function ( index ) {
    	if ( this.index !== index ) {
    		this.index = index;
    		this.$element.data( 'index', index );
    	}
    };

    /**
     * Get item index
     *
     * @private
     * @return {number} Item index
     */
    OO.ui.mixin.DraggableElement.prototype.getIndex = function () {
    	return this.index;
    };