Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contextitems/ve.ui.LinearContextItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LinearContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Item in a context.
9 *
10 * @class
11 * @extends ve.ui.ContextItem
12 * @mixes OO.ui.mixin.PendingElement
13 *
14 * @constructor
15 * @param {ve.ui.LinearContext} context Context the item is in
16 * @param {ve.dm.Model} [model] Model the item is related to
17 * @param {Object} [config] Configuration options
18 */
19 ve.ui.LinearContextItem = function VeUiLinearContextItem( context, model, config ) {
20 config = config || {};
21
22 // Parent constructor
23 ve.ui.LinearContextItem.super.apply( this, arguments );
24
25 // Mixin constructors
26 OO.ui.mixin.PendingElement.call( this, config );
27
28 // Properties
29 this.$head = $( '<div>' );
30 this.$title = $( '<div>' );
31 this.$actions = $( '<div>' );
32 this.$body = $( '<div>' );
33 // Don't use mixins as they expect the icon and label to be children of this.$element.
34 this.icon = new OO.ui.IconWidget( { icon: config.icon || this.constructor.static.icon } );
35 this.label = new OO.ui.LabelWidget( { label: config.label || this.constructor.static.label } );
36 this.actionButtons = new OO.ui.ButtonGroupWidget();
37
38 if ( this.context.isMobile() ) {
39 this.editButton = new OO.ui.ButtonWidget( {
40 framed: false,
41 label: ve.msg( this.isReadOnly() ? 'visualeditor-contextitemwidget-label-view' : 'visualeditor-contextitemwidget-label-secondary' ),
42 invisibleLabel: true,
43 icon: this.isReadOnly() ? 'eye' : 'edit',
44 flags: [ 'progressive' ]
45 } );
46 this.$foot = $( '<div>' );
47 this.$bodyAction = $( '<div>' );
48 } else {
49 // Desktop
50 this.editButton = new OO.ui.ButtonWidget( {
51 label: ve.msg( this.isReadOnly() ? 'visualeditor-contextitemwidget-label-view' : 'visualeditor-contextitemwidget-label-secondary' ),
52 flags: [ 'progressive' ]
53 } );
54 }
55 if ( this.isEditable() ) {
56 this.actionButtons.addItems( [ this.editButton ] );
57 }
58
59 // Events
60 this.editButton.connect( this, { click: 'onEditButtonClick' } );
61
62 // Initialization
63 this.$title
64 .addClass( 've-ui-linearContextItem-title' )
65 .append( this.icon.$element, this.label.$element );
66 this.$actions
67 .addClass( 've-ui-linearContextItem-actions' )
68 .append( this.actionButtons.$element );
69 this.$head
70 .addClass( 've-ui-linearContextItem-head' )
71 .append( this.$title, this.$actions );
72 this.$body.addClass( 've-ui-linearContextItem-body' );
73 this.$element
74 .addClass( 've-ui-linearContextItem' )
75 .append( this.$head, this.$body );
76
77 if ( this.context.isMobile() ) {
78 this.$foot.addClass( 've-ui-linearContextItem-foot' );
79 this.$bodyAction.addClass( 've-ui-linearContextItem-body-action' ).append( this.$body, this.$actions );
80 this.$element.append(
81 this.$head,
82 $( '<div>' ).addClass( 've-ui-linearContextItem-body-action-wrapper' ).append( this.$bodyAction ),
83 this.$foot
84 );
85 }
86 };
87
88 /* Inheritance */
89
90 OO.inheritClass( ve.ui.LinearContextItem, ve.ui.ContextItem );
91 OO.mixinClass( ve.ui.ContextItem, OO.ui.mixin.PendingElement );
92
93 /* Static Properties */
94
95 ve.ui.LinearContextItem.static.editable = true;
96
97 /**
98 * Whether the context item should try (if space permits) to go inside the node,
99 * rather than below with an arrow
100 *
101 * @static
102 * @property {boolean}
103 * @inheritable
104 */
105 ve.ui.LinearContextItem.static.embeddable = true;
106
107 /* Methods */
108
109 /**
110 * Handle edit button click events.
111 *
112 * @localdoc Executes the command related to #static-commandName on the context's surface
113 *
114 * @protected
115 * @fires ve.ui.ContextItem#command
116 */
117 ve.ui.LinearContextItem.prototype.onEditButtonClick = function () {
118 const command = this.getCommand();
119
120 if ( command ) {
121 command.execute( this.context.getSurface(), undefined, 'context' );
122 this.emit( 'command' );
123 }
124 };
125
126 /**
127 * Check if item is editable.
128 *
129 * @return {boolean} Item is editable
130 */
131 ve.ui.LinearContextItem.prototype.isEditable = function () {
132 return this.constructor.static.editable && ( !this.model || this.model.isEditable() );
133 };
134
135 /**
136 * Get the description.
137 *
138 * @localdoc Override for custom description content
139 * @return {string} Item description
140 */
141 ve.ui.LinearContextItem.prototype.getDescription = function () {
142 return '';
143 };
144
145 ve.ui.LinearContextItem.prototype.setIcon = function ( icon ) {
146 return this.icon.setIcon( icon );
147 };
148
149 ve.ui.LinearContextItem.prototype.setLabel = function ( label ) {
150 return this.label.setLabel( label );
151 };
152
153 /**
154 * Render the body.
155 *
156 * @localdoc Renders the result of #getDescription, override for custom body rendering
157 */
158 ve.ui.LinearContextItem.prototype.renderBody = function () {
159 this.$body.text( this.getDescription() );
160 };
161
162 /**
163 * @inheritdoc
164 */
165 ve.ui.LinearContextItem.prototype.setup = function ( refreshing ) {
166 this.renderBody();
167
168 const isEmpty = this.$body.is( ':empty' );
169 if ( isEmpty && this.context.isMobile() ) {
170 if ( this.isEditable() ) {
171 this.$head.append( this.editButton.$element );
172 }
173 }
174 this.$element.toggleClass( 've-ui-linearContextItem-empty', isEmpty );
175
176 if ( !refreshing ) {
177 // Re-showing a context for some reason (probably a selection-change
178 // within the context on a persistent item) shouldn't re-fire this.
179 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-show' } );
180 }
181
182 return this;
183 };
184
185 /**
186 * @inheritdoc
187 */
188 ve.ui.LinearContextItem.prototype.teardown = function () {
189 this.$body.empty();
190 return this;
191 };