Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWCategoryPopupWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Creates an ve.ui.MWCategoryPopupWidget object.
10 *
11 * @class
12 * @extends OO.ui.PopupWidget
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWCategoryPopupWidget = function VeUiMWCategoryPopupWidget( config ) {
18 // Configuration initialization
19 config = ve.extendObject( { autoClose: true }, config );
20
21 // Parent constructor
22 ve.ui.MWCategoryPopupWidget.super.call( this, config );
23
24 // Properties
25 this.category = null;
26 this.origSortkey = null;
27 this.removed = false;
28 this.$title = $( '<label>' );
29 this.$menu = $( '<div>' );
30 this.fallbackSortKey = ve.init.target.getPageName();
31 this.removeButton = new OO.ui.ButtonWidget( {
32 framed: false,
33 icon: 'trash',
34 title: ve.msg( 'visualeditor-inspector-remove-tooltip' )
35 } );
36 this.sortKeyInput = new OO.ui.TextInputWidget();
37 this.sortKeyField = new OO.ui.FieldLayout( this.sortKeyInput, {
38 align: 'top',
39 label: ve.msg( 'visualeditor-dialog-meta-categories-sortkey-label' )
40 } );
41 this.$sortKeyForm = $( '<form>' ).addClass( 've-ui-mwCategoryPopupWidget-sortKeyForm' )
42 .append( this.sortKeyField.$element );
43
44 // Events
45 this.connect( this, { toggle: 'onToggle' } );
46 this.removeButton.connect( this, { click: 'onRemoveCategory' } );
47 this.$sortKeyForm.on( 'submit', this.onSortKeySubmit.bind( this ) );
48
49 // Initialization
50 this.$element
51 .addClass( 've-ui-mwCategoryPopupWidget' );
52 this.toggle( false );
53 this.$title
54 .addClass( 've-ui-mwCategoryPopupWidget-title oo-ui-icon-tag' )
55 .text( ve.msg( 'visualeditor-dialog-meta-categories-category' ) );
56 this.$hiddenStatus = $( '<div>' );
57 this.$menu
58 .addClass( 've-ui-mwCategoryPopupWidget-content' )
59 .append(
60 this.$title,
61 this.$hiddenStatus,
62 this.removeButton.$element.addClass( 've-ui-mwCategoryPopupWidget-removeButton' ),
63 this.$sortKeyForm
64 );
65 this.$body.append( this.$menu );
66 };
67
68 /* Inheritance */
69
70 OO.inheritClass( ve.ui.MWCategoryPopupWidget, OO.ui.PopupWidget );
71
72 /* Events */
73
74 /**
75 * @event ve.ui.MWCategoryPopupWidget#removeCategory
76 * @param {string} category Category name
77 */
78
79 /**
80 * @event ve.ui.MWCategoryPopupWidget#updateSortkey
81 * @param {string} category Category name
82 * @param {string} sortkey New sortkey
83 */
84
85 /* Methods */
86
87 /**
88 * Handle category remove events.
89 *
90 * @fires ve.ui.MWCategoryPopupWidget#removeCategory
91 */
92 ve.ui.MWCategoryPopupWidget.prototype.onRemoveCategory = function () {
93 this.removed = true;
94 this.emit( 'removeCategory', this.category );
95 this.closePopup();
96 };
97
98 /**
99 * Handle sort key form submit events.
100 *
101 * @param {jQuery.Event} e Form submit event
102 * @return {boolean}
103 * @fires ve.ui.MWCategoryPopupWidget#updateSortkey
104 */
105 ve.ui.MWCategoryPopupWidget.prototype.onSortKeySubmit = function () {
106 this.closePopup();
107 return false;
108 };
109
110 /**
111 * Open a category item popup.
112 *
113 * @param {ve.ui.MWCategoryItemWidget} item Category item
114 */
115 ve.ui.MWCategoryPopupWidget.prototype.openPopup = function ( item ) {
116 this.toggle( true );
117 this.popupOpen = true;
118 this.category = item.value;
119 this.loadCategoryIntoPopup( item );
120 this.setPopup( item );
121 };
122
123 /**
124 * Handle popup toggle events.
125 *
126 * @param {boolean} show Widget is being made visible
127 * @fires ve.ui.MWCategoryPopupWidget#updateSortkey
128 */
129 ve.ui.MWCategoryPopupWidget.prototype.onToggle = function ( show ) {
130 if ( show ) {
131 return;
132 }
133 const newSortkey = this.sortKeyInput.$input.val();
134 if ( !this.removed && newSortkey !== ( this.origSortkey || '' ) ) {
135 if ( newSortkey === this.fallbackSortKey ) {
136 this.emit( 'updateSortkey', this.category, '' );
137 } else {
138 this.emit( 'updateSortkey', this.category, this.sortKeyInput.$input.val() );
139 }
140 }
141 };
142
143 /**
144 * Load item information into the popup.
145 *
146 * @param {ve.ui.MWCategoryItemWidget} item Category item
147 */
148 ve.ui.MWCategoryPopupWidget.prototype.loadCategoryIntoPopup = function ( item ) {
149 this.origSortkey = item.sortKey || this.fallbackSortKey;
150 if ( item.isHidden ) {
151 this.$hiddenStatus.text( ve.msg( 'visualeditor-dialog-meta-categories-hidden' ) );
152 } else if ( item.isMissing ) {
153 this.$hiddenStatus.text( ve.msg( 'visualeditor-dialog-meta-categories-missing' ) );
154 } else {
155 this.$hiddenStatus.empty();
156 }
157 this.sortKeyInput.$input.val( this.origSortkey );
158 };
159
160 /**
161 * Close the popup.
162 */
163 ve.ui.MWCategoryPopupWidget.prototype.closePopup = function () {
164 this.toggle( false );
165 this.popupOpen = false;
166 this.category = null;
167 this.origSortkey = null;
168 this.removed = false;
169 };
170
171 /**
172 * Set the default sort key.
173 *
174 * @param {string} value Default sort key value
175 */
176 ve.ui.MWCategoryPopupWidget.prototype.setDefaultSortKey = function ( value ) {
177 this.fallbackSortKey = value;
178 };
179
180 /**
181 * Display the popup next to an item.
182 *
183 * @param {ve.ui.MWCategoryItemWidget} item Category item
184 */
185 ve.ui.MWCategoryPopupWidget.prototype.setPopup = function ( item ) {
186 this.setFloatableContainer( item.$element );
187 this.updateDimensions();
188 };