All files / src/ui/contextitems ve.ui.MobileActionsContextItem.js

27.77% Statements 10/36
8.33% Branches 1/12
25% Functions 1/4
27.77% Lines 10/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      1x                                                             1x       1x   1x     1x         1x 24x                   1x                                                               1x                               1x  
/*!
 * VisualEditor MobileActionsContextItem class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Context item to show extra actions required on mobile.
 *
 * These actions allow users to do things that are not possible with
 * a virtual keyboard and a fake selection, specifically copy & delete.
 *
 * @class
 * @extends ve.ui.LinearContextItem
 *
 * @param {ve.ui.LinearContext} context Context the item is in
 * @param {ve.dm.Model} model Model the item is related to
 * @param {Object} [config] Configuration options
 */
ve.ui.MobileActionsContextItem = function VeUiMobileActionsContextItem( context, model, config ) {
	// Parent constructor
	ve.ui.MobileActionsContextItem.super.call( this, context, model, config );
 
	this.copyButton = new OO.ui.ButtonWidget( {
		framed: false,
		label: ve.msg( 'visualeditor-clipboard-copy' ),
		icon: 'copy'
	} );
	this.deleteButton = new OO.ui.ButtonWidget( {
		framed: false,
		label: ve.msg( 'visualeditor-contextitemwidget-label-remove' ),
		icon: 'trash',
		flags: [ 'destructive' ]
	} );
 
	this.$head.append( this.copyButton.$element );
	if ( !this.isReadOnly() ) {
		this.$head.append( this.deleteButton.$element );
	}
 
	// Events
	this.copyButton.connect( this, { click: 'onCopyButtonClick' } );
	this.deleteButton.connect( this, { click: 'onDeleteButtonClick' } );
 
	// Initialization
	this.$element.addClass( 've-ui-mobileActionsContextItem' );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.MobileActionsContextItem, ve.ui.LinearContextItem );
 
/* Static Properties */
 
ve.ui.MobileActionsContextItem.static.name = 'mobileActions';
 
ve.ui.MobileActionsContextItem.static.editable = false;
 
// Show this context last
ve.ui.MobileActionsContextItem.static.sortOrder = 1;
 
/**
 * @inheritdoc
 */
ve.ui.MobileActionsContextItem.static.isCompatibleWith = function ( model ) {
	return OO.ui.isMobile() && model instanceof ve.dm.Node && (
		model.isFocusable() || model.isCellable()
	);
};
 
/* Methods */
 
/**
 * Handle copy button click events.
 */
ve.ui.MobileActionsContextItem.prototype.onCopyButtonClick = function () {
	var surfaceView = this.context.getSurface().getView();
 
	surfaceView.activate();
	// Force a native selection on mobile
	surfaceView.preparePasteTargetForCopy( true );
 
	var copied;
	try {
		copied = document.execCommand( 'copy' );
	} catch ( e ) {
		copied = false;
	}
 
	ve.init.platform.notify( ve.msg( copied ? 'visualeditor-clipboard-copy-success' : 'visualeditor-clipboard-copy-fail' ) );
 
	// Restore normal selection for device type
	surfaceView.preparePasteTargetForCopy();
	if ( OO.ui.isMobile() ) {
		// Support: Mobile Safari
		// Force remove the selection to hide the keyboard
		document.activeElement.blur();
	}
 
	ve.track( 'activity.' + this.constructor.static.name, { action: 'context-copy' } );
};
 
/**
 * Handle delete button click events.
 *
 * @fires ve.ui.ContextItem#command
 */
ve.ui.MobileActionsContextItem.prototype.onDeleteButtonClick = function () {
	var surface = this.context.getSurface();
	var command = surface.commandRegistry.lookup( 'backspace' );
 
	// Use the 'backspace' command as this triggers the KeyDownHandler for the
	// current selection, e.g. ve.ce.TableDeleteKeyDownHandler will be used to
	// clear table cells for TableSelection's.
	if ( command ) {
		command.execute( surface, undefined, 'context' );
		this.emit( 'command' );
		ve.track( 'activity.' + this.constructor.static.name, { action: 'context-delete' } );
	}
};
 
/* Registration */
 
ve.ui.contextItemFactory.register( ve.ui.MobileActionsContextItem );