Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/pages/ve.ui.MWTemplatePlaceholderPage.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWTemplatePlaceholderPage class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * The placeholder is shown in the template dialog content pane, and allows the
10 * user to enter a template name. Once a name is chosen, the placeholder is
11 * replaced with elements for the concrete template.
12 *
13 * @class
14 * @extends OO.ui.PageLayout
15 *
16 * @constructor
17 * @param {ve.dm.MWTemplatePlaceholderModel} placeholder Template placeholder
18 * @param {string} name Unique symbolic name of page
19 * @param {Object} [config] Configuration options
20 * @param {jQuery} [config.$overlay] Overlay to render dropdowns in
21 */
22 ve.ui.MWTemplatePlaceholderPage = function VeUiMWTemplatePlaceholderPage( placeholder, name, config ) {
23 // Configuration initialization
24 config = ve.extendObject( {
25 scrollable: false
26 }, config );
27
28 // Parent constructor
29 ve.ui.MWTemplatePlaceholderPage.super.call( this, name, config );
30
31 // Properties
32 this.placeholder = placeholder;
33
34 this.addTemplateInput = new ve.ui.MWTemplateTitleInputWidget( {
35 $overlay: config.$overlay,
36 showDescriptions: true,
37 api: ve.init.target.getContentApi()
38 } )
39 .connect( this, {
40 change: 'onTemplateInputChange',
41 enter: 'onAddTemplate'
42 } );
43
44 this.addTemplateInput.getLookupMenu().connect( this, {
45 choose: 'onAddTemplate'
46 } );
47
48 this.addTemplateButton = new OO.ui.ButtonWidget( {
49 label: ve.msg( 'visualeditor-dialog-transclusion-add-template-save' ),
50 flags: [ 'progressive' ],
51 classes: [ 've-ui-mwTransclusionDialog-addButton' ],
52 disabled: true
53 } )
54 .connect( this, { click: 'onAddTemplate' } );
55
56 const addTemplateActionFieldLayout = new OO.ui.ActionFieldLayout(
57 this.addTemplateInput,
58 this.addTemplateButton,
59 {
60 label: ve.msg( 'visualeditor-dialog-transclusion-template-search-help' ),
61 align: 'top'
62 }
63 );
64
65 const dialogTitle = this.placeholder.getTransclusion().isSingleTemplate() ?
66 'visualeditor-dialog-transclusion-template-search' :
67 'visualeditor-dialog-transclusion-add-template';
68
69 const addTemplateFieldsetConfig = {
70 // The following messages are used here:
71 // * visualeditor-dialog-transclusion-template-search
72 // * visualeditor-dialog-transclusion-add-template
73 label: ve.msg( dialogTitle ),
74 icon: 'puzzle',
75 classes: [ 've-ui-mwTransclusionDialog-addTemplateFieldset' ],
76 items: [ addTemplateActionFieldLayout ]
77 };
78
79 this.addTemplateFieldset = new OO.ui.FieldsetLayout( addTemplateFieldsetConfig );
80
81 // Initialization
82 this.$element
83 .addClass( 've-ui-mwTemplatePlaceholderPage' )
84 .append( this.addTemplateFieldset.$element );
85 };
86
87 /* Inheritance */
88
89 OO.inheritClass( ve.ui.MWTemplatePlaceholderPage, OO.ui.PageLayout );
90
91 /* Methods */
92
93 /**
94 * @inheritDoc OO.ui.PanelLayout
95 */
96 ve.ui.MWTemplatePlaceholderPage.prototype.focus = function () {
97 // The parent method would focus the first element, which might be the message widget
98 this.addTemplateInput.focus();
99
100 // HACK: Set the width of the lookupMenu to the width of the input
101 // TODO: This should be handled upstream in OOUI
102 this.addTemplateInput.lookupMenu.width = this.addTemplateInput.$input[ 0 ].clientWidth;
103 };
104
105 /**
106 * @private
107 */
108 ve.ui.MWTemplatePlaceholderPage.prototype.onAddTemplate = function () {
109 const transclusion = this.placeholder.getTransclusion(),
110 menu = this.addTemplateInput.getLookupMenu();
111
112 if ( menu.isVisible() ) {
113 menu.chooseItem( menu.findSelectedItem() );
114 }
115 const name = this.addTemplateInput.getMWTitle();
116 if ( !name ) {
117 // Invalid titles return null, so abort here.
118 return;
119 }
120
121 // TODO tracking will only be implemented temporarily to answer questions on
122 // template usage for the Technical Wishes topic area see T258917
123 const event = {
124 action: 'add-template',
125 // eslint-disable-next-line camelcase
126 template_names: [ name.getPrefixedText() ]
127 };
128 const editCountBucket = mw.config.get( 'wgUserEditCountBucket' );
129 if ( editCountBucket !== null ) {
130 // eslint-disable-next-line camelcase
131 event.user_edit_count_bucket = editCountBucket;
132 }
133 mw.track( 'event.VisualEditorTemplateDialogUse', event );
134
135 const part = ve.dm.MWTemplateModel.newFromName( transclusion, name );
136 transclusion.replacePart( this.placeholder, part ).then(
137 transclusion.addPromptedParameters.bind( transclusion )
138 );
139 this.addTemplateInput.pushPending();
140 // abort pending lookups, also, so the menu can't appear after we've left the page
141 this.addTemplateInput.closeLookupMenu();
142 this.addTemplateButton.setDisabled( true );
143 };
144
145 /**
146 * @private
147 */
148 ve.ui.MWTemplatePlaceholderPage.prototype.onTemplateInputChange = function () {
149 this.addTemplateButton.setDisabled( this.addTemplateInput.getMWTitle() === null );
150 };