all files / src/ui/contextitems/ ve.ui.LinearContextItem.js

65.48% Statements 55/84
56.76% Branches 21/37
58.33% Functions 7/12
65.48% Lines 55/84
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                                    22×   22×     22×     22×     22× 22× 22× 22×   22× 22× 22×   22×                                                         22×       22×       22×     22× 22×       22× 22×     22×     22×     22×     22× 22×       22×                                                                                                                               22×               21×                                           10×           21×   21×   21× 21×                   21×   21×   21×           20× 20×    
/*!
 * VisualEditor UserInterface LinearContextItem class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Item in a context.
 *
 * @class
 * @extends ve.ui.ContextItem
 * @mixins OO.ui.mixin.PendingElement
 *
 * @constructor
 * @param {ve.ui.Context} context Context item is in
 * @param {ve.dm.Model} [model] Model item is related to
 * @param {Object} [config] Configuration options
 */
ve.ui.LinearContextItem = function VeUiLinearContextItem( context, model, config ) {
	var contextItem = this;
 
	config = config || {};
 
	// Parent constructor
	ve.ui.LinearContextItem.super.apply( this, arguments );
 
	// Mixin constructors
	OO.ui.mixin.PendingElement.call( this, config );
 
	// Properties
	this.$head = $( '<div>' );
	this.$title = $( '<div>' );
	this.$actions = $( '<div>' );
	this.$body = $( '<div>' );
	// Don't use mixins as they expect the icon and label to be children of this.$element.
	this.icon = new OO.ui.IconWidget( { icon: config.icon || this.constructor.static.icon } );
	this.label = new OO.ui.LabelWidget( { label: config.label || this.constructor.static.label } );
	this.actionButtons = new OO.ui.ButtonGroupWidget();
 
	Iif ( this.context.isMobile() ) {
		this.closeButton = new OO.ui.ButtonWidget( {
			classes: [ 've-ui-linearContextItem-close' ],
			framed: false,
			icon: 'close'
		} );
		this.editButton = new OO.ui.ButtonWidget( {
			framed: false,
			icon: this.isReadOnly() ? 'eye' : 'edit',
			flags: [ 'progressive' ]
		} );
		this.deleteButton = new OO.ui.ButtonWidget( {
			framed: false,
			label: ve.msg( 'visualeditor-contextitemwidget-label-remove' ),
			icon: 'trash',
			flags: [ 'destructive' ]
		} );
		this.$foot = $( '<div>' );
		this.$bodyAction = $( '<div>' );
		if ( this.isDeletable() ) {
			this.$foot.append( this.deleteButton.$element );
		}
		this.closeButton.on( 'click', function () {
			context.toggleMenu( false );
			context.toggle( false );
			ve.track( 'activity.' + contextItem.constructor.static.name, { action: 'context-close' } );
		} );
	} else {
		// Desktop
		this.editButton = new OO.ui.ButtonWidget( {
			label: ve.msg( this.isReadOnly() ? 'visualeditor-contextitemwidget-label-view' : 'visualeditor-contextitemwidget-label-secondary' ),
			flags: [ 'progressive' ]
		} );
		this.deleteButton = new OO.ui.ButtonWidget( {
			label: ve.msg( 'visualeditor-contextitemwidget-label-remove' ),
			flags: [ 'destructive' ]
		} );
		if ( this.isDeletable() ) {
			this.actionButtons.addItems( [ this.deleteButton ] );
		}
	}
	Eif ( this.isEditable() ) {
		this.actionButtons.addItems( [ this.editButton ] );
	}
 
	// Events
	this.editButton.connect( this, { click: 'onEditButtonClick' } );
	this.deleteButton.connect( this, { click: 'onDeleteButtonClick' } );
 
	// Initialization
	this.$title
		.addClass( 've-ui-linearContextItem-title' )
		.append( this.icon.$element, this.label.$element );
	this.$actions
		.addClass( 've-ui-linearContextItem-actions' )
		.append( this.actionButtons.$element );
	this.$head
		.addClass( 've-ui-linearContextItem-head' )
		.append( this.$title, this.$actions );
	this.$body.addClass( 've-ui-linearContextItem-body' );
	this.$element
		.addClass( 've-ui-linearContextItem' )
		.append( this.$head, this.$body );
 
	Iif ( this.context.isMobile() ) {
		this.$foot.addClass( 've-ui-linearContextItem-foot' );
		this.$head.append( this.closeButton.$element );
		this.$bodyAction.addClass( 've-ui-linearContextItem-body-action' ).append( this.$body, this.$actions );
		this.$element.append(
			this.$head,
			$( '<div>' ).addClass( 've-ui-linearContextItem-body-action-wrapper' ).append( this.$bodyAction ),
			this.$foot
		);
	}
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LinearContextItem, ve.ui.ContextItem );
OO.mixinClass( ve.ui.ContextItem, OO.ui.mixin.PendingElement );
 
/* Events */
 
/**
 * @event command
 */
 
/* Static Properties */
 
ve.ui.LinearContextItem.static.editable = true;
 
ve.ui.LinearContextItem.static.deletable = true;
 
/**
 * Whether the context item should try (if space permits) to go inside the node,
 * rather than below with an arrow
 *
 * @static
 * @property {boolean}
 * @inheritable
 */
ve.ui.LinearContextItem.static.embeddable = true;
 
/* Methods */
 
/**
 * Handle edit button click events.
 *
 * @localdoc Executes the command related to #static-commandName on the context's surface
 *
 * @protected
 */
ve.ui.LinearContextItem.prototype.onEditButtonClick = function () {
	var command = this.getCommand();
 
	if ( command ) {
		command.execute( this.context.getSurface(), undefined, 'context' );
		this.emit( 'command' );
	}
};
 
/**
 * Handle delete button click events.
 */
ve.ui.LinearContextItem.prototype.onDeleteButtonClick = function () {
	this.getFragment().removeContent();
 
	ve.track( 'activity.' + this.constructor.static.name, { action: 'context-delete' } );
};
 
/**
 * Check if item is editable.
 *
 * @return {boolean} Item is editable
 */
ve.ui.LinearContextItem.prototype.isEditable = function () {
	return this.constructor.static.editable && ( !this.model || this.model.isEditable() );
};
 
/**
 * Check if item is deletable.
 *
 * @return {boolean} Item is deletable
 */
ve.ui.LinearContextItem.prototype.isDeletable = function () {
	return this.constructor.static.deletable && this.isNode() && this.context.showDeleteButton() && !this.isReadOnly();
};
 
/**
 * Get the description.
 *
 * @localdoc Override for custom description content
 * @return {string} Item description
 */
ve.ui.LinearContextItem.prototype.getDescription = function () {
	return '';
};
 
ve.ui.LinearContextItem.prototype.setIcon = function ( icon ) {
	return this.icon.setIcon( icon );
};
 
ve.ui.LinearContextItem.prototype.setLabel = function ( label ) {
	return this.label.setLabel( label );
};
 
/**
 * Render the body.
 *
 * @localdoc Renders the result of #getDescription, override for custom body rendering
 */
ve.ui.LinearContextItem.prototype.renderBody = function () {
	this.$body.text( this.getDescription() );
};
 
/**
 * @inheritdoc
 */
ve.ui.LinearContextItem.prototype.setup = function () {
	var isEmpty;
 
	this.renderBody();
 
	isEmpty = this.$body.is( ':empty' );
	Iif ( isEmpty && this.context.isMobile() ) {
		this.deleteButton.setLabel( null );
		if ( this.isDeletable() ) {
			this.$head.append( this.deleteButton.$element );
		}
		if ( this.isEditable() ) {
			this.$head.append( this.editButton.$element );
		}
		this.closeButton.$element.remove();
	}
	this.$element.toggleClass( 've-ui-linearContextItem-empty', isEmpty );
 
	ve.track( 'activity.' + this.constructor.static.name, { action: 'context-show' } );
 
	return this;
};
 
/**
 * @inheritdoc
 */
ve.ui.LinearContextItem.prototype.teardown = function () {
	this.$body.empty();
	return this;
};