Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWTransclusionOutlinePartWidget.js

raw symbols SCIP
1 /**
2 * Common base class for top-level items (a.k.a. "parts") in the template editor sidebar. Subclasses
3 * should exist for all subclasses of {@see ve.dm.MWTransclusionPartModel}:
4 * - {@see ve.dm.MWTemplateModel}
5 * - {@see ve.dm.MWTemplatePlaceholderModel}
6 * - {@see ve.dm.MWTransclusionContentModel}
7 *
8 * This is inspired by and meant to replace {@see OO.ui.DecoratedOptionWidget} in the context of the
9 * template dialog. Also see {@see OO.ui.ButtonWidget} for inspiration.
10 *
11 * @abstract
12 * @class
13 * @extends OO.ui.Widget
14 *
15 * @constructor
16 * @param {ve.dm.MWTransclusionPartModel} part
17 * @param {Object} config
18 * @param {string} [config.icon=''] Symbolic name of an icon, e.g. "puzzle" or "wikiText"
19 * @param {string} config.label
20 * @param {string} config.ariaDescriptionUnselected
21 * @param {string} config.ariaDescriptionSelected
22 * @param {string} config.ariaDescriptionSelectedSingle
23 */
24 ve.ui.MWTransclusionOutlinePartWidget = function VeUiMWTransclusionOutlinePartWidget( part, config ) {
25 this.part = part;
26
27 // Parent constructor
28 ve.ui.MWTransclusionOutlinePartWidget.super.call( this, ve.extendObject( config, {
29 classes: [ 've-ui-mwTransclusionOutlinePartWidget' ],
30 data: part.getId()
31 } ) );
32
33 this.header = new ve.ui.MWTransclusionOutlineButtonWidget( config )
34 .connect( this, {
35 keyPressed: 'onHeaderKeyPressed',
36 // The array syntax is a way to call `this.emit( 'transclusionOutlineItemSelected', … )`.
37 click: [ 'emit', 'transclusionOutlineItemSelected', part.getId() ]
38 } );
39
40 if ( config.ariaDescriptionUnselected ) {
41 this.$ariaDescriptionUnselected = $( '<span>' )
42 .text( config.ariaDescriptionUnselected )
43 .addClass( 've-ui-mwTransclusionOutline-ariaHidden' );
44
45 this.$ariaDescriptionSelected = $( '<span>' )
46 .text( config.ariaDescriptionSelected )
47 .addClass( 've-ui-mwTransclusionOutline-ariaHidden' );
48
49 this.$ariaDescriptionSelectedSingle = $( '<span>' )
50 .text( config.ariaDescriptionSelectedSingle )
51 .addClass( 've-ui-mwTransclusionOutline-ariaHidden' );
52
53 this.header
54 .setAriaDescribedBy( this.$ariaDescriptionUnselected )
55 .$element.prepend(
56 this.$ariaDescriptionUnselected,
57 this.$ariaDescriptionSelected,
58 this.$ariaDescriptionSelectedSingle
59 );
60 }
61
62 this.transclusionModel = this.part.getTransclusion().connect( this, {
63 replace: 'updateButtonAriaDescription'
64 } );
65
66 this.$element.append( this.header.$element );
67 };
68
69 /* Inheritance */
70
71 OO.inheritClass( ve.ui.MWTransclusionOutlinePartWidget, OO.ui.Widget );
72
73 /* Events */
74
75 /**
76 * "Soft" selection with space.
77 *
78 * @event ve.ui.MWTransclusionOutlinePartWidget#transclusionPartSoftSelected
79 * @param {string} partId Unique id of the {@see ve.dm.MWTransclusionPartModel}, e.g. something like
80 * "part_1".
81 */
82
83 /**
84 * Triggered when the user interacts with any sidebar element in a meaningful way, and that should
85 * be reflected in the content pane of the dialog. This includes e.g. selecting something that was
86 * already selected.
87 *
88 * @event ve.ui.MWTransclusionOutlinePartWidget#transclusionOutlineItemSelected
89 * @param {string} pageName Unique id of the {@see OO.ui.BookletLayout} page, e.g. something like
90 * "part_1" or "part_1/param1".
91 * @param {boolean} [soft] If true, focus should stay in the sidebar. Defaults to false.
92 */
93
94 /* Methods */
95
96 /**
97 * @private
98 * @param {number} key
99 * @fires ve.ui.MWTransclusionOutlinePartWidget#transclusionPartSoftSelected
100 */
101 ve.ui.MWTransclusionOutlinePartWidget.prototype.onHeaderKeyPressed = function ( key ) {
102 if ( key === OO.ui.Keys.SPACE ) {
103 this.emit( 'transclusionPartSoftSelected', this.getData() );
104 }
105 };
106
107 /**
108 * Convenience method, modelled after {@see OO.ui.OptionWidget}, but this isn't one.
109 *
110 * @return {boolean}
111 */
112 ve.ui.MWTransclusionOutlinePartWidget.prototype.isSelected = function () {
113 return this.header.isSelected();
114 };
115
116 /**
117 * Convenience method, modelled after {@see OO.ui.OptionWidget}, but this isn't one.
118 *
119 * @param {boolean} state
120 */
121 ve.ui.MWTransclusionOutlinePartWidget.prototype.setSelected = function ( state ) {
122 if ( state !== this.isSelected() ) {
123 this.updateButtonAriaDescription( state );
124 this.header
125 .setSelected( state )
126 .setFlags( { progressive: state } );
127 }
128 };
129
130 /**
131 * @private
132 * @param {boolean} state
133 */
134 ve.ui.MWTransclusionOutlinePartWidget.prototype.updateButtonAriaDescription = function ( state ) {
135 this.header.setAriaDescribedBy( !state ? this.$ariaDescriptionUnselected :
136 ( this.transclusionModel.isSingleTemplate() ? this.$ariaDescriptionSelectedSingle : this.$ariaDescriptionSelected )
137 );
138 };