# all files / src/widgets/ OutlineControlsWidget.js

12.24% Statements 6/49
0% Branches 0/24
0% Functions 0/3
12.24% Lines 6/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

    1×

    1×
    1×
    1×

    1×

    1×

     /**
     * OutlineControlsWidget is a set of controls for an {@link OO.ui.OutlineSelectWidget outline select widget}.
     * Controls include moving items up and down, removing items, and adding different kinds of items.
     *
     * **Currently, this class is only used by {@link OO.ui.BookletLayout booklet layouts}.**
     *
     * @class
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.GroupElement
     * @mixins OO.ui.mixin.IconElement
     *
     * @constructor
     * @param {OO.ui.OutlineSelectWidget} outline Outline to control
     * @param {Object} [config] Configuration options
     * @cfg {Object} [abilities] List of abilties
     * @cfg {boolean} [abilities.move=true] Allow moving movable items
     * @cfg {boolean} [abilities.remove=true] Allow removing removable items
     */
    OO.ui.OutlineControlsWidget = function OoUiOutlineControlsWidget( outline, config ) {
    	// Allow passing positional parameters inside the config object
    	if ( OO.isPlainObject( outline ) && config === undefined ) {
    		config = outline;
    		outline = config.outline;
    	}

    	// Configuration initialization
    	config = $.extend( { icon: 'add' }, config );

    	// Parent constructor
    	OO.ui.OutlineControlsWidget.parent.call( this, config );

    	// Mixin constructors
    	OO.ui.mixin.GroupElement.call( this, config );
    	OO.ui.mixin.IconElement.call( this, config );

    	// Properties
    	this.outline = outline;
    	this.$movers = $( '<div>' );
    	this.upButton = new OO.ui.ButtonWidget( {
    		framed: false,
    		icon: 'collapse',
    		title: OO.ui.msg( 'ooui-outline-control-move-up' )
    	} );
    	this.downButton = new OO.ui.ButtonWidget( {
    		framed: false,
    		icon: 'expand',
    		title: OO.ui.msg( 'ooui-outline-control-move-down' )
    	} );
    	this.removeButton = new OO.ui.ButtonWidget( {
    		framed: false,
    		icon: 'trash',
    		title: OO.ui.msg( 'ooui-outline-control-remove' )
    	} );
    	this.abilities = { move: true, remove: true };

    	// Events
    	outline.connect( this, {
    		select: 'onOutlineChange',
    		add: 'onOutlineChange',
    		remove: 'onOutlineChange'
    	} );
    	this.upButton.connect( this, { click: [ 'emit', 'move', -1 ] } );
    	this.downButton.connect( this, { click: [ 'emit', 'move', 1 ] } );
    	this.removeButton.connect( this, { click: [ 'emit', 'remove' ] } );

    	// Initialization
    	this.$element.addClass( 'oo-ui-outlineControlsWidget' );
    	this.$group.addClass( 'oo-ui-outlineControlsWidget-items' );
    	this.$movers
    		.addClass( 'oo-ui-outlineControlsWidget-movers' )
    		.append( this.removeButton.$element, this.upButton.$element, this.downButton.$element );
    	this.$element.append( this.$icon, this.$group, this.$movers );
    	this.setAbilities( config.abilities || {} );
    };

    /* Setup */

    OO.inheritClass( OO.ui.OutlineControlsWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.OutlineControlsWidget, OO.ui.mixin.GroupElement );
    OO.mixinClass( OO.ui.OutlineControlsWidget, OO.ui.mixin.IconElement );

    /* Events */

    /**
     * @event move
     * @param {number} places Number of places to move
     */

    /**
     * @event remove
     */

    /* Methods */

    /**
     * Set abilities.
     *
     * @param {Object} abilities List of abilties
     * @param {boolean} [abilities.move] Allow moving movable items
     * @param {boolean} [abilities.remove] Allow removing removable items
     */
    OO.ui.OutlineControlsWidget.prototype.setAbilities = function ( abilities ) {
    	var ability;

    	for ( ability in this.abilities ) {
    		if ( abilities[ ability ] !== undefined ) {
    			this.abilities[ ability ] = !!abilities[ ability ];
    		}
    	}

    	this.onOutlineChange();
    };

    /**
     * Handle outline change events.
     *
     * @private
     */
    OO.ui.OutlineControlsWidget.prototype.onOutlineChange = function () {
    	var i, len, firstMovable, lastMovable,
    		items = this.outline.getItems(),
    		selectedItem = this.outline.getSelectedItem(),
    		movable = this.abilities.move && selectedItem && selectedItem.isMovable(),
    		removable = this.abilities.remove && selectedItem && selectedItem.isRemovable();

    	if ( movable ) {
    		i = -1;
    		len = items.length;
    		while ( ++i < len ) {
    			if ( items[ i ].isMovable() ) {
    				firstMovable = items[ i ];
    				break;
    			}
    		}
    		i = len;
    		while ( i-- ) {
    			if ( items[ i ].isMovable() ) {
    				lastMovable = items[ i ];
    				break;
    			}
    		}
    	}
    	this.upButton.setDisabled( !movable || selectedItem === firstMovable );
    	this.downButton.setDisabled( !movable || selectedItem === lastMovable );
    	this.removeButton.setDisabled( !removable );
    };