Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * Container for the entire transclusion dialog sidebar, may contain a single or
3 * multiple templates or raw wikitext snippets.
4 *
5 * @class
6 * @extends OO.ui.Widget
7 *
8 * @constructor
9 * @property {Object.<string,ve.ui.MWTransclusionOutlinePartWidget>} partWidgets Map of top-level
10 * items currently visible in this container, indexed by part id
11 */
12 ve.ui.MWTransclusionOutlineWidget = function VeUiMWTransclusionOutlineWidget() {
13 // Parent constructor
14 ve.ui.MWTransclusionOutlineWidget.super.call( this, {
15 classes: [ 've-ui-mwTransclusionOutlineWidget' ]
16 } );
17
18 // Initialization
19 this.partWidgets = {};
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWTransclusionOutlineWidget, OO.ui.Widget );
25
26 /* Events */
27
28 /**
29 * @event ve.ui.MWTransclusionOutlineWidget#filterPagesByName
30 * @param {Object.<string,boolean>} visibility Keyed by unique id of the {@see OO.ui.BookletLayout}
31 * page, e.g. something like "part_1/param1".
32 */
33
34 /**
35 * Respond to the intent to select a sidebar item
36 *
37 * @event ve.ui.MWTransclusionOutlineWidget#sidebarItemSelected
38 * @param {string} pageName Unique id of the {@see OO.ui.BookletLayout} page, e.g. something like
39 * "part_1" or "part_1/param1".
40 * @param {boolean} [soft] If true, don't focus the content pane. Defaults to false.
41 */
42
43 /* Methods */
44
45 /**
46 * @param {ve.dm.MWTransclusionPartModel|null} removed Removed part
47 * @param {ve.dm.MWTransclusionPartModel|null} added Added part
48 * @param {number} [newPosition]
49 */
50 ve.ui.MWTransclusionOutlineWidget.prototype.onReplacePart = function ( removed, added, newPosition ) {
51 if ( removed ) {
52 this.removePartWidget( removed );
53 }
54 if ( added ) {
55 this.addPartWidget( added, newPosition, removed );
56 }
57 };
58
59 /**
60 * Handle spacebar in a part header
61 *
62 * @param {string} pageName
63 * @fires ve.ui.MWTransclusionOutlineWidget#sidebarItemSelected
64 */
65 ve.ui.MWTransclusionOutlineWidget.prototype.onTransclusionPartSoftSelected = function ( pageName ) {
66 this.emit( 'sidebarItemSelected', pageName, true );
67 };
68
69 /**
70 * @private
71 * @param {ve.dm.MWTransclusionPartModel} part
72 */
73 ve.ui.MWTransclusionOutlineWidget.prototype.removePartWidget = function ( part ) {
74 const id = part.getId();
75 if ( id in this.partWidgets ) {
76 this.partWidgets[ id ]
77 .disconnect( this )
78 .$element.remove();
79 delete this.partWidgets[ id ];
80 }
81 };
82
83 /**
84 * @private
85 * @param {ve.dm.MWTransclusionPartModel} part
86 * @param {number} [newPosition]
87 * @param {ve.dm.MWTransclusionPartModel|null} [removed]
88 * @fires ve.ui.MWTransclusionOutlineWidget#filterPagesByName
89 */
90 ve.ui.MWTransclusionOutlineWidget.prototype.addPartWidget = function ( part, newPosition, removed ) {
91 const keys = Object.keys( this.partWidgets ),
92 onlyPart = keys.length === 1 && this.partWidgets[ keys[ 0 ] ];
93 if ( onlyPart instanceof ve.ui.MWTransclusionOutlineTemplateWidget ) {
94 // To recalculate the height of the sticky header when we enter multi-part mode
95 onlyPart.recalculateStickyHeaderHeight();
96 }
97
98 let widget;
99 if ( part instanceof ve.dm.MWTemplateModel ) {
100 widget = new ve.ui.MWTransclusionOutlineTemplateWidget( part, removed instanceof ve.dm.MWTemplatePlaceholderModel );
101 // This forwards events from the nested ve.ui.MWTransclusionOutlineTemplateWidget upwards.
102 widget.connect( this, {
103 filterParametersById: 'onFilterParametersByName'
104 } );
105 } else if ( part instanceof ve.dm.MWTemplatePlaceholderModel ) {
106 widget = new ve.ui.MWTransclusionOutlinePlaceholderWidget( part );
107 } else if ( part instanceof ve.dm.MWTransclusionContentModel ) {
108 widget = new ve.ui.MWTransclusionOutlineWikitextWidget( part );
109 }
110
111 widget.connect( this, {
112 transclusionPartSoftSelected: 'onTransclusionPartSoftSelected',
113 transclusionOutlineItemSelected: [ 'emit', 'sidebarItemSelected' ]
114 } );
115
116 this.partWidgets[ part.getId() ] = widget;
117 if ( typeof newPosition === 'number' && newPosition < this.$element.children().length ) {
118 this.$element.children().eq( newPosition ).before( widget.$element );
119 } else {
120 this.$element.append( widget.$element );
121 }
122
123 if ( widget instanceof ve.ui.MWTransclusionOutlineTemplateWidget ) {
124 // We can do this only after the widget is visible on screen
125 widget.recalculateStickyHeaderHeight();
126 }
127 };
128
129 ve.ui.MWTransclusionOutlineWidget.prototype.hideAllUnusedParameters = function () {
130 for ( const id in this.partWidgets ) {
131 const partWidget = this.partWidgets[ id ];
132 if ( partWidget instanceof ve.ui.MWTransclusionOutlineTemplateWidget &&
133 partWidget.toggleUnusedWidget
134 ) {
135 partWidget.toggleUnusedWidget.toggleUnusedParameters( false );
136 }
137 }
138 };
139
140 ve.ui.MWTransclusionOutlineWidget.prototype.initializeAllStickyHeaderHeights = function () {
141 for ( const id in this.partWidgets ) {
142 const partWidget = this.partWidgets[ id ];
143 if ( partWidget instanceof ve.ui.MWTransclusionOutlineTemplateWidget ) {
144 partWidget.recalculateStickyHeaderHeight();
145 }
146 }
147 };
148
149 /**
150 * This is inspired by {@see OO.ui.SelectWidget.selectItem}, but isn't one.
151 *
152 * @param {string} [pageName] Symbolic name of page. Omit to remove current selection.
153 */
154 ve.ui.MWTransclusionOutlineWidget.prototype.setSelectionByPageName = function ( pageName ) {
155 const selectedPartId = pageName ? pageName.split( '/', 1 )[ 0 ] : null,
156 isParameter = pageName ? pageName.length > selectedPartId.length : false;
157
158 for ( const partId in this.partWidgets ) {
159 const partWidget = this.partWidgets[ partId ],
160 selected = partId === pageName;
161
162 partWidget.setSelected( selected );
163 if ( selected && !isParameter ) {
164 partWidget.scrollElementIntoView();
165 }
166
167 if ( partWidget instanceof ve.ui.MWTransclusionOutlineTemplateWidget ) {
168 const selectedParamName = ( partId === selectedPartId && isParameter ) ?
169 pageName.slice( selectedPartId.length + 1 ) : null;
170 partWidget.setParameter( selectedParamName );
171 }
172 }
173 };
174
175 /**
176 * @param {string} pageName
177 * @param {boolean} hasValue
178 */
179 ve.ui.MWTransclusionOutlineWidget.prototype.toggleHasValueByPageName = function ( pageName, hasValue ) {
180 const idParts = pageName.split( '/', 2 ),
181 templatePartWidget = this.partWidgets[ idParts[ 0 ] ];
182
183 templatePartWidget.toggleHasValue( idParts[ 1 ], hasValue );
184 };
185
186 /**
187 * Checks if the provided DOM element belongs to the DOM structure of one of the top-level
188 * {@see ve.ui.MWTransclusionOutlinePartWidget}s, and returns its id. Useful for e.g. mouse click or
189 * keyboard handlers.
190 *
191 * @param {HTMLElement} element
192 * @return {string|undefined} Always a top-level part id, e.g. "part_0"
193 */
194 ve.ui.MWTransclusionOutlineWidget.prototype.findPartIdContainingElement = function ( element ) {
195 if ( element ) {
196 for ( const id in this.partWidgets ) {
197 const part = this.partWidgets[ id ];
198 if ( $.contains( part.$element[ 0 ], element ) ) {
199 return id;
200 }
201 }
202 }
203 };
204
205 /**
206 * Removes all {@see ve.ui.MWTransclusionOutlinePartWidget}, i.e. empties the list.
207 */
208 ve.ui.MWTransclusionOutlineWidget.prototype.clear = function () {
209 for ( const id in this.partWidgets ) {
210 this.partWidgets[ id ]
211 .disconnect( this )
212 .$element.remove();
213 }
214 this.partWidgets = {};
215 };
216
217 /**
218 * @private
219 * @param {Object.<string,boolean>} visibility
220 * @fires ve.ui.MWTransclusionOutlineWidget#filterPagesByName
221 */
222 ve.ui.MWTransclusionOutlineWidget.prototype.onFilterParametersByName = function ( visibility ) {
223 this.emit( 'filterPagesByName', visibility );
224 this.setSelectionByPageName();
225 };