3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2017 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2017-09-05T21:23:58Z
16 * Namespace for all classes, static methods and static properties.
48 * Constants for MouseEvent.which
52 OO
.ui
.MouseButtons
= {
65 * Generate a unique ID for element
69 OO
.ui
.generateElementId = function () {
71 return 'oojsui-' + OO
.ui
.elementId
;
75 * Check if an element is focusable.
76 * Inspired by :focusable in jQueryUI v1.11.4 - 2015-04-14
78 * @param {jQuery} $element Element to test
79 * @return {boolean} Element is focusable
81 OO
.ui
.isFocusableElement = function ( $element
) {
83 element
= $element
[ 0 ];
85 // Anything disabled is not focusable
86 if ( element
.disabled
) {
90 // Check if the element is visible
92 // This is quicker than calling $element.is( ':visible' )
93 $.expr
.pseudos
.visible( element
) &&
94 // Check that all parents are visible
95 !$element
.parents().addBack().filter( function () {
96 return $.css( this, 'visibility' ) === 'hidden';
102 // Check if the element is ContentEditable, which is the string 'true'
103 if ( element
.contentEditable
=== 'true' ) {
107 // Anything with a non-negative numeric tabIndex is focusable.
108 // Use .prop to avoid browser bugs
109 if ( $element
.prop( 'tabIndex' ) >= 0 ) {
113 // Some element types are naturally focusable
114 // (indexOf is much faster than regex in Chrome and about the
115 // same in FF: https://jsperf.com/regex-vs-indexof-array2)
116 nodeName
= element
.nodeName
.toLowerCase();
117 if ( [ 'input', 'select', 'textarea', 'button', 'object' ].indexOf( nodeName
) !== -1 ) {
121 // Links and areas are focusable if they have an href
122 if ( ( nodeName
=== 'a' || nodeName
=== 'area' ) && $element
.attr( 'href' ) !== undefined ) {
130 * Find a focusable child
132 * @param {jQuery} $container Container to search in
133 * @param {boolean} [backwards] Search backwards
134 * @return {jQuery} Focusable child, or an empty jQuery object if none found
136 OO
.ui
.findFocusable = function ( $container
, backwards
) {
137 var $focusable
= $( [] ),
138 // $focusableCandidates is a superset of things that
139 // could get matched by isFocusableElement
140 $focusableCandidates
= $container
141 .find( 'input, select, textarea, button, object, a, area, [contenteditable], [tabindex]' );
144 $focusableCandidates
= Array
.prototype.reverse
.call( $focusableCandidates
);
147 $focusableCandidates
.each( function () {
148 var $this = $( this );
149 if ( OO
.ui
.isFocusableElement( $this ) ) {
158 * Get the user's language and any fallback languages.
160 * These language codes are used to localize user interface elements in the user's language.
162 * In environments that provide a localization system, this function should be overridden to
163 * return the user's language(s). The default implementation returns English (en) only.
165 * @return {string[]} Language codes, in descending order of priority
167 OO
.ui
.getUserLanguages = function () {
172 * Get a value in an object keyed by language code.
174 * @param {Object.<string,Mixed>} obj Object keyed by language code
175 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
176 * @param {string} [fallback] Fallback code, used if no matching language can be found
177 * @return {Mixed} Local value
179 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
182 // Requested language
186 // Known user language
187 langs
= OO
.ui
.getUserLanguages();
188 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
195 if ( obj
[ fallback
] ) {
196 return obj
[ fallback
];
198 // First existing language
199 for ( lang
in obj
) {
207 * Check if a node is contained within another node
209 * Similar to jQuery#contains except a list of containers can be supplied
210 * and a boolean argument allows you to include the container in the match list
212 * @param {HTMLElement|HTMLElement[]} containers Container node(s) to search in
213 * @param {HTMLElement} contained Node to find
214 * @param {boolean} [matchContainers] Include the container(s) in the list of nodes to match, otherwise only match descendants
215 * @return {boolean} The node is in the list of target nodes
217 OO
.ui
.contains = function ( containers
, contained
, matchContainers
) {
219 if ( !Array
.isArray( containers
) ) {
220 containers
= [ containers
];
222 for ( i
= containers
.length
- 1; i
>= 0; i
-- ) {
223 if ( ( matchContainers
&& contained
=== containers
[ i
] ) || $.contains( containers
[ i
], contained
) ) {
231 * Return a function, that, as long as it continues to be invoked, will not
232 * be triggered. The function will be called after it stops being called for
233 * N milliseconds. If `immediate` is passed, trigger the function on the
234 * leading edge, instead of the trailing.
236 * Ported from: http://underscorejs.org/underscore.js
238 * @param {Function} func Function to debounce
239 * @param {number} [wait=0] Wait period in milliseconds
240 * @param {boolean} [immediate] Trigger on leading edge
241 * @return {Function} Debounced function
243 OO
.ui
.debounce = function ( func
, wait
, immediate
) {
248 later = function () {
251 func
.apply( context
, args
);
254 if ( immediate
&& !timeout
) {
255 func
.apply( context
, args
);
257 if ( !timeout
|| wait
) {
258 clearTimeout( timeout
);
259 timeout
= setTimeout( later
, wait
);
265 * Puts a console warning with provided message.
267 * @param {string} message Message
269 OO
.ui
.warnDeprecation = function ( message
) {
270 if ( OO
.getProp( window
, 'console', 'warn' ) !== undefined ) {
271 // eslint-disable-next-line no-console
272 console
.warn( message
);
277 * Returns a function, that, when invoked, will only be triggered at most once
278 * during a given window of time. If called again during that window, it will
279 * wait until the window ends and then trigger itself again.
281 * As it's not knowable to the caller whether the function will actually run
282 * when the wrapper is called, return values from the function are entirely
285 * @param {Function} func Function to throttle
286 * @param {number} wait Throttle window length, in milliseconds
287 * @return {Function} Throttled function
289 OO
.ui
.throttle = function ( func
, wait
) {
290 var context
, args
, timeout
,
294 previous
= OO
.ui
.now();
295 func
.apply( context
, args
);
298 // Check how long it's been since the last time the function was
299 // called, and whether it's more or less than the requested throttle
300 // period. If it's less, run the function immediately. If it's more,
301 // set a timeout for the remaining time -- but don't replace an
302 // existing timeout, since that'd indefinitely prolong the wait.
303 var remaining
= wait
- ( OO
.ui
.now() - previous
);
306 if ( remaining
<= 0 ) {
307 // Note: unless wait was ridiculously large, this means we'll
308 // automatically run the first time the function was called in a
309 // given period. (If you provide a wait period larger than the
310 // current Unix timestamp, you *deserve* unexpected behavior.)
311 clearTimeout( timeout
);
313 } else if ( !timeout
) {
314 timeout
= setTimeout( run
, remaining
);
320 * A (possibly faster) way to get the current timestamp as an integer
322 * @return {number} Current timestamp, in milliseconds since the Unix epoch
324 OO
.ui
.now
= Date
.now
|| function () {
325 return new Date().getTime();
329 * Reconstitute a JavaScript object corresponding to a widget created by
330 * the PHP implementation.
332 * This is an alias for `OO.ui.Element.static.infuse()`.
334 * @param {string|HTMLElement|jQuery} idOrNode
335 * A DOM id (if a string) or node for the widget to infuse.
336 * @return {OO.ui.Element}
337 * The `OO.ui.Element` corresponding to this (infusable) document node.
339 OO
.ui
.infuse = function ( idOrNode
) {
340 return OO
.ui
.Element
.static.infuse( idOrNode
);
345 * Message store for the default implementation of OO.ui.msg
347 * Environments that provide a localization system should not use this, but should override
348 * OO.ui.msg altogether.
353 // Tool tip for a button that moves items in a list down one place
354 'ooui-outline-control-move-down': 'Move item down',
355 // Tool tip for a button that moves items in a list up one place
356 'ooui-outline-control-move-up': 'Move item up',
357 // Tool tip for a button that removes items from a list
358 'ooui-outline-control-remove': 'Remove item',
359 // Label for the toolbar group that contains a list of all other available tools
360 'ooui-toolbar-more': 'More',
361 // Label for the fake tool that expands the full list of tools in a toolbar group
362 'ooui-toolgroup-expand': 'More',
363 // Label for the fake tool that collapses the full list of tools in a toolbar group
364 'ooui-toolgroup-collapse': 'Fewer',
365 // Default label for the tooltip for the button that removes a tag item
366 'ooui-item-remove': 'Remove',
367 // Default label for the accept button of a confirmation dialog
368 'ooui-dialog-message-accept': 'OK',
369 // Default label for the reject button of a confirmation dialog
370 'ooui-dialog-message-reject': 'Cancel',
371 // Title for process dialog error description
372 'ooui-dialog-process-error': 'Something went wrong',
373 // Label for process dialog dismiss error button, visible when describing errors
374 'ooui-dialog-process-dismiss': 'Dismiss',
375 // Label for process dialog retry action button, visible when describing only recoverable errors
376 'ooui-dialog-process-retry': 'Try again',
377 // Label for process dialog retry action button, visible when describing only warnings
378 'ooui-dialog-process-continue': 'Continue',
379 // Label for the file selection widget's select file button
380 'ooui-selectfile-button-select': 'Select a file',
381 // Label for the file selection widget if file selection is not supported
382 'ooui-selectfile-not-supported': 'File selection is not supported',
383 // Label for the file selection widget when no file is currently selected
384 'ooui-selectfile-placeholder': 'No file is selected',
385 // Label for the file selection widget's drop target
386 'ooui-selectfile-dragdrop-placeholder': 'Drop file here'
390 * Get a localized message.
392 * After the message key, message parameters may optionally be passed. In the default implementation,
393 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
394 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
395 * they support unnamed, ordered message parameters.
397 * In environments that provide a localization system, this function should be overridden to
398 * return the message translated in the user's language. The default implementation always returns
399 * English messages. An example of doing this with [jQuery.i18n](https://github.com/wikimedia/jquery.i18n)
403 * var i, iLen, button,
404 * messagePath = 'oojs-ui/dist/i18n/',
405 * languages = [ $.i18n().locale, 'ur', 'en' ],
408 * for ( i = 0, iLen = languages.length; i < iLen; i++ ) {
409 * languageMap[ languages[ i ] ] = messagePath + languages[ i ].toLowerCase() + '.json';
412 * $.i18n().load( languageMap ).done( function() {
413 * // Replace the built-in `msg` only once we've loaded the internationalization.
414 * // OOjs UI uses `OO.ui.deferMsg` for all initially-loaded messages. So long as
415 * // you put off creating any widgets until this promise is complete, no English
416 * // will be displayed.
417 * OO.ui.msg = $.i18n;
419 * // A button displaying "OK" in the default locale
420 * button = new OO.ui.ButtonWidget( {
421 * label: OO.ui.msg( 'ooui-dialog-message-accept' ),
424 * $( 'body' ).append( button.$element );
426 * // A button displaying "OK" in Urdu
427 * $.i18n().locale = 'ur';
428 * button = new OO.ui.ButtonWidget( {
429 * label: OO.ui.msg( 'ooui-dialog-message-accept' ),
432 * $( 'body' ).append( button.$element );
435 * @param {string} key Message key
436 * @param {...Mixed} [params] Message parameters
437 * @return {string} Translated message with parameters substituted
439 OO
.ui
.msg = function ( key
) {
440 var message
= messages
[ key
],
441 params
= Array
.prototype.slice
.call( arguments
, 1 );
442 if ( typeof message
=== 'string' ) {
443 // Perform $1 substitution
444 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
445 var i
= parseInt( n
, 10 );
446 return params
[ i
- 1 ] !== undefined ? params
[ i
- 1 ] : '$' + n
;
449 // Return placeholder if message not found
450 message
= '[' + key
+ ']';
457 * Package a message and arguments for deferred resolution.
459 * Use this when you are statically specifying a message and the message may not yet be present.
461 * @param {string} key Message key
462 * @param {...Mixed} [params] Message parameters
463 * @return {Function} Function that returns the resolved message when executed
465 OO
.ui
.deferMsg = function () {
466 var args
= arguments
;
468 return OO
.ui
.msg
.apply( OO
.ui
, args
);
475 * If the message is a function it will be executed, otherwise it will pass through directly.
477 * @param {Function|string} msg Deferred message, or message text
478 * @return {string} Resolved message
480 OO
.ui
.resolveMsg = function ( msg
) {
481 if ( $.isFunction( msg
) ) {
488 * @param {string} url
491 OO
.ui
.isSafeUrl = function ( url
) {
492 // Keep this function in sync with php/Tag.php
493 var i
, protocolWhitelist
;
495 function stringStartsWith( haystack
, needle
) {
496 return haystack
.substr( 0, needle
.length
) === needle
;
499 protocolWhitelist
= [
500 'bitcoin', 'ftp', 'ftps', 'geo', 'git', 'gopher', 'http', 'https', 'irc', 'ircs',
501 'magnet', 'mailto', 'mms', 'news', 'nntp', 'redis', 'sftp', 'sip', 'sips', 'sms', 'ssh',
502 'svn', 'tel', 'telnet', 'urn', 'worldwind', 'xmpp'
509 for ( i
= 0; i
< protocolWhitelist
.length
; i
++ ) {
510 if ( stringStartsWith( url
, protocolWhitelist
[ i
] + ':' ) ) {
515 // This matches '//' too
516 if ( stringStartsWith( url
, '/' ) || stringStartsWith( url
, './' ) ) {
519 if ( stringStartsWith( url
, '?' ) || stringStartsWith( url
, '#' ) ) {
527 * Check if the user has a 'mobile' device.
529 * For our purposes this means the user is primarily using an
530 * on-screen keyboard, touch input instead of a mouse and may
531 * have a physically small display.
533 * It is left up to implementors to decide how to compute this
534 * so the default implementation always returns false.
536 * @return {boolean} Use is on a mobile device
538 OO
.ui
.isMobile = function () {
547 * Namespace for OOjs UI mixins.
549 * Mixins are named according to the type of object they are intended to
550 * be mixed in to. For example, OO.ui.mixin.GroupElement is intended to be
551 * mixed in to an instance of OO.ui.Element, and OO.ui.mixin.GroupWidget
552 * is intended to be mixed in to an instance of OO.ui.Widget.
560 * Each Element represents a rendering in the DOM—a button or an icon, for example, or anything
561 * that is visible to a user. Unlike {@link OO.ui.Widget widgets}, plain elements usually do not have events
562 * connected to them and can't be interacted with.
568 * @param {Object} [config] Configuration options
569 * @cfg {string[]} [classes] The names of the CSS classes to apply to the element. CSS styles are added
570 * to the top level (e.g., the outermost div) of the element. See the [OOjs UI documentation on MediaWiki][2]
572 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#cssExample
573 * @cfg {string} [id] The HTML id attribute used in the rendered tag.
574 * @cfg {string} [text] Text to insert
575 * @cfg {Array} [content] An array of content elements to append (after #text).
576 * Strings will be html-escaped; use an OO.ui.HtmlSnippet to append raw HTML.
577 * Instances of OO.ui.Element will have their $element appended.
578 * @cfg {jQuery} [$content] Content elements to append (after #text).
579 * @cfg {jQuery} [$element] Wrapper element. Defaults to a new element with #getTagName.
580 * @cfg {Mixed} [data] Custom data of any type or combination of types (e.g., string, number, array, object).
581 * Data can also be specified with the #setData method.
583 OO
.ui
.Element
= function OoUiElement( config
) {
584 this.initialConfig
= config
;
585 // Configuration initialization
586 config
= config
|| {};
590 this.elementId
= null;
592 this.data
= config
.data
;
593 this.$element
= config
.$element
||
594 $( document
.createElement( this.getTagName() ) );
595 this.elementGroup
= null;
598 if ( Array
.isArray( config
.classes
) ) {
599 this.$element
.addClass( config
.classes
.join( ' ' ) );
602 this.setElementId( config
.id
);
605 this.$element
.text( config
.text
);
607 if ( config
.content
) {
608 // The `content` property treats plain strings as text; use an
609 // HtmlSnippet to append HTML content. `OO.ui.Element`s get their
610 // appropriate $element appended.
611 this.$element
.append( config
.content
.map( function ( v
) {
612 if ( typeof v
=== 'string' ) {
613 // Escape string so it is properly represented in HTML.
614 return document
.createTextNode( v
);
615 } else if ( v
instanceof OO
.ui
.HtmlSnippet
) {
618 } else if ( v
instanceof OO
.ui
.Element
) {
624 if ( config
.$content
) {
625 // The `$content` property treats plain strings as HTML.
626 this.$element
.append( config
.$content
);
632 OO
.initClass( OO
.ui
.Element
);
634 /* Static Properties */
637 * The name of the HTML tag used by the element.
639 * The static value may be ignored if the #getTagName method is overridden.
645 OO
.ui
.Element
.static.tagName
= 'div';
650 * Reconstitute a JavaScript object corresponding to a widget created
651 * by the PHP implementation.
653 * @param {string|HTMLElement|jQuery} idOrNode
654 * A DOM id (if a string) or node for the widget to infuse.
655 * @return {OO.ui.Element}
656 * The `OO.ui.Element` corresponding to this (infusable) document node.
657 * For `Tag` objects emitted on the HTML side (used occasionally for content)
658 * the value returned is a newly-created Element wrapping around the existing
661 OO
.ui
.Element
.static.infuse = function ( idOrNode
) {
662 var obj
= OO
.ui
.Element
.static.unsafeInfuse( idOrNode
, false );
663 // Verify that the type matches up.
664 // FIXME: uncomment after T89721 is fixed, see T90929.
666 if ( !( obj instanceof this['class'] ) ) {
667 throw new Error( 'Infusion type mismatch!' );
674 * Implementation helper for `infuse`; skips the type check and has an
675 * extra property so that only the top-level invocation touches the DOM.
678 * @param {string|HTMLElement|jQuery} idOrNode
679 * @param {jQuery.Promise|boolean} domPromise A promise that will be resolved
680 * when the top-level widget of this infusion is inserted into DOM,
681 * replacing the original node; or false for top-level invocation.
682 * @return {OO.ui.Element}
684 OO
.ui
.Element
.static.unsafeInfuse = function ( idOrNode
, domPromise
) {
685 // look for a cached result of a previous infusion.
686 var id
, $elem
, error
, data
, cls
, parts
, parent
, obj
, top
, state
, infusedChildren
;
687 if ( typeof idOrNode
=== 'string' ) {
689 $elem
= $( document
.getElementById( id
) );
691 $elem
= $( idOrNode
);
692 id
= $elem
.attr( 'id' );
694 if ( !$elem
.length
) {
695 if ( typeof idOrNode
=== 'string' ) {
696 error
= 'Widget not found: ' + idOrNode
;
697 } else if ( idOrNode
&& idOrNode
.selector
) {
698 error
= 'Widget not found: ' + idOrNode
.selector
;
700 error
= 'Widget not found';
702 throw new Error( error
);
704 if ( $elem
[ 0 ].oouiInfused
) {
705 $elem
= $elem
[ 0 ].oouiInfused
;
707 data
= $elem
.data( 'ooui-infused' );
710 if ( data
=== true ) {
711 throw new Error( 'Circular dependency! ' + id
);
714 // pick up dynamic state, like focus, value of form inputs, scroll position, etc.
715 state
= data
.constructor.static.gatherPreInfuseState( $elem
, data
);
716 // restore dynamic state after the new element is re-inserted into DOM under infused parent
717 domPromise
.done( data
.restorePreInfuseState
.bind( data
, state
) );
718 infusedChildren
= $elem
.data( 'ooui-infused-children' );
719 if ( infusedChildren
&& infusedChildren
.length
) {
720 infusedChildren
.forEach( function ( data
) {
721 var state
= data
.constructor.static.gatherPreInfuseState( $elem
, data
);
722 domPromise
.done( data
.restorePreInfuseState
.bind( data
, state
) );
728 data
= $elem
.attr( 'data-ooui' );
730 throw new Error( 'No infusion data found: ' + id
);
733 data
= JSON
.parse( data
);
737 if ( !( data
&& data
._
) ) {
738 throw new Error( 'No valid infusion data found: ' + id
);
740 if ( data
._
=== 'Tag' ) {
741 // Special case: this is a raw Tag; wrap existing node, don't rebuild.
742 return new OO
.ui
.Element( { $element
: $elem
} );
744 parts
= data
._
.split( '.' );
745 cls
= OO
.getProp
.apply( OO
, [ window
].concat( parts
) );
746 if ( cls
=== undefined ) {
747 throw new Error( 'Unknown widget type: id: ' + id
+ ', class: ' + data
._
);
750 // Verify that we're creating an OO.ui.Element instance
753 while ( parent
!== undefined ) {
754 if ( parent
=== OO
.ui
.Element
) {
759 parent
= parent
.parent
;
762 if ( parent
!== OO
.ui
.Element
) {
763 throw new Error( 'Unknown widget type: id: ' + id
+ ', class: ' + data
._
);
766 if ( domPromise
=== false ) {
768 domPromise
= top
.promise();
770 $elem
.data( 'ooui-infused', true ); // prevent loops
771 data
.id
= id
; // implicit
772 infusedChildren
= [];
773 data
= OO
.copy( data
, null, function deserialize( value
) {
775 if ( OO
.isPlainObject( value
) ) {
777 infused
= OO
.ui
.Element
.static.unsafeInfuse( value
.tag
, domPromise
);
778 infusedChildren
.push( infused
);
779 // Flatten the structure
780 infusedChildren
.push
.apply( infusedChildren
, infused
.$element
.data( 'ooui-infused-children' ) || [] );
781 infused
.$element
.removeData( 'ooui-infused-children' );
784 if ( value
.html
!== undefined ) {
785 return new OO
.ui
.HtmlSnippet( value
.html
);
789 // allow widgets to reuse parts of the DOM
790 data
= cls
.static.reusePreInfuseDOM( $elem
[ 0 ], data
);
791 // pick up dynamic state, like focus, value of form inputs, scroll position, etc.
792 state
= cls
.static.gatherPreInfuseState( $elem
[ 0 ], data
);
794 // eslint-disable-next-line new-cap
795 obj
= new cls( data
);
796 // now replace old DOM with this new DOM.
798 // An efficient constructor might be able to reuse the entire DOM tree of the original element,
799 // so only mutate the DOM if we need to.
800 if ( $elem
[ 0 ] !== obj
.$element
[ 0 ] ) {
801 $elem
.replaceWith( obj
.$element
);
802 // This element is now gone from the DOM, but if anyone is holding a reference to it,
803 // let's allow them to OO.ui.infuse() it and do what they expect, see T105828.
804 // Do not use jQuery.data(), as using it on detached nodes leaks memory in 1.x line by design.
805 $elem
[ 0 ].oouiInfused
= obj
.$element
;
809 obj
.$element
.data( 'ooui-infused', obj
);
810 obj
.$element
.data( 'ooui-infused-children', infusedChildren
);
811 // set the 'data-ooui' attribute so we can identify infused widgets
812 obj
.$element
.attr( 'data-ooui', '' );
813 // restore dynamic state after the new element is inserted into DOM
814 domPromise
.done( obj
.restorePreInfuseState
.bind( obj
, state
) );
819 * Pick out parts of `node`'s DOM to be reused when infusing a widget.
821 * This method **must not** make any changes to the DOM, only find interesting pieces and add them
822 * to `config` (which should then be returned). Actual DOM juggling should then be done by the
823 * constructor, which will be given the enhanced config.
826 * @param {HTMLElement} node
827 * @param {Object} config
830 OO
.ui
.Element
.static.reusePreInfuseDOM = function ( node
, config
) {
835 * Gather the dynamic state (focus, value of form inputs, scroll position, etc.) of an HTML DOM node
836 * (and its children) that represent an Element of the same class and the given configuration,
837 * generated by the PHP implementation.
839 * This method is called just before `node` is detached from the DOM. The return value of this
840 * function will be passed to #restorePreInfuseState after the newly created widget's #$element
841 * is inserted into DOM to replace `node`.
844 * @param {HTMLElement} node
845 * @param {Object} config
848 OO
.ui
.Element
.static.gatherPreInfuseState = function () {
853 * Get a jQuery function within a specific document.
856 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
857 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
859 * @return {Function} Bound jQuery function
861 OO
.ui
.Element
.static.getJQuery = function ( context
, $iframe
) {
862 function wrapper( selector
) {
863 return $( selector
, wrapper
.context
);
866 wrapper
.context
= this.getDocument( context
);
869 wrapper
.$iframe
= $iframe
;
876 * Get the document of an element.
879 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
880 * @return {HTMLDocument|null} Document object
882 OO
.ui
.Element
.static.getDocument = function ( obj
) {
883 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
884 return ( obj
[ 0 ] && obj
[ 0 ].ownerDocument
) ||
885 // Empty jQuery selections might have a context
892 ( obj
.nodeType
=== Node
.DOCUMENT_NODE
&& obj
) ||
897 * Get the window of an element or document.
900 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
901 * @return {Window} Window object
903 OO
.ui
.Element
.static.getWindow = function ( obj
) {
904 var doc
= this.getDocument( obj
);
905 return doc
.defaultView
;
909 * Get the direction of an element or document.
912 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
913 * @return {string} Text direction, either 'ltr' or 'rtl'
915 OO
.ui
.Element
.static.getDir = function ( obj
) {
918 if ( obj
instanceof jQuery
) {
921 isDoc
= obj
.nodeType
=== Node
.DOCUMENT_NODE
;
922 isWin
= obj
.document
!== undefined;
923 if ( isDoc
|| isWin
) {
929 return $( obj
).css( 'direction' );
933 * Get the offset between two frames.
935 * TODO: Make this function not use recursion.
938 * @param {Window} from Window of the child frame
939 * @param {Window} [to=window] Window of the parent frame
940 * @param {Object} [offset] Offset to start with, used internally
941 * @return {Object} Offset object, containing left and top properties
943 OO
.ui
.Element
.static.getFrameOffset = function ( from, to
, offset
) {
944 var i
, len
, frames
, frame
, rect
;
950 offset
= { top
: 0, left
: 0 };
952 if ( from.parent
=== from ) {
956 // Get iframe element
957 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
958 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
959 if ( frames
[ i
].contentWindow
=== from ) {
965 // Recursively accumulate offset values
967 rect
= frame
.getBoundingClientRect();
968 offset
.left
+= rect
.left
;
969 offset
.top
+= rect
.top
;
971 this.getFrameOffset( from.parent
, offset
);
978 * Get the offset between two elements.
980 * The two elements may be in a different frame, but in that case the frame $element is in must
981 * be contained in the frame $anchor is in.
984 * @param {jQuery} $element Element whose position to get
985 * @param {jQuery} $anchor Element to get $element's position relative to
986 * @return {Object} Translated position coordinates, containing top and left properties
988 OO
.ui
.Element
.static.getRelativePosition = function ( $element
, $anchor
) {
989 var iframe
, iframePos
,
990 pos
= $element
.offset(),
991 anchorPos
= $anchor
.offset(),
992 elementDocument
= this.getDocument( $element
),
993 anchorDocument
= this.getDocument( $anchor
);
995 // If $element isn't in the same document as $anchor, traverse up
996 while ( elementDocument
!== anchorDocument
) {
997 iframe
= elementDocument
.defaultView
.frameElement
;
999 throw new Error( '$element frame is not contained in $anchor frame' );
1001 iframePos
= $( iframe
).offset();
1002 pos
.left
+= iframePos
.left
;
1003 pos
.top
+= iframePos
.top
;
1004 elementDocument
= iframe
.ownerDocument
;
1006 pos
.left
-= anchorPos
.left
;
1007 pos
.top
-= anchorPos
.top
;
1012 * Get element border sizes.
1015 * @param {HTMLElement} el Element to measure
1016 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
1018 OO
.ui
.Element
.static.getBorders = function ( el
) {
1019 var doc
= el
.ownerDocument
,
1020 win
= doc
.defaultView
,
1021 style
= win
.getComputedStyle( el
, null ),
1023 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
1024 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
1025 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
1026 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
1037 * Get dimensions of an element or window.
1040 * @param {HTMLElement|Window} el Element to measure
1041 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
1043 OO
.ui
.Element
.static.getDimensions = function ( el
) {
1045 doc
= el
.ownerDocument
|| el
.document
,
1046 win
= doc
.defaultView
;
1048 if ( win
=== el
|| el
=== doc
.documentElement
) {
1051 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
1053 top
: $win
.scrollTop(),
1054 left
: $win
.scrollLeft()
1056 scrollbar
: { right
: 0, bottom
: 0 },
1060 bottom
: $win
.innerHeight(),
1061 right
: $win
.innerWidth()
1067 borders
: this.getBorders( el
),
1069 top
: $el
.scrollTop(),
1070 left
: $el
.scrollLeft()
1073 right
: $el
.innerWidth() - el
.clientWidth
,
1074 bottom
: $el
.innerHeight() - el
.clientHeight
1076 rect
: el
.getBoundingClientRect()
1082 * Get the number of pixels that an element's content is scrolled to the left.
1084 * Adapted from <https://github.com/othree/jquery.rtl-scroll-type>.
1085 * Original code copyright 2012 Wei-Ko Kao, licensed under the MIT License.
1087 * This function smooths out browser inconsistencies (nicely described in the README at
1088 * <https://github.com/othree/jquery.rtl-scroll-type>) and produces a result consistent
1089 * with Firefox's 'scrollLeft', which seems the sanest.
1093 * @param {HTMLElement|Window} el Element to measure
1094 * @return {number} Scroll position from the left.
1095 * If the element's direction is LTR, this is a positive number between `0` (initial scroll position)
1096 * and `el.scrollWidth - el.clientWidth` (furthest possible scroll position).
1097 * If the element's direction is RTL, this is a negative number between `0` (initial scroll position)
1098 * and `-el.scrollWidth + el.clientWidth` (furthest possible scroll position).
1100 OO
.ui
.Element
.static.getScrollLeft
= ( function () {
1101 var rtlScrollType
= null;
1104 var $definer
= $( '<div dir="rtl" style="font-size: 14px; width: 1px; height: 1px; position: absolute; top: -1000px; overflow: scroll">A</div>' ),
1105 definer
= $definer
[ 0 ];
1107 $definer
.appendTo( 'body' );
1108 if ( definer
.scrollLeft
> 0 ) {
1110 rtlScrollType
= 'default';
1112 definer
.scrollLeft
= 1;
1113 if ( definer
.scrollLeft
=== 0 ) {
1114 // Firefox, old Opera
1115 rtlScrollType
= 'negative';
1117 // Internet Explorer, Edge
1118 rtlScrollType
= 'reverse';
1124 return function getScrollLeft( el
) {
1125 var isRoot
= el
.window
=== el
||
1126 el
=== el
.ownerDocument
.body
||
1127 el
=== el
.ownerDocument
.documentElement
,
1128 scrollLeft
= isRoot
? $( window
).scrollLeft() : el
.scrollLeft
,
1129 // All browsers use the correct scroll type ('negative') on the root, so don't
1130 // do any fixups when looking at the root element
1131 direction
= isRoot
? 'ltr' : $( el
).css( 'direction' );
1133 if ( direction
=== 'rtl' ) {
1134 if ( rtlScrollType
=== null ) {
1137 if ( rtlScrollType
=== 'reverse' ) {
1138 scrollLeft
= -scrollLeft
;
1139 } else if ( rtlScrollType
=== 'default' ) {
1140 scrollLeft
= scrollLeft
- el
.scrollWidth
+ el
.clientWidth
;
1149 * Get the root scrollable element of given element's document.
1151 * On Blink-based browsers (Chrome etc.), `document.documentElement` can't be used to get or set
1152 * the scrollTop property; instead we have to use `document.body`. Changing and testing the value
1153 * lets us use 'body' or 'documentElement' based on what is working.
1155 * https://code.google.com/p/chromium/issues/detail?id=303131
1158 * @param {HTMLElement} el Element to find root scrollable parent for
1159 * @return {HTMLElement} Scrollable parent, `document.body` or `document.documentElement`
1160 * depending on browser
1162 OO
.ui
.Element
.static.getRootScrollableElement = function ( el
) {
1163 var scrollTop
, body
;
1165 if ( OO
.ui
.scrollableElement
=== undefined ) {
1166 body
= el
.ownerDocument
.body
;
1167 scrollTop
= body
.scrollTop
;
1170 // In some browsers (observed in Chrome 56 on Linux Mint 18.1),
1171 // body.scrollTop doesn't become exactly 1, but a fractional value like 0.76
1172 if ( Math
.round( body
.scrollTop
) === 1 ) {
1173 body
.scrollTop
= scrollTop
;
1174 OO
.ui
.scrollableElement
= 'body';
1176 OO
.ui
.scrollableElement
= 'documentElement';
1180 return el
.ownerDocument
[ OO
.ui
.scrollableElement
];
1184 * Get closest scrollable container.
1186 * Traverses up until either a scrollable element or the root is reached, in which case the root
1187 * scrollable element will be returned (see #getRootScrollableElement).
1190 * @param {HTMLElement} el Element to find scrollable container for
1191 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
1192 * @return {HTMLElement} Closest scrollable container
1194 OO
.ui
.Element
.static.getClosestScrollableContainer = function ( el
, dimension
) {
1196 // Browsers do not correctly return the computed value of 'overflow' when 'overflow-x' and
1197 // 'overflow-y' have different values, so we need to check the separate properties.
1198 props
= [ 'overflow-x', 'overflow-y' ],
1199 $parent
= $( el
).parent();
1201 if ( dimension
=== 'x' || dimension
=== 'y' ) {
1202 props
= [ 'overflow-' + dimension
];
1205 // Special case for the document root (which doesn't really have any scrollable container, since
1206 // it is the ultimate scrollable container, but this is probably saner than null or exception)
1207 if ( $( el
).is( 'html, body' ) ) {
1208 return this.getRootScrollableElement( el
);
1211 while ( $parent
.length
) {
1212 if ( $parent
[ 0 ] === this.getRootScrollableElement( el
) ) {
1213 return $parent
[ 0 ];
1217 val
= $parent
.css( props
[ i
] );
1218 // We assume that elements with 'overflow' (in any direction) set to 'hidden' will never be
1219 // scrolled in that direction, but they can actually be scrolled programatically. The user can
1220 // unintentionally perform a scroll in such case even if the application doesn't scroll
1221 // programatically, e.g. when jumping to an anchor, or when using built-in find functionality.
1222 // This could cause funny issues...
1223 if ( val
=== 'auto' || val
=== 'scroll' ) {
1224 return $parent
[ 0 ];
1227 $parent
= $parent
.parent();
1229 // The element is unattached... return something mostly sane
1230 return this.getRootScrollableElement( el
);
1234 * Scroll element into view.
1237 * @param {HTMLElement} el Element to scroll into view
1238 * @param {Object} [config] Configuration options
1239 * @param {string} [config.duration='fast'] jQuery animation duration value
1240 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
1241 * to scroll in both directions
1242 * @return {jQuery.Promise} Promise which resolves when the scroll is complete
1244 OO
.ui
.Element
.static.scrollIntoView = function ( el
, config
) {
1245 var position
, animations
, container
, $container
, elementDimensions
, containerDimensions
, $window
,
1246 deferred
= $.Deferred();
1248 // Configuration initialization
1249 config
= config
|| {};
1252 container
= this.getClosestScrollableContainer( el
, config
.direction
);
1253 $container
= $( container
);
1254 elementDimensions
= this.getDimensions( el
);
1255 containerDimensions
= this.getDimensions( container
);
1256 $window
= $( this.getWindow( el
) );
1258 // Compute the element's position relative to the container
1259 if ( $container
.is( 'html, body' ) ) {
1260 // If the scrollable container is the root, this is easy
1262 top
: elementDimensions
.rect
.top
,
1263 bottom
: $window
.innerHeight() - elementDimensions
.rect
.bottom
,
1264 left
: elementDimensions
.rect
.left
,
1265 right
: $window
.innerWidth() - elementDimensions
.rect
.right
1268 // Otherwise, we have to subtract el's coordinates from container's coordinates
1270 top
: elementDimensions
.rect
.top
- ( containerDimensions
.rect
.top
+ containerDimensions
.borders
.top
),
1271 bottom
: containerDimensions
.rect
.bottom
- containerDimensions
.borders
.bottom
- containerDimensions
.scrollbar
.bottom
- elementDimensions
.rect
.bottom
,
1272 left
: elementDimensions
.rect
.left
- ( containerDimensions
.rect
.left
+ containerDimensions
.borders
.left
),
1273 right
: containerDimensions
.rect
.right
- containerDimensions
.borders
.right
- containerDimensions
.scrollbar
.right
- elementDimensions
.rect
.right
1277 if ( !config
.direction
|| config
.direction
=== 'y' ) {
1278 if ( position
.top
< 0 ) {
1279 animations
.scrollTop
= containerDimensions
.scroll
.top
+ position
.top
;
1280 } else if ( position
.top
> 0 && position
.bottom
< 0 ) {
1281 animations
.scrollTop
= containerDimensions
.scroll
.top
+ Math
.min( position
.top
, -position
.bottom
);
1284 if ( !config
.direction
|| config
.direction
=== 'x' ) {
1285 if ( position
.left
< 0 ) {
1286 animations
.scrollLeft
= containerDimensions
.scroll
.left
+ position
.left
;
1287 } else if ( position
.left
> 0 && position
.right
< 0 ) {
1288 animations
.scrollLeft
= containerDimensions
.scroll
.left
+ Math
.min( position
.left
, -position
.right
);
1291 if ( !$.isEmptyObject( animations
) ) {
1292 $container
.stop( true ).animate( animations
, config
.duration
=== undefined ? 'fast' : config
.duration
);
1293 $container
.queue( function ( next
) {
1300 return deferred
.promise();
1304 * Force the browser to reconsider whether it really needs to render scrollbars inside the element
1305 * and reserve space for them, because it probably doesn't.
1307 * Workaround primarily for <https://code.google.com/p/chromium/issues/detail?id=387290>, but also
1308 * similar bugs in other browsers. "Just" forcing a reflow is not sufficient in all cases, we need
1309 * to first actually detach (or hide, but detaching is simpler) all children, *then* force a reflow,
1310 * and then reattach (or show) them back.
1313 * @param {HTMLElement} el Element to reconsider the scrollbars on
1315 OO
.ui
.Element
.static.reconsiderScrollbars = function ( el
) {
1316 var i
, len
, scrollLeft
, scrollTop
, nodes
= [];
1317 // Save scroll position
1318 scrollLeft
= el
.scrollLeft
;
1319 scrollTop
= el
.scrollTop
;
1320 // Detach all children
1321 while ( el
.firstChild
) {
1322 nodes
.push( el
.firstChild
);
1323 el
.removeChild( el
.firstChild
);
1326 void el
.offsetHeight
;
1327 // Reattach all children
1328 for ( i
= 0, len
= nodes
.length
; i
< len
; i
++ ) {
1329 el
.appendChild( nodes
[ i
] );
1331 // Restore scroll position (no-op if scrollbars disappeared)
1332 el
.scrollLeft
= scrollLeft
;
1333 el
.scrollTop
= scrollTop
;
1339 * Toggle visibility of an element.
1341 * @param {boolean} [show] Make element visible, omit to toggle visibility
1345 OO
.ui
.Element
.prototype.toggle = function ( show
) {
1346 show
= show
=== undefined ? !this.visible
: !!show
;
1348 if ( show
!== this.isVisible() ) {
1349 this.visible
= show
;
1350 this.$element
.toggleClass( 'oo-ui-element-hidden', !this.visible
);
1351 this.emit( 'toggle', show
);
1358 * Check if element is visible.
1360 * @return {boolean} element is visible
1362 OO
.ui
.Element
.prototype.isVisible = function () {
1363 return this.visible
;
1369 * @return {Mixed} Element data
1371 OO
.ui
.Element
.prototype.getData = function () {
1378 * @param {Mixed} data Element data
1381 OO
.ui
.Element
.prototype.setData = function ( data
) {
1387 * Set the element has an 'id' attribute.
1389 * @param {string} id
1392 OO
.ui
.Element
.prototype.setElementId = function ( id
) {
1393 this.elementId
= id
;
1394 this.$element
.attr( 'id', id
);
1399 * Ensure that the element has an 'id' attribute, setting it to an unique value if it's missing,
1400 * and return its value.
1404 OO
.ui
.Element
.prototype.getElementId = function () {
1405 if ( this.elementId
=== null ) {
1406 this.setElementId( OO
.ui
.generateElementId() );
1408 return this.elementId
;
1412 * Check if element supports one or more methods.
1414 * @param {string|string[]} methods Method or list of methods to check
1415 * @return {boolean} All methods are supported
1417 OO
.ui
.Element
.prototype.supports = function ( methods
) {
1421 methods
= Array
.isArray( methods
) ? methods
: [ methods
];
1422 for ( i
= 0, len
= methods
.length
; i
< len
; i
++ ) {
1423 if ( $.isFunction( this[ methods
[ i
] ] ) ) {
1428 return methods
.length
=== support
;
1432 * Update the theme-provided classes.
1434 * @localdoc This is called in element mixins and widget classes any time state changes.
1435 * Updating is debounced, minimizing overhead of changing multiple attributes and
1436 * guaranteeing that theme updates do not occur within an element's constructor
1438 OO
.ui
.Element
.prototype.updateThemeClasses = function () {
1439 OO
.ui
.theme
.queueUpdateElementClasses( this );
1443 * Get the HTML tag name.
1445 * Override this method to base the result on instance information.
1447 * @return {string} HTML tag name
1449 OO
.ui
.Element
.prototype.getTagName = function () {
1450 return this.constructor.static.tagName
;
1454 * Check if the element is attached to the DOM
1456 * @return {boolean} The element is attached to the DOM
1458 OO
.ui
.Element
.prototype.isElementAttached = function () {
1459 return $.contains( this.getElementDocument(), this.$element
[ 0 ] );
1463 * Get the DOM document.
1465 * @return {HTMLDocument} Document object
1467 OO
.ui
.Element
.prototype.getElementDocument = function () {
1468 // Don't cache this in other ways either because subclasses could can change this.$element
1469 return OO
.ui
.Element
.static.getDocument( this.$element
);
1473 * Get the DOM window.
1475 * @return {Window} Window object
1477 OO
.ui
.Element
.prototype.getElementWindow = function () {
1478 return OO
.ui
.Element
.static.getWindow( this.$element
);
1482 * Get closest scrollable container.
1484 * @return {HTMLElement} Closest scrollable container
1486 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1487 return OO
.ui
.Element
.static.getClosestScrollableContainer( this.$element
[ 0 ] );
1491 * Get group element is in.
1493 * @return {OO.ui.mixin.GroupElement|null} Group element, null if none
1495 OO
.ui
.Element
.prototype.getElementGroup = function () {
1496 return this.elementGroup
;
1500 * Set group element is in.
1502 * @param {OO.ui.mixin.GroupElement|null} group Group element, null if none
1505 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1506 this.elementGroup
= group
;
1511 * Scroll element into view.
1513 * @param {Object} [config] Configuration options
1514 * @return {jQuery.Promise} Promise which resolves when the scroll is complete
1516 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1518 !this.isElementAttached() ||
1519 !this.isVisible() ||
1520 ( this.getElementGroup() && !this.getElementGroup().isVisible() )
1522 return $.Deferred().resolve();
1524 return OO
.ui
.Element
.static.scrollIntoView( this.$element
[ 0 ], config
);
1528 * Restore the pre-infusion dynamic state for this widget.
1530 * This method is called after #$element has been inserted into DOM. The parameter is the return
1531 * value of #gatherPreInfuseState.
1534 * @param {Object} state
1536 OO
.ui
.Element
.prototype.restorePreInfuseState = function () {
1540 * Wraps an HTML snippet for use with configuration values which default
1541 * to strings. This bypasses the default html-escaping done to string
1547 * @param {string} [content] HTML content
1549 OO
.ui
.HtmlSnippet
= function OoUiHtmlSnippet( content
) {
1551 this.content
= content
;
1556 OO
.initClass( OO
.ui
.HtmlSnippet
);
1563 * @return {string} Unchanged HTML snippet.
1565 OO
.ui
.HtmlSnippet
.prototype.toString = function () {
1566 return this.content
;
1570 * Layouts are containers for elements and are used to arrange other widgets of arbitrary type in a way
1571 * that is centrally controlled and can be updated dynamically. Layouts can be, and usually are, combined.
1572 * See {@link OO.ui.FieldsetLayout FieldsetLayout}, {@link OO.ui.FieldLayout FieldLayout}, {@link OO.ui.FormLayout FormLayout},
1573 * {@link OO.ui.PanelLayout PanelLayout}, {@link OO.ui.StackLayout StackLayout}, {@link OO.ui.PageLayout PageLayout},
1574 * {@link OO.ui.HorizontalLayout HorizontalLayout}, and {@link OO.ui.BookletLayout BookletLayout} for more information and examples.
1578 * @extends OO.ui.Element
1579 * @mixins OO.EventEmitter
1582 * @param {Object} [config] Configuration options
1584 OO
.ui
.Layout
= function OoUiLayout( config
) {
1585 // Configuration initialization
1586 config
= config
|| {};
1588 // Parent constructor
1589 OO
.ui
.Layout
.parent
.call( this, config
);
1591 // Mixin constructors
1592 OO
.EventEmitter
.call( this );
1595 this.$element
.addClass( 'oo-ui-layout' );
1600 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1601 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1604 * Widgets are compositions of one or more OOjs UI elements that users can both view
1605 * and interact with. All widgets can be configured and modified via a standard API,
1606 * and their state can change dynamically according to a model.
1610 * @extends OO.ui.Element
1611 * @mixins OO.EventEmitter
1614 * @param {Object} [config] Configuration options
1615 * @cfg {boolean} [disabled=false] Disable the widget. Disabled widgets cannot be used and their
1616 * appearance reflects this state.
1618 OO
.ui
.Widget
= function OoUiWidget( config
) {
1619 // Initialize config
1620 config
= $.extend( { disabled
: false }, config
);
1622 // Parent constructor
1623 OO
.ui
.Widget
.parent
.call( this, config
);
1625 // Mixin constructors
1626 OO
.EventEmitter
.call( this );
1629 this.disabled
= null;
1630 this.wasDisabled
= null;
1633 this.$element
.addClass( 'oo-ui-widget' );
1634 this.setDisabled( !!config
.disabled
);
1639 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1640 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1647 * A 'disable' event is emitted when the disabled state of the widget changes
1648 * (i.e. on disable **and** enable).
1650 * @param {boolean} disabled Widget is disabled
1656 * A 'toggle' event is emitted when the visibility of the widget changes.
1658 * @param {boolean} visible Widget is visible
1664 * Check if the widget is disabled.
1666 * @return {boolean} Widget is disabled
1668 OO
.ui
.Widget
.prototype.isDisabled = function () {
1669 return this.disabled
;
1673 * Set the 'disabled' state of the widget.
1675 * When a widget is disabled, it cannot be used and its appearance is updated to reflect this state.
1677 * @param {boolean} disabled Disable widget
1680 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1683 this.disabled
= !!disabled
;
1684 isDisabled
= this.isDisabled();
1685 if ( isDisabled
!== this.wasDisabled
) {
1686 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1687 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1688 this.$element
.attr( 'aria-disabled', isDisabled
.toString() );
1689 this.emit( 'disable', isDisabled
);
1690 this.updateThemeClasses();
1692 this.wasDisabled
= isDisabled
;
1698 * Update the disabled state, in case of changes in parent widget.
1702 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1703 this.setDisabled( this.disabled
);
1708 * Get an ID of a labelable node which is part of this widget, if any, to be used for `<label for>`
1711 * If this function returns null, the widget should have a meaningful #simulateLabelClick method
1714 * @return {string|null} The ID of the labelable element
1716 OO
.ui
.Widget
.prototype.getInputId = function () {
1721 * Simulate the behavior of clicking on a label (a HTML `<label>` element) bound to this input.
1722 * HTML only allows `<label>` to act on specific "labelable" elements; complex widgets might need to
1723 * override this method to provide intuitive, accessible behavior.
1725 * By default, this does nothing. OO.ui.mixin.TabIndexedElement overrides it for focusable widgets.
1726 * Individual widgets may override it too.
1728 * This method is called by OO.ui.LabelWidget and OO.ui.FieldLayout. It should not be called
1731 OO
.ui
.Widget
.prototype.simulateLabelClick = function () {
1742 OO
.ui
.Theme
= function OoUiTheme() {
1743 this.elementClassesQueue
= [];
1744 this.debouncedUpdateQueuedElementClasses
= OO
.ui
.debounce( this.updateQueuedElementClasses
);
1749 OO
.initClass( OO
.ui
.Theme
);
1754 * Get a list of classes to be applied to a widget.
1756 * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
1757 * otherwise state transitions will not work properly.
1759 * @param {OO.ui.Element} element Element for which to get classes
1760 * @return {Object.<string,string[]>} Categorized class names with `on` and `off` lists
1762 OO
.ui
.Theme
.prototype.getElementClasses = function () {
1763 return { on
: [], off
: [] };
1767 * Update CSS classes provided by the theme.
1769 * For elements with theme logic hooks, this should be called any time there's a state change.
1771 * @param {OO.ui.Element} element Element for which to update classes
1773 OO
.ui
.Theme
.prototype.updateElementClasses = function ( element
) {
1774 var $elements
= $( [] ),
1775 classes
= this.getElementClasses( element
);
1777 if ( element
.$icon
) {
1778 $elements
= $elements
.add( element
.$icon
);
1780 if ( element
.$indicator
) {
1781 $elements
= $elements
.add( element
.$indicator
);
1785 .removeClass( classes
.off
.join( ' ' ) )
1786 .addClass( classes
.on
.join( ' ' ) );
1792 OO
.ui
.Theme
.prototype.updateQueuedElementClasses = function () {
1794 for ( i
= 0; i
< this.elementClassesQueue
.length
; i
++ ) {
1795 this.updateElementClasses( this.elementClassesQueue
[ i
] );
1798 this.elementClassesQueue
= [];
1802 * Queue #updateElementClasses to be called for this element.
1804 * @localdoc QUnit tests override this method to directly call #queueUpdateElementClasses,
1805 * to make them synchronous.
1807 * @param {OO.ui.Element} element Element for which to update classes
1809 OO
.ui
.Theme
.prototype.queueUpdateElementClasses = function ( element
) {
1810 // Keep items in the queue unique. Use lastIndexOf to start checking from the end because that's
1811 // the most common case (this method is often called repeatedly for the same element).
1812 if ( this.elementClassesQueue
.lastIndexOf( element
) !== -1 ) {
1815 this.elementClassesQueue
.push( element
);
1816 this.debouncedUpdateQueuedElementClasses();
1820 * Get the transition duration in milliseconds for dialogs opening/closing
1822 * The dialog should be fully rendered this many milliseconds after the
1823 * ready process has executed.
1825 * @return {number} Transition duration in milliseconds
1827 OO
.ui
.Theme
.prototype.getDialogTransitionDuration = function () {
1832 * The TabIndexedElement class is an attribute mixin used to add additional functionality to an
1833 * element created by another class. The mixin provides a ‘tabIndex’ property, which specifies the
1834 * order in which users will navigate through the focusable elements via the "tab" key.
1837 * // TabIndexedElement is mixed into the ButtonWidget class
1838 * // to provide a tabIndex property.
1839 * var button1 = new OO.ui.ButtonWidget( {
1843 * var button2 = new OO.ui.ButtonWidget( {
1847 * var button3 = new OO.ui.ButtonWidget( {
1851 * var button4 = new OO.ui.ButtonWidget( {
1855 * $( 'body' ).append( button1.$element, button2.$element, button3.$element, button4.$element );
1861 * @param {Object} [config] Configuration options
1862 * @cfg {jQuery} [$tabIndexed] The element that should use the tabindex functionality. By default,
1863 * the functionality is applied to the element created by the class ($element). If a different element is specified, the tabindex
1864 * functionality will be applied to it instead.
1865 * @cfg {string|number|null} [tabIndex=0] Number that specifies the element’s position in the tab-navigation
1866 * order (e.g., 1 for the first focusable element). Use 0 to use the default navigation order; use -1
1867 * to remove the element from the tab-navigation flow.
1869 OO
.ui
.mixin
.TabIndexedElement
= function OoUiMixinTabIndexedElement( config
) {
1870 // Configuration initialization
1871 config
= $.extend( { tabIndex
: 0 }, config
);
1874 this.$tabIndexed
= null;
1875 this.tabIndex
= null;
1878 this.connect( this, { disable
: 'onTabIndexedElementDisable' } );
1881 this.setTabIndex( config
.tabIndex
);
1882 this.setTabIndexedElement( config
.$tabIndexed
|| this.$element
);
1887 OO
.initClass( OO
.ui
.mixin
.TabIndexedElement
);
1892 * Set the element that should use the tabindex functionality.
1894 * This method is used to retarget a tabindex mixin so that its functionality applies
1895 * to the specified element. If an element is currently using the functionality, the mixin’s
1896 * effect on that element is removed before the new element is set up.
1898 * @param {jQuery} $tabIndexed Element that should use the tabindex functionality
1901 OO
.ui
.mixin
.TabIndexedElement
.prototype.setTabIndexedElement = function ( $tabIndexed
) {
1902 var tabIndex
= this.tabIndex
;
1903 // Remove attributes from old $tabIndexed
1904 this.setTabIndex( null );
1905 // Force update of new $tabIndexed
1906 this.$tabIndexed
= $tabIndexed
;
1907 this.tabIndex
= tabIndex
;
1908 return this.updateTabIndex();
1912 * Set the value of the tabindex.
1914 * @param {string|number|null} tabIndex Tabindex value, or `null` for no tabindex
1917 OO
.ui
.mixin
.TabIndexedElement
.prototype.setTabIndex = function ( tabIndex
) {
1918 tabIndex
= /^-?\d+$/.test( tabIndex
) ? Number( tabIndex
) : null;
1920 if ( this.tabIndex
!== tabIndex
) {
1921 this.tabIndex
= tabIndex
;
1922 this.updateTabIndex();
1929 * Update the `tabindex` attribute, in case of changes to tab index or
1935 OO
.ui
.mixin
.TabIndexedElement
.prototype.updateTabIndex = function () {
1936 if ( this.$tabIndexed
) {
1937 if ( this.tabIndex
!== null ) {
1938 // Do not index over disabled elements
1939 this.$tabIndexed
.attr( {
1940 tabindex
: this.isDisabled() ? -1 : this.tabIndex
,
1941 // Support: ChromeVox and NVDA
1942 // These do not seem to inherit aria-disabled from parent elements
1943 'aria-disabled': this.isDisabled().toString()
1946 this.$tabIndexed
.removeAttr( 'tabindex aria-disabled' );
1953 * Handle disable events.
1956 * @param {boolean} disabled Element is disabled
1958 OO
.ui
.mixin
.TabIndexedElement
.prototype.onTabIndexedElementDisable = function () {
1959 this.updateTabIndex();
1963 * Get the value of the tabindex.
1965 * @return {number|null} Tabindex value
1967 OO
.ui
.mixin
.TabIndexedElement
.prototype.getTabIndex = function () {
1968 return this.tabIndex
;
1972 * Get an ID of a focusable element of this widget, if any, to be used for `<label for>` value.
1974 * If the element already has an ID then that is returned, otherwise unique ID is
1975 * generated, set on the element, and returned.
1977 * @return {string|null} The ID of the focusable element
1979 OO
.ui
.mixin
.TabIndexedElement
.prototype.getInputId = function () {
1982 if ( !this.$tabIndexed
) {
1985 if ( !this.isLabelableNode( this.$tabIndexed
) ) {
1989 id
= this.$tabIndexed
.attr( 'id' );
1990 if ( id
=== undefined ) {
1991 id
= OO
.ui
.generateElementId();
1992 this.$tabIndexed
.attr( 'id', id
);
1999 * Whether the node is 'labelable' according to the HTML spec
2000 * (i.e., whether it can be interacted with through a `<label for="…">`).
2001 * See: <https://html.spec.whatwg.org/multipage/forms.html#category-label>.
2004 * @param {jQuery} $node
2007 OO
.ui
.mixin
.TabIndexedElement
.prototype.isLabelableNode = function ( $node
) {
2009 labelableTags
= [ 'button', 'meter', 'output', 'progress', 'select', 'textarea' ],
2010 tagName
= $node
.prop( 'tagName' ).toLowerCase();
2012 if ( tagName
=== 'input' && $node
.attr( 'type' ) !== 'hidden' ) {
2015 if ( labelableTags
.indexOf( tagName
) !== -1 ) {
2022 * Focus this element.
2026 OO
.ui
.mixin
.TabIndexedElement
.prototype.focus = function () {
2027 if ( !this.isDisabled() ) {
2028 this.$tabIndexed
.focus();
2034 * Blur this element.
2038 OO
.ui
.mixin
.TabIndexedElement
.prototype.blur = function () {
2039 this.$tabIndexed
.blur();
2044 * @inheritdoc OO.ui.Widget
2046 OO
.ui
.mixin
.TabIndexedElement
.prototype.simulateLabelClick = function () {
2051 * ButtonElement is often mixed into other classes to generate a button, which is a clickable
2052 * interface element that can be configured with access keys for accessibility.
2053 * See the [OOjs UI documentation on MediaWiki] [1] for examples.
2055 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Buttons
2061 * @param {Object} [config] Configuration options
2062 * @cfg {jQuery} [$button] The button element created by the class.
2063 * If this configuration is omitted, the button element will use a generated `<a>`.
2064 * @cfg {boolean} [framed=true] Render the button with a frame
2066 OO
.ui
.mixin
.ButtonElement
= function OoUiMixinButtonElement( config
) {
2067 // Configuration initialization
2068 config
= config
|| {};
2071 this.$button
= null;
2073 this.active
= config
.active
!== undefined && config
.active
;
2074 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
2075 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
2076 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
2077 this.onKeyUpHandler
= this.onKeyUp
.bind( this );
2078 this.onClickHandler
= this.onClick
.bind( this );
2079 this.onKeyPressHandler
= this.onKeyPress
.bind( this );
2082 this.$element
.addClass( 'oo-ui-buttonElement' );
2083 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
2084 this.setButtonElement( config
.$button
|| $( '<a>' ) );
2089 OO
.initClass( OO
.ui
.mixin
.ButtonElement
);
2091 /* Static Properties */
2094 * Cancel mouse down events.
2096 * This property is usually set to `true` to prevent the focus from changing when the button is clicked.
2097 * Classes such as {@link OO.ui.mixin.DraggableElement DraggableElement} and {@link OO.ui.ButtonOptionWidget ButtonOptionWidget}
2098 * use a value of `false` so that dragging behavior is possible and mousedown events can be handled by a
2103 * @property {boolean}
2105 OO
.ui
.mixin
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
2110 * A 'click' event is emitted when the button element is clicked.
2118 * Set the button element.
2120 * This method is used to retarget a button mixin so that its functionality applies to
2121 * the specified button element instead of the one created by the class. If a button element
2122 * is already set, the method will remove the mixin’s effect on that element.
2124 * @param {jQuery} $button Element to use as button
2126 OO
.ui
.mixin
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
2127 if ( this.$button
) {
2129 .removeClass( 'oo-ui-buttonElement-button' )
2130 .removeAttr( 'role accesskey' )
2132 mousedown
: this.onMouseDownHandler
,
2133 keydown
: this.onKeyDownHandler
,
2134 click
: this.onClickHandler
,
2135 keypress
: this.onKeyPressHandler
2139 this.$button
= $button
2140 .addClass( 'oo-ui-buttonElement-button' )
2142 mousedown
: this.onMouseDownHandler
,
2143 keydown
: this.onKeyDownHandler
,
2144 click
: this.onClickHandler
,
2145 keypress
: this.onKeyPressHandler
2148 // Add `role="button"` on `<a>` elements, where it's needed
2149 // `toUppercase()` is added for XHTML documents
2150 if ( this.$button
.prop( 'tagName' ).toUpperCase() === 'A' ) {
2151 this.$button
.attr( 'role', 'button' );
2156 * Handles mouse down events.
2159 * @param {jQuery.Event} e Mouse down event
2161 OO
.ui
.mixin
.ButtonElement
.prototype.onMouseDown = function ( e
) {
2162 if ( this.isDisabled() || e
.which
!== OO
.ui
.MouseButtons
.LEFT
) {
2165 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
2166 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
2167 // reliably remove the pressed class
2168 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
2169 // Prevent change of focus unless specifically configured otherwise
2170 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
2176 * Handles mouse up events.
2179 * @param {MouseEvent} e Mouse up event
2181 OO
.ui
.mixin
.ButtonElement
.prototype.onMouseUp = function ( e
) {
2182 if ( this.isDisabled() || e
.which
!== OO
.ui
.MouseButtons
.LEFT
) {
2185 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
2186 // Stop listening for mouseup, since we only needed this once
2187 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
2191 * Handles mouse click events.
2194 * @param {jQuery.Event} e Mouse click event
2197 OO
.ui
.mixin
.ButtonElement
.prototype.onClick = function ( e
) {
2198 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
2199 if ( this.emit( 'click' ) ) {
2206 * Handles key down events.
2209 * @param {jQuery.Event} e Key down event
2211 OO
.ui
.mixin
.ButtonElement
.prototype.onKeyDown = function ( e
) {
2212 if ( this.isDisabled() || ( e
.which
!== OO
.ui
.Keys
.SPACE
&& e
.which
!== OO
.ui
.Keys
.ENTER
) ) {
2215 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
2216 // Run the keyup handler no matter where the key is when the button is let go, so we can
2217 // reliably remove the pressed class
2218 this.getElementDocument().addEventListener( 'keyup', this.onKeyUpHandler
, true );
2222 * Handles key up events.
2225 * @param {KeyboardEvent} e Key up event
2227 OO
.ui
.mixin
.ButtonElement
.prototype.onKeyUp = function ( e
) {
2228 if ( this.isDisabled() || ( e
.which
!== OO
.ui
.Keys
.SPACE
&& e
.which
!== OO
.ui
.Keys
.ENTER
) ) {
2231 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
2232 // Stop listening for keyup, since we only needed this once
2233 this.getElementDocument().removeEventListener( 'keyup', this.onKeyUpHandler
, true );
2237 * Handles key press events.
2240 * @param {jQuery.Event} e Key press event
2243 OO
.ui
.mixin
.ButtonElement
.prototype.onKeyPress = function ( e
) {
2244 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
2245 if ( this.emit( 'click' ) ) {
2252 * Check if button has a frame.
2254 * @return {boolean} Button is framed
2256 OO
.ui
.mixin
.ButtonElement
.prototype.isFramed = function () {
2261 * Render the button with or without a frame. Omit the `framed` parameter to toggle the button frame on and off.
2263 * @param {boolean} [framed] Make button framed, omit to toggle
2266 OO
.ui
.mixin
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
2267 framed
= framed
=== undefined ? !this.framed
: !!framed
;
2268 if ( framed
!== this.framed
) {
2269 this.framed
= framed
;
2271 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
2272 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
2273 this.updateThemeClasses();
2280 * Set the button's active state.
2282 * The active state can be set on:
2284 * - {@link OO.ui.ButtonOptionWidget ButtonOptionWidget} when it is selected
2285 * - {@link OO.ui.ToggleButtonWidget ToggleButtonWidget} when it is toggle on
2286 * - {@link OO.ui.ButtonWidget ButtonWidget} when clicking the button would only refresh the page
2289 * @param {boolean} value Make button active
2292 OO
.ui
.mixin
.ButtonElement
.prototype.setActive = function ( value
) {
2293 this.active
= !!value
;
2294 this.$element
.toggleClass( 'oo-ui-buttonElement-active', this.active
);
2295 this.updateThemeClasses();
2300 * Check if the button is active
2303 * @return {boolean} The button is active
2305 OO
.ui
.mixin
.ButtonElement
.prototype.isActive = function () {
2310 * Any OOjs UI widget that contains other widgets (such as {@link OO.ui.ButtonWidget buttons} or
2311 * {@link OO.ui.OptionWidget options}) mixes in GroupElement. Adding, removing, and clearing
2312 * items from the group is done through the interface the class provides.
2313 * For more information, please see the [OOjs UI documentation on MediaWiki] [1].
2315 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Elements/Groups
2318 * @mixins OO.EmitterList
2322 * @param {Object} [config] Configuration options
2323 * @cfg {jQuery} [$group] The container element created by the class. If this configuration
2324 * is omitted, the group element will use a generated `<div>`.
2326 OO
.ui
.mixin
.GroupElement
= function OoUiMixinGroupElement( config
) {
2327 // Configuration initialization
2328 config
= config
|| {};
2330 // Mixin constructors
2331 OO
.EmitterList
.call( this, config
);
2337 this.setGroupElement( config
.$group
|| $( '<div>' ) );
2342 OO
.mixinClass( OO
.ui
.mixin
.GroupElement
, OO
.EmitterList
);
2349 * A change event is emitted when the set of selected items changes.
2351 * @param {OO.ui.Element[]} items Items currently in the group
2357 * Set the group element.
2359 * If an element is already set, items will be moved to the new element.
2361 * @param {jQuery} $group Element to use as group
2363 OO
.ui
.mixin
.GroupElement
.prototype.setGroupElement = function ( $group
) {
2366 this.$group
= $group
;
2367 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2368 this.$group
.append( this.items
[ i
].$element
);
2373 * Get an item by its data.
2375 * Only the first item with matching data will be returned. To return all matching items,
2376 * use the #getItemsFromData method.
2378 * @param {Object} data Item data to search for
2379 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
2381 OO
.ui
.mixin
.GroupElement
.prototype.getItemFromData = function ( data
) {
2383 hash
= OO
.getHash( data
);
2385 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2386 item
= this.items
[ i
];
2387 if ( hash
=== OO
.getHash( item
.getData() ) ) {
2396 * Get items by their data.
2398 * All items with matching data will be returned. To return only the first match, use the #getItemFromData method instead.
2400 * @param {Object} data Item data to search for
2401 * @return {OO.ui.Element[]} Items with equivalent data
2403 OO
.ui
.mixin
.GroupElement
.prototype.getItemsFromData = function ( data
) {
2405 hash
= OO
.getHash( data
),
2408 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2409 item
= this.items
[ i
];
2410 if ( hash
=== OO
.getHash( item
.getData() ) ) {
2419 * Add items to the group.
2421 * Items will be added to the end of the group array unless the optional `index` parameter specifies
2422 * a different insertion point. Adding an existing item will move it to the end of the array or the point specified by the `index`.
2424 * @param {OO.ui.Element[]} items An array of items to add to the group
2425 * @param {number} [index] Index of the insertion point
2428 OO
.ui
.mixin
.GroupElement
.prototype.addItems = function ( items
, index
) {
2430 OO
.EmitterList
.prototype.addItems
.call( this, items
, index
);
2432 this.emit( 'change', this.getItems() );
2439 OO
.ui
.mixin
.GroupElement
.prototype.moveItem = function ( items
, newIndex
) {
2440 // insertItemElements expects this.items to not have been modified yet, so call before the mixin
2441 this.insertItemElements( items
, newIndex
);
2444 newIndex
= OO
.EmitterList
.prototype.moveItem
.call( this, items
, newIndex
);
2452 OO
.ui
.mixin
.GroupElement
.prototype.insertItem = function ( item
, index
) {
2453 item
.setElementGroup( this );
2454 this.insertItemElements( item
, index
);
2457 index
= OO
.EmitterList
.prototype.insertItem
.call( this, item
, index
);
2463 * Insert elements into the group
2466 * @param {OO.ui.Element} itemWidget Item to insert
2467 * @param {number} index Insertion index
2469 OO
.ui
.mixin
.GroupElement
.prototype.insertItemElements = function ( itemWidget
, index
) {
2470 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
2471 this.$group
.append( itemWidget
.$element
);
2472 } else if ( index
=== 0 ) {
2473 this.$group
.prepend( itemWidget
.$element
);
2475 this.items
[ index
].$element
.before( itemWidget
.$element
);
2480 * Remove the specified items from a group.
2482 * Removed items are detached (not removed) from the DOM so that they may be reused.
2483 * To remove all items from a group, you may wish to use the #clearItems method instead.
2485 * @param {OO.ui.Element[]} items An array of items to remove
2488 OO
.ui
.mixin
.GroupElement
.prototype.removeItems = function ( items
) {
2489 var i
, len
, item
, index
;
2491 // Remove specific items elements
2492 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
2494 index
= this.items
.indexOf( item
);
2495 if ( index
!== -1 ) {
2496 item
.setElementGroup( null );
2497 item
.$element
.detach();
2502 OO
.EmitterList
.prototype.removeItems
.call( this, items
);
2504 this.emit( 'change', this.getItems() );
2509 * Clear all items from the group.
2511 * Cleared items are detached from the DOM, not removed, so that they may be reused.
2512 * To remove only a subset of items from a group, use the #removeItems method.
2516 OO
.ui
.mixin
.GroupElement
.prototype.clearItems = function () {
2519 // Remove all item elements
2520 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
2521 this.items
[ i
].setElementGroup( null );
2522 this.items
[ i
].$element
.detach();
2526 OO
.EmitterList
.prototype.clearItems
.call( this );
2528 this.emit( 'change', this.getItems() );
2533 * IconElement is often mixed into other classes to generate an icon.
2534 * Icons are graphics, about the size of normal text. They are used to aid the user
2535 * in locating a control or to convey information in a space-efficient way. See the
2536 * [OOjs UI documentation on MediaWiki] [1] for a list of icons
2537 * included in the library.
2539 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Icons
2545 * @param {Object} [config] Configuration options
2546 * @cfg {jQuery} [$icon] The icon element created by the class. If this configuration is omitted,
2547 * the icon element will use a generated `<span>`. To use a different HTML tag, or to specify that
2548 * the icon element be set to an existing icon instead of the one generated by this class, set a
2549 * value using a jQuery selection. For example:
2551 * // Use a <div> tag instead of a <span>
2553 * // Use an existing icon element instead of the one generated by the class
2554 * $icon: this.$element
2555 * // Use an icon element from a child widget
2556 * $icon: this.childwidget.$element
2557 * @cfg {Object|string} [icon=''] The symbolic name of the icon (e.g., ‘remove’ or ‘menu’), or a map of
2558 * symbolic names. A map is used for i18n purposes and contains a `default` icon
2559 * name and additional names keyed by language code. The `default` name is used when no icon is keyed
2560 * by the user's language.
2562 * Example of an i18n map:
2564 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
2565 * See the [OOjs UI documentation on MediaWiki] [2] for a list of icons included in the library.
2566 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Icons
2567 * @cfg {string|Function} [iconTitle] A text string used as the icon title, or a function that returns title
2568 * text. The icon title is displayed when users move the mouse over the icon.
2570 OO
.ui
.mixin
.IconElement
= function OoUiMixinIconElement( config
) {
2571 // Configuration initialization
2572 config
= config
|| {};
2577 this.iconTitle
= null;
2580 this.setIcon( config
.icon
|| this.constructor.static.icon
);
2581 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
2582 this.setIconElement( config
.$icon
|| $( '<span>' ) );
2587 OO
.initClass( OO
.ui
.mixin
.IconElement
);
2589 /* Static Properties */
2592 * The symbolic name of the icon (e.g., ‘remove’ or ‘menu’), or a map of symbolic names. A map is used
2593 * for i18n purposes and contains a `default` icon name and additional names keyed by
2594 * language code. The `default` name is used when no icon is keyed by the user's language.
2596 * Example of an i18n map:
2598 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
2600 * Note: the static property will be overridden if the #icon configuration is used.
2604 * @property {Object|string}
2606 OO
.ui
.mixin
.IconElement
.static.icon
= null;
2609 * The icon title, displayed when users move the mouse over the icon. The value can be text, a
2610 * function that returns title text, or `null` for no title.
2612 * The static property will be overridden if the #iconTitle configuration is used.
2616 * @property {string|Function|null}
2618 OO
.ui
.mixin
.IconElement
.static.iconTitle
= null;
2623 * Set the icon element. This method is used to retarget an icon mixin so that its functionality
2624 * applies to the specified icon element instead of the one created by the class. If an icon
2625 * element is already set, the mixin’s effect on that element is removed. Generated CSS classes
2626 * and mixin methods will no longer affect the element.
2628 * @param {jQuery} $icon Element to use as icon
2630 OO
.ui
.mixin
.IconElement
.prototype.setIconElement = function ( $icon
) {
2633 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
2634 .removeAttr( 'title' );
2638 .addClass( 'oo-ui-iconElement-icon' )
2639 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
2640 if ( this.iconTitle
!== null ) {
2641 this.$icon
.attr( 'title', this.iconTitle
);
2644 this.updateThemeClasses();
2648 * Set icon by symbolic name (e.g., ‘remove’ or ‘menu’). Use `null` to remove an icon.
2649 * The icon parameter can also be set to a map of icon names. See the #icon config setting
2652 * @param {Object|string|null} icon A symbolic icon name, a {@link #icon map of icon names} keyed
2653 * by language code, or `null` to remove the icon.
2656 OO
.ui
.mixin
.IconElement
.prototype.setIcon = function ( icon
) {
2657 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
2658 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
2660 if ( this.icon
!== icon
) {
2662 if ( this.icon
!== null ) {
2663 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
2665 if ( icon
!== null ) {
2666 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
2672 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
2673 this.updateThemeClasses();
2679 * Set the icon title. Use `null` to remove the title.
2681 * @param {string|Function|null} iconTitle A text string used as the icon title,
2682 * a function that returns title text, or `null` for no title.
2685 OO
.ui
.mixin
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
2687 ( typeof iconTitle
=== 'function' || ( typeof iconTitle
=== 'string' && iconTitle
.length
) ) ?
2688 OO
.ui
.resolveMsg( iconTitle
) : null;
2690 if ( this.iconTitle
!== iconTitle
) {
2691 this.iconTitle
= iconTitle
;
2693 if ( this.iconTitle
!== null ) {
2694 this.$icon
.attr( 'title', iconTitle
);
2696 this.$icon
.removeAttr( 'title' );
2705 * Get the symbolic name of the icon.
2707 * @return {string} Icon name
2709 OO
.ui
.mixin
.IconElement
.prototype.getIcon = function () {
2714 * Get the icon title. The title text is displayed when a user moves the mouse over the icon.
2716 * @return {string} Icon title text
2718 OO
.ui
.mixin
.IconElement
.prototype.getIconTitle = function () {
2719 return this.iconTitle
;
2723 * IndicatorElement is often mixed into other classes to generate an indicator.
2724 * Indicators are small graphics that are generally used in two ways:
2726 * - To draw attention to the status of an item. For example, an indicator might be
2727 * used to show that an item in a list has errors that need to be resolved.
2728 * - To clarify the function of a control that acts in an exceptional way (a button
2729 * that opens a menu instead of performing an action directly, for example).
2731 * For a list of indicators included in the library, please see the
2732 * [OOjs UI documentation on MediaWiki] [1].
2734 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Indicators
2740 * @param {Object} [config] Configuration options
2741 * @cfg {jQuery} [$indicator] The indicator element created by the class. If this
2742 * configuration is omitted, the indicator element will use a generated `<span>`.
2743 * @cfg {string} [indicator] Symbolic name of the indicator (e.g., ‘alert’ or ‘down’).
2744 * See the [OOjs UI documentation on MediaWiki][2] for a list of indicators included
2746 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Indicators
2747 * @cfg {string|Function} [indicatorTitle] A text string used as the indicator title,
2748 * or a function that returns title text. The indicator title is displayed when users move
2749 * the mouse over the indicator.
2751 OO
.ui
.mixin
.IndicatorElement
= function OoUiMixinIndicatorElement( config
) {
2752 // Configuration initialization
2753 config
= config
|| {};
2756 this.$indicator
= null;
2757 this.indicator
= null;
2758 this.indicatorTitle
= null;
2761 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
2762 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
2763 this.setIndicatorElement( config
.$indicator
|| $( '<span>' ) );
2768 OO
.initClass( OO
.ui
.mixin
.IndicatorElement
);
2770 /* Static Properties */
2773 * Symbolic name of the indicator (e.g., ‘alert’ or ‘down’).
2774 * The static property will be overridden if the #indicator configuration is used.
2778 * @property {string|null}
2780 OO
.ui
.mixin
.IndicatorElement
.static.indicator
= null;
2783 * A text string used as the indicator title, a function that returns title text, or `null`
2784 * for no title. The static property will be overridden if the #indicatorTitle configuration is used.
2788 * @property {string|Function|null}
2790 OO
.ui
.mixin
.IndicatorElement
.static.indicatorTitle
= null;
2795 * Set the indicator element.
2797 * If an element is already set, it will be cleaned up before setting up the new element.
2799 * @param {jQuery} $indicator Element to use as indicator
2801 OO
.ui
.mixin
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
2802 if ( this.$indicator
) {
2804 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
2805 .removeAttr( 'title' );
2808 this.$indicator
= $indicator
2809 .addClass( 'oo-ui-indicatorElement-indicator' )
2810 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
2811 if ( this.indicatorTitle
!== null ) {
2812 this.$indicator
.attr( 'title', this.indicatorTitle
);
2815 this.updateThemeClasses();
2819 * Set the indicator by its symbolic name: ‘alert’, ‘down’, ‘next’, ‘previous’, ‘required’, ‘up’. Use `null` to remove the indicator.
2821 * @param {string|null} indicator Symbolic name of indicator, or `null` for no indicator
2824 OO
.ui
.mixin
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
2825 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
2827 if ( this.indicator
!== indicator
) {
2828 if ( this.$indicator
) {
2829 if ( this.indicator
!== null ) {
2830 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
2832 if ( indicator
!== null ) {
2833 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
2836 this.indicator
= indicator
;
2839 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
2840 this.updateThemeClasses();
2846 * Set the indicator title.
2848 * The title is displayed when a user moves the mouse over the indicator.
2850 * @param {string|Function|null} indicatorTitle Indicator title text, a function that returns text, or
2851 * `null` for no indicator title
2854 OO
.ui
.mixin
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
2856 ( typeof indicatorTitle
=== 'function' || ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ) ?
2857 OO
.ui
.resolveMsg( indicatorTitle
) : null;
2859 if ( this.indicatorTitle
!== indicatorTitle
) {
2860 this.indicatorTitle
= indicatorTitle
;
2861 if ( this.$indicator
) {
2862 if ( this.indicatorTitle
!== null ) {
2863 this.$indicator
.attr( 'title', indicatorTitle
);
2865 this.$indicator
.removeAttr( 'title' );
2874 * Get the symbolic name of the indicator (e.g., ‘alert’ or ‘down’).
2876 * @return {string} Symbolic name of indicator
2878 OO
.ui
.mixin
.IndicatorElement
.prototype.getIndicator = function () {
2879 return this.indicator
;
2883 * Get the indicator title.
2885 * The title is displayed when a user moves the mouse over the indicator.
2887 * @return {string} Indicator title text
2889 OO
.ui
.mixin
.IndicatorElement
.prototype.getIndicatorTitle = function () {
2890 return this.indicatorTitle
;
2894 * LabelElement is often mixed into other classes to generate a label, which
2895 * helps identify the function of an interface element.
2896 * See the [OOjs UI documentation on MediaWiki] [1] for more information.
2898 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Labels
2904 * @param {Object} [config] Configuration options
2905 * @cfg {jQuery} [$label] The label element created by the class. If this
2906 * configuration is omitted, the label element will use a generated `<span>`.
2907 * @cfg {jQuery|string|Function|OO.ui.HtmlSnippet} [label] The label text. The label can be specified
2908 * as a plaintext string, a jQuery selection of elements, or a function that will produce a string
2909 * in the future. See the [OOjs UI documentation on MediaWiki] [2] for examples.
2910 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Labels
2912 OO
.ui
.mixin
.LabelElement
= function OoUiMixinLabelElement( config
) {
2913 // Configuration initialization
2914 config
= config
|| {};
2921 this.setLabel( config
.label
|| this.constructor.static.label
);
2922 this.setLabelElement( config
.$label
|| $( '<span>' ) );
2927 OO
.initClass( OO
.ui
.mixin
.LabelElement
);
2932 * @event labelChange
2933 * @param {string} value
2936 /* Static Properties */
2939 * The label text. The label can be specified as a plaintext string, a function that will
2940 * produce a string in the future, or `null` for no label. The static value will
2941 * be overridden if a label is specified with the #label config option.
2945 * @property {string|Function|null}
2947 OO
.ui
.mixin
.LabelElement
.static.label
= null;
2949 /* Static methods */
2952 * Highlight the first occurrence of the query in the given text
2954 * @param {string} text Text
2955 * @param {string} query Query to find
2956 * @return {jQuery} Text with the first match of the query
2957 * sub-string wrapped in highlighted span
2959 OO
.ui
.mixin
.LabelElement
.static.highlightQuery = function ( text
, query
) {
2960 var $result
= $( '<span>' ),
2961 offset
= text
.toLowerCase().indexOf( query
.toLowerCase() );
2963 if ( !query
.length
|| offset
=== -1 ) {
2964 return $result
.text( text
);
2967 document
.createTextNode( text
.slice( 0, offset
) ),
2969 .addClass( 'oo-ui-labelElement-label-highlight' )
2970 .text( text
.slice( offset
, offset
+ query
.length
) ),
2971 document
.createTextNode( text
.slice( offset
+ query
.length
) )
2973 return $result
.contents();
2979 * Set the label element.
2981 * If an element is already set, it will be cleaned up before setting up the new element.
2983 * @param {jQuery} $label Element to use as label
2985 OO
.ui
.mixin
.LabelElement
.prototype.setLabelElement = function ( $label
) {
2986 if ( this.$label
) {
2987 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
2990 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
2991 this.setLabelContent( this.label
);
2997 * An empty string will result in the label being hidden. A string containing only whitespace will
2998 * be converted to a single ` `.
3000 * @param {jQuery|string|OO.ui.HtmlSnippet|Function|null} label Label nodes; text; a function that returns nodes or
3001 * text; or null for no label
3004 OO
.ui
.mixin
.LabelElement
.prototype.setLabel = function ( label
) {
3005 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
3006 label
= ( ( typeof label
=== 'string' || label
instanceof jQuery
) && label
.length
) || ( label
instanceof OO
.ui
.HtmlSnippet
&& label
.toString().length
) ? label
: null;
3008 if ( this.label
!== label
) {
3009 if ( this.$label
) {
3010 this.setLabelContent( label
);
3013 this.emit( 'labelChange' );
3016 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
3022 * Set the label as plain text with a highlighted query
3024 * @param {string} text Text label to set
3025 * @param {string} query Substring of text to highlight
3028 OO
.ui
.mixin
.LabelElement
.prototype.setHighlightedQuery = function ( text
, query
) {
3029 return this.setLabel( this.constructor.static.highlightQuery( text
, query
) );
3035 * @return {jQuery|string|Function|null} Label nodes; text; a function that returns nodes or
3036 * text; or null for no label
3038 OO
.ui
.mixin
.LabelElement
.prototype.getLabel = function () {
3043 * Set the content of the label.
3045 * Do not call this method until after the label element has been set by #setLabelElement.
3048 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
3049 * text; or null for no label
3051 OO
.ui
.mixin
.LabelElement
.prototype.setLabelContent = function ( label
) {
3052 if ( typeof label
=== 'string' ) {
3053 if ( label
.match( /^\s*$/ ) ) {
3054 // Convert whitespace only string to a single non-breaking space
3055 this.$label
.html( ' ' );
3057 this.$label
.text( label
);
3059 } else if ( label
instanceof OO
.ui
.HtmlSnippet
) {
3060 this.$label
.html( label
.toString() );
3061 } else if ( label
instanceof jQuery
) {
3062 this.$label
.empty().append( label
);
3064 this.$label
.empty();
3069 * The FlaggedElement class is an attribute mixin, meaning that it is used to add
3070 * additional functionality to an element created by another class. The class provides
3071 * a ‘flags’ property assigned the name (or an array of names) of styling flags,
3072 * which are used to customize the look and feel of a widget to better describe its
3073 * importance and functionality.
3075 * The library currently contains the following styling flags for general use:
3077 * - **progressive**: Progressive styling is applied to convey that the widget will move the user forward in a process.
3078 * - **destructive**: Destructive styling is applied to convey that the widget will remove something.
3079 * - **constructive**: Constructive styling is applied to convey that the widget will create something.
3081 * The flags affect the appearance of the buttons:
3084 * // FlaggedElement is mixed into ButtonWidget to provide styling flags
3085 * var button1 = new OO.ui.ButtonWidget( {
3086 * label: 'Constructive',
3087 * flags: 'constructive'
3089 * var button2 = new OO.ui.ButtonWidget( {
3090 * label: 'Destructive',
3091 * flags: 'destructive'
3093 * var button3 = new OO.ui.ButtonWidget( {
3094 * label: 'Progressive',
3095 * flags: 'progressive'
3097 * $( 'body' ).append( button1.$element, button2.$element, button3.$element );
3099 * {@link OO.ui.ActionWidget ActionWidgets}, which are a special kind of button that execute an action, use these flags: **primary** and **safe**.
3100 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information.
3102 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Elements/Flagged
3108 * @param {Object} [config] Configuration options
3109 * @cfg {string|string[]} [flags] The name or names of the flags (e.g., 'constructive' or 'primary') to apply.
3110 * Please see the [OOjs UI documentation on MediaWiki] [2] for more information about available flags.
3111 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Elements/Flagged
3112 * @cfg {jQuery} [$flagged] The flagged element. By default,
3113 * the flagged functionality is applied to the element created by the class ($element).
3114 * If a different element is specified, the flagged functionality will be applied to it instead.
3116 OO
.ui
.mixin
.FlaggedElement
= function OoUiMixinFlaggedElement( config
) {
3117 // Configuration initialization
3118 config
= config
|| {};
3122 this.$flagged
= null;
3125 this.setFlags( config
.flags
);
3126 this.setFlaggedElement( config
.$flagged
|| this.$element
);
3133 * A flag event is emitted when the #clearFlags or #setFlags methods are used. The `changes`
3134 * parameter contains the name of each modified flag and indicates whether it was
3137 * @param {Object.<string,boolean>} changes Object keyed by flag name. A Boolean `true` indicates
3138 * that the flag was added, `false` that the flag was removed.
3144 * Set the flagged element.
3146 * This method is used to retarget a flagged mixin so that its functionality applies to the specified element.
3147 * If an element is already set, the method will remove the mixin’s effect on that element.
3149 * @param {jQuery} $flagged Element that should be flagged
3151 OO
.ui
.mixin
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
3152 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
3153 return 'oo-ui-flaggedElement-' + flag
;
3156 if ( this.$flagged
) {
3157 this.$flagged
.removeClass( classNames
);
3160 this.$flagged
= $flagged
.addClass( classNames
);
3164 * Check if the specified flag is set.
3166 * @param {string} flag Name of flag
3167 * @return {boolean} The flag is set
3169 OO
.ui
.mixin
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
3170 // This may be called before the constructor, thus before this.flags is set
3171 return this.flags
&& ( flag
in this.flags
);
3175 * Get the names of all flags set.
3177 * @return {string[]} Flag names
3179 OO
.ui
.mixin
.FlaggedElement
.prototype.getFlags = function () {
3180 // This may be called before the constructor, thus before this.flags is set
3181 return Object
.keys( this.flags
|| {} );
3190 OO
.ui
.mixin
.FlaggedElement
.prototype.clearFlags = function () {
3191 var flag
, className
,
3194 classPrefix
= 'oo-ui-flaggedElement-';
3196 for ( flag
in this.flags
) {
3197 className
= classPrefix
+ flag
;
3198 changes
[ flag
] = false;
3199 delete this.flags
[ flag
];
3200 remove
.push( className
);
3203 if ( this.$flagged
) {
3204 this.$flagged
.removeClass( remove
.join( ' ' ) );
3207 this.updateThemeClasses();
3208 this.emit( 'flag', changes
);
3214 * Add one or more flags.
3216 * @param {string|string[]|Object.<string, boolean>} flags A flag name, an array of flag names,
3217 * or an object keyed by flag name with a boolean value that indicates whether the flag should
3218 * be added (`true`) or removed (`false`).
3222 OO
.ui
.mixin
.FlaggedElement
.prototype.setFlags = function ( flags
) {
3223 var i
, len
, flag
, className
,
3227 classPrefix
= 'oo-ui-flaggedElement-';
3229 if ( typeof flags
=== 'string' ) {
3230 className
= classPrefix
+ flags
;
3232 if ( !this.flags
[ flags
] ) {
3233 this.flags
[ flags
] = true;
3234 add
.push( className
);
3236 } else if ( Array
.isArray( flags
) ) {
3237 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
3239 className
= classPrefix
+ flag
;
3241 if ( !this.flags
[ flag
] ) {
3242 changes
[ flag
] = true;
3243 this.flags
[ flag
] = true;
3244 add
.push( className
);
3247 } else if ( OO
.isPlainObject( flags
) ) {
3248 for ( flag
in flags
) {
3249 className
= classPrefix
+ flag
;
3250 if ( flags
[ flag
] ) {
3252 if ( !this.flags
[ flag
] ) {
3253 changes
[ flag
] = true;
3254 this.flags
[ flag
] = true;
3255 add
.push( className
);
3259 if ( this.flags
[ flag
] ) {
3260 changes
[ flag
] = false;
3261 delete this.flags
[ flag
];
3262 remove
.push( className
);
3268 if ( this.$flagged
) {
3270 .addClass( add
.join( ' ' ) )
3271 .removeClass( remove
.join( ' ' ) );
3274 this.updateThemeClasses();
3275 this.emit( 'flag', changes
);
3281 * TitledElement is mixed into other classes to provide a `title` attribute.
3282 * Titles are rendered by the browser and are made visible when the user moves
3283 * the mouse over the element. Titles are not visible on touch devices.
3286 * // TitledElement provides a 'title' attribute to the
3287 * // ButtonWidget class
3288 * var button = new OO.ui.ButtonWidget( {
3289 * label: 'Button with Title',
3290 * title: 'I am a button'
3292 * $( 'body' ).append( button.$element );
3298 * @param {Object} [config] Configuration options
3299 * @cfg {jQuery} [$titled] The element to which the `title` attribute is applied.
3300 * If this config is omitted, the title functionality is applied to $element, the
3301 * element created by the class.
3302 * @cfg {string|Function} [title] The title text or a function that returns text. If
3303 * this config is omitted, the value of the {@link #static-title static title} property is used.
3305 OO
.ui
.mixin
.TitledElement
= function OoUiMixinTitledElement( config
) {
3306 // Configuration initialization
3307 config
= config
|| {};
3310 this.$titled
= null;
3314 this.setTitle( config
.title
!== undefined ? config
.title
: this.constructor.static.title
);
3315 this.setTitledElement( config
.$titled
|| this.$element
);
3320 OO
.initClass( OO
.ui
.mixin
.TitledElement
);
3322 /* Static Properties */
3325 * The title text, a function that returns text, or `null` for no title. The value of the static property
3326 * is overridden if the #title config option is used.
3330 * @property {string|Function|null}
3332 OO
.ui
.mixin
.TitledElement
.static.title
= null;
3337 * Set the titled element.
3339 * This method is used to retarget a titledElement mixin so that its functionality applies to the specified element.
3340 * If an element is already set, the mixin’s effect on that element is removed before the new element is set up.
3342 * @param {jQuery} $titled Element that should use the 'titled' functionality
3344 OO
.ui
.mixin
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
3345 if ( this.$titled
) {
3346 this.$titled
.removeAttr( 'title' );
3349 this.$titled
= $titled
;
3358 * @param {string|Function|null} title Title text, a function that returns text, or `null` for no title
3361 OO
.ui
.mixin
.TitledElement
.prototype.setTitle = function ( title
) {
3362 title
= typeof title
=== 'function' ? OO
.ui
.resolveMsg( title
) : title
;
3363 title
= ( typeof title
=== 'string' && title
.length
) ? title
: null;
3365 if ( this.title
!== title
) {
3374 * Update the title attribute, in case of changes to title or accessKey.
3379 OO
.ui
.mixin
.TitledElement
.prototype.updateTitle = function () {
3380 var title
= this.getTitle();
3381 if ( this.$titled
) {
3382 if ( title
!== null ) {
3383 // Only if this is an AccessKeyedElement
3384 if ( this.formatTitleWithAccessKey
) {
3385 title
= this.formatTitleWithAccessKey( title
);
3387 this.$titled
.attr( 'title', title
);
3389 this.$titled
.removeAttr( 'title' );
3398 * @return {string} Title string
3400 OO
.ui
.mixin
.TitledElement
.prototype.getTitle = function () {
3405 * AccessKeyedElement is mixed into other classes to provide an `accesskey` attribute.
3406 * Accesskeys allow an user to go to a specific element by using
3407 * a shortcut combination of a browser specific keys + the key
3411 * // AccessKeyedElement provides an 'accesskey' attribute to the
3412 * // ButtonWidget class
3413 * var button = new OO.ui.ButtonWidget( {
3414 * label: 'Button with Accesskey',
3417 * $( 'body' ).append( button.$element );
3423 * @param {Object} [config] Configuration options
3424 * @cfg {jQuery} [$accessKeyed] The element to which the `accesskey` attribute is applied.
3425 * If this config is omitted, the accesskey functionality is applied to $element, the
3426 * element created by the class.
3427 * @cfg {string|Function} [accessKey] The key or a function that returns the key. If
3428 * this config is omitted, no accesskey will be added.
3430 OO
.ui
.mixin
.AccessKeyedElement
= function OoUiMixinAccessKeyedElement( config
) {
3431 // Configuration initialization
3432 config
= config
|| {};
3435 this.$accessKeyed
= null;
3436 this.accessKey
= null;
3439 this.setAccessKey( config
.accessKey
|| null );
3440 this.setAccessKeyedElement( config
.$accessKeyed
|| this.$element
);
3442 // If this is also a TitledElement and it initialized before we did, we may have
3443 // to update the title with the access key
3444 if ( this.updateTitle
) {
3451 OO
.initClass( OO
.ui
.mixin
.AccessKeyedElement
);
3453 /* Static Properties */
3456 * The access key, a function that returns a key, or `null` for no accesskey.
3460 * @property {string|Function|null}
3462 OO
.ui
.mixin
.AccessKeyedElement
.static.accessKey
= null;
3467 * Set the accesskeyed element.
3469 * This method is used to retarget a AccessKeyedElement mixin so that its functionality applies to the specified element.
3470 * If an element is already set, the mixin's effect on that element is removed before the new element is set up.
3472 * @param {jQuery} $accessKeyed Element that should use the 'accesskeyes' functionality
3474 OO
.ui
.mixin
.AccessKeyedElement
.prototype.setAccessKeyedElement = function ( $accessKeyed
) {
3475 if ( this.$accessKeyed
) {
3476 this.$accessKeyed
.removeAttr( 'accesskey' );
3479 this.$accessKeyed
= $accessKeyed
;
3480 if ( this.accessKey
) {
3481 this.$accessKeyed
.attr( 'accesskey', this.accessKey
);
3488 * @param {string|Function|null} accessKey Key, a function that returns a key, or `null` for no accesskey
3491 OO
.ui
.mixin
.AccessKeyedElement
.prototype.setAccessKey = function ( accessKey
) {
3492 accessKey
= typeof accessKey
=== 'string' ? OO
.ui
.resolveMsg( accessKey
) : null;
3494 if ( this.accessKey
!== accessKey
) {
3495 if ( this.$accessKeyed
) {
3496 if ( accessKey
!== null ) {
3497 this.$accessKeyed
.attr( 'accesskey', accessKey
);
3499 this.$accessKeyed
.removeAttr( 'accesskey' );
3502 this.accessKey
= accessKey
;
3504 // Only if this is a TitledElement
3505 if ( this.updateTitle
) {
3516 * @return {string} accessKey string
3518 OO
.ui
.mixin
.AccessKeyedElement
.prototype.getAccessKey = function () {
3519 return this.accessKey
;
3523 * Add information about the access key to the element's tooltip label.
3524 * (This is only public for hacky usage in FieldLayout.)
3526 * @param {string} title Tooltip label for `title` attribute
3529 OO
.ui
.mixin
.AccessKeyedElement
.prototype.formatTitleWithAccessKey = function ( title
) {
3532 if ( !this.$accessKeyed
) {
3533 // Not initialized yet; the constructor will call updateTitle() which will rerun this function
3536 // Use jquery.accessKeyLabel if available to show modifiers, otherwise just display the single key
3537 if ( $.fn
.updateTooltipAccessKeys
&& $.fn
.updateTooltipAccessKeys
.getAccessKeyLabel
) {
3538 accessKey
= $.fn
.updateTooltipAccessKeys
.getAccessKeyLabel( this.$accessKeyed
[ 0 ] );
3540 accessKey
= this.getAccessKey();
3543 title
+= ' [' + accessKey
+ ']';
3549 * ButtonWidget is a generic widget for buttons. A wide variety of looks,
3550 * feels, and functionality can be customized via the class’s configuration options
3551 * and methods. Please see the [OOjs UI documentation on MediaWiki] [1] for more information
3554 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches
3557 * // A button widget
3558 * var button = new OO.ui.ButtonWidget( {
3559 * label: 'Button with Icon',
3561 * iconTitle: 'Remove'
3563 * $( 'body' ).append( button.$element );
3565 * NOTE: HTML form buttons should use the OO.ui.ButtonInputWidget class.
3568 * @extends OO.ui.Widget
3569 * @mixins OO.ui.mixin.ButtonElement
3570 * @mixins OO.ui.mixin.IconElement
3571 * @mixins OO.ui.mixin.IndicatorElement
3572 * @mixins OO.ui.mixin.LabelElement
3573 * @mixins OO.ui.mixin.TitledElement
3574 * @mixins OO.ui.mixin.FlaggedElement
3575 * @mixins OO.ui.mixin.TabIndexedElement
3576 * @mixins OO.ui.mixin.AccessKeyedElement
3579 * @param {Object} [config] Configuration options
3580 * @cfg {boolean} [active=false] Whether button should be shown as active
3581 * @cfg {string} [href] Hyperlink to visit when the button is clicked.
3582 * @cfg {string} [target] The frame or window in which to open the hyperlink.
3583 * @cfg {boolean} [noFollow] Search engine traversal hint (default: true)
3585 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
3586 // Configuration initialization
3587 config
= config
|| {};
3589 // Parent constructor
3590 OO
.ui
.ButtonWidget
.parent
.call( this, config
);
3592 // Mixin constructors
3593 OO
.ui
.mixin
.ButtonElement
.call( this, config
);
3594 OO
.ui
.mixin
.IconElement
.call( this, config
);
3595 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
3596 OO
.ui
.mixin
.LabelElement
.call( this, config
);
3597 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$button
} ) );
3598 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
3599 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$button
} ) );
3600 OO
.ui
.mixin
.AccessKeyedElement
.call( this, $.extend( {}, config
, { $accessKeyed
: this.$button
} ) );
3605 this.noFollow
= false;
3608 this.connect( this, { disable
: 'onDisable' } );
3611 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
3613 .addClass( 'oo-ui-buttonWidget' )
3614 .append( this.$button
);
3615 this.setActive( config
.active
);
3616 this.setHref( config
.href
);
3617 this.setTarget( config
.target
);
3618 this.setNoFollow( config
.noFollow
);
3623 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
3624 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.ButtonElement
);
3625 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.IconElement
);
3626 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.IndicatorElement
);
3627 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.LabelElement
);
3628 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.TitledElement
);
3629 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.FlaggedElement
);
3630 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.TabIndexedElement
);
3631 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.mixin
.AccessKeyedElement
);
3633 /* Static Properties */
3639 OO
.ui
.ButtonWidget
.static.cancelButtonMouseDownEvents
= false;
3645 OO
.ui
.ButtonWidget
.static.tagName
= 'span';
3650 * Get hyperlink location.
3652 * @return {string} Hyperlink location
3654 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
3659 * Get hyperlink target.
3661 * @return {string} Hyperlink target
3663 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
3668 * Get search engine traversal hint.
3670 * @return {boolean} Whether search engines should avoid traversing this hyperlink
3672 OO
.ui
.ButtonWidget
.prototype.getNoFollow = function () {
3673 return this.noFollow
;
3677 * Set hyperlink location.
3679 * @param {string|null} href Hyperlink location, null to remove
3681 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
3682 href
= typeof href
=== 'string' ? href
: null;
3683 if ( href
!== null && !OO
.ui
.isSafeUrl( href
) ) {
3687 if ( href
!== this.href
) {
3696 * Update the `href` attribute, in case of changes to href or
3702 OO
.ui
.ButtonWidget
.prototype.updateHref = function () {
3703 if ( this.href
!== null && !this.isDisabled() ) {
3704 this.$button
.attr( 'href', this.href
);
3706 this.$button
.removeAttr( 'href' );
3713 * Handle disable events.
3716 * @param {boolean} disabled Element is disabled
3718 OO
.ui
.ButtonWidget
.prototype.onDisable = function () {
3723 * Set hyperlink target.
3725 * @param {string|null} target Hyperlink target, null to remove
3727 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
3728 target
= typeof target
=== 'string' ? target
: null;
3730 if ( target
!== this.target
) {
3731 this.target
= target
;
3732 if ( target
!== null ) {
3733 this.$button
.attr( 'target', target
);
3735 this.$button
.removeAttr( 'target' );
3743 * Set search engine traversal hint.
3745 * @param {boolean} noFollow True if search engines should avoid traversing this hyperlink
3747 OO
.ui
.ButtonWidget
.prototype.setNoFollow = function ( noFollow
) {
3748 noFollow
= typeof noFollow
=== 'boolean' ? noFollow
: true;
3750 if ( noFollow
!== this.noFollow
) {
3751 this.noFollow
= noFollow
;
3753 this.$button
.attr( 'rel', 'nofollow' );
3755 this.$button
.removeAttr( 'rel' );
3762 // Override method visibility hints from ButtonElement
3773 * A ButtonGroupWidget groups related buttons and is used together with OO.ui.ButtonWidget and
3774 * its subclasses. Each button in a group is addressed by a unique reference. Buttons can be added,
3775 * removed, and cleared from the group.
3778 * // Example: A ButtonGroupWidget with two buttons
3779 * var button1 = new OO.ui.PopupButtonWidget( {
3780 * label: 'Select a category',
3783 * $content: $( '<p>List of categories...</p>' ),
3788 * var button2 = new OO.ui.ButtonWidget( {
3791 * var buttonGroup = new OO.ui.ButtonGroupWidget( {
3792 * items: [button1, button2]
3794 * $( 'body' ).append( buttonGroup.$element );
3797 * @extends OO.ui.Widget
3798 * @mixins OO.ui.mixin.GroupElement
3801 * @param {Object} [config] Configuration options
3802 * @cfg {OO.ui.ButtonWidget[]} [items] Buttons to add
3804 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
3805 // Configuration initialization
3806 config
= config
|| {};
3808 // Parent constructor
3809 OO
.ui
.ButtonGroupWidget
.parent
.call( this, config
);
3811 // Mixin constructors
3812 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
3815 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
3816 if ( Array
.isArray( config
.items
) ) {
3817 this.addItems( config
.items
);
3823 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
3824 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.mixin
.GroupElement
);
3826 /* Static Properties */
3832 OO
.ui
.ButtonGroupWidget
.static.tagName
= 'span';
3841 OO
.ui
.ButtonGroupWidget
.prototype.focus = function () {
3842 if ( !this.isDisabled() ) {
3843 if ( this.items
[ 0 ] ) {
3844 this.items
[ 0 ].focus();
3853 OO
.ui
.ButtonGroupWidget
.prototype.simulateLabelClick = function () {
3858 * IconWidget is a generic widget for {@link OO.ui.mixin.IconElement icons}. In general, IconWidgets should be used with OO.ui.LabelWidget,
3859 * which creates a label that identifies the icon’s function. See the [OOjs UI documentation on MediaWiki] [1]
3860 * for a list of icons included in the library.
3863 * // An icon widget with a label
3864 * var myIcon = new OO.ui.IconWidget( {
3868 * // Create a label.
3869 * var iconLabel = new OO.ui.LabelWidget( {
3872 * $( 'body' ).append( myIcon.$element, iconLabel.$element );
3874 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Icons
3877 * @extends OO.ui.Widget
3878 * @mixins OO.ui.mixin.IconElement
3879 * @mixins OO.ui.mixin.TitledElement
3880 * @mixins OO.ui.mixin.FlaggedElement
3883 * @param {Object} [config] Configuration options
3885 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
3886 // Configuration initialization
3887 config
= config
|| {};
3889 // Parent constructor
3890 OO
.ui
.IconWidget
.parent
.call( this, config
);
3892 // Mixin constructors
3893 OO
.ui
.mixin
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
3894 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
3895 OO
.ui
.mixin
.FlaggedElement
.call( this, $.extend( {}, config
, { $flagged
: this.$element
} ) );
3898 this.$element
.addClass( 'oo-ui-iconWidget' );
3903 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
3904 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.mixin
.IconElement
);
3905 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.mixin
.TitledElement
);
3906 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.mixin
.FlaggedElement
);
3908 /* Static Properties */
3914 OO
.ui
.IconWidget
.static.tagName
= 'span';
3917 * IndicatorWidgets create indicators, which are small graphics that are generally used to draw
3918 * attention to the status of an item or to clarify the function of a control. For a list of
3919 * indicators included in the library, please see the [OOjs UI documentation on MediaWiki][1].
3922 * // Example of an indicator widget
3923 * var indicator1 = new OO.ui.IndicatorWidget( {
3924 * indicator: 'alert'
3927 * // Create a fieldset layout to add a label
3928 * var fieldset = new OO.ui.FieldsetLayout();
3929 * fieldset.addItems( [
3930 * new OO.ui.FieldLayout( indicator1, { label: 'An alert indicator:' } )
3932 * $( 'body' ).append( fieldset.$element );
3934 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Icons,_Indicators,_and_Labels#Indicators
3937 * @extends OO.ui.Widget
3938 * @mixins OO.ui.mixin.IndicatorElement
3939 * @mixins OO.ui.mixin.TitledElement
3942 * @param {Object} [config] Configuration options
3944 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
3945 // Configuration initialization
3946 config
= config
|| {};
3948 // Parent constructor
3949 OO
.ui
.IndicatorWidget
.parent
.call( this, config
);
3951 // Mixin constructors
3952 OO
.ui
.mixin
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
3953 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
3956 this.$element
.addClass( 'oo-ui-indicatorWidget' );
3961 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
3962 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.mixin
.IndicatorElement
);
3963 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.mixin
.TitledElement
);
3965 /* Static Properties */
3971 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
3974 * LabelWidgets help identify the function of interface elements. Each LabelWidget can
3975 * be configured with a `label` option that is set to a string, a label node, or a function:
3977 * - String: a plaintext string
3978 * - jQuery selection: a jQuery selection, used for anything other than a plaintext label, e.g., a
3979 * label that includes a link or special styling, such as a gray color or additional graphical elements.
3980 * - Function: a function that will produce a string in the future. Functions are used
3981 * in cases where the value of the label is not currently defined.
3983 * In addition, the LabelWidget can be associated with an {@link OO.ui.InputWidget input widget}, which
3984 * will come into focus when the label is clicked.
3987 * // Examples of LabelWidgets
3988 * var label1 = new OO.ui.LabelWidget( {
3989 * label: 'plaintext label'
3991 * var label2 = new OO.ui.LabelWidget( {
3992 * label: $( '<a href="default.html">jQuery label</a>' )
3994 * // Create a fieldset layout with fields for each example
3995 * var fieldset = new OO.ui.FieldsetLayout();
3996 * fieldset.addItems( [
3997 * new OO.ui.FieldLayout( label1 ),
3998 * new OO.ui.FieldLayout( label2 )
4000 * $( 'body' ).append( fieldset.$element );
4003 * @extends OO.ui.Widget
4004 * @mixins OO.ui.mixin.LabelElement
4005 * @mixins OO.ui.mixin.TitledElement
4008 * @param {Object} [config] Configuration options
4009 * @cfg {OO.ui.InputWidget} [input] {@link OO.ui.InputWidget Input widget} that uses the label.
4010 * Clicking the label will focus the specified input field.
4012 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
4013 // Configuration initialization
4014 config
= config
|| {};
4016 // Parent constructor
4017 OO
.ui
.LabelWidget
.parent
.call( this, config
);
4019 // Mixin constructors
4020 OO
.ui
.mixin
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
4021 OO
.ui
.mixin
.TitledElement
.call( this, config
);
4024 this.input
= config
.input
;
4028 if ( this.input
.getInputId() ) {
4029 this.$element
.attr( 'for', this.input
.getInputId() );
4031 this.$label
.on( 'click', function () {
4032 this.input
.simulateLabelClick();
4037 this.$element
.addClass( 'oo-ui-labelWidget' );
4042 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
4043 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.mixin
.LabelElement
);
4044 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.mixin
.TitledElement
);
4046 /* Static Properties */
4052 OO
.ui
.LabelWidget
.static.tagName
= 'label';
4055 * PendingElement is a mixin that is used to create elements that notify users that something is happening
4056 * and that they should wait before proceeding. The pending state is visually represented with a pending
4057 * texture that appears in the head of a pending {@link OO.ui.ProcessDialog process dialog} or in the input
4058 * field of a {@link OO.ui.TextInputWidget text input widget}.
4060 * Currently, {@link OO.ui.ActionWidget Action widgets}, which mix in this class, can also be marked as pending, but only when
4061 * used in {@link OO.ui.MessageDialog message dialogs}. The behavior is not currently supported for action widgets used
4062 * in process dialogs.
4065 * function MessageDialog( config ) {
4066 * MessageDialog.parent.call( this, config );
4068 * OO.inheritClass( MessageDialog, OO.ui.MessageDialog );
4070 * MessageDialog.static.name = 'myMessageDialog';
4071 * MessageDialog.static.actions = [
4072 * { action: 'save', label: 'Done', flags: 'primary' },
4073 * { label: 'Cancel', flags: 'safe' }
4076 * MessageDialog.prototype.initialize = function () {
4077 * MessageDialog.parent.prototype.initialize.apply( this, arguments );
4078 * this.content = new OO.ui.PanelLayout( { $: this.$, padded: true } );
4079 * this.content.$element.append( '<p>Click the \'Done\' action widget to see its pending state. Note that action widgets can be marked pending in message dialogs but not process dialogs.</p>' );
4080 * this.$body.append( this.content.$element );
4082 * MessageDialog.prototype.getBodyHeight = function () {
4085 * MessageDialog.prototype.getActionProcess = function ( action ) {
4086 * var dialog = this;
4087 * if ( action === 'save' ) {
4088 * dialog.getActions().get({actions: 'save'})[0].pushPending();
4089 * return new OO.ui.Process()
4091 * .next( function () {
4092 * dialog.getActions().get({actions: 'save'})[0].popPending();
4095 * return MessageDialog.parent.prototype.getActionProcess.call( this, action );
4098 * var windowManager = new OO.ui.WindowManager();
4099 * $( 'body' ).append( windowManager.$element );
4101 * var dialog = new MessageDialog();
4102 * windowManager.addWindows( [ dialog ] );
4103 * windowManager.openWindow( dialog );
4109 * @param {Object} [config] Configuration options
4110 * @cfg {jQuery} [$pending] Element to mark as pending, defaults to this.$element
4112 OO
.ui
.mixin
.PendingElement
= function OoUiMixinPendingElement( config
) {
4113 // Configuration initialization
4114 config
= config
|| {};
4118 this.$pending
= null;
4121 this.setPendingElement( config
.$pending
|| this.$element
);
4126 OO
.initClass( OO
.ui
.mixin
.PendingElement
);
4131 * Set the pending element (and clean up any existing one).
4133 * @param {jQuery} $pending The element to set to pending.
4135 OO
.ui
.mixin
.PendingElement
.prototype.setPendingElement = function ( $pending
) {
4136 if ( this.$pending
) {
4137 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
4140 this.$pending
= $pending
;
4141 if ( this.pending
> 0 ) {
4142 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
4147 * Check if an element is pending.
4149 * @return {boolean} Element is pending
4151 OO
.ui
.mixin
.PendingElement
.prototype.isPending = function () {
4152 return !!this.pending
;
4156 * Increase the pending counter. The pending state will remain active until the counter is zero
4157 * (i.e., the number of calls to #pushPending and #popPending is the same).
4161 OO
.ui
.mixin
.PendingElement
.prototype.pushPending = function () {
4162 if ( this.pending
=== 0 ) {
4163 this.$pending
.addClass( 'oo-ui-pendingElement-pending' );
4164 this.updateThemeClasses();
4172 * Decrease the pending counter. The pending state will remain active until the counter is zero
4173 * (i.e., the number of calls to #pushPending and #popPending is the same).
4177 OO
.ui
.mixin
.PendingElement
.prototype.popPending = function () {
4178 if ( this.pending
=== 1 ) {
4179 this.$pending
.removeClass( 'oo-ui-pendingElement-pending' );
4180 this.updateThemeClasses();
4182 this.pending
= Math
.max( 0, this.pending
- 1 );
4188 * Element that will stick adjacent to a specified container, even when it is inserted elsewhere
4189 * in the document (for example, in an OO.ui.Window's $overlay).
4191 * The elements's position is automatically calculated and maintained when window is resized or the
4192 * page is scrolled. If you reposition the container manually, you have to call #position to make
4193 * sure the element is still placed correctly.
4195 * As positioning is only possible when both the element and the container are attached to the DOM
4196 * and visible, it's only done after you call #togglePositioning. You might want to do this inside
4197 * the #toggle method to display a floating popup, for example.
4203 * @param {Object} [config] Configuration options
4204 * @cfg {jQuery} [$floatable] Node to position, assigned to #$floatable, omit to use #$element
4205 * @cfg {jQuery} [$floatableContainer] Node to position adjacent to
4206 * @cfg {string} [verticalPosition='below'] Where to position $floatable vertically:
4207 * 'below': Directly below $floatableContainer, aligning f's top edge with fC's bottom edge
4208 * 'above': Directly above $floatableContainer, aligning f's bottom edge with fC's top edge
4209 * 'top': Align the top edge with $floatableContainer's top edge
4210 * 'bottom': Align the bottom edge with $floatableContainer's bottom edge
4211 * 'center': Vertically align the center with $floatableContainer's center
4212 * @cfg {string} [horizontalPosition='start'] Where to position $floatable horizontally:
4213 * 'before': Directly before $floatableContainer, aligning f's end edge with fC's start edge
4214 * 'after': Directly after $floatableContainer, algining f's start edge with fC's end edge
4215 * 'start': Align the start (left in LTR, right in RTL) edge with $floatableContainer's start edge
4216 * 'end': Align the end (right in LTR, left in RTL) edge with $floatableContainer's end edge
4217 * 'center': Horizontally align the center with $floatableContainer's center
4218 * @cfg {boolean} [hideWhenOutOfView=true] Whether to hide the floatable element if the container
4221 OO
.ui
.mixin
.FloatableElement
= function OoUiMixinFloatableElement( config
) {
4222 // Configuration initialization
4223 config
= config
|| {};
4226 this.$floatable
= null;
4227 this.$floatableContainer
= null;
4228 this.$floatableWindow
= null;
4229 this.$floatableClosestScrollable
= null;
4230 this.onFloatableScrollHandler
= this.position
.bind( this );
4231 this.onFloatableWindowResizeHandler
= this.position
.bind( this );
4234 this.setFloatableContainer( config
.$floatableContainer
);
4235 this.setFloatableElement( config
.$floatable
|| this.$element
);
4236 this.setVerticalPosition( config
.verticalPosition
|| 'below' );
4237 this.setHorizontalPosition( config
.horizontalPosition
|| 'start' );
4238 this.hideWhenOutOfView
= config
.hideWhenOutOfView
=== undefined ? true : !!config
.hideWhenOutOfView
;
4244 * Set floatable element.
4246 * If an element is already set, it will be cleaned up before setting up the new element.
4248 * @param {jQuery} $floatable Element to make floatable
4250 OO
.ui
.mixin
.FloatableElement
.prototype.setFloatableElement = function ( $floatable
) {
4251 if ( this.$floatable
) {
4252 this.$floatable
.removeClass( 'oo-ui-floatableElement-floatable' );
4253 this.$floatable
.css( { left
: '', top
: '' } );
4256 this.$floatable
= $floatable
.addClass( 'oo-ui-floatableElement-floatable' );
4261 * Set floatable container.
4263 * The element will be positioned relative to the specified container.
4265 * @param {jQuery|null} $floatableContainer Container to keep visible, or null to unset
4267 OO
.ui
.mixin
.FloatableElement
.prototype.setFloatableContainer = function ( $floatableContainer
) {
4268 this.$floatableContainer
= $floatableContainer
;
4269 if ( this.$floatable
) {
4275 * Change how the element is positioned vertically.
4277 * @param {string} position 'below', 'above', 'top', 'bottom' or 'center'
4279 OO
.ui
.mixin
.FloatableElement
.prototype.setVerticalPosition = function ( position
) {
4280 if ( [ 'below', 'above', 'top', 'bottom', 'center' ].indexOf( position
) === -1 ) {
4281 throw new Error( 'Invalid value for vertical position: ' + position
);
4283 if ( this.verticalPosition
!== position
) {
4284 this.verticalPosition
= position
;
4285 if ( this.$floatable
) {
4292 * Change how the element is positioned horizontally.
4294 * @param {string} position 'before', 'after', 'start', 'end' or 'center'
4296 OO
.ui
.mixin
.FloatableElement
.prototype.setHorizontalPosition = function ( position
) {
4297 if ( [ 'before', 'after', 'start', 'end', 'center' ].indexOf( position
) === -1 ) {
4298 throw new Error( 'Invalid value for horizontal position: ' + position
);
4300 if ( this.horizontalPosition
!== position
) {
4301 this.horizontalPosition
= position
;
4302 if ( this.$floatable
) {
4309 * Toggle positioning.
4311 * Do not turn positioning on until after the element is attached to the DOM and visible.
4313 * @param {boolean} [positioning] Enable positioning, omit to toggle
4316 OO
.ui
.mixin
.FloatableElement
.prototype.togglePositioning = function ( positioning
) {
4317 var closestScrollableOfContainer
;
4319 if ( !this.$floatable
|| !this.$floatableContainer
) {
4323 positioning
= positioning
=== undefined ? !this.positioning
: !!positioning
;
4325 if ( positioning
&& !this.warnedUnattached
&& !this.isElementAttached() ) {
4326 OO
.ui
.warnDeprecation( 'FloatableElement#togglePositioning: Before calling this method, the element must be attached to the DOM.' );
4327 this.warnedUnattached
= true;
4330 if ( this.positioning
!== positioning
) {
4331 this.positioning
= positioning
;
4333 this.needsCustomPosition
=
4334 this.verticalPostion
!== 'below' ||
4335 this.horizontalPosition
!== 'start' ||
4336 !OO
.ui
.contains( this.$floatableContainer
[ 0 ], this.$floatable
[ 0 ] );
4338 closestScrollableOfContainer
= OO
.ui
.Element
.static.getClosestScrollableContainer( this.$floatableContainer
[ 0 ] );
4339 // If the scrollable is the root, we have to listen to scroll events
4340 // on the window because of browser inconsistencies.
4341 if ( $( closestScrollableOfContainer
).is( 'html, body' ) ) {
4342 closestScrollableOfContainer
= OO
.ui
.Element
.static.getWindow( closestScrollableOfContainer
);
4345 if ( positioning
) {
4346 this.$floatableWindow
= $( this.getElementWindow() );
4347 this.$floatableWindow
.on( 'resize', this.onFloatableWindowResizeHandler
);
4349 this.$floatableClosestScrollable
= $( closestScrollableOfContainer
);
4350 this.$floatableClosestScrollable
.on( 'scroll', this.onFloatableScrollHandler
);
4352 // Initial position after visible
4355 if ( this.$floatableWindow
) {
4356 this.$floatableWindow
.off( 'resize', this.onFloatableWindowResizeHandler
);
4357 this.$floatableWindow
= null;
4360 if ( this.$floatableClosestScrollable
) {
4361 this.$floatableClosestScrollable
.off( 'scroll', this.onFloatableScrollHandler
);
4362 this.$floatableClosestScrollable
= null;
4365 this.$floatable
.css( { left
: '', right
: '', top
: '' } );
4373 * Check whether the bottom edge of the given element is within the viewport of the given container.
4376 * @param {jQuery} $element
4377 * @param {jQuery} $container
4380 OO
.ui
.mixin
.FloatableElement
.prototype.isElementInViewport = function ( $element
, $container
) {
4381 var elemRect
, contRect
, topEdgeInBounds
, bottomEdgeInBounds
, leftEdgeInBounds
, rightEdgeInBounds
,
4382 startEdgeInBounds
, endEdgeInBounds
,
4383 direction
= $element
.css( 'direction' );
4385 elemRect
= $element
[ 0 ].getBoundingClientRect();
4386 if ( $container
[ 0 ] === window
) {
4390 right
: document
.documentElement
.clientWidth
,
4391 bottom
: document
.documentElement
.clientHeight
4394 contRect
= $container
[ 0 ].getBoundingClientRect();
4397 topEdgeInBounds
= elemRect
.top
>= contRect
.top
&& elemRect
.top
<= contRect
.bottom
;
4398 bottomEdgeInBounds
= elemRect
.bottom
>= contRect
.top
&& elemRect
.bottom
<= contRect
.bottom
;
4399 leftEdgeInBounds
= elemRect
.left
>= contRect
.left
&& elemRect
.left
<= contRect
.right
;
4400 rightEdgeInBounds
= elemRect
.right
>= contRect
.left
&& elemRect
.right
<= contRect
.right
;
4401 if ( direction
=== 'rtl' ) {
4402 startEdgeInBounds
= rightEdgeInBounds
;
4403 endEdgeInBounds
= leftEdgeInBounds
;
4405 startEdgeInBounds
= leftEdgeInBounds
;
4406 endEdgeInBounds
= rightEdgeInBounds
;
4409 if ( this.verticalPosition
=== 'below' && !bottomEdgeInBounds
) {
4412 if ( this.verticalPosition
=== 'above' && !topEdgeInBounds
) {
4415 if ( this.horizontalPosition
=== 'before' && !startEdgeInBounds
) {
4418 if ( this.horizontalPosition
=== 'after' && !endEdgeInBounds
) {
4422 // The other positioning values are all about being inside the container,
4423 // so in those cases all we care about is that any part of the container is visible.
4424 return elemRect
.top
<= contRect
.bottom
&& elemRect
.bottom
>= contRect
.top
&&
4425 elemRect
.left
<= contRect
.right
&& elemRect
.right
>= contRect
.left
;
4429 * Position the floatable below its container.
4431 * This should only be done when both of them are attached to the DOM and visible.
4435 OO
.ui
.mixin
.FloatableElement
.prototype.position = function () {
4436 if ( !this.positioning
) {
4441 // To continue, some things need to be true:
4442 // The element must actually be in the DOM
4443 this.isElementAttached() && (
4444 // The closest scrollable is the current window
4445 this.$floatableClosestScrollable
[ 0 ] === this.getElementWindow() ||
4446 // OR is an element in the element's DOM
4447 $.contains( this.getElementDocument(), this.$floatableClosestScrollable
[ 0 ] )
4450 // Abort early if important parts of the widget are no longer attached to the DOM
4454 if ( this.hideWhenOutOfView
&& !this.isElementInViewport( this.$floatableContainer
, this.$floatableClosestScrollable
) ) {
4455 this.$floatable
.addClass( 'oo-ui-element-hidden' );
4458 this.$floatable
.removeClass( 'oo-ui-element-hidden' );
4461 if ( !this.needsCustomPosition
) {
4465 this.$floatable
.css( this.computePosition() );
4467 // We updated the position, so re-evaluate the clipping state.
4468 // (ClippableElement does not listen to 'scroll' events on $floatableContainer's parent, and so
4469 // will not notice the need to update itself.)
4470 // TODO: This is terrible, we shouldn't need to know about ClippableElement at all here. Why does
4471 // it not listen to the right events in the right places?
4480 * Compute how #$floatable should be positioned based on the position of #$floatableContainer
4481 * and the positioning settings. This is a helper for #position that shouldn't be called directly,
4482 * but may be overridden by subclasses if they want to change or add to the positioning logic.
4484 * @return {Object} New position to apply with .css(). Keys are 'top', 'left', 'bottom' and 'right'.
4486 OO
.ui
.mixin
.FloatableElement
.prototype.computePosition = function () {
4487 var isBody
, scrollableX
, scrollableY
, containerPos
,
4488 horizScrollbarHeight
, vertScrollbarWidth
, scrollTop
, scrollLeft
,
4489 newPos
= { top
: '', left
: '', bottom
: '', right
: '' },
4490 direction
= this.$floatableContainer
.css( 'direction' ),
4491 $offsetParent
= this.$floatable
.offsetParent();
4493 if ( $offsetParent
.is( 'html' ) ) {
4494 // The innerHeight/Width and clientHeight/Width calculations don't work well on the
4495 // <html> element, but they do work on the <body>
4496 $offsetParent
= $( $offsetParent
[ 0 ].ownerDocument
.body
);
4498 isBody
= $offsetParent
.is( 'body' );
4499 scrollableX
= $offsetParent
.css( 'overflow-x' ) === 'scroll' || $offsetParent
.css( 'overflow-x' ) === 'auto';
4500 scrollableY
= $offsetParent
.css( 'overflow-y' ) === 'scroll' || $offsetParent
.css( 'overflow-y' ) === 'auto';
4502 vertScrollbarWidth
= $offsetParent
.innerWidth() - $offsetParent
.prop( 'clientWidth' );
4503 horizScrollbarHeight
= $offsetParent
.innerHeight() - $offsetParent
.prop( 'clientHeight' );
4504 // We don't need to compute and add scrollTop and scrollLeft if the scrollable container is the body,
4505 // or if it isn't scrollable
4506 scrollTop
= scrollableY
&& !isBody
? $offsetParent
.scrollTop() : 0;
4507 scrollLeft
= scrollableX
&& !isBody
? OO
.ui
.Element
.static.getScrollLeft( $offsetParent
[ 0 ] ) : 0;
4509 // Avoid passing the <body> to getRelativePosition(), because it won't return what we expect
4510 // if the <body> has a margin
4511 containerPos
= isBody
?
4512 this.$floatableContainer
.offset() :
4513 OO
.ui
.Element
.static.getRelativePosition( this.$floatableContainer
, $offsetParent
);
4514 containerPos
.bottom
= containerPos
.top
+ this.$floatableContainer
.outerHeight();
4515 containerPos
.right
= containerPos
.left
+ this.$floatableContainer
.outerWidth();
4516 containerPos
.start
= direction
=== 'rtl' ? containerPos
.right
: containerPos
.left
;
4517 containerPos
.end
= direction
=== 'rtl' ? containerPos
.left
: containerPos
.right
;
4519 if ( this.verticalPosition
=== 'below' ) {
4520 newPos
.top
= containerPos
.bottom
;
4521 } else if ( this.verticalPosition
=== 'above' ) {
4522 newPos
.bottom
= $offsetParent
.outerHeight() - containerPos
.top
;
4523 } else if ( this.verticalPosition
=== 'top' ) {
4524 newPos
.top
= containerPos
.top
;
4525 } else if ( this.verticalPosition
=== 'bottom' ) {
4526 newPos
.bottom
= $offsetParent
.outerHeight() - containerPos
.bottom
;
4527 } else if ( this.verticalPosition
=== 'center' ) {
4528 newPos
.top
= containerPos
.top
+
4529 ( this.$floatableContainer
.height() - this.$floatable
.height() ) / 2;
4532 if ( this.horizontalPosition
=== 'before' ) {
4533 newPos
.end
= containerPos
.start
;
4534 } else if ( this.horizontalPosition
=== 'after' ) {
4535 newPos
.start
= containerPos
.end
;
4536 } else if ( this.horizontalPosition
=== 'start' ) {
4537 newPos
.start
= containerPos
.start
;
4538 } else if ( this.horizontalPosition
=== 'end' ) {
4539 newPos
.end
= containerPos
.end
;
4540 } else if ( this.horizontalPosition
=== 'center' ) {
4541 newPos
.left
= containerPos
.left
+
4542 ( this.$floatableContainer
.width() - this.$floatable
.width() ) / 2;
4545 if ( newPos
.start
!== undefined ) {
4546 if ( direction
=== 'rtl' ) {
4547 newPos
.right
= ( isBody
? $( $offsetParent
[ 0 ].ownerDocument
.documentElement
) : $offsetParent
).outerWidth() - newPos
.start
;
4549 newPos
.left
= newPos
.start
;
4551 delete newPos
.start
;
4553 if ( newPos
.end
!== undefined ) {
4554 if ( direction
=== 'rtl' ) {
4555 newPos
.left
= newPos
.end
;
4557 newPos
.right
= ( isBody
? $( $offsetParent
[ 0 ].ownerDocument
.documentElement
) : $offsetParent
).outerWidth() - newPos
.end
;
4562 // Account for scroll position
4563 if ( newPos
.top
!== '' ) {
4564 newPos
.top
+= scrollTop
;
4566 if ( newPos
.bottom
!== '' ) {
4567 newPos
.bottom
-= scrollTop
;
4569 if ( newPos
.left
!== '' ) {
4570 newPos
.left
+= scrollLeft
;
4572 if ( newPos
.right
!== '' ) {
4573 newPos
.right
-= scrollLeft
;
4576 // Account for scrollbar gutter
4577 if ( newPos
.bottom
!== '' ) {
4578 newPos
.bottom
-= horizScrollbarHeight
;
4580 if ( direction
=== 'rtl' ) {
4581 if ( newPos
.left
!== '' ) {
4582 newPos
.left
-= vertScrollbarWidth
;
4585 if ( newPos
.right
!== '' ) {
4586 newPos
.right
-= vertScrollbarWidth
;
4594 * Element that can be automatically clipped to visible boundaries.
4596 * Whenever the element's natural height changes, you have to call
4597 * {@link OO.ui.mixin.ClippableElement#clip} to make sure it's still
4598 * clipping correctly.
4600 * The dimensions of #$clippableContainer will be compared to the boundaries of the
4601 * nearest scrollable container. If #$clippableContainer is too tall and/or too wide,
4602 * then #$clippable will be given a fixed reduced height and/or width and will be made
4603 * scrollable. By default, #$clippable and #$clippableContainer are the same element,
4604 * but you can build a static footer by setting #$clippableContainer to an element that contains
4605 * #$clippable and the footer.
4611 * @param {Object} [config] Configuration options
4612 * @cfg {jQuery} [$clippable] Node to clip, assigned to #$clippable, omit to use #$element
4613 * @cfg {jQuery} [$clippableContainer] Node to keep visible, assigned to #$clippableContainer,
4614 * omit to use #$clippable
4616 OO
.ui
.mixin
.ClippableElement
= function OoUiMixinClippableElement( config
) {
4617 // Configuration initialization
4618 config
= config
|| {};
4621 this.$clippable
= null;
4622 this.$clippableContainer
= null;
4623 this.clipping
= false;
4624 this.clippedHorizontally
= false;
4625 this.clippedVertically
= false;
4626 this.$clippableScrollableContainer
= null;
4627 this.$clippableScroller
= null;
4628 this.$clippableWindow
= null;
4629 this.idealWidth
= null;
4630 this.idealHeight
= null;
4631 this.onClippableScrollHandler
= this.clip
.bind( this );
4632 this.onClippableWindowResizeHandler
= this.clip
.bind( this );
4635 if ( config
.$clippableContainer
) {
4636 this.setClippableContainer( config
.$clippableContainer
);
4638 this.setClippableElement( config
.$clippable
|| this.$element
);
4644 * Set clippable element.
4646 * If an element is already set, it will be cleaned up before setting up the new element.
4648 * @param {jQuery} $clippable Element to make clippable
4650 OO
.ui
.mixin
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4651 if ( this.$clippable
) {
4652 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4653 this.$clippable
.css( { width
: '', height
: '', overflowX
: '', overflowY
: '' } );
4654 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
4657 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4662 * Set clippable container.
4664 * This is the container that will be measured when deciding whether to clip. When clipping,
4665 * #$clippable will be resized in order to keep the clippable container fully visible.
4667 * If the clippable container is unset, #$clippable will be used.
4669 * @param {jQuery|null} $clippableContainer Container to keep visible, or null to unset
4671 OO
.ui
.mixin
.ClippableElement
.prototype.setClippableContainer = function ( $clippableContainer
) {
4672 this.$clippableContainer
= $clippableContainer
;
4673 if ( this.$clippable
) {
4681 * Do not turn clipping on until after the element is attached to the DOM and visible.
4683 * @param {boolean} [clipping] Enable clipping, omit to toggle
4686 OO
.ui
.mixin
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4687 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4689 if ( clipping
&& !this.warnedUnattached
&& !this.isElementAttached() ) {
4690 OO
.ui
.warnDeprecation( 'ClippableElement#toggleClipping: Before calling this method, the element must be attached to the DOM.' );
4691 this.warnedUnattached
= true;
4694 if ( this.clipping
!== clipping
) {
4695 this.clipping
= clipping
;
4697 this.$clippableScrollableContainer
= $( this.getClosestScrollableElementContainer() );
4698 // If the clippable container is the root, we have to listen to scroll events and check
4699 // jQuery.scrollTop on the window because of browser inconsistencies
4700 this.$clippableScroller
= this.$clippableScrollableContainer
.is( 'html, body' ) ?
4701 $( OO
.ui
.Element
.static.getWindow( this.$clippableScrollableContainer
) ) :
4702 this.$clippableScrollableContainer
;
4703 this.$clippableScroller
.on( 'scroll', this.onClippableScrollHandler
);
4704 this.$clippableWindow
= $( this.getElementWindow() )
4705 .on( 'resize', this.onClippableWindowResizeHandler
);
4706 // Initial clip after visible
4709 this.$clippable
.css( {
4717 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
4719 this.$clippableScrollableContainer
= null;
4720 this.$clippableScroller
.off( 'scroll', this.onClippableScrollHandler
);
4721 this.$clippableScroller
= null;
4722 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4723 this.$clippableWindow
= null;
4731 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4733 * @return {boolean} Element will be clipped to the visible area
4735 OO
.ui
.mixin
.ClippableElement
.prototype.isClipping = function () {
4736 return this.clipping
;
4740 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4742 * @return {boolean} Part of the element is being clipped
4744 OO
.ui
.mixin
.ClippableElement
.prototype.isClipped = function () {
4745 return this.clippedHorizontally
|| this.clippedVertically
;
4749 * Check if the right of the element is being clipped by the nearest scrollable container.
4751 * @return {boolean} Part of the element is being clipped
4753 OO
.ui
.mixin
.ClippableElement
.prototype.isClippedHorizontally = function () {
4754 return this.clippedHorizontally
;
4758 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4760 * @return {boolean} Part of the element is being clipped
4762 OO
.ui
.mixin
.ClippableElement
.prototype.isClippedVertically = function () {
4763 return this.clippedVertically
;
4767 * Set the ideal size. These are the dimensions #$clippable will have when it's not being clipped.
4769 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4770 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4772 OO
.ui
.mixin
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4773 this.idealWidth
= width
;
4774 this.idealHeight
= height
;
4776 if ( !this.clipping
) {
4777 // Update dimensions
4778 this.$clippable
.css( { width
: width
, height
: height
} );
4780 // While clipping, idealWidth and idealHeight are not considered
4784 * Clip element to visible boundaries and allow scrolling when needed. You should call this method
4785 * when the element's natural height changes.
4787 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4788 * overlapped by, the visible area of the nearest scrollable container.
4790 * Because calling clip() when the natural height changes isn't always possible, we also set
4791 * max-height when the element isn't being clipped. This means that if the element tries to grow
4792 * beyond the edge, something reasonable will happen before clip() is called.
4796 OO
.ui
.mixin
.ClippableElement
.prototype.clip = function () {
4797 var $container
, extraHeight
, extraWidth
, ccOffset
,
4798 $scrollableContainer
, scOffset
, scHeight
, scWidth
,
4799 ccWidth
, scrollerIsWindow
, scrollTop
, scrollLeft
,
4800 desiredWidth
, desiredHeight
, allotedWidth
, allotedHeight
,
4801 naturalWidth
, naturalHeight
, clipWidth
, clipHeight
,
4802 buffer
= 7; // Chosen by fair dice roll
4804 if ( !this.clipping
) {
4805 // this.$clippableScrollableContainer and this.$clippableWindow are null, so the below will fail
4809 $container
= this.$clippableContainer
|| this.$clippable
;
4810 extraHeight
= $container
.outerHeight() - this.$clippable
.outerHeight();
4811 extraWidth
= $container
.outerWidth() - this.$clippable
.outerWidth();
4812 ccOffset
= $container
.offset();
4813 if ( this.$clippableScrollableContainer
.is( 'html, body' ) ) {
4814 $scrollableContainer
= this.$clippableWindow
;
4815 scOffset
= { top
: 0, left
: 0 };
4817 $scrollableContainer
= this.$clippableScrollableContainer
;
4818 scOffset
= $scrollableContainer
.offset();
4820 scHeight
= $scrollableContainer
.innerHeight() - buffer
;
4821 scWidth
= $scrollableContainer
.innerWidth() - buffer
;
4822 ccWidth
= $container
.outerWidth() + buffer
;
4823 scrollerIsWindow
= this.$clippableScroller
[ 0 ] === this.$clippableWindow
[ 0 ];
4824 scrollTop
= scrollerIsWindow
? this.$clippableScroller
.scrollTop() : 0;
4825 scrollLeft
= scrollerIsWindow
? this.$clippableScroller
.scrollLeft() : 0;
4826 desiredWidth
= ccOffset
.left
< 0 ?
4827 ccWidth
+ ccOffset
.left
:
4828 ( scOffset
.left
+ scrollLeft
+ scWidth
) - ccOffset
.left
;
4829 desiredHeight
= ( scOffset
.top
+ scrollTop
+ scHeight
) - ccOffset
.top
;
4830 // It should never be desirable to exceed the dimensions of the browser viewport... right?
4831 desiredWidth
= Math
.min( desiredWidth
, document
.documentElement
.clientWidth
);
4832 desiredHeight
= Math
.min( desiredHeight
, document
.documentElement
.clientHeight
);
4833 allotedWidth
= Math
.ceil( desiredWidth
- extraWidth
);
4834 allotedHeight
= Math
.ceil( desiredHeight
- extraHeight
);
4835 naturalWidth
= this.$clippable
.prop( 'scrollWidth' );
4836 naturalHeight
= this.$clippable
.prop( 'scrollHeight' );
4837 clipWidth
= allotedWidth
< naturalWidth
;
4838 clipHeight
= allotedHeight
< naturalHeight
;
4841 // The order matters here. If overflow is not set first, Chrome displays bogus scrollbars. See T157672.
4842 // Forcing a reflow is a smaller workaround than calling reconsiderScrollbars() for this case.
4843 this.$clippable
.css( 'overflowX', 'scroll' );
4844 void this.$clippable
[ 0 ].offsetHeight
; // Force reflow
4845 this.$clippable
.css( {
4846 width
: Math
.max( 0, allotedWidth
),
4850 this.$clippable
.css( {
4852 width
: this.idealWidth
|| '',
4853 maxWidth
: Math
.max( 0, allotedWidth
)
4857 // The order matters here. If overflow is not set first, Chrome displays bogus scrollbars. See T157672.
4858 // Forcing a reflow is a smaller workaround than calling reconsiderScrollbars() for this case.
4859 this.$clippable
.css( 'overflowY', 'scroll' );
4860 void this.$clippable
[ 0 ].offsetHeight
; // Force reflow
4861 this.$clippable
.css( {
4862 height
: Math
.max( 0, allotedHeight
),
4866 this.$clippable
.css( {
4868 height
: this.idealHeight
|| '',
4869 maxHeight
: Math
.max( 0, allotedHeight
)
4873 // If we stopped clipping in at least one of the dimensions
4874 if ( ( this.clippedHorizontally
&& !clipWidth
) || ( this.clippedVertically
&& !clipHeight
) ) {
4875 OO
.ui
.Element
.static.reconsiderScrollbars( this.$clippable
[ 0 ] );
4878 this.clippedHorizontally
= clipWidth
;
4879 this.clippedVertically
= clipHeight
;
4885 * PopupWidget is a container for content. The popup is overlaid and positioned absolutely.
4886 * By default, each popup has an anchor that points toward its origin.
4887 * Please see the [OOjs UI documentation on Mediawiki] [1] for more information and examples.
4889 * Unlike most widgets, PopupWidget is initially hidden and must be shown by calling #toggle.
4892 * // A popup widget.
4893 * var popup = new OO.ui.PopupWidget( {
4894 * $content: $( '<p>Hi there!</p>' ),
4899 * $( 'body' ).append( popup.$element );
4900 * // To display the popup, toggle the visibility to 'true'.
4901 * popup.toggle( true );
4903 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups
4906 * @extends OO.ui.Widget
4907 * @mixins OO.ui.mixin.LabelElement
4908 * @mixins OO.ui.mixin.ClippableElement
4909 * @mixins OO.ui.mixin.FloatableElement
4912 * @param {Object} [config] Configuration options
4913 * @cfg {number} [width=320] Width of popup in pixels
4914 * @cfg {number} [height] Height of popup in pixels. Omit to use the automatic height.
4915 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
4916 * @cfg {string} [position='below'] Where to position the popup relative to $floatableContainer
4917 * 'above': Put popup above $floatableContainer; anchor points down to the horizontal center
4918 * of $floatableContainer
4919 * 'below': Put popup below $floatableContainer; anchor points up to the horizontal center
4920 * of $floatableContainer
4921 * 'before': Put popup to the left (LTR) / right (RTL) of $floatableContainer; anchor points
4922 * endwards (right/left) to the vertical center of $floatableContainer
4923 * 'after': Put popup to the right (LTR) / left (RTL) of $floatableContainer; anchor points
4924 * startwards (left/right) to the vertical center of $floatableContainer
4925 * @cfg {string} [align='center'] How to align the popup to $floatableContainer
4926 * 'forwards': If position is above/below, move the popup as far endwards (right in LTR, left in RTL)
4927 * as possible while still keeping the anchor within the popup;
4928 * if position is before/after, move the popup as far downwards as possible.
4929 * 'backwards': If position is above/below, move the popup as far startwards (left in LTR, right in RTL)
4930 * as possible while still keeping the anchor within the popup;
4931 * if position in before/after, move the popup as far upwards as possible.
4932 * 'center': Horizontally (if position is above/below) or vertically (before/after) align the center
4933 * of the popup with the center of $floatableContainer.
4934 * 'force-left': Alias for 'forwards' in LTR and 'backwards' in RTL
4935 * 'force-right': Alias for 'backwards' in RTL and 'forwards' in LTR
4936 * @cfg {jQuery} [$container] Constrain the popup to the boundaries of the specified container.
4937 * See the [OOjs UI docs on MediaWiki][3] for an example.
4938 * [3]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups#containerExample
4939 * @cfg {number} [containerPadding=10] Padding between the popup and its container, specified as a number of pixels.
4940 * @cfg {jQuery} [$content] Content to append to the popup's body
4941 * @cfg {jQuery} [$footer] Content to append to the popup's footer
4942 * @cfg {boolean} [autoClose=false] Automatically close the popup when it loses focus.
4943 * @cfg {jQuery} [$autoCloseIgnore] Elements that will not close the popup when clicked.
4944 * This config option is only relevant if #autoClose is set to `true`. See the [OOjs UI docs on MediaWiki][2]
4946 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups#autocloseExample
4947 * @cfg {boolean} [head=false] Show a popup header that contains a #label (if specified) and close
4949 * @cfg {boolean} [padded=false] Add padding to the popup's body
4951 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
4952 // Configuration initialization
4953 config
= config
|| {};
4955 // Parent constructor
4956 OO
.ui
.PopupWidget
.parent
.call( this, config
);
4958 // Properties (must be set before ClippableElement constructor call)
4959 this.$body
= $( '<div>' );
4960 this.$popup
= $( '<div>' );
4962 // Mixin constructors
4963 OO
.ui
.mixin
.LabelElement
.call( this, config
);
4964 OO
.ui
.mixin
.ClippableElement
.call( this, $.extend( {}, config
, {
4965 $clippable
: this.$body
,
4966 $clippableContainer
: this.$popup
4968 OO
.ui
.mixin
.FloatableElement
.call( this, config
);
4971 this.$anchor
= $( '<div>' );
4972 // If undefined, will be computed lazily in computePosition()
4973 this.$container
= config
.$container
;
4974 this.containerPadding
= config
.containerPadding
!== undefined ? config
.containerPadding
: 10;
4975 this.autoClose
= !!config
.autoClose
;
4976 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
4977 this.transitionTimeout
= null;
4978 this.anchored
= false;
4979 this.width
= config
.width
!== undefined ? config
.width
: 320;
4980 this.height
= config
.height
!== undefined ? config
.height
: null;
4981 this.onMouseDownHandler
= this.onMouseDown
.bind( this );
4982 this.onDocumentKeyDownHandler
= this.onDocumentKeyDown
.bind( this );
4985 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
4986 this.setAlignment( config
.align
|| 'center' );
4987 this.setPosition( config
.position
|| 'below' );
4988 this.$body
.addClass( 'oo-ui-popupWidget-body' );
4989 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
4991 .addClass( 'oo-ui-popupWidget-popup' )
4992 .append( this.$body
);
4994 .addClass( 'oo-ui-popupWidget' )
4995 .append( this.$popup
, this.$anchor
);
4996 // Move content, which was added to #$element by OO.ui.Widget, to the body
4997 // FIXME This is gross, we should use '$body' or something for the config
4998 if ( config
.$content
instanceof jQuery
) {
4999 this.$body
.append( config
.$content
);
5002 if ( config
.padded
) {
5003 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
5006 if ( config
.head
) {
5007 this.closeButton
= new OO
.ui
.ButtonWidget( { framed
: false, icon
: 'close' } );
5008 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
5009 this.$head
= $( '<div>' )
5010 .addClass( 'oo-ui-popupWidget-head' )
5011 .append( this.$label
, this.closeButton
.$element
);
5012 this.$popup
.prepend( this.$head
);
5015 if ( config
.$footer
) {
5016 this.$footer
= $( '<div>' )
5017 .addClass( 'oo-ui-popupWidget-footer' )
5018 .append( config
.$footer
);
5019 this.$popup
.append( this.$footer
);
5022 // Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
5023 // that reference properties not initialized at that time of parent class construction
5024 // TODO: Find a better way to handle post-constructor setup
5025 this.visible
= false;
5026 this.$element
.addClass( 'oo-ui-element-hidden' );
5031 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
5032 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.mixin
.LabelElement
);
5033 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.mixin
.ClippableElement
);
5034 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.mixin
.FloatableElement
);
5041 * The popup is ready: it is visible and has been positioned and clipped.
5047 * Handles mouse down events.
5050 * @param {MouseEvent} e Mouse down event
5052 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
5055 !OO
.ui
.contains( this.$element
.add( this.$autoCloseIgnore
).get(), e
.target
, true )
5057 this.toggle( false );
5062 * Bind mouse down listener.
5066 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
5067 // Capture clicks outside popup
5068 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
5072 * Handles close button click events.
5076 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
5077 if ( this.isVisible() ) {
5078 this.toggle( false );
5083 * Unbind mouse down listener.
5087 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
5088 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
5092 * Handles key down events.
5095 * @param {KeyboardEvent} e Key down event
5097 OO
.ui
.PopupWidget
.prototype.onDocumentKeyDown = function ( e
) {
5099 e
.which
=== OO
.ui
.Keys
.ESCAPE
&&
5102 this.toggle( false );
5104 e
.stopPropagation();
5109 * Bind key down listener.
5113 OO
.ui
.PopupWidget
.prototype.bindKeyDownListener = function () {
5114 this.getElementWindow().addEventListener( 'keydown', this.onDocumentKeyDownHandler
, true );
5118 * Unbind key down listener.
5122 OO
.ui
.PopupWidget
.prototype.unbindKeyDownListener = function () {
5123 this.getElementWindow().removeEventListener( 'keydown', this.onDocumentKeyDownHandler
, true );
5127 * Show, hide, or toggle the visibility of the anchor.
5129 * @param {boolean} [show] Show anchor, omit to toggle
5131 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
5132 show
= show
=== undefined ? !this.anchored
: !!show
;
5134 if ( this.anchored
!== show
) {
5136 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
5137 this.$element
.addClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge
);
5139 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
5140 this.$element
.removeClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge
);
5142 this.anchored
= show
;
5146 * Change which edge the anchor appears on.
5148 * @param {string} edge 'top', 'bottom', 'start' or 'end'
5150 OO
.ui
.PopupWidget
.prototype.setAnchorEdge = function ( edge
) {
5151 if ( [ 'top', 'bottom', 'start', 'end' ].indexOf( edge
) === -1 ) {
5152 throw new Error( 'Invalid value for edge: ' + edge
);
5154 if ( this.anchorEdge
!== null ) {
5155 this.$element
.removeClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge
);
5157 this.anchorEdge
= edge
;
5158 if ( this.anchored
) {
5159 this.$element
.addClass( 'oo-ui-popupWidget-anchored-' + edge
);
5164 * Check if the anchor is visible.
5166 * @return {boolean} Anchor is visible
5168 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
5169 return this.anchored
;
5173 * Toggle visibility of the popup. The popup is initially hidden and must be shown by calling
5174 * `.toggle( true )` after its #$element is attached to the DOM.
5176 * Do not show the popup while it is not attached to the DOM. The calculations required to display
5177 * it in the right place and with the right dimensions only work correctly while it is attached.
5178 * Side-effects may include broken interface and exceptions being thrown. This wasn't always
5179 * strictly enforced, so currently it only generates a warning in the browser console.
5184 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
5186 show
= show
=== undefined ? !this.isVisible() : !!show
;
5188 change
= show
!== this.isVisible();
5190 if ( show
&& !this.warnedUnattached
&& !this.isElementAttached() ) {
5191 OO
.ui
.warnDeprecation( 'PopupWidget#toggle: Before calling this method, the popup must be attached to the DOM.' );
5192 this.warnedUnattached
= true;
5194 if ( show
&& !this.$floatableContainer
&& this.isElementAttached() ) {
5195 // Fall back to the parent node if the floatableContainer is not set
5196 this.setFloatableContainer( this.$element
.parent() );
5200 OO
.ui
.PopupWidget
.parent
.prototype.toggle
.call( this, show
);
5203 this.togglePositioning( show
&& !!this.$floatableContainer
);
5206 if ( this.autoClose
) {
5207 this.bindMouseDownListener();
5208 this.bindKeyDownListener();
5210 this.updateDimensions();
5211 this.toggleClipping( true );
5212 this.emit( 'ready' );
5214 this.toggleClipping( false );
5215 if ( this.autoClose
) {
5216 this.unbindMouseDownListener();
5217 this.unbindKeyDownListener();
5226 * Set the size of the popup.
5228 * Changing the size may also change the popup's position depending on the alignment.
5230 * @param {number} width Width in pixels
5231 * @param {number} height Height in pixels
5232 * @param {boolean} [transition=false] Use a smooth transition
5235 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
5237 this.height
= height
!== undefined ? height
: null;
5238 if ( this.isVisible() ) {
5239 this.updateDimensions( transition
);
5244 * Update the size and position.
5246 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
5247 * be called automatically.
5249 * @param {boolean} [transition=false] Use a smooth transition
5252 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
5255 // Prevent transition from being interrupted
5256 clearTimeout( this.transitionTimeout
);
5258 // Enable transition
5259 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
5265 // Prevent transitioning after transition is complete
5266 this.transitionTimeout
= setTimeout( function () {
5267 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
5270 // Prevent transitioning immediately
5271 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
5278 OO
.ui
.PopupWidget
.prototype.computePosition = function () {
5279 var direction
, align
, vertical
, start
, end
, near
, far
, sizeProp
, popupSize
, anchorSize
, anchorPos
,
5280 anchorOffset
, anchorMargin
, parentPosition
, positionProp
, positionAdjustment
, floatablePos
,
5281 offsetParentPos
, containerPos
,
5283 anchorCss
= { left
: '', right
: '', top
: '', bottom
: '' },
5286 'force-left': 'backwards',
5287 'force-right': 'forwards'
5290 'force-left': 'forwards',
5291 'force-right': 'backwards'
5303 backwards
: this.anchored
? 'before' : 'end'
5311 if ( !this.$container
) {
5312 // Lazy-initialize $container if not specified in constructor
5313 this.$container
= $( this.getClosestScrollableElementContainer() );
5315 direction
= this.$container
.css( 'direction' );
5317 // Set height and width before we do anything else, since it might cause our measurements
5318 // to change (e.g. due to scrollbars appearing or disappearing), and it also affects centering
5321 height
: this.height
!== null ? this.height
: 'auto'
5324 align
= alignMap
[ direction
][ this.align
] || this.align
;
5325 // If the popup is positioned before or after, then the anchor positioning is vertical, otherwise horizontal
5326 vertical
= this.popupPosition
=== 'before' || this.popupPosition
=== 'after';
5327 start
= vertical
? 'top' : ( direction
=== 'rtl' ? 'right' : 'left' );
5328 end
= vertical
? 'bottom' : ( direction
=== 'rtl' ? 'left' : 'right' );
5329 near
= vertical
? 'top' : 'left';
5330 far
= vertical
? 'bottom' : 'right';
5331 sizeProp
= vertical
? 'Height' : 'Width';
5332 popupSize
= vertical
? ( this.height
|| this.$popup
.height() ) : this.width
;
5334 this.setAnchorEdge( anchorEdgeMap
[ this.popupPosition
] );
5335 this.horizontalPosition
= vertical
? this.popupPosition
: hPosMap
[ align
];
5336 this.verticalPosition
= vertical
? vPosMap
[ align
] : this.popupPosition
;
5339 parentPosition
= OO
.ui
.mixin
.FloatableElement
.prototype.computePosition
.call( this );
5340 // Find out which property FloatableElement used for positioning, and adjust that value
5341 positionProp
= vertical
?
5342 ( parentPosition
.top
!== '' ? 'top' : 'bottom' ) :
5343 ( parentPosition
.left
!== '' ? 'left' : 'right' );
5345 // Figure out where the near and far edges of the popup and $floatableContainer are
5346 floatablePos
= this.$floatableContainer
.offset();
5347 floatablePos
[ far
] = floatablePos
[ near
] + this.$floatableContainer
[ 'outer' + sizeProp
]();
5348 // Measure where the offsetParent is and compute our position based on that and parentPosition
5349 offsetParentPos
= this.$element
.offsetParent().offset();
5351 if ( positionProp
=== near
) {
5352 popupPos
[ near
] = offsetParentPos
[ near
] + parentPosition
[ near
];
5353 popupPos
[ far
] = popupPos
[ near
] + popupSize
;
5355 popupPos
[ far
] = offsetParentPos
[ near
] +
5356 this.$element
.offsetParent()[ 'inner' + sizeProp
]() - parentPosition
[ far
];
5357 popupPos
[ near
] = popupPos
[ far
] - popupSize
;
5360 if ( this.anchored
) {
5361 // Position the anchor (which is positioned relative to the popup) to point to $floatableContainer
5362 anchorPos
= ( floatablePos
[ start
] + floatablePos
[ end
] ) / 2;
5363 anchorOffset
= ( start
=== far
? -1 : 1 ) * ( anchorPos
- popupPos
[ start
] );
5365 // If the anchor is less than 2*anchorSize from either edge, move the popup to make more space
5366 // this.$anchor.width()/height() returns 0 because of the CSS trickery we use, so use scrollWidth/Height
5367 anchorSize
= this.$anchor
[ 0 ][ 'scroll' + sizeProp
];
5368 anchorMargin
= parseFloat( this.$anchor
.css( 'margin-' + start
) );
5369 if ( anchorOffset
+ anchorMargin
< 2 * anchorSize
) {
5370 // Not enough space for the anchor on the start side; pull the popup startwards
5371 positionAdjustment
= ( positionProp
=== start
? -1 : 1 ) *
5372 ( 2 * anchorSize
- ( anchorOffset
+ anchorMargin
) );
5373 } else if ( anchorOffset
+ anchorMargin
> popupSize
- 2 * anchorSize
) {
5374 // Not enough space for the anchor on the end side; pull the popup endwards
5375 positionAdjustment
= ( positionProp
=== end
? -1 : 1 ) *
5376 ( anchorOffset
+ anchorMargin
- ( popupSize
- 2 * anchorSize
) );
5378 positionAdjustment
= 0;
5381 positionAdjustment
= 0;
5384 // Check if the popup will go beyond the edge of this.$container
5385 containerPos
= this.$container
.offset();
5386 containerPos
[ far
] = containerPos
[ near
] + this.$container
[ 'inner' + sizeProp
]();
5387 // Take into account how much the popup will move because of the adjustments we're going to make
5388 popupPos
[ near
] += ( positionProp
=== near
? 1 : -1 ) * positionAdjustment
;
5389 popupPos
[ far
] += ( positionProp
=== near
? 1 : -1 ) * positionAdjustment
;
5390 if ( containerPos
[ near
] + this.containerPadding
> popupPos
[ near
] ) {
5391 // Popup goes beyond the near (left/top) edge, move it to the right/bottom
5392 positionAdjustment
+= ( positionProp
=== near
? 1 : -1 ) *
5393 ( containerPos
[ near
] + this.containerPadding
- popupPos
[ near
] );
5394 } else if ( containerPos
[ far
] - this.containerPadding
< popupPos
[ far
] ) {
5395 // Popup goes beyond the far (right/bottom) edge, move it to the left/top
5396 positionAdjustment
+= ( positionProp
=== far
? 1 : -1 ) *
5397 ( popupPos
[ far
] - ( containerPos
[ far
] - this.containerPadding
) );
5400 if ( this.anchored
) {
5401 // Adjust anchorOffset for positionAdjustment
5402 anchorOffset
+= ( positionProp
=== start
? -1 : 1 ) * positionAdjustment
;
5404 // Position the anchor
5405 anchorCss
[ start
] = anchorOffset
;
5406 this.$anchor
.css( anchorCss
);
5409 // Move the popup if needed
5410 parentPosition
[ positionProp
] += positionAdjustment
;
5412 return parentPosition
;
5416 * Set popup alignment
5418 * @param {string} [align=center] Alignment of the popup, `center`, `force-left`, `force-right`,
5419 * `backwards` or `forwards`.
5421 OO
.ui
.PopupWidget
.prototype.setAlignment = function ( align
) {
5422 // Validate alignment
5423 if ( [ 'force-left', 'force-right', 'backwards', 'forwards', 'center' ].indexOf( align
) > -1 ) {
5426 this.align
= 'center';
5432 * Get popup alignment
5434 * @return {string} Alignment of the popup, `center`, `force-left`, `force-right`,
5435 * `backwards` or `forwards`.
5437 OO
.ui
.PopupWidget
.prototype.getAlignment = function () {
5442 * Change the positioning of the popup.
5444 * @param {string} position 'above', 'below', 'before' or 'after'
5446 OO
.ui
.PopupWidget
.prototype.setPosition = function ( position
) {
5447 if ( [ 'above', 'below', 'before', 'after' ].indexOf( position
) === -1 ) {
5450 this.popupPosition
= position
;
5455 * Get popup positioning.
5457 * @return {string} 'above', 'below', 'before' or 'after'
5459 OO
.ui
.PopupWidget
.prototype.getPosition = function () {
5460 return this.popupPosition
;
5464 * Get an ID of the body element, this can be used as the
5465 * `aria-describedby` attribute for an input field.
5467 * @return {string} The ID of the body element
5469 OO
.ui
.PopupWidget
.prototype.getBodyId = function () {
5470 var id
= this.$body
.attr( 'id' );
5471 if ( id
=== undefined ) {
5472 id
= OO
.ui
.generateElementId();
5473 this.$body
.attr( 'id', id
);
5479 * PopupElement is mixed into other classes to generate a {@link OO.ui.PopupWidget popup widget}.
5480 * A popup is a container for content. It is overlaid and positioned absolutely. By default, each
5481 * popup has an anchor, which is an arrow-like protrusion that points toward the popup’s origin.
5482 * See {@link OO.ui.PopupWidget PopupWidget} for an example.
5488 * @param {Object} [config] Configuration options
5489 * @cfg {Object} [popup] Configuration to pass to popup
5490 * @cfg {boolean} [popup.autoClose=true] Popup auto-closes when it loses focus
5492 OO
.ui
.mixin
.PopupElement
= function OoUiMixinPopupElement( config
) {
5493 // Configuration initialization
5494 config
= config
|| {};
5497 this.popup
= new OO
.ui
.PopupWidget( $.extend(
5500 $floatableContainer
: this.$element
5504 $autoCloseIgnore
: this.$element
.add( config
.popup
&& config
.popup
.$autoCloseIgnore
)
5514 * @return {OO.ui.PopupWidget} Popup widget
5516 OO
.ui
.mixin
.PopupElement
.prototype.getPopup = function () {
5521 * PopupButtonWidgets toggle the visibility of a contained {@link OO.ui.PopupWidget PopupWidget},
5522 * which is used to display additional information or options.
5525 * // Example of a popup button.
5526 * var popupButton = new OO.ui.PopupButtonWidget( {
5527 * label: 'Popup button with options',
5530 * $content: $( '<p>Additional options here.</p>' ),
5532 * align: 'force-left'
5535 * // Append the button to the DOM.
5536 * $( 'body' ).append( popupButton.$element );
5539 * @extends OO.ui.ButtonWidget
5540 * @mixins OO.ui.mixin.PopupElement
5543 * @param {Object} [config] Configuration options
5544 * @cfg {jQuery} [$overlay] Render the popup into a separate layer. This configuration is useful in cases where
5545 * the expanded popup is larger than its containing `<div>`. The specified overlay layer is usually on top of the
5546 * containing `<div>` and has a larger area. By default, the popup uses relative positioning.
5547 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
5549 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
5550 // Configuration initialization
5551 config
= config
|| {};
5553 // Parent constructor
5554 OO
.ui
.PopupButtonWidget
.parent
.call( this, config
);
5556 // Mixin constructors
5557 OO
.ui
.mixin
.PopupElement
.call( this, config
);
5560 this.$overlay
= config
.$overlay
|| this.$element
;
5563 this.connect( this, { click
: 'onAction' } );
5567 .addClass( 'oo-ui-popupButtonWidget' )
5568 .attr( 'aria-haspopup', 'true' );
5570 .addClass( 'oo-ui-popupButtonWidget-popup' )
5571 .toggleClass( 'oo-ui-popupButtonWidget-framed-popup', this.isFramed() )
5572 .toggleClass( 'oo-ui-popupButtonWidget-frameless-popup', !this.isFramed() );
5573 this.$overlay
.append( this.popup
.$element
);
5578 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
5579 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.mixin
.PopupElement
);
5584 * Handle the button action being triggered.
5588 OO
.ui
.PopupButtonWidget
.prototype.onAction = function () {
5589 this.popup
.toggle();
5593 * Mixin for OO.ui.Widget subclasses to provide OO.ui.mixin.GroupElement.
5595 * Use together with OO.ui.mixin.ItemWidget to make disabled state inheritable.
5600 * @mixins OO.ui.mixin.GroupElement
5603 * @param {Object} [config] Configuration options
5605 OO
.ui
.mixin
.GroupWidget
= function OoUiMixinGroupWidget( config
) {
5606 // Mixin constructors
5607 OO
.ui
.mixin
.GroupElement
.call( this, config
);
5612 OO
.mixinClass( OO
.ui
.mixin
.GroupWidget
, OO
.ui
.mixin
.GroupElement
);
5617 * Set the disabled state of the widget.
5619 * This will also update the disabled state of child widgets.
5621 * @param {boolean} disabled Disable widget
5624 OO
.ui
.mixin
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
5628 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
5629 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
5631 // During construction, #setDisabled is called before the OO.ui.mixin.GroupElement constructor
5633 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5634 this.items
[ i
].updateDisabled();
5642 * Mixin for widgets used as items in widgets that mix in OO.ui.mixin.GroupWidget.
5644 * Item widgets have a reference to a OO.ui.mixin.GroupWidget while they are attached to the group. This
5645 * allows bidirectional communication.
5647 * Use together with OO.ui.mixin.GroupWidget to make disabled state inheritable.
5655 OO
.ui
.mixin
.ItemWidget
= function OoUiMixinItemWidget() {
5662 * Check if widget is disabled.
5664 * Checks parent if present, making disabled state inheritable.
5666 * @return {boolean} Widget is disabled
5668 OO
.ui
.mixin
.ItemWidget
.prototype.isDisabled = function () {
5669 return this.disabled
||
5670 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
5674 * Set group element is in.
5676 * @param {OO.ui.mixin.GroupElement|null} group Group element, null if none
5679 OO
.ui
.mixin
.ItemWidget
.prototype.setElementGroup = function ( group
) {
5681 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
5682 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
5684 // Initialize item disabled states
5685 this.updateDisabled();
5691 * OptionWidgets are special elements that can be selected and configured with data. The
5692 * data is often unique for each option, but it does not have to be. OptionWidgets are used
5693 * with OO.ui.SelectWidget to create a selection of mutually exclusive options. For more information
5694 * and examples, please see the [OOjs UI documentation on MediaWiki][1].
5696 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
5699 * @extends OO.ui.Widget
5700 * @mixins OO.ui.mixin.ItemWidget
5701 * @mixins OO.ui.mixin.LabelElement
5702 * @mixins OO.ui.mixin.FlaggedElement
5703 * @mixins OO.ui.mixin.AccessKeyedElement
5706 * @param {Object} [config] Configuration options
5708 OO
.ui
.OptionWidget
= function OoUiOptionWidget( config
) {
5709 // Configuration initialization
5710 config
= config
|| {};
5712 // Parent constructor
5713 OO
.ui
.OptionWidget
.parent
.call( this, config
);
5715 // Mixin constructors
5716 OO
.ui
.mixin
.ItemWidget
.call( this );
5717 OO
.ui
.mixin
.LabelElement
.call( this, config
);
5718 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
5719 OO
.ui
.mixin
.AccessKeyedElement
.call( this, config
);
5722 this.selected
= false;
5723 this.highlighted
= false;
5724 this.pressed
= false;
5728 .data( 'oo-ui-optionWidget', this )
5729 // Allow programmatic focussing (and by accesskey), but not tabbing
5730 .attr( 'tabindex', '-1' )
5731 .attr( 'role', 'option' )
5732 .attr( 'aria-selected', 'false' )
5733 .addClass( 'oo-ui-optionWidget' )
5734 .append( this.$label
);
5739 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
5740 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.mixin
.ItemWidget
);
5741 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.mixin
.LabelElement
);
5742 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.mixin
.FlaggedElement
);
5743 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.mixin
.AccessKeyedElement
);
5745 /* Static Properties */
5748 * Whether this option can be selected. See #setSelected.
5752 * @property {boolean}
5754 OO
.ui
.OptionWidget
.static.selectable
= true;
5757 * Whether this option can be highlighted. See #setHighlighted.
5761 * @property {boolean}
5763 OO
.ui
.OptionWidget
.static.highlightable
= true;
5766 * Whether this option can be pressed. See #setPressed.
5770 * @property {boolean}
5772 OO
.ui
.OptionWidget
.static.pressable
= true;
5775 * Whether this option will be scrolled into view when it is selected.
5779 * @property {boolean}
5781 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
5786 * Check if the option can be selected.
5788 * @return {boolean} Item is selectable
5790 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
5791 return this.constructor.static.selectable
&& !this.isDisabled() && this.isVisible();
5795 * Check if the option can be highlighted. A highlight indicates that the option
5796 * may be selected when a user presses enter or clicks. Disabled items cannot
5799 * @return {boolean} Item is highlightable
5801 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
5802 return this.constructor.static.highlightable
&& !this.isDisabled() && this.isVisible();
5806 * Check if the option can be pressed. The pressed state occurs when a user mouses
5807 * down on an item, but has not yet let go of the mouse.
5809 * @return {boolean} Item is pressable
5811 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
5812 return this.constructor.static.pressable
&& !this.isDisabled() && this.isVisible();
5816 * Check if the option is selected.
5818 * @return {boolean} Item is selected
5820 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
5821 return this.selected
;
5825 * Check if the option is highlighted. A highlight indicates that the
5826 * item may be selected when a user presses enter or clicks.
5828 * @return {boolean} Item is highlighted
5830 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
5831 return this.highlighted
;
5835 * Check if the option is pressed. The pressed state occurs when a user mouses
5836 * down on an item, but has not yet let go of the mouse. The item may appear
5837 * selected, but it will not be selected until the user releases the mouse.
5839 * @return {boolean} Item is pressed
5841 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
5842 return this.pressed
;
5846 * Set the option’s selected state. In general, all modifications to the selection
5847 * should be handled by the SelectWidget’s {@link OO.ui.SelectWidget#selectItem selectItem( [item] )}
5848 * method instead of this method.
5850 * @param {boolean} [state=false] Select option
5853 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
5854 if ( this.constructor.static.selectable
) {
5855 this.selected
= !!state
;
5857 .toggleClass( 'oo-ui-optionWidget-selected', state
)
5858 .attr( 'aria-selected', state
.toString() );
5859 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
5860 this.scrollElementIntoView();
5862 this.updateThemeClasses();
5868 * Set the option’s highlighted state. In general, all programmatic
5869 * modifications to the highlight should be handled by the
5870 * SelectWidget’s {@link OO.ui.SelectWidget#highlightItem highlightItem( [item] )}
5871 * method instead of this method.
5873 * @param {boolean} [state=false] Highlight option
5876 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
5877 if ( this.constructor.static.highlightable
) {
5878 this.highlighted
= !!state
;
5879 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
5880 this.updateThemeClasses();
5886 * Set the option’s pressed state. In general, all
5887 * programmatic modifications to the pressed state should be handled by the
5888 * SelectWidget’s {@link OO.ui.SelectWidget#pressItem pressItem( [item] )}
5889 * method instead of this method.
5891 * @param {boolean} [state=false] Press option
5894 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
5895 if ( this.constructor.static.pressable
) {
5896 this.pressed
= !!state
;
5897 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
5898 this.updateThemeClasses();
5904 * Get text to match search strings against.
5906 * The default implementation returns the label text, but subclasses
5907 * can override this to provide more complex behavior.
5909 * @return {string|boolean} String to match search string against
5911 OO
.ui
.OptionWidget
.prototype.getMatchText = function () {
5912 var label
= this.getLabel();
5913 return typeof label
=== 'string' ? label
: this.$label
.text();
5917 * A SelectWidget is of a generic selection of options. The OOjs UI library contains several types of
5918 * select widgets, including {@link OO.ui.ButtonSelectWidget button selects},
5919 * {@link OO.ui.RadioSelectWidget radio selects}, and {@link OO.ui.MenuSelectWidget
5922 * This class should be used together with OO.ui.OptionWidget or OO.ui.DecoratedOptionWidget. For more
5923 * information, please see the [OOjs UI documentation on MediaWiki][1].
5926 * // Example of a select widget with three options
5927 * var select = new OO.ui.SelectWidget( {
5929 * new OO.ui.OptionWidget( {
5931 * label: 'Option One',
5933 * new OO.ui.OptionWidget( {
5935 * label: 'Option Two',
5937 * new OO.ui.OptionWidget( {
5939 * label: 'Option Three',
5943 * $( 'body' ).append( select.$element );
5945 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
5949 * @extends OO.ui.Widget
5950 * @mixins OO.ui.mixin.GroupWidget
5953 * @param {Object} [config] Configuration options
5954 * @cfg {OO.ui.OptionWidget[]} [items] An array of options to add to the select.
5955 * Options are created with {@link OO.ui.OptionWidget OptionWidget} classes. See
5956 * the [OOjs UI documentation on MediaWiki] [2] for examples.
5957 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
5959 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
5960 // Configuration initialization
5961 config
= config
|| {};
5963 // Parent constructor
5964 OO
.ui
.SelectWidget
.parent
.call( this, config
);
5966 // Mixin constructors
5967 OO
.ui
.mixin
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
5970 this.pressed
= false;
5971 this.selecting
= null;
5972 this.onMouseUpHandler
= this.onMouseUp
.bind( this );
5973 this.onMouseMoveHandler
= this.onMouseMove
.bind( this );
5974 this.onKeyDownHandler
= this.onKeyDown
.bind( this );
5975 this.onKeyPressHandler
= this.onKeyPress
.bind( this );
5976 this.keyPressBuffer
= '';
5977 this.keyPressBufferTimer
= null;
5978 this.blockMouseOverEvents
= 0;
5981 this.connect( this, {
5985 focusin
: this.onFocus
.bind( this ),
5986 mousedown
: this.onMouseDown
.bind( this ),
5987 mouseover
: this.onMouseOver
.bind( this ),
5988 mouseleave
: this.onMouseLeave
.bind( this )
5993 .addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' )
5994 .attr( 'role', 'listbox' );
5995 this.setFocusOwner( this.$element
);
5996 if ( Array
.isArray( config
.items
) ) {
5997 this.addItems( config
.items
);
6003 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
6004 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.mixin
.GroupWidget
);
6011 * A `highlight` event is emitted when the highlight is changed with the #highlightItem method.
6013 * @param {OO.ui.OptionWidget|null} item Highlighted item
6019 * A `press` event is emitted when the #pressItem method is used to programmatically modify the
6020 * pressed state of an option.
6022 * @param {OO.ui.OptionWidget|null} item Pressed item
6028 * A `select` event is emitted when the selection is modified programmatically with the #selectItem method.
6030 * @param {OO.ui.OptionWidget|null} item Selected item
6035 * A `choose` event is emitted when an item is chosen with the #chooseItem method.
6036 * @param {OO.ui.OptionWidget} item Chosen item
6042 * An `add` event is emitted when options are added to the select with the #addItems method.
6044 * @param {OO.ui.OptionWidget[]} items Added items
6045 * @param {number} index Index of insertion point
6051 * A `remove` event is emitted when options are removed from the select with the #clearItems
6052 * or #removeItems methods.
6054 * @param {OO.ui.OptionWidget[]} items Removed items
6060 * Handle focus events
6063 * @param {jQuery.Event} event
6065 OO
.ui
.SelectWidget
.prototype.onFocus = function ( event
) {
6067 if ( event
.target
=== this.$element
[ 0 ] ) {
6068 // This widget was focussed, e.g. by the user tabbing to it.
6069 // The styles for focus state depend on one of the items being selected.
6070 if ( !this.getSelectedItem() ) {
6071 item
= this.getFirstSelectableItem();
6074 // One of the options got focussed (and the event bubbled up here).
6075 // They can't be tabbed to, but they can be activated using accesskeys.
6076 item
= this.findTargetItem( event
);
6080 if ( item
.constructor.static.highlightable
) {
6081 this.highlightItem( item
);
6083 this.selectItem( item
);
6087 if ( event
.target
!== this.$element
[ 0 ] ) {
6088 this.$focusOwner
.focus();
6093 * Handle mouse down events.
6096 * @param {jQuery.Event} e Mouse down event
6098 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
6101 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
6102 this.togglePressed( true );
6103 item
= this.findTargetItem( e
);
6104 if ( item
&& item
.isSelectable() ) {
6105 this.pressItem( item
);
6106 this.selecting
= item
;
6107 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
6108 this.getElementDocument().addEventListener( 'mousemove', this.onMouseMoveHandler
, true );
6115 * Handle mouse up events.
6118 * @param {MouseEvent} e Mouse up event
6120 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
6123 this.togglePressed( false );
6124 if ( !this.selecting
) {
6125 item
= this.findTargetItem( e
);
6126 if ( item
&& item
.isSelectable() ) {
6127 this.selecting
= item
;
6130 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
&& this.selecting
) {
6131 this.pressItem( null );
6132 this.chooseItem( this.selecting
);
6133 this.selecting
= null;
6136 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
6137 this.getElementDocument().removeEventListener( 'mousemove', this.onMouseMoveHandler
, true );
6143 * Handle mouse move events.
6146 * @param {MouseEvent} e Mouse move event
6148 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
6151 if ( !this.isDisabled() && this.pressed
) {
6152 item
= this.findTargetItem( e
);
6153 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
6154 this.pressItem( item
);
6155 this.selecting
= item
;
6161 * Handle mouse over events.
6164 * @param {jQuery.Event} e Mouse over event
6166 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
6168 if ( this.blockMouseOverEvents
) {
6171 if ( !this.isDisabled() ) {
6172 item
= this.findTargetItem( e
);
6173 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
6179 * Handle mouse leave events.
6182 * @param {jQuery.Event} e Mouse over event
6184 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
6185 if ( !this.isDisabled() ) {
6186 this.highlightItem( null );
6192 * Handle key down events.
6195 * @param {KeyboardEvent} e Key down event
6197 OO
.ui
.SelectWidget
.prototype.onKeyDown = function ( e
) {
6200 currentItem
= this.getHighlightedItem() || this.getSelectedItem();
6202 if ( !this.isDisabled() && this.isVisible() ) {
6203 switch ( e
.keyCode
) {
6204 case OO
.ui
.Keys
.ENTER
:
6205 if ( currentItem
&& currentItem
.constructor.static.highlightable
) {
6206 // Was only highlighted, now let's select it. No-op if already selected.
6207 this.chooseItem( currentItem
);
6212 case OO
.ui
.Keys
.LEFT
:
6213 this.clearKeyPressBuffer();
6214 nextItem
= this.getRelativeSelectableItem( currentItem
, -1 );
6217 case OO
.ui
.Keys
.DOWN
:
6218 case OO
.ui
.Keys
.RIGHT
:
6219 this.clearKeyPressBuffer();
6220 nextItem
= this.getRelativeSelectableItem( currentItem
, 1 );
6223 case OO
.ui
.Keys
.ESCAPE
:
6224 case OO
.ui
.Keys
.TAB
:
6225 if ( currentItem
&& currentItem
.constructor.static.highlightable
) {
6226 currentItem
.setHighlighted( false );
6228 this.unbindKeyDownListener();
6229 this.unbindKeyPressListener();
6230 // Don't prevent tabbing away / defocusing
6236 if ( nextItem
.constructor.static.highlightable
) {
6237 this.highlightItem( nextItem
);
6239 this.chooseItem( nextItem
);
6241 this.scrollItemIntoView( nextItem
);
6246 e
.stopPropagation();
6252 * Bind key down listener.
6256 OO
.ui
.SelectWidget
.prototype.bindKeyDownListener = function () {
6257 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
6261 * Unbind key down listener.
6265 OO
.ui
.SelectWidget
.prototype.unbindKeyDownListener = function () {
6266 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
6270 * Scroll item into view, preventing spurious mouse highlight actions from happening.
6272 * @param {OO.ui.OptionWidget} item Item to scroll into view
6274 OO
.ui
.SelectWidget
.prototype.scrollItemIntoView = function ( item
) {
6276 // Chromium's Blink engine will generate spurious 'mouseover' events during programmatic scrolling
6277 // and around 100-150 ms after it is finished.
6278 this.blockMouseOverEvents
++;
6279 item
.scrollElementIntoView().done( function () {
6280 setTimeout( function () {
6281 widget
.blockMouseOverEvents
--;
6287 * Clear the key-press buffer
6291 OO
.ui
.SelectWidget
.prototype.clearKeyPressBuffer = function () {
6292 if ( this.keyPressBufferTimer
) {
6293 clearTimeout( this.keyPressBufferTimer
);
6294 this.keyPressBufferTimer
= null;
6296 this.keyPressBuffer
= '';
6300 * Handle key press events.
6303 * @param {KeyboardEvent} e Key press event
6305 OO
.ui
.SelectWidget
.prototype.onKeyPress = function ( e
) {
6306 var c
, filter
, item
;
6308 if ( !e
.charCode
) {
6309 if ( e
.keyCode
=== OO
.ui
.Keys
.BACKSPACE
&& this.keyPressBuffer
!== '' ) {
6310 this.keyPressBuffer
= this.keyPressBuffer
.substr( 0, this.keyPressBuffer
.length
- 1 );
6315 if ( String
.fromCodePoint
) {
6316 c
= String
.fromCodePoint( e
.charCode
);
6318 c
= String
.fromCharCode( e
.charCode
);
6321 if ( this.keyPressBufferTimer
) {
6322 clearTimeout( this.keyPressBufferTimer
);
6324 this.keyPressBufferTimer
= setTimeout( this.clearKeyPressBuffer
.bind( this ), 1500 );
6326 item
= this.getHighlightedItem() || this.getSelectedItem();
6328 if ( this.keyPressBuffer
=== c
) {
6329 // Common (if weird) special case: typing "xxxx" will cycle through all
6330 // the items beginning with "x".
6332 item
= this.getRelativeSelectableItem( item
, 1 );
6335 this.keyPressBuffer
+= c
;
6338 filter
= this.getItemMatcher( this.keyPressBuffer
, false );
6339 if ( !item
|| !filter( item
) ) {
6340 item
= this.getRelativeSelectableItem( item
, 1, filter
);
6343 if ( this.isVisible() && item
.constructor.static.highlightable
) {
6344 this.highlightItem( item
);
6346 this.chooseItem( item
);
6348 this.scrollItemIntoView( item
);
6352 e
.stopPropagation();
6356 * Get a matcher for the specific string
6359 * @param {string} s String to match against items
6360 * @param {boolean} [exact=false] Only accept exact matches
6361 * @return {Function} function ( OO.ui.OptionWidget ) => boolean
6363 OO
.ui
.SelectWidget
.prototype.getItemMatcher = function ( s
, exact
) {
6366 if ( s
.normalize
) {
6369 s
= exact
? s
.trim() : s
.replace( /^\s+/, '' );
6370 re
= '^\\s*' + s
.replace( /([\\{}()|.?*+\-^$[\]])/g, '\\$1' ).replace( /\s+/g, '\\s+' );
6374 re
= new RegExp( re
, 'i' );
6375 return function ( item
) {
6376 var matchText
= item
.getMatchText();
6377 if ( matchText
.normalize
) {
6378 matchText
= matchText
.normalize();
6380 return re
.test( matchText
);
6385 * Bind key press listener.
6389 OO
.ui
.SelectWidget
.prototype.bindKeyPressListener = function () {
6390 this.getElementWindow().addEventListener( 'keypress', this.onKeyPressHandler
, true );
6394 * Unbind key down listener.
6396 * If you override this, be sure to call this.clearKeyPressBuffer() from your
6401 OO
.ui
.SelectWidget
.prototype.unbindKeyPressListener = function () {
6402 this.getElementWindow().removeEventListener( 'keypress', this.onKeyPressHandler
, true );
6403 this.clearKeyPressBuffer();
6407 * Visibility change handler
6410 * @param {boolean} visible
6412 OO
.ui
.SelectWidget
.prototype.onToggle = function ( visible
) {
6414 this.clearKeyPressBuffer();
6419 * Get the closest item to a jQuery.Event.
6422 * @param {jQuery.Event} e
6423 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
6425 OO
.ui
.SelectWidget
.prototype.findTargetItem = function ( e
) {
6426 var $option
= $( e
.target
).closest( '.oo-ui-optionWidget' );
6427 if ( !$option
.closest( '.oo-ui-selectWidget' ).is( this.$element
) ) {
6430 return $option
.data( 'oo-ui-optionWidget' ) || null;
6434 * Get selected item.
6436 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
6438 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
6441 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6442 if ( this.items
[ i
].isSelected() ) {
6443 return this.items
[ i
];
6450 * Get highlighted item.
6452 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
6454 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
6457 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6458 if ( this.items
[ i
].isHighlighted() ) {
6459 return this.items
[ i
];
6466 * Toggle pressed state.
6468 * Press is a state that occurs when a user mouses down on an item, but
6469 * has not yet let go of the mouse. The item may appear selected, but it will not be selected
6470 * until the user releases the mouse.
6472 * @param {boolean} pressed An option is being pressed
6474 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
6475 if ( pressed
=== undefined ) {
6476 pressed
= !this.pressed
;
6478 if ( pressed
!== this.pressed
) {
6480 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
6481 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
6482 this.pressed
= pressed
;
6487 * Highlight an option. If the `item` param is omitted, no options will be highlighted
6488 * and any existing highlight will be removed. The highlight is mutually exclusive.
6490 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit for no highlight
6494 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
6495 var i
, len
, highlighted
,
6498 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6499 highlighted
= this.items
[ i
] === item
;
6500 if ( this.items
[ i
].isHighlighted() !== highlighted
) {
6501 this.items
[ i
].setHighlighted( highlighted
);
6507 this.$focusOwner
.attr( 'aria-activedescendant', item
.getElementId() );
6509 this.$focusOwner
.removeAttr( 'aria-activedescendant' );
6511 this.emit( 'highlight', item
);
6518 * Fetch an item by its label.
6520 * @param {string} label Label of the item to select.
6521 * @param {boolean} [prefix=false] Allow a prefix match, if only a single item matches
6522 * @return {OO.ui.Element|null} Item with equivalent label, `null` if none exists
6524 OO
.ui
.SelectWidget
.prototype.getItemFromLabel = function ( label
, prefix
) {
6526 len
= this.items
.length
,
6527 filter
= this.getItemMatcher( label
, true );
6529 for ( i
= 0; i
< len
; i
++ ) {
6530 item
= this.items
[ i
];
6531 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() && filter( item
) ) {
6538 filter
= this.getItemMatcher( label
, false );
6539 for ( i
= 0; i
< len
; i
++ ) {
6540 item
= this.items
[ i
];
6541 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() && filter( item
) ) {
6557 * Programmatically select an option by its label. If the item does not exist,
6558 * all options will be deselected.
6560 * @param {string} [label] Label of the item to select.
6561 * @param {boolean} [prefix=false] Allow a prefix match, if only a single item matches
6565 OO
.ui
.SelectWidget
.prototype.selectItemByLabel = function ( label
, prefix
) {
6566 var itemFromLabel
= this.getItemFromLabel( label
, !!prefix
);
6567 if ( label
=== undefined || !itemFromLabel
) {
6568 return this.selectItem();
6570 return this.selectItem( itemFromLabel
);
6574 * Programmatically select an option by its data. If the `data` parameter is omitted,
6575 * or if the item does not exist, all options will be deselected.
6577 * @param {Object|string} [data] Value of the item to select, omit to deselect all
6581 OO
.ui
.SelectWidget
.prototype.selectItemByData = function ( data
) {
6582 var itemFromData
= this.getItemFromData( data
);
6583 if ( data
=== undefined || !itemFromData
) {
6584 return this.selectItem();
6586 return this.selectItem( itemFromData
);
6590 * Programmatically select an option by its reference. If the `item` parameter is omitted,
6591 * all options will be deselected.
6593 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
6597 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
6598 var i
, len
, selected
,
6601 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6602 selected
= this.items
[ i
] === item
;
6603 if ( this.items
[ i
].isSelected() !== selected
) {
6604 this.items
[ i
].setSelected( selected
);
6609 if ( item
&& !item
.constructor.static.highlightable
) {
6611 this.$focusOwner
.attr( 'aria-activedescendant', item
.getElementId() );
6613 this.$focusOwner
.removeAttr( 'aria-activedescendant' );
6616 this.emit( 'select', item
);
6625 * Press is a state that occurs when a user mouses down on an item, but has not
6626 * yet let go of the mouse. The item may appear selected, but it will not be selected until the user
6627 * releases the mouse.
6629 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
6633 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
6634 var i
, len
, pressed
,
6637 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
6638 pressed
= this.items
[ i
] === item
;
6639 if ( this.items
[ i
].isPressed() !== pressed
) {
6640 this.items
[ i
].setPressed( pressed
);
6645 this.emit( 'press', item
);
6654 * Note that ‘choose’ should never be modified programmatically. A user can choose
6655 * an option with the keyboard or mouse and it becomes selected. To select an item programmatically,
6656 * use the #selectItem method.
6658 * This method is identical to #selectItem, but may vary in subclasses that take additional action
6659 * when users choose an item with the keyboard or mouse.
6661 * @param {OO.ui.OptionWidget} item Item to choose
6665 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
6667 this.selectItem( item
);
6668 this.emit( 'choose', item
);
6675 * Get an option by its position relative to the specified item (or to the start of the option array,
6676 * if item is `null`). The direction in which to search through the option array is specified with a
6677 * number: -1 for reverse (the default) or 1 for forward. The method will return an option, or
6678 * `null` if there are no options in the array.
6680 * @param {OO.ui.OptionWidget|null} item Item to describe the start position, or `null` to start at the beginning of the array.
6681 * @param {number} direction Direction to move in: -1 to move backward, 1 to move forward
6682 * @param {Function} [filter] Only consider items for which this function returns
6683 * true. Function takes an OO.ui.OptionWidget and returns a boolean.
6684 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the select
6686 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
, filter
) {
6687 var currentIndex
, nextIndex
, i
,
6688 increase
= direction
> 0 ? 1 : -1,
6689 len
= this.items
.length
;
6691 if ( item
instanceof OO
.ui
.OptionWidget
) {
6692 currentIndex
= this.items
.indexOf( item
);
6693 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
6695 // If no item is selected and moving forward, start at the beginning.
6696 // If moving backward, start at the end.
6697 nextIndex
= direction
> 0 ? 0 : len
- 1;
6700 for ( i
= 0; i
< len
; i
++ ) {
6701 item
= this.items
[ nextIndex
];
6703 item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() &&
6704 ( !filter
|| filter( item
) )
6708 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
6714 * Get the next selectable item or `null` if there are no selectable items.
6715 * Disabled options and menu-section markers and breaks are not selectable.
6717 * @return {OO.ui.OptionWidget|null} Item, `null` if there aren't any selectable items
6719 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
6720 return this.getRelativeSelectableItem( null, 1 );
6724 * Add an array of options to the select. Optionally, an index number can be used to
6725 * specify an insertion point.
6727 * @param {OO.ui.OptionWidget[]} items Items to add
6728 * @param {number} [index] Index to insert items after
6732 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
6734 OO
.ui
.mixin
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
6736 // Always provide an index, even if it was omitted
6737 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
6743 * Remove the specified array of options from the select. Options will be detached
6744 * from the DOM, not removed, so they can be reused later. To remove all options from
6745 * the select, you may wish to use the #clearItems method instead.
6747 * @param {OO.ui.OptionWidget[]} items Items to remove
6751 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
6754 // Deselect items being removed
6755 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
6757 if ( item
.isSelected() ) {
6758 this.selectItem( null );
6763 OO
.ui
.mixin
.GroupWidget
.prototype.removeItems
.call( this, items
);
6765 this.emit( 'remove', items
);
6771 * Clear all options from the select. Options will be detached from the DOM, not removed,
6772 * so that they can be reused later. To remove a subset of options from the select, use
6773 * the #removeItems method.
6778 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
6779 var items
= this.items
.slice();
6782 OO
.ui
.mixin
.GroupWidget
.prototype.clearItems
.call( this );
6785 this.selectItem( null );
6787 this.emit( 'remove', items
);
6793 * Set the DOM element which has focus while the user is interacting with this SelectWidget.
6795 * Currently this is just used to set `aria-activedescendant` on it.
6798 * @param {jQuery} $focusOwner
6800 OO
.ui
.SelectWidget
.prototype.setFocusOwner = function ( $focusOwner
) {
6801 this.$focusOwner
= $focusOwner
;
6805 * DecoratedOptionWidgets are {@link OO.ui.OptionWidget options} that can be configured
6806 * with an {@link OO.ui.mixin.IconElement icon} and/or {@link OO.ui.mixin.IndicatorElement indicator}.
6807 * This class is used with OO.ui.SelectWidget to create a selection of mutually exclusive
6808 * options. For more information about options and selects, please see the
6809 * [OOjs UI documentation on MediaWiki][1].
6812 * // Decorated options in a select widget
6813 * var select = new OO.ui.SelectWidget( {
6815 * new OO.ui.DecoratedOptionWidget( {
6817 * label: 'Option with icon',
6820 * new OO.ui.DecoratedOptionWidget( {
6822 * label: 'Option with indicator',
6827 * $( 'body' ).append( select.$element );
6829 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
6832 * @extends OO.ui.OptionWidget
6833 * @mixins OO.ui.mixin.IconElement
6834 * @mixins OO.ui.mixin.IndicatorElement
6837 * @param {Object} [config] Configuration options
6839 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( config
) {
6840 // Parent constructor
6841 OO
.ui
.DecoratedOptionWidget
.parent
.call( this, config
);
6843 // Mixin constructors
6844 OO
.ui
.mixin
.IconElement
.call( this, config
);
6845 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
6849 .addClass( 'oo-ui-decoratedOptionWidget' )
6850 .prepend( this.$icon
)
6851 .append( this.$indicator
);
6856 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
6857 OO
.mixinClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.mixin
.IconElement
);
6858 OO
.mixinClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.mixin
.IndicatorElement
);
6861 * MenuOptionWidget is an option widget that looks like a menu item. The class is used with
6862 * OO.ui.MenuSelectWidget to create a menu of mutually exclusive options. Please see
6863 * the [OOjs UI documentation on MediaWiki] [1] for more information.
6865 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
6868 * @extends OO.ui.DecoratedOptionWidget
6871 * @param {Object} [config] Configuration options
6873 OO
.ui
.MenuOptionWidget
= function OoUiMenuOptionWidget( config
) {
6874 // Parent constructor
6875 OO
.ui
.MenuOptionWidget
.parent
.call( this, config
);
6878 this.$element
.addClass( 'oo-ui-menuOptionWidget' );
6883 OO
.inheritClass( OO
.ui
.MenuOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
6885 /* Static Properties */
6891 OO
.ui
.MenuOptionWidget
.static.scrollIntoViewOnSelect
= true;
6894 * MenuSectionOptionWidgets are used inside {@link OO.ui.MenuSelectWidget menu select widgets} to group one or more related
6895 * {@link OO.ui.MenuOptionWidget menu options}. MenuSectionOptionWidgets cannot be highlighted or selected.
6898 * var myDropdown = new OO.ui.DropdownWidget( {
6901 * new OO.ui.MenuSectionOptionWidget( {
6904 * new OO.ui.MenuOptionWidget( {
6906 * label: 'Welsh Corgi'
6908 * new OO.ui.MenuOptionWidget( {
6910 * label: 'Standard Poodle'
6912 * new OO.ui.MenuSectionOptionWidget( {
6915 * new OO.ui.MenuOptionWidget( {
6922 * $( 'body' ).append( myDropdown.$element );
6925 * @extends OO.ui.DecoratedOptionWidget
6928 * @param {Object} [config] Configuration options
6930 OO
.ui
.MenuSectionOptionWidget
= function OoUiMenuSectionOptionWidget( config
) {
6931 // Parent constructor
6932 OO
.ui
.MenuSectionOptionWidget
.parent
.call( this, config
);
6935 this.$element
.addClass( 'oo-ui-menuSectionOptionWidget' )
6936 .removeAttr( 'role aria-selected' );
6941 OO
.inheritClass( OO
.ui
.MenuSectionOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
6943 /* Static Properties */
6949 OO
.ui
.MenuSectionOptionWidget
.static.selectable
= false;
6955 OO
.ui
.MenuSectionOptionWidget
.static.highlightable
= false;
6958 * MenuSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains options and
6959 * is used together with OO.ui.MenuOptionWidget. It is designed be used as part of another widget.
6960 * See {@link OO.ui.DropdownWidget DropdownWidget}, {@link OO.ui.ComboBoxInputWidget ComboBoxInputWidget},
6961 * and {@link OO.ui.mixin.LookupElement LookupElement} for examples of widgets that contain menus.
6962 * MenuSelectWidgets themselves are not instantiated directly, rather subclassed
6963 * and customized to be opened, closed, and displayed as needed.
6965 * By default, menus are clipped to the visible viewport and are not visible when a user presses the
6966 * mouse outside the menu.
6968 * Menus also have support for keyboard interaction:
6970 * - Enter/Return key: choose and select a menu option
6971 * - Up-arrow key: highlight the previous menu option
6972 * - Down-arrow key: highlight the next menu option
6973 * - Esc key: hide the menu
6975 * Unlike most widgets, MenuSelectWidget is initially hidden and must be shown by calling #toggle.
6977 * Please see the [OOjs UI documentation on MediaWiki][1] for more information.
6978 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
6981 * @extends OO.ui.SelectWidget
6982 * @mixins OO.ui.mixin.ClippableElement
6983 * @mixins OO.ui.mixin.FloatableElement
6986 * @param {Object} [config] Configuration options
6987 * @cfg {OO.ui.TextInputWidget} [input] Text input used to implement option highlighting for menu items that match
6988 * the text the user types. This config is used by {@link OO.ui.ComboBoxInputWidget ComboBoxInputWidget}
6989 * and {@link OO.ui.mixin.LookupElement LookupElement}
6990 * @cfg {jQuery} [$input] Text input used to implement option highlighting for menu items that match
6991 * the text the user types. This config is used by {@link OO.ui.CapsuleMultiselectWidget CapsuleMultiselectWidget}
6992 * @cfg {OO.ui.Widget} [widget] Widget associated with the menu's active state. If the user clicks the mouse
6993 * anywhere on the page outside of this widget, the menu is hidden. For example, if there is a button
6994 * that toggles the menu's visibility on click, the menu will be hidden then re-shown when the user clicks
6995 * that button, unless the button (or its parent widget) is passed in here.
6996 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu.
6997 * @cfg {jQuery} [$autoCloseIgnore] If these elements are clicked, don't auto-hide the menu.
6998 * @cfg {boolean} [hideOnChoose=true] Hide the menu when the user chooses an option.
6999 * @cfg {boolean} [filterFromInput=false] Filter the displayed options from the input
7000 * @cfg {boolean} [highlightOnFilter] Highlight the first result when filtering
7001 * @cfg {number} [width] Width of the menu
7003 OO
.ui
.MenuSelectWidget
= function OoUiMenuSelectWidget( config
) {
7004 // Configuration initialization
7005 config
= config
|| {};
7007 // Parent constructor
7008 OO
.ui
.MenuSelectWidget
.parent
.call( this, config
);
7010 // Mixin constructors
7011 OO
.ui
.mixin
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7012 OO
.ui
.mixin
.FloatableElement
.call( this, config
);
7015 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
7016 this.hideOnChoose
= config
.hideOnChoose
=== undefined || !!config
.hideOnChoose
;
7017 this.filterFromInput
= !!config
.filterFromInput
;
7018 this.$input
= config
.$input
? config
.$input
: config
.input
? config
.input
.$input
: null;
7019 this.$widget
= config
.widget
? config
.widget
.$element
: null;
7020 this.$autoCloseIgnore
= config
.$autoCloseIgnore
|| $( [] );
7021 this.onDocumentMouseDownHandler
= this.onDocumentMouseDown
.bind( this );
7022 this.onInputEditHandler
= OO
.ui
.debounce( this.updateItemVisibility
.bind( this ), 100 );
7023 this.highlightOnFilter
= !!config
.highlightOnFilter
;
7024 this.width
= config
.width
;
7027 this.$element
.addClass( 'oo-ui-menuSelectWidget' );
7028 if ( config
.widget
) {
7029 this.setFocusOwner( config
.widget
.$tabIndexed
);
7032 // Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
7033 // that reference properties not initialized at that time of parent class construction
7034 // TODO: Find a better way to handle post-constructor setup
7035 this.visible
= false;
7036 this.$element
.addClass( 'oo-ui-element-hidden' );
7041 OO
.inheritClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.SelectWidget
);
7042 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.mixin
.ClippableElement
);
7043 OO
.mixinClass( OO
.ui
.MenuSelectWidget
, OO
.ui
.mixin
.FloatableElement
);
7050 * The menu is ready: it is visible and has been positioned and clipped.
7056 * Handles document mouse down events.
7059 * @param {MouseEvent} e Mouse down event
7061 OO
.ui
.MenuSelectWidget
.prototype.onDocumentMouseDown = function ( e
) {
7065 this.$element
.add( this.$widget
).add( this.$autoCloseIgnore
).get(),
7070 this.toggle( false );
7077 OO
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
7078 var currentItem
= this.getHighlightedItem() || this.getSelectedItem();
7080 if ( !this.isDisabled() && this.isVisible() ) {
7081 switch ( e
.keyCode
) {
7082 case OO
.ui
.Keys
.LEFT
:
7083 case OO
.ui
.Keys
.RIGHT
:
7084 // Do nothing if a text field is associated, arrow keys will be handled natively
7085 if ( !this.$input
) {
7086 OO
.ui
.MenuSelectWidget
.parent
.prototype.onKeyDown
.call( this, e
);
7089 case OO
.ui
.Keys
.ESCAPE
:
7090 case OO
.ui
.Keys
.TAB
:
7091 if ( currentItem
) {
7092 currentItem
.setHighlighted( false );
7094 this.toggle( false );
7095 // Don't prevent tabbing away, prevent defocusing
7096 if ( e
.keyCode
=== OO
.ui
.Keys
.ESCAPE
) {
7098 e
.stopPropagation();
7102 OO
.ui
.MenuSelectWidget
.parent
.prototype.onKeyDown
.call( this, e
);
7109 * Update menu item visibility and clipping after input changes (if filterFromInput is enabled)
7110 * or after items were added/removed (always).
7114 OO
.ui
.MenuSelectWidget
.prototype.updateItemVisibility = function () {
7115 var i
, item
, visible
, section
, sectionEmpty
, filter
, exactFilter
,
7116 firstItemFound
= false,
7118 len
= this.items
.length
,
7119 showAll
= !this.isVisible(),
7122 if ( this.$input
&& this.filterFromInput
) {
7123 filter
= showAll
? null : this.getItemMatcher( this.$input
.val() );
7124 exactFilter
= this.getItemMatcher( this.$input
.val(), true );
7126 // Hide non-matching options, and also hide section headers if all options
7127 // in their section are hidden.
7128 for ( i
= 0; i
< len
; i
++ ) {
7129 item
= this.items
[ i
];
7130 if ( item
instanceof OO
.ui
.MenuSectionOptionWidget
) {
7132 // If the previous section was empty, hide its header
7133 section
.toggle( showAll
|| !sectionEmpty
);
7136 sectionEmpty
= true;
7137 } else if ( item
instanceof OO
.ui
.OptionWidget
) {
7138 visible
= showAll
|| filter( item
);
7139 exactMatch
= exactMatch
|| exactFilter( item
);
7140 anyVisible
= anyVisible
|| visible
;
7141 sectionEmpty
= sectionEmpty
&& !visible
;
7142 item
.toggle( visible
);
7143 if ( this.highlightOnFilter
&& visible
&& !firstItemFound
) {
7144 // Highlight the first item in the list
7145 this.highlightItem( item
);
7146 firstItemFound
= true;
7150 // Process the final section
7152 section
.toggle( showAll
|| !sectionEmpty
);
7155 if ( anyVisible
&& this.items
.length
&& !exactMatch
) {
7156 this.scrollItemIntoView( this.items
[ 0 ] );
7159 this.$element
.toggleClass( 'oo-ui-menuSelectWidget-invisible', !anyVisible
);
7162 // Reevaluate clipping
7169 OO
.ui
.MenuSelectWidget
.prototype.bindKeyDownListener = function () {
7170 if ( this.$input
) {
7171 this.$input
.on( 'keydown', this.onKeyDownHandler
);
7173 OO
.ui
.MenuSelectWidget
.parent
.prototype.bindKeyDownListener
.call( this );
7180 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyDownListener = function () {
7181 if ( this.$input
) {
7182 this.$input
.off( 'keydown', this.onKeyDownHandler
);
7184 OO
.ui
.MenuSelectWidget
.parent
.prototype.unbindKeyDownListener
.call( this );
7191 OO
.ui
.MenuSelectWidget
.prototype.bindKeyPressListener = function () {
7192 if ( this.$input
) {
7193 if ( this.filterFromInput
) {
7194 this.$input
.on( 'keydown mouseup cut paste change input select', this.onInputEditHandler
);
7195 this.updateItemVisibility();
7198 OO
.ui
.MenuSelectWidget
.parent
.prototype.bindKeyPressListener
.call( this );
7205 OO
.ui
.MenuSelectWidget
.prototype.unbindKeyPressListener = function () {
7206 if ( this.$input
) {
7207 if ( this.filterFromInput
) {
7208 this.$input
.off( 'keydown mouseup cut paste change input select', this.onInputEditHandler
);
7209 this.updateItemVisibility();
7212 OO
.ui
.MenuSelectWidget
.parent
.prototype.unbindKeyPressListener
.call( this );
7219 * When a user chooses an item, the menu is closed, unless the hideOnChoose config option is set to false.
7221 * Note that ‘choose’ should never be modified programmatically. A user can choose an option with the keyboard
7222 * or mouse and it becomes selected. To select an item programmatically, use the #selectItem method.
7224 * @param {OO.ui.OptionWidget} item Item to choose
7227 OO
.ui
.MenuSelectWidget
.prototype.chooseItem = function ( item
) {
7228 OO
.ui
.MenuSelectWidget
.parent
.prototype.chooseItem
.call( this, item
);
7229 if ( this.hideOnChoose
) {
7230 this.toggle( false );
7238 OO
.ui
.MenuSelectWidget
.prototype.addItems = function ( items
, index
) {
7240 OO
.ui
.MenuSelectWidget
.parent
.prototype.addItems
.call( this, items
, index
);
7242 this.updateItemVisibility();
7250 OO
.ui
.MenuSelectWidget
.prototype.removeItems = function ( items
) {
7252 OO
.ui
.MenuSelectWidget
.parent
.prototype.removeItems
.call( this, items
);
7254 this.updateItemVisibility();
7262 OO
.ui
.MenuSelectWidget
.prototype.clearItems = function () {
7264 OO
.ui
.MenuSelectWidget
.parent
.prototype.clearItems
.call( this );
7266 this.updateItemVisibility();
7272 * Toggle visibility of the menu. The menu is initially hidden and must be shown by calling
7273 * `.toggle( true )` after its #$element is attached to the DOM.
7275 * Do not show the menu while it is not attached to the DOM. The calculations required to display
7276 * it in the right place and with the right dimensions only work correctly while it is attached.
7277 * Side-effects may include broken interface and exceptions being thrown. This wasn't always
7278 * strictly enforced, so currently it only generates a warning in the browser console.
7283 OO
.ui
.MenuSelectWidget
.prototype.toggle = function ( visible
) {
7286 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
7287 change
= visible
!== this.isVisible();
7289 if ( visible
&& !this.warnedUnattached
&& !this.isElementAttached() ) {
7290 OO
.ui
.warnDeprecation( 'MenuSelectWidget#toggle: Before calling this method, the menu must be attached to the DOM.' );
7291 this.warnedUnattached
= true;
7294 if ( change
&& visible
&& ( this.width
|| this.$floatableContainer
) ) {
7295 this.setIdealSize( this.width
|| this.$floatableContainer
.width() );
7299 OO
.ui
.MenuSelectWidget
.parent
.prototype.toggle
.call( this, visible
);
7303 this.bindKeyDownListener();
7304 this.bindKeyPressListener();
7306 this.togglePositioning( !!this.$floatableContainer
);
7307 this.toggleClipping( true );
7309 this.$focusOwner
.attr( 'aria-expanded', 'true' );
7311 if ( this.getSelectedItem() ) {
7312 this.$focusOwner
.attr( 'aria-activedescendant', this.getSelectedItem().getElementId() );
7313 this.getSelectedItem().scrollElementIntoView( { duration
: 0 } );
7317 if ( this.autoHide
) {
7318 this.getElementDocument().addEventListener( 'mousedown', this.onDocumentMouseDownHandler
, true );
7321 this.emit( 'ready' );
7323 this.$focusOwner
.removeAttr( 'aria-activedescendant' );
7324 this.unbindKeyDownListener();
7325 this.unbindKeyPressListener();
7326 this.$focusOwner
.attr( 'aria-expanded', 'false' );
7327 this.getElementDocument().removeEventListener( 'mousedown', this.onDocumentMouseDownHandler
, true );
7328 this.togglePositioning( false );
7329 this.toggleClipping( false );
7337 * DropdownWidgets are not menus themselves, rather they contain a menu of options created with
7338 * OO.ui.MenuOptionWidget. The DropdownWidget takes care of opening and displaying the menu so that
7339 * users can interact with it.
7341 * If you want to use this within an HTML form, such as a OO.ui.FormLayout, use
7342 * OO.ui.DropdownInputWidget instead.
7345 * // Example: A DropdownWidget with a menu that contains three options
7346 * var dropDown = new OO.ui.DropdownWidget( {
7347 * label: 'Dropdown menu: Select a menu option',
7350 * new OO.ui.MenuOptionWidget( {
7354 * new OO.ui.MenuOptionWidget( {
7358 * new OO.ui.MenuOptionWidget( {
7366 * $( 'body' ).append( dropDown.$element );
7368 * dropDown.getMenu().selectItemByData( 'b' );
7370 * dropDown.getMenu().getSelectedItem().getData(); // returns 'b'
7372 * For more information, please see the [OOjs UI documentation on MediaWiki] [1].
7374 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
7377 * @extends OO.ui.Widget
7378 * @mixins OO.ui.mixin.IconElement
7379 * @mixins OO.ui.mixin.IndicatorElement
7380 * @mixins OO.ui.mixin.LabelElement
7381 * @mixins OO.ui.mixin.TitledElement
7382 * @mixins OO.ui.mixin.TabIndexedElement
7385 * @param {Object} [config] Configuration options
7386 * @cfg {Object} [menu] Configuration options to pass to {@link OO.ui.MenuSelectWidget menu select widget}
7387 * @cfg {jQuery} [$overlay] Render the menu into a separate layer. This configuration is useful in cases where
7388 * the expanded menu is larger than its containing `<div>`. The specified overlay layer is usually on top of the
7389 * containing `<div>` and has a larger area. By default, the menu uses relative positioning.
7390 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
7392 OO
.ui
.DropdownWidget
= function OoUiDropdownWidget( config
) {
7393 // Configuration initialization
7394 config
= $.extend( { indicator
: 'down' }, config
);
7396 // Parent constructor
7397 OO
.ui
.DropdownWidget
.parent
.call( this, config
);
7399 // Properties (must be set before TabIndexedElement constructor call)
7400 this.$handle
= this.$( '<span>' );
7401 this.$overlay
= config
.$overlay
|| this.$element
;
7403 // Mixin constructors
7404 OO
.ui
.mixin
.IconElement
.call( this, config
);
7405 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
7406 OO
.ui
.mixin
.LabelElement
.call( this, config
);
7407 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
7408 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$handle
} ) );
7411 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend( {
7413 $floatableContainer
: this.$element
7418 click
: this.onClick
.bind( this ),
7419 keydown
: this.onKeyDown
.bind( this ),
7420 // Hack? Handle type-to-search when menu is not expanded and not handling its own events
7421 keypress
: this.menu
.onKeyPressHandler
,
7422 blur
: this.menu
.clearKeyPressBuffer
.bind( this.menu
)
7424 this.menu
.connect( this, {
7425 select
: 'onMenuSelect',
7426 toggle
: 'onMenuToggle'
7431 .addClass( 'oo-ui-dropdownWidget-handle' )
7434 'aria-owns': this.menu
.getElementId(),
7435 'aria-autocomplete': 'list'
7437 .append( this.$icon
, this.$label
, this.$indicator
);
7439 .addClass( 'oo-ui-dropdownWidget' )
7440 .append( this.$handle
);
7441 this.$overlay
.append( this.menu
.$element
);
7446 OO
.inheritClass( OO
.ui
.DropdownWidget
, OO
.ui
.Widget
);
7447 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.mixin
.IconElement
);
7448 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.mixin
.IndicatorElement
);
7449 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.mixin
.LabelElement
);
7450 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.mixin
.TitledElement
);
7451 OO
.mixinClass( OO
.ui
.DropdownWidget
, OO
.ui
.mixin
.TabIndexedElement
);
7458 * @return {OO.ui.MenuSelectWidget} Menu of widget
7460 OO
.ui
.DropdownWidget
.prototype.getMenu = function () {
7465 * Handles menu select events.
7468 * @param {OO.ui.MenuOptionWidget} item Selected menu item
7470 OO
.ui
.DropdownWidget
.prototype.onMenuSelect = function ( item
) {
7474 this.setLabel( null );
7478 selectedLabel
= item
.getLabel();
7480 // If the label is a DOM element, clone it, because setLabel will append() it
7481 if ( selectedLabel
instanceof jQuery
) {
7482 selectedLabel
= selectedLabel
.clone();
7485 this.setLabel( selectedLabel
);
7489 * Handle menu toggle events.
7492 * @param {boolean} isVisible Menu toggle event
7494 OO
.ui
.DropdownWidget
.prototype.onMenuToggle = function ( isVisible
) {
7495 this.$element
.toggleClass( 'oo-ui-dropdownWidget-open', isVisible
);
7498 this.$element
.hasClass( 'oo-ui-dropdownWidget-open' ).toString()
7503 * Handle mouse click events.
7506 * @param {jQuery.Event} e Mouse click event
7508 OO
.ui
.DropdownWidget
.prototype.onClick = function ( e
) {
7509 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
7516 * Handle key down events.
7519 * @param {jQuery.Event} e Key down event
7521 OO
.ui
.DropdownWidget
.prototype.onKeyDown = function ( e
) {
7523 !this.isDisabled() &&
7525 e
.which
=== OO
.ui
.Keys
.ENTER
||
7527 !this.menu
.isVisible() &&
7529 e
.which
=== OO
.ui
.Keys
.SPACE
||
7530 e
.which
=== OO
.ui
.Keys
.UP
||
7531 e
.which
=== OO
.ui
.Keys
.DOWN
7542 * RadioOptionWidget is an option widget that looks like a radio button.
7543 * The class is used with OO.ui.RadioSelectWidget to create a selection of radio options.
7544 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information.
7546 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_option
7549 * @extends OO.ui.OptionWidget
7552 * @param {Object} [config] Configuration options
7554 OO
.ui
.RadioOptionWidget
= function OoUiRadioOptionWidget( config
) {
7555 // Configuration initialization
7556 config
= config
|| {};
7558 // Properties (must be done before parent constructor which calls #setDisabled)
7559 this.radio
= new OO
.ui
.RadioInputWidget( { value
: config
.data
, tabIndex
: -1 } );
7561 // Parent constructor
7562 OO
.ui
.RadioOptionWidget
.parent
.call( this, config
);
7565 // Remove implicit role, we're handling it ourselves
7566 this.radio
.$input
.attr( 'role', 'presentation' );
7568 .addClass( 'oo-ui-radioOptionWidget' )
7569 .attr( 'role', 'radio' )
7570 .attr( 'aria-checked', 'false' )
7571 .removeAttr( 'aria-selected' )
7572 .prepend( this.radio
.$element
);
7577 OO
.inheritClass( OO
.ui
.RadioOptionWidget
, OO
.ui
.OptionWidget
);
7579 /* Static Properties */
7585 OO
.ui
.RadioOptionWidget
.static.highlightable
= false;
7591 OO
.ui
.RadioOptionWidget
.static.scrollIntoViewOnSelect
= true;
7597 OO
.ui
.RadioOptionWidget
.static.pressable
= false;
7603 OO
.ui
.RadioOptionWidget
.static.tagName
= 'label';
7610 OO
.ui
.RadioOptionWidget
.prototype.setSelected = function ( state
) {
7611 OO
.ui
.RadioOptionWidget
.parent
.prototype.setSelected
.call( this, state
);
7613 this.radio
.setSelected( state
);
7615 .attr( 'aria-checked', state
.toString() )
7616 .removeAttr( 'aria-selected' );
7624 OO
.ui
.RadioOptionWidget
.prototype.setDisabled = function ( disabled
) {
7625 OO
.ui
.RadioOptionWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
7627 this.radio
.setDisabled( this.isDisabled() );
7633 * RadioSelectWidget is a {@link OO.ui.SelectWidget select widget} that contains radio
7634 * options and is used together with OO.ui.RadioOptionWidget. The RadioSelectWidget provides
7635 * an interface for adding, removing and selecting options.
7636 * Please see the [OOjs UI documentation on MediaWiki][1] for more information.
7638 * If you want to use this within an HTML form, such as a OO.ui.FormLayout, use
7639 * OO.ui.RadioSelectInputWidget instead.
7642 * // A RadioSelectWidget with RadioOptions.
7643 * var option1 = new OO.ui.RadioOptionWidget( {
7645 * label: 'Selected radio option'
7648 * var option2 = new OO.ui.RadioOptionWidget( {
7650 * label: 'Unselected radio option'
7653 * var radioSelect=new OO.ui.RadioSelectWidget( {
7654 * items: [ option1, option2 ]
7657 * // Select 'option 1' using the RadioSelectWidget's selectItem() method.
7658 * radioSelect.selectItem( option1 );
7660 * $( 'body' ).append( radioSelect.$element );
7662 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
7666 * @extends OO.ui.SelectWidget
7667 * @mixins OO.ui.mixin.TabIndexedElement
7670 * @param {Object} [config] Configuration options
7672 OO
.ui
.RadioSelectWidget
= function OoUiRadioSelectWidget( config
) {
7673 // Parent constructor
7674 OO
.ui
.RadioSelectWidget
.parent
.call( this, config
);
7676 // Mixin constructors
7677 OO
.ui
.mixin
.TabIndexedElement
.call( this, config
);
7681 focus
: this.bindKeyDownListener
.bind( this ),
7682 blur
: this.unbindKeyDownListener
.bind( this )
7687 .addClass( 'oo-ui-radioSelectWidget' )
7688 .attr( 'role', 'radiogroup' );
7693 OO
.inheritClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.SelectWidget
);
7694 OO
.mixinClass( OO
.ui
.RadioSelectWidget
, OO
.ui
.mixin
.TabIndexedElement
);
7697 * MultioptionWidgets are special elements that can be selected and configured with data. The
7698 * data is often unique for each option, but it does not have to be. MultioptionWidgets are used
7699 * with OO.ui.SelectWidget to create a selection of mutually exclusive options. For more information
7700 * and examples, please see the [OOjs UI documentation on MediaWiki][1].
7702 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Multioptions
7705 * @extends OO.ui.Widget
7706 * @mixins OO.ui.mixin.ItemWidget
7707 * @mixins OO.ui.mixin.LabelElement
7710 * @param {Object} [config] Configuration options
7711 * @cfg {boolean} [selected=false] Whether the option is initially selected
7713 OO
.ui
.MultioptionWidget
= function OoUiMultioptionWidget( config
) {
7714 // Configuration initialization
7715 config
= config
|| {};
7717 // Parent constructor
7718 OO
.ui
.MultioptionWidget
.parent
.call( this, config
);
7720 // Mixin constructors
7721 OO
.ui
.mixin
.ItemWidget
.call( this );
7722 OO
.ui
.mixin
.LabelElement
.call( this, config
);
7725 this.selected
= null;
7729 .addClass( 'oo-ui-multioptionWidget' )
7730 .append( this.$label
);
7731 this.setSelected( config
.selected
);
7736 OO
.inheritClass( OO
.ui
.MultioptionWidget
, OO
.ui
.Widget
);
7737 OO
.mixinClass( OO
.ui
.MultioptionWidget
, OO
.ui
.mixin
.ItemWidget
);
7738 OO
.mixinClass( OO
.ui
.MultioptionWidget
, OO
.ui
.mixin
.LabelElement
);
7745 * A change event is emitted when the selected state of the option changes.
7747 * @param {boolean} selected Whether the option is now selected
7753 * Check if the option is selected.
7755 * @return {boolean} Item is selected
7757 OO
.ui
.MultioptionWidget
.prototype.isSelected = function () {
7758 return this.selected
;
7762 * Set the option’s selected state. In general, all modifications to the selection
7763 * should be handled by the SelectWidget’s {@link OO.ui.SelectWidget#selectItem selectItem( [item] )}
7764 * method instead of this method.
7766 * @param {boolean} [state=false] Select option
7769 OO
.ui
.MultioptionWidget
.prototype.setSelected = function ( state
) {
7771 if ( this.selected
!== state
) {
7772 this.selected
= state
;
7773 this.emit( 'change', state
);
7774 this.$element
.toggleClass( 'oo-ui-multioptionWidget-selected', state
);
7780 * MultiselectWidget allows selecting multiple options from a list.
7782 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
7784 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
7788 * @extends OO.ui.Widget
7789 * @mixins OO.ui.mixin.GroupWidget
7792 * @param {Object} [config] Configuration options
7793 * @cfg {OO.ui.MultioptionWidget[]} [items] An array of options to add to the multiselect.
7795 OO
.ui
.MultiselectWidget
= function OoUiMultiselectWidget( config
) {
7796 // Parent constructor
7797 OO
.ui
.MultiselectWidget
.parent
.call( this, config
);
7799 // Configuration initialization
7800 config
= config
|| {};
7802 // Mixin constructors
7803 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
7806 this.aggregate( { change
: 'select' } );
7807 // This is mostly for compatibility with CapsuleMultiselectWidget... normally, 'change' is emitted
7808 // by GroupElement only when items are added/removed
7809 this.connect( this, { select
: [ 'emit', 'change' ] } );
7812 if ( config
.items
) {
7813 this.addItems( config
.items
);
7815 this.$group
.addClass( 'oo-ui-multiselectWidget-group' );
7816 this.$element
.addClass( 'oo-ui-multiselectWidget' )
7817 .append( this.$group
);
7822 OO
.inheritClass( OO
.ui
.MultiselectWidget
, OO
.ui
.Widget
);
7823 OO
.mixinClass( OO
.ui
.MultiselectWidget
, OO
.ui
.mixin
.GroupWidget
);
7830 * A change event is emitted when the set of items changes, or an item is selected or deselected.
7836 * A select event is emitted when an item is selected or deselected.
7842 * Get options that are selected.
7844 * @return {OO.ui.MultioptionWidget[]} Selected options
7846 OO
.ui
.MultiselectWidget
.prototype.getSelectedItems = function () {
7847 return this.items
.filter( function ( item
) {
7848 return item
.isSelected();
7853 * Get the data of options that are selected.
7855 * @return {Object[]|string[]} Values of selected options
7857 OO
.ui
.MultiselectWidget
.prototype.getSelectedItemsData = function () {
7858 return this.getSelectedItems().map( function ( item
) {
7864 * Select options by reference. Options not mentioned in the `items` array will be deselected.
7866 * @param {OO.ui.MultioptionWidget[]} items Items to select
7869 OO
.ui
.MultiselectWidget
.prototype.selectItems = function ( items
) {
7870 this.items
.forEach( function ( item
) {
7871 var selected
= items
.indexOf( item
) !== -1;
7872 item
.setSelected( selected
);
7878 * Select items by their data. Options not mentioned in the `datas` array will be deselected.
7880 * @param {Object[]|string[]} datas Values of items to select
7883 OO
.ui
.MultiselectWidget
.prototype.selectItemsByData = function ( datas
) {
7886 items
= datas
.map( function ( data
) {
7887 return widget
.getItemFromData( data
);
7889 this.selectItems( items
);
7894 * CheckboxMultioptionWidget is an option widget that looks like a checkbox.
7895 * The class is used with OO.ui.CheckboxMultiselectWidget to create a selection of checkbox options.
7896 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information.
7898 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Button_selects_and_option
7901 * @extends OO.ui.MultioptionWidget
7904 * @param {Object} [config] Configuration options
7906 OO
.ui
.CheckboxMultioptionWidget
= function OoUiCheckboxMultioptionWidget( config
) {
7907 // Configuration initialization
7908 config
= config
|| {};
7910 // Properties (must be done before parent constructor which calls #setDisabled)
7911 this.checkbox
= new OO
.ui
.CheckboxInputWidget();
7913 // Parent constructor
7914 OO
.ui
.CheckboxMultioptionWidget
.parent
.call( this, config
);
7917 this.checkbox
.on( 'change', this.onCheckboxChange
.bind( this ) );
7918 this.$element
.on( 'keydown', this.onKeyDown
.bind( this ) );
7922 .addClass( 'oo-ui-checkboxMultioptionWidget' )
7923 .prepend( this.checkbox
.$element
);
7928 OO
.inheritClass( OO
.ui
.CheckboxMultioptionWidget
, OO
.ui
.MultioptionWidget
);
7930 /* Static Properties */
7936 OO
.ui
.CheckboxMultioptionWidget
.static.tagName
= 'label';
7941 * Handle checkbox selected state change.
7945 OO
.ui
.CheckboxMultioptionWidget
.prototype.onCheckboxChange = function () {
7946 this.setSelected( this.checkbox
.isSelected() );
7952 OO
.ui
.CheckboxMultioptionWidget
.prototype.setSelected = function ( state
) {
7953 OO
.ui
.CheckboxMultioptionWidget
.parent
.prototype.setSelected
.call( this, state
);
7954 this.checkbox
.setSelected( state
);
7961 OO
.ui
.CheckboxMultioptionWidget
.prototype.setDisabled = function ( disabled
) {
7962 OO
.ui
.CheckboxMultioptionWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
7963 this.checkbox
.setDisabled( this.isDisabled() );
7970 OO
.ui
.CheckboxMultioptionWidget
.prototype.focus = function () {
7971 this.checkbox
.focus();
7975 * Handle key down events.
7978 * @param {jQuery.Event} e
7980 OO
.ui
.CheckboxMultioptionWidget
.prototype.onKeyDown = function ( e
) {
7982 element
= this.getElementGroup(),
7985 if ( e
.keyCode
=== OO
.ui
.Keys
.LEFT
|| e
.keyCode
=== OO
.ui
.Keys
.UP
) {
7986 nextItem
= element
.getRelativeFocusableItem( this, -1 );
7987 } else if ( e
.keyCode
=== OO
.ui
.Keys
.RIGHT
|| e
.keyCode
=== OO
.ui
.Keys
.DOWN
) {
7988 nextItem
= element
.getRelativeFocusableItem( this, 1 );
7998 * CheckboxMultiselectWidget is a {@link OO.ui.MultiselectWidget multiselect widget} that contains
7999 * checkboxes and is used together with OO.ui.CheckboxMultioptionWidget. The
8000 * CheckboxMultiselectWidget provides an interface for adding, removing and selecting options.
8001 * Please see the [OOjs UI documentation on MediaWiki][1] for more information.
8003 * If you want to use this within an HTML form, such as a OO.ui.FormLayout, use
8004 * OO.ui.CheckboxMultiselectInputWidget instead.
8007 * // A CheckboxMultiselectWidget with CheckboxMultioptions.
8008 * var option1 = new OO.ui.CheckboxMultioptionWidget( {
8011 * label: 'Selected checkbox'
8014 * var option2 = new OO.ui.CheckboxMultioptionWidget( {
8016 * label: 'Unselected checkbox'
8019 * var multiselect=new OO.ui.CheckboxMultiselectWidget( {
8020 * items: [ option1, option2 ]
8023 * $( 'body' ).append( multiselect.$element );
8025 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options
8028 * @extends OO.ui.MultiselectWidget
8031 * @param {Object} [config] Configuration options
8033 OO
.ui
.CheckboxMultiselectWidget
= function OoUiCheckboxMultiselectWidget( config
) {
8034 // Parent constructor
8035 OO
.ui
.CheckboxMultiselectWidget
.parent
.call( this, config
);
8038 this.$lastClicked
= null;
8041 this.$group
.on( 'click', this.onClick
.bind( this ) );
8045 .addClass( 'oo-ui-checkboxMultiselectWidget' );
8050 OO
.inheritClass( OO
.ui
.CheckboxMultiselectWidget
, OO
.ui
.MultiselectWidget
);
8055 * Get an option by its position relative to the specified item (or to the start of the option array,
8056 * if item is `null`). The direction in which to search through the option array is specified with a
8057 * number: -1 for reverse (the default) or 1 for forward. The method will return an option, or
8058 * `null` if there are no options in the array.
8060 * @param {OO.ui.CheckboxMultioptionWidget|null} item Item to describe the start position, or `null` to start at the beginning of the array.
8061 * @param {number} direction Direction to move in: -1 to move backward, 1 to move forward
8062 * @return {OO.ui.CheckboxMultioptionWidget|null} Item at position, `null` if there are no items in the select
8064 OO
.ui
.CheckboxMultiselectWidget
.prototype.getRelativeFocusableItem = function ( item
, direction
) {
8065 var currentIndex
, nextIndex
, i
,
8066 increase
= direction
> 0 ? 1 : -1,
8067 len
= this.items
.length
;
8070 currentIndex
= this.items
.indexOf( item
);
8071 nextIndex
= ( currentIndex
+ increase
+ len
) % len
;
8073 // If no item is selected and moving forward, start at the beginning.
8074 // If moving backward, start at the end.
8075 nextIndex
= direction
> 0 ? 0 : len
- 1;
8078 for ( i
= 0; i
< len
; i
++ ) {
8079 item
= this.items
[ nextIndex
];
8080 if ( item
&& !item
.isDisabled() ) {
8083 nextIndex
= ( nextIndex
+ increase
+ len
) % len
;
8089 * Handle click events on checkboxes.
8091 * @param {jQuery.Event} e
8093 OO
.ui
.CheckboxMultiselectWidget
.prototype.onClick = function ( e
) {
8094 var $options
, lastClickedIndex
, nowClickedIndex
, i
, direction
, wasSelected
, items
,
8095 $lastClicked
= this.$lastClicked
,
8096 $nowClicked
= $( e
.target
).closest( '.oo-ui-checkboxMultioptionWidget' )
8097 .not( '.oo-ui-widget-disabled' );
8099 // Allow selecting multiple options at once by Shift-clicking them
8100 if ( $lastClicked
&& $nowClicked
.length
&& e
.shiftKey
) {
8101 $options
= this.$group
.find( '.oo-ui-checkboxMultioptionWidget' );
8102 lastClickedIndex
= $options
.index( $lastClicked
);
8103 nowClickedIndex
= $options
.index( $nowClicked
);
8104 // If it's the same item, either the user is being silly, or it's a fake event generated by the
8105 // browser. In either case we don't need custom handling.
8106 if ( nowClickedIndex
!== lastClickedIndex
) {
8108 wasSelected
= items
[ nowClickedIndex
].isSelected();
8109 direction
= nowClickedIndex
> lastClickedIndex
? 1 : -1;
8111 // This depends on the DOM order of the items and the order of the .items array being the same.
8112 for ( i
= lastClickedIndex
; i
!== nowClickedIndex
; i
+= direction
) {
8113 if ( !items
[ i
].isDisabled() ) {
8114 items
[ i
].setSelected( !wasSelected
);
8117 // For the now-clicked element, use immediate timeout to allow the browser to do its own
8118 // handling first, then set our value. The order in which events happen is different for
8119 // clicks on the <input> and on the <label> and there are additional fake clicks fired for
8120 // non-click actions that change the checkboxes.
8122 setTimeout( function () {
8123 if ( !items
[ nowClickedIndex
].isDisabled() ) {
8124 items
[ nowClickedIndex
].setSelected( !wasSelected
);
8130 if ( $nowClicked
.length
) {
8131 this.$lastClicked
= $nowClicked
;
8140 OO
.ui
.CheckboxMultiselectWidget
.prototype.focus = function () {
8142 if ( !this.isDisabled() ) {
8143 item
= this.getRelativeFocusableItem( null, 1 );
8154 OO
.ui
.CheckboxMultiselectWidget
.prototype.simulateLabelClick = function () {
8159 * Progress bars visually display the status of an operation, such as a download,
8160 * and can be either determinate or indeterminate:
8162 * - **determinate** process bars show the percent of an operation that is complete.
8164 * - **indeterminate** process bars use a visual display of motion to indicate that an operation
8165 * is taking place. Because the extent of an indeterminate operation is unknown, the bar does
8166 * not use percentages.
8168 * The value of the `progress` configuration determines whether the bar is determinate or indeterminate.
8171 * // Examples of determinate and indeterminate progress bars.
8172 * var progressBar1 = new OO.ui.ProgressBarWidget( {
8175 * var progressBar2 = new OO.ui.ProgressBarWidget();
8177 * // Create a FieldsetLayout to layout progress bars
8178 * var fieldset = new OO.ui.FieldsetLayout;
8179 * fieldset.addItems( [
8180 * new OO.ui.FieldLayout( progressBar1, {label: 'Determinate', align: 'top'}),
8181 * new OO.ui.FieldLayout( progressBar2, {label: 'Indeterminate', align: 'top'})
8183 * $( 'body' ).append( fieldset.$element );
8186 * @extends OO.ui.Widget
8189 * @param {Object} [config] Configuration options
8190 * @cfg {number|boolean} [progress=false] The type of progress bar (determinate or indeterminate).
8191 * To create a determinate progress bar, specify a number that reflects the initial percent complete.
8192 * By default, the progress bar is indeterminate.
8194 OO
.ui
.ProgressBarWidget
= function OoUiProgressBarWidget( config
) {
8195 // Configuration initialization
8196 config
= config
|| {};
8198 // Parent constructor
8199 OO
.ui
.ProgressBarWidget
.parent
.call( this, config
);
8202 this.$bar
= $( '<div>' );
8203 this.progress
= null;
8206 this.setProgress( config
.progress
!== undefined ? config
.progress
: false );
8207 this.$bar
.addClass( 'oo-ui-progressBarWidget-bar' );
8210 role
: 'progressbar',
8212 'aria-valuemax': 100
8214 .addClass( 'oo-ui-progressBarWidget' )
8215 .append( this.$bar
);
8220 OO
.inheritClass( OO
.ui
.ProgressBarWidget
, OO
.ui
.Widget
);
8222 /* Static Properties */
8228 OO
.ui
.ProgressBarWidget
.static.tagName
= 'div';
8233 * Get the percent of the progress that has been completed. Indeterminate progresses will return `false`.
8235 * @return {number|boolean} Progress percent
8237 OO
.ui
.ProgressBarWidget
.prototype.getProgress = function () {
8238 return this.progress
;
8242 * Set the percent of the process completed or `false` for an indeterminate process.
8244 * @param {number|boolean} progress Progress percent or `false` for indeterminate
8246 OO
.ui
.ProgressBarWidget
.prototype.setProgress = function ( progress
) {
8247 this.progress
= progress
;
8249 if ( progress
!== false ) {
8250 this.$bar
.css( 'width', this.progress
+ '%' );
8251 this.$element
.attr( 'aria-valuenow', this.progress
);
8253 this.$bar
.css( 'width', '' );
8254 this.$element
.removeAttr( 'aria-valuenow' );
8256 this.$element
.toggleClass( 'oo-ui-progressBarWidget-indeterminate', progress
=== false );
8260 * InputWidget is the base class for all input widgets, which
8261 * include {@link OO.ui.TextInputWidget text inputs}, {@link OO.ui.CheckboxInputWidget checkbox inputs},
8262 * {@link OO.ui.RadioInputWidget radio inputs}, and {@link OO.ui.ButtonInputWidget button inputs}.
8263 * See the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
8265 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
8269 * @extends OO.ui.Widget
8270 * @mixins OO.ui.mixin.FlaggedElement
8271 * @mixins OO.ui.mixin.TabIndexedElement
8272 * @mixins OO.ui.mixin.TitledElement
8273 * @mixins OO.ui.mixin.AccessKeyedElement
8276 * @param {Object} [config] Configuration options
8277 * @cfg {string} [name=''] The value of the input’s HTML `name` attribute.
8278 * @cfg {string} [value=''] The value of the input.
8279 * @cfg {string} [dir] The directionality of the input (ltr/rtl).
8280 * @cfg {string} [inputId] The value of the input’s HTML `id` attribute.
8281 * @cfg {Function} [inputFilter] The name of an input filter function. Input filters modify the value of an input
8282 * before it is accepted.
8284 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8285 // Configuration initialization
8286 config
= config
|| {};
8288 // Parent constructor
8289 OO
.ui
.InputWidget
.parent
.call( this, config
);
8292 // See #reusePreInfuseDOM about config.$input
8293 this.$input
= config
.$input
|| this.getInputElement( config
);
8295 this.inputFilter
= config
.inputFilter
;
8297 // Mixin constructors
8298 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
8299 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$input
} ) );
8300 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
8301 OO
.ui
.mixin
.AccessKeyedElement
.call( this, $.extend( {}, config
, { $accessKeyed
: this.$input
} ) );
8304 this.$input
.on( 'keydown mouseup cut paste change input select', this.onEdit
.bind( this ) );
8308 .addClass( 'oo-ui-inputWidget-input' )
8309 .attr( 'name', config
.name
)
8310 .prop( 'disabled', this.isDisabled() );
8312 .addClass( 'oo-ui-inputWidget' )
8313 .append( this.$input
);
8314 this.setValue( config
.value
);
8316 this.setDir( config
.dir
);
8318 if ( config
.inputId
!== undefined ) {
8319 this.setInputId( config
.inputId
);
8325 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8326 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.mixin
.FlaggedElement
);
8327 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.mixin
.TabIndexedElement
);
8328 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.mixin
.TitledElement
);
8329 OO
.mixinClass( OO
.ui
.InputWidget
, OO
.ui
.mixin
.AccessKeyedElement
);
8331 /* Static Methods */
8336 OO
.ui
.InputWidget
.static.reusePreInfuseDOM = function ( node
, config
) {
8337 config
= OO
.ui
.InputWidget
.parent
.static.reusePreInfuseDOM( node
, config
);
8338 // Reusing `$input` lets browsers preserve inputted values across page reloads, see T114134.
8339 config
.$input
= $( node
).find( '.oo-ui-inputWidget-input' );
8346 OO
.ui
.InputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
8347 var state
= OO
.ui
.InputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
8348 if ( config
.$input
&& config
.$input
.length
) {
8349 state
.value
= config
.$input
.val();
8350 // Might be better in TabIndexedElement, but it's awkward to do there because mixins are awkward
8351 state
.focus
= config
.$input
.is( ':focus' );
8361 * A change event is emitted when the value of the input changes.
8363 * @param {string} value
8369 * Get input element.
8371 * Subclasses of OO.ui.InputWidget use the `config` parameter to produce different elements in
8372 * different circumstances. The element must have a `value` property (like form elements).
8375 * @param {Object} config Configuration options
8376 * @return {jQuery} Input element
8378 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8379 return $( '<input>' );
8383 * Handle potentially value-changing events.
8386 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8388 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8390 if ( !this.isDisabled() ) {
8391 // Allow the stack to clear so the value will be updated
8392 setTimeout( function () {
8393 widget
.setValue( widget
.$input
.val() );
8399 * Get the value of the input.
8401 * @return {string} Input value
8403 OO
.ui
.InputWidget
.prototype.getValue = function () {
8404 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
8405 // it, and we won't know unless they're kind enough to trigger a 'change' event.
8406 var value
= this.$input
.val();
8407 if ( this.value
!== value
) {
8408 this.setValue( value
);
8414 * Set the directionality of the input.
8416 * @param {string} dir Text directionality: 'ltr', 'rtl' or 'auto'
8419 OO
.ui
.InputWidget
.prototype.setDir = function ( dir
) {
8420 this.$input
.prop( 'dir', dir
);
8425 * Set the value of the input.
8427 * @param {string} value New value
8431 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8432 value
= this.cleanUpValue( value
);
8433 // Update the DOM if it has changed. Note that with cleanUpValue, it
8434 // is possible for the DOM value to change without this.value changing.
8435 if ( this.$input
.val() !== value
) {
8436 this.$input
.val( value
);
8438 if ( this.value
!== value
) {
8440 this.emit( 'change', this.value
);
8446 * Clean up incoming value.
8448 * Ensures value is a string, and converts undefined and null to empty string.
8451 * @param {string} value Original value
8452 * @return {string} Cleaned up value
8454 OO
.ui
.InputWidget
.prototype.cleanUpValue = function ( value
) {
8455 if ( value
=== undefined || value
=== null ) {
8457 } else if ( this.inputFilter
) {
8458 return this.inputFilter( String( value
) );
8460 return String( value
);
8467 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8468 OO
.ui
.InputWidget
.parent
.prototype.setDisabled
.call( this, state
);
8469 if ( this.$input
) {
8470 this.$input
.prop( 'disabled', this.isDisabled() );
8476 * Set the 'id' attribute of the `<input>` element.
8478 * @param {string} id
8481 OO
.ui
.InputWidget
.prototype.setInputId = function ( id
) {
8482 this.$input
.attr( 'id', id
);
8489 OO
.ui
.InputWidget
.prototype.restorePreInfuseState = function ( state
) {
8490 OO
.ui
.InputWidget
.parent
.prototype.restorePreInfuseState
.call( this, state
);
8491 if ( state
.value
!== undefined && state
.value
!== this.getValue() ) {
8492 this.setValue( state
.value
);
8494 if ( state
.focus
) {
8500 * Data widget intended for creating 'hidden'-type inputs.
8503 * @extends OO.ui.Widget
8506 * @param {Object} [config] Configuration options
8507 * @cfg {string} [value=''] The value of the input.
8508 * @cfg {string} [name=''] The value of the input’s HTML `name` attribute.
8510 OO
.ui
.HiddenInputWidget
= function OoUiHiddenInputWidget( config
) {
8511 // Configuration initialization
8512 config
= $.extend( { value
: '', name
: '' }, config
);
8514 // Parent constructor
8515 OO
.ui
.HiddenInputWidget
.parent
.call( this, config
);
8518 this.$element
.attr( {
8520 value
: config
.value
,
8523 this.$element
.removeAttr( 'aria-disabled' );
8528 OO
.inheritClass( OO
.ui
.HiddenInputWidget
, OO
.ui
.Widget
);
8530 /* Static Properties */
8536 OO
.ui
.HiddenInputWidget
.static.tagName
= 'input';
8539 * ButtonInputWidget is used to submit HTML forms and is intended to be used within
8540 * a OO.ui.FormLayout. If you do not need the button to work with HTML forms, you probably
8541 * want to use OO.ui.ButtonWidget instead. Button input widgets can be rendered as either an
8542 * HTML `<button>` (the default) or an HTML `<input>` tags. See the
8543 * [OOjs UI documentation on MediaWiki] [1] for more information.
8546 * // A ButtonInputWidget rendered as an HTML button, the default.
8547 * var button = new OO.ui.ButtonInputWidget( {
8548 * label: 'Input button',
8552 * $( 'body' ).append( button.$element );
8554 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs#Button_inputs
8557 * @extends OO.ui.InputWidget
8558 * @mixins OO.ui.mixin.ButtonElement
8559 * @mixins OO.ui.mixin.IconElement
8560 * @mixins OO.ui.mixin.IndicatorElement
8561 * @mixins OO.ui.mixin.LabelElement
8562 * @mixins OO.ui.mixin.TitledElement
8565 * @param {Object} [config] Configuration options
8566 * @cfg {string} [type='button'] The value of the HTML `'type'` attribute: 'button', 'submit' or 'reset'.
8567 * @cfg {boolean} [useInputTag=false] Use an `<input>` tag instead of a `<button>` tag, the default.
8568 * Widgets configured to be an `<input>` do not support {@link #icon icons} and {@link #indicator indicators},
8569 * non-plaintext {@link #label labels}, or {@link #value values}. In general, useInputTag should only
8570 * be set to `true` when there’s need to support IE 6 in a form with multiple buttons.
8572 OO
.ui
.ButtonInputWidget
= function OoUiButtonInputWidget( config
) {
8573 // Configuration initialization
8574 config
= $.extend( { type
: 'button', useInputTag
: false }, config
);
8576 // See InputWidget#reusePreInfuseDOM about config.$input
8577 if ( config
.$input
) {
8578 config
.$input
.empty();
8581 // Properties (must be set before parent constructor, which calls #setValue)
8582 this.useInputTag
= config
.useInputTag
;
8584 // Parent constructor
8585 OO
.ui
.ButtonInputWidget
.parent
.call( this, config
);
8587 // Mixin constructors
8588 OO
.ui
.mixin
.ButtonElement
.call( this, $.extend( {}, config
, { $button
: this.$input
} ) );
8589 OO
.ui
.mixin
.IconElement
.call( this, config
);
8590 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
8591 OO
.ui
.mixin
.LabelElement
.call( this, config
);
8592 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$input
} ) );
8595 if ( !config
.useInputTag
) {
8596 this.$input
.append( this.$icon
, this.$label
, this.$indicator
);
8598 this.$element
.addClass( 'oo-ui-buttonInputWidget' );
8603 OO
.inheritClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.InputWidget
);
8604 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.mixin
.ButtonElement
);
8605 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.mixin
.IconElement
);
8606 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
8607 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.mixin
.LabelElement
);
8608 OO
.mixinClass( OO
.ui
.ButtonInputWidget
, OO
.ui
.mixin
.TitledElement
);
8610 /* Static Properties */
8616 OO
.ui
.ButtonInputWidget
.static.tagName
= 'span';
8624 OO
.ui
.ButtonInputWidget
.prototype.getInputElement = function ( config
) {
8626 type
= [ 'button', 'submit', 'reset' ].indexOf( config
.type
) !== -1 ? config
.type
: 'button';
8627 return $( '<' + ( config
.useInputTag
? 'input' : 'button' ) + ' type="' + type
+ '">' );
8633 * If #useInputTag is `true`, the label is set as the `value` of the `<input>` tag.
8635 * @param {jQuery|string|Function|null} label Label nodes, text, a function that returns nodes or
8636 * text, or `null` for no label
8639 OO
.ui
.ButtonInputWidget
.prototype.setLabel = function ( label
) {
8640 if ( typeof label
=== 'function' ) {
8641 label
= OO
.ui
.resolveMsg( label
);
8644 if ( this.useInputTag
) {
8645 // Discard non-plaintext labels
8646 if ( typeof label
!== 'string' ) {
8650 this.$input
.val( label
);
8653 return OO
.ui
.mixin
.LabelElement
.prototype.setLabel
.call( this, label
);
8657 * Set the value of the input.
8659 * This method is disabled for button inputs configured as {@link #useInputTag <input> tags}, as
8660 * they do not support {@link #value values}.
8662 * @param {string} value New value
8665 OO
.ui
.ButtonInputWidget
.prototype.setValue = function ( value
) {
8666 if ( !this.useInputTag
) {
8667 OO
.ui
.ButtonInputWidget
.parent
.prototype.setValue
.call( this, value
);
8675 OO
.ui
.ButtonInputWidget
.prototype.getInputId = function () {
8676 // Disable generating `<label>` elements for buttons. One would very rarely need additional label
8677 // for a button, and it's already a big clickable target, and it causes unexpected rendering.
8682 * CheckboxInputWidgets, like HTML checkboxes, can be selected and/or configured with a value.
8683 * Note that these {@link OO.ui.InputWidget input widgets} are best laid out
8684 * in {@link OO.ui.FieldLayout field layouts} that use the {@link OO.ui.FieldLayout#align inline}
8685 * alignment. For more information, please see the [OOjs UI documentation on MediaWiki][1].
8687 * This widget can be used inside an HTML form, such as a OO.ui.FormLayout.
8690 * // An example of selected, unselected, and disabled checkbox inputs
8691 * var checkbox1=new OO.ui.CheckboxInputWidget( {
8695 * var checkbox2=new OO.ui.CheckboxInputWidget( {
8698 * var checkbox3=new OO.ui.CheckboxInputWidget( {
8702 * // Create a fieldset layout with fields for each checkbox.
8703 * var fieldset = new OO.ui.FieldsetLayout( {
8704 * label: 'Checkboxes'
8706 * fieldset.addItems( [
8707 * new OO.ui.FieldLayout( checkbox1, { label: 'Selected checkbox', align: 'inline' } ),
8708 * new OO.ui.FieldLayout( checkbox2, { label: 'Unselected checkbox', align: 'inline' } ),
8709 * new OO.ui.FieldLayout( checkbox3, { label: 'Disabled checkbox', align: 'inline' } ),
8711 * $( 'body' ).append( fieldset.$element );
8713 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
8716 * @extends OO.ui.InputWidget
8719 * @param {Object} [config] Configuration options
8720 * @cfg {boolean} [selected=false] Select the checkbox initially. By default, the checkbox is not selected.
8722 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8723 // Configuration initialization
8724 config
= config
|| {};
8726 // Parent constructor
8727 OO
.ui
.CheckboxInputWidget
.parent
.call( this, config
);
8731 .addClass( 'oo-ui-checkboxInputWidget' )
8732 // Required for pretty styling in WikimediaUI theme
8733 .append( $( '<span>' ) );
8734 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
8739 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8741 /* Static Properties */
8747 OO
.ui
.CheckboxInputWidget
.static.tagName
= 'span';
8749 /* Static Methods */
8754 OO
.ui
.CheckboxInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
8755 var state
= OO
.ui
.CheckboxInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
8756 state
.checked
= config
.$input
.prop( 'checked' );
8766 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8767 return $( '<input>' ).attr( 'type', 'checkbox' );
8773 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8775 if ( !this.isDisabled() ) {
8776 // Allow the stack to clear so the value will be updated
8777 setTimeout( function () {
8778 widget
.setSelected( widget
.$input
.prop( 'checked' ) );
8784 * Set selection state of this checkbox.
8786 * @param {boolean} state `true` for selected
8789 OO
.ui
.CheckboxInputWidget
.prototype.setSelected = function ( state
) {
8791 if ( this.selected
!== state
) {
8792 this.selected
= state
;
8793 this.$input
.prop( 'checked', this.selected
);
8794 this.emit( 'change', this.selected
);
8800 * Check if this checkbox is selected.
8802 * @return {boolean} Checkbox is selected
8804 OO
.ui
.CheckboxInputWidget
.prototype.isSelected = function () {
8805 // Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
8806 // it, and we won't know unless they're kind enough to trigger a 'change' event.
8807 var selected
= this.$input
.prop( 'checked' );
8808 if ( this.selected
!== selected
) {
8809 this.setSelected( selected
);
8811 return this.selected
;
8817 OO
.ui
.CheckboxInputWidget
.prototype.simulateLabelClick = function () {
8818 if ( !this.isDisabled() ) {
8819 this.$input
.click();
8827 OO
.ui
.CheckboxInputWidget
.prototype.restorePreInfuseState = function ( state
) {
8828 OO
.ui
.CheckboxInputWidget
.parent
.prototype.restorePreInfuseState
.call( this, state
);
8829 if ( state
.checked
!== undefined && state
.checked
!== this.isSelected() ) {
8830 this.setSelected( state
.checked
);
8835 * DropdownInputWidget is a {@link OO.ui.DropdownWidget DropdownWidget} intended to be used
8836 * within an HTML form, such as a OO.ui.FormLayout. The selected value is synchronized with the value
8837 * of a hidden HTML `input` tag. Please see the [OOjs UI documentation on MediaWiki][1] for
8838 * more information about input widgets.
8840 * A DropdownInputWidget always has a value (one of the options is always selected), unless there
8841 * are no options. If no `value` configuration option is provided, the first option is selected.
8842 * If you need a state representing no value (no option being selected), use a DropdownWidget.
8844 * This and OO.ui.RadioSelectInputWidget support the same configuration options.
8847 * // Example: A DropdownInputWidget with three options
8848 * var dropdownInput = new OO.ui.DropdownInputWidget( {
8850 * { data: 'a', label: 'First' },
8851 * { data: 'b', label: 'Second'},
8852 * { data: 'c', label: 'Third' }
8855 * $( 'body' ).append( dropdownInput.$element );
8857 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
8860 * @extends OO.ui.InputWidget
8861 * @mixins OO.ui.mixin.TitledElement
8864 * @param {Object} [config] Configuration options
8865 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
8866 * @cfg {Object} [dropdown] Configuration options for {@link OO.ui.DropdownWidget DropdownWidget}
8868 OO
.ui
.DropdownInputWidget
= function OoUiDropdownInputWidget( config
) {
8869 // Configuration initialization
8870 config
= config
|| {};
8872 // See InputWidget#reusePreInfuseDOM about config.$input
8873 if ( config
.$input
) {
8874 config
.$input
.addClass( 'oo-ui-element-hidden' );
8877 // Properties (must be done before parent constructor which calls #setDisabled)
8878 this.dropdownWidget
= new OO
.ui
.DropdownWidget( config
.dropdown
);
8880 // Parent constructor
8881 OO
.ui
.DropdownInputWidget
.parent
.call( this, config
);
8883 // Mixin constructors
8884 OO
.ui
.mixin
.TitledElement
.call( this, config
);
8887 this.dropdownWidget
.getMenu().connect( this, { select
: 'onMenuSelect' } );
8890 this.setOptions( config
.options
|| [] );
8891 // Set the value again, after we did setOptions(). The call from parent doesn't work because the
8892 // widget has no valid options when it happens.
8893 this.setValue( config
.value
);
8895 .addClass( 'oo-ui-dropdownInputWidget' )
8896 .append( this.dropdownWidget
.$element
);
8897 this.setTabIndexedElement( null );
8902 OO
.inheritClass( OO
.ui
.DropdownInputWidget
, OO
.ui
.InputWidget
);
8903 OO
.mixinClass( OO
.ui
.DropdownInputWidget
, OO
.ui
.mixin
.TitledElement
);
8911 OO
.ui
.DropdownInputWidget
.prototype.getInputElement = function () {
8912 return $( '<input>' ).attr( 'type', 'hidden' );
8916 * Handles menu select events.
8919 * @param {OO.ui.MenuOptionWidget|null} item Selected menu item
8921 OO
.ui
.DropdownInputWidget
.prototype.onMenuSelect = function ( item
) {
8922 this.setValue( item
? item
.getData() : '' );
8928 OO
.ui
.DropdownInputWidget
.prototype.setValue = function ( value
) {
8930 value
= this.cleanUpValue( value
);
8931 // Only allow setting values that are actually present in the dropdown
8932 selected
= this.dropdownWidget
.getMenu().getItemFromData( value
) ||
8933 this.dropdownWidget
.getMenu().getFirstSelectableItem();
8934 this.dropdownWidget
.getMenu().selectItem( selected
);
8935 value
= selected
? selected
.getData() : '';
8936 OO
.ui
.DropdownInputWidget
.parent
.prototype.setValue
.call( this, value
);
8943 OO
.ui
.DropdownInputWidget
.prototype.setDisabled = function ( state
) {
8944 this.dropdownWidget
.setDisabled( state
);
8945 OO
.ui
.DropdownInputWidget
.parent
.prototype.setDisabled
.call( this, state
);
8950 * Set the options available for this input.
8952 * @param {Object[]} options Array of menu options in the format `{ data: …, label: … }`
8955 OO
.ui
.DropdownInputWidget
.prototype.setOptions = function ( options
) {
8957 value
= this.getValue(),
8960 // Rebuild the dropdown menu
8961 this.dropdownWidget
.getMenu()
8963 .addItems( options
.map( function ( opt
) {
8964 var optValue
= widget
.cleanUpValue( opt
.data
);
8966 if ( opt
.optgroup
=== undefined ) {
8967 return new OO
.ui
.MenuOptionWidget( {
8969 label
: opt
.label
!== undefined ? opt
.label
: optValue
8972 return new OO
.ui
.MenuSectionOptionWidget( {
8978 // Restore the previous value, or reset to something sensible
8979 if ( this.dropdownWidget
.getMenu().getItemFromData( value
) ) {
8980 // Previous value is still available, ensure consistency with the dropdown
8981 this.setValue( value
);
8983 // No longer valid, reset
8984 if ( options
.length
) {
8985 this.setValue( options
[ 0 ].data
);
8995 OO
.ui
.DropdownInputWidget
.prototype.focus = function () {
8996 this.dropdownWidget
.focus();
9003 OO
.ui
.DropdownInputWidget
.prototype.blur = function () {
9004 this.dropdownWidget
.blur();
9009 * RadioInputWidget creates a single radio button. Because radio buttons are usually used as a set,
9010 * in most cases you will want to use a {@link OO.ui.RadioSelectWidget radio select}
9011 * with {@link OO.ui.RadioOptionWidget radio options} instead of this class. For more information,
9012 * please see the [OOjs UI documentation on MediaWiki][1].
9014 * This widget can be used inside an HTML form, such as a OO.ui.FormLayout.
9017 * // An example of selected, unselected, and disabled radio inputs
9018 * var radio1 = new OO.ui.RadioInputWidget( {
9022 * var radio2 = new OO.ui.RadioInputWidget( {
9025 * var radio3 = new OO.ui.RadioInputWidget( {
9029 * // Create a fieldset layout with fields for each radio button.
9030 * var fieldset = new OO.ui.FieldsetLayout( {
9031 * label: 'Radio inputs'
9033 * fieldset.addItems( [
9034 * new OO.ui.FieldLayout( radio1, { label: 'Selected', align: 'inline' } ),
9035 * new OO.ui.FieldLayout( radio2, { label: 'Unselected', align: 'inline' } ),
9036 * new OO.ui.FieldLayout( radio3, { label: 'Disabled', align: 'inline' } ),
9038 * $( 'body' ).append( fieldset.$element );
9040 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
9043 * @extends OO.ui.InputWidget
9046 * @param {Object} [config] Configuration options
9047 * @cfg {boolean} [selected=false] Select the radio button initially. By default, the radio button is not selected.
9049 OO
.ui
.RadioInputWidget
= function OoUiRadioInputWidget( config
) {
9050 // Configuration initialization
9051 config
= config
|| {};
9053 // Parent constructor
9054 OO
.ui
.RadioInputWidget
.parent
.call( this, config
);
9058 .addClass( 'oo-ui-radioInputWidget' )
9059 // Required for pretty styling in WikimediaUI theme
9060 .append( $( '<span>' ) );
9061 this.setSelected( config
.selected
!== undefined ? config
.selected
: false );
9066 OO
.inheritClass( OO
.ui
.RadioInputWidget
, OO
.ui
.InputWidget
);
9068 /* Static Properties */
9074 OO
.ui
.RadioInputWidget
.static.tagName
= 'span';
9076 /* Static Methods */
9081 OO
.ui
.RadioInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
9082 var state
= OO
.ui
.RadioInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
9083 state
.checked
= config
.$input
.prop( 'checked' );
9093 OO
.ui
.RadioInputWidget
.prototype.getInputElement = function () {
9094 return $( '<input>' ).attr( 'type', 'radio' );
9100 OO
.ui
.RadioInputWidget
.prototype.onEdit = function () {
9101 // RadioInputWidget doesn't track its state.
9105 * Set selection state of this radio button.
9107 * @param {boolean} state `true` for selected
9110 OO
.ui
.RadioInputWidget
.prototype.setSelected = function ( state
) {
9111 // RadioInputWidget doesn't track its state.
9112 this.$input
.prop( 'checked', state
);
9117 * Check if this radio button is selected.
9119 * @return {boolean} Radio is selected
9121 OO
.ui
.RadioInputWidget
.prototype.isSelected = function () {
9122 return this.$input
.prop( 'checked' );
9128 OO
.ui
.RadioInputWidget
.prototype.simulateLabelClick = function () {
9129 if ( !this.isDisabled() ) {
9130 this.$input
.click();
9138 OO
.ui
.RadioInputWidget
.prototype.restorePreInfuseState = function ( state
) {
9139 OO
.ui
.RadioInputWidget
.parent
.prototype.restorePreInfuseState
.call( this, state
);
9140 if ( state
.checked
!== undefined && state
.checked
!== this.isSelected() ) {
9141 this.setSelected( state
.checked
);
9146 * RadioSelectInputWidget is a {@link OO.ui.RadioSelectWidget RadioSelectWidget} intended to be used
9147 * within an HTML form, such as a OO.ui.FormLayout. The selected value is synchronized with the value
9148 * of a hidden HTML `input` tag. Please see the [OOjs UI documentation on MediaWiki][1] for
9149 * more information about input widgets.
9151 * This and OO.ui.DropdownInputWidget support the same configuration options.
9154 * // Example: A RadioSelectInputWidget with three options
9155 * var radioSelectInput = new OO.ui.RadioSelectInputWidget( {
9157 * { data: 'a', label: 'First' },
9158 * { data: 'b', label: 'Second'},
9159 * { data: 'c', label: 'Third' }
9162 * $( 'body' ).append( radioSelectInput.$element );
9164 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
9167 * @extends OO.ui.InputWidget
9170 * @param {Object} [config] Configuration options
9171 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
9173 OO
.ui
.RadioSelectInputWidget
= function OoUiRadioSelectInputWidget( config
) {
9174 // Configuration initialization
9175 config
= config
|| {};
9177 // Properties (must be done before parent constructor which calls #setDisabled)
9178 this.radioSelectWidget
= new OO
.ui
.RadioSelectWidget();
9180 // Parent constructor
9181 OO
.ui
.RadioSelectInputWidget
.parent
.call( this, config
);
9184 this.radioSelectWidget
.connect( this, { select
: 'onMenuSelect' } );
9187 this.setOptions( config
.options
|| [] );
9189 .addClass( 'oo-ui-radioSelectInputWidget' )
9190 .append( this.radioSelectWidget
.$element
);
9191 this.setTabIndexedElement( null );
9196 OO
.inheritClass( OO
.ui
.RadioSelectInputWidget
, OO
.ui
.InputWidget
);
9198 /* Static Methods */
9203 OO
.ui
.RadioSelectInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
9204 var state
= OO
.ui
.RadioSelectInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
9205 state
.value
= $( node
).find( '.oo-ui-radioInputWidget .oo-ui-inputWidget-input:checked' ).val();
9212 OO
.ui
.RadioSelectInputWidget
.static.reusePreInfuseDOM = function ( node
, config
) {
9213 config
= OO
.ui
.RadioSelectInputWidget
.parent
.static.reusePreInfuseDOM( node
, config
);
9214 // Cannot reuse the `<input type=radio>` set
9215 delete config
.$input
;
9225 OO
.ui
.RadioSelectInputWidget
.prototype.getInputElement = function () {
9226 return $( '<input>' ).attr( 'type', 'hidden' );
9230 * Handles menu select events.
9233 * @param {OO.ui.RadioOptionWidget} item Selected menu item
9235 OO
.ui
.RadioSelectInputWidget
.prototype.onMenuSelect = function ( item
) {
9236 this.setValue( item
.getData() );
9242 OO
.ui
.RadioSelectInputWidget
.prototype.setValue = function ( value
) {
9243 value
= this.cleanUpValue( value
);
9244 this.radioSelectWidget
.selectItemByData( value
);
9245 OO
.ui
.RadioSelectInputWidget
.parent
.prototype.setValue
.call( this, value
);
9252 OO
.ui
.RadioSelectInputWidget
.prototype.setDisabled = function ( state
) {
9253 this.radioSelectWidget
.setDisabled( state
);
9254 OO
.ui
.RadioSelectInputWidget
.parent
.prototype.setDisabled
.call( this, state
);
9259 * Set the options available for this input.
9261 * @param {Object[]} options Array of menu options in the format `{ data: …, label: … }`
9264 OO
.ui
.RadioSelectInputWidget
.prototype.setOptions = function ( options
) {
9266 value
= this.getValue(),
9269 // Rebuild the radioSelect menu
9270 this.radioSelectWidget
9272 .addItems( options
.map( function ( opt
) {
9273 var optValue
= widget
.cleanUpValue( opt
.data
);
9274 return new OO
.ui
.RadioOptionWidget( {
9276 label
: opt
.label
!== undefined ? opt
.label
: optValue
9280 // Restore the previous value, or reset to something sensible
9281 if ( this.radioSelectWidget
.getItemFromData( value
) ) {
9282 // Previous value is still available, ensure consistency with the radioSelect
9283 this.setValue( value
);
9285 // No longer valid, reset
9286 if ( options
.length
) {
9287 this.setValue( options
[ 0 ].data
);
9297 OO
.ui
.RadioSelectInputWidget
.prototype.focus = function () {
9298 this.radioSelectWidget
.focus();
9305 OO
.ui
.RadioSelectInputWidget
.prototype.blur = function () {
9306 this.radioSelectWidget
.blur();
9311 * CheckboxMultiselectInputWidget is a
9312 * {@link OO.ui.CheckboxMultiselectWidget CheckboxMultiselectWidget} intended to be used within a
9313 * HTML form, such as a OO.ui.FormLayout. The selected values are synchronized with the value of
9314 * HTML `<input type=checkbox>` tags. Please see the [OOjs UI documentation on MediaWiki][1] for
9315 * more information about input widgets.
9318 * // Example: A CheckboxMultiselectInputWidget with three options
9319 * var multiselectInput = new OO.ui.CheckboxMultiselectInputWidget( {
9321 * { data: 'a', label: 'First' },
9322 * { data: 'b', label: 'Second'},
9323 * { data: 'c', label: 'Third' }
9326 * $( 'body' ).append( multiselectInput.$element );
9328 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
9331 * @extends OO.ui.InputWidget
9334 * @param {Object} [config] Configuration options
9335 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: …, disabled: … }`
9337 OO
.ui
.CheckboxMultiselectInputWidget
= function OoUiCheckboxMultiselectInputWidget( config
) {
9338 // Configuration initialization
9339 config
= config
|| {};
9341 // Properties (must be done before parent constructor which calls #setDisabled)
9342 this.checkboxMultiselectWidget
= new OO
.ui
.CheckboxMultiselectWidget();
9344 // Parent constructor
9345 OO
.ui
.CheckboxMultiselectInputWidget
.parent
.call( this, config
);
9348 this.inputName
= config
.name
;
9352 .addClass( 'oo-ui-checkboxMultiselectInputWidget' )
9353 .append( this.checkboxMultiselectWidget
.$element
);
9354 // We don't use this.$input, but rather the CheckboxInputWidgets inside each option
9355 this.$input
.detach();
9356 this.setOptions( config
.options
|| [] );
9357 // Have to repeat this from parent, as we need options to be set up for this to make sense
9358 this.setValue( config
.value
);
9360 // setValue when checkboxMultiselectWidget changes
9361 this.checkboxMultiselectWidget
.on( 'change', function () {
9362 this.setValue( this.checkboxMultiselectWidget
.getSelectedItemsData() );
9368 OO
.inheritClass( OO
.ui
.CheckboxMultiselectInputWidget
, OO
.ui
.InputWidget
);
9370 /* Static Methods */
9375 OO
.ui
.CheckboxMultiselectInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
9376 var state
= OO
.ui
.CheckboxMultiselectInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
9377 state
.value
= $( node
).find( '.oo-ui-checkboxInputWidget .oo-ui-inputWidget-input:checked' )
9378 .toArray().map( function ( el
) { return el
.value
; } );
9385 OO
.ui
.CheckboxMultiselectInputWidget
.static.reusePreInfuseDOM = function ( node
, config
) {
9386 config
= OO
.ui
.CheckboxMultiselectInputWidget
.parent
.static.reusePreInfuseDOM( node
, config
);
9387 // Cannot reuse the `<input type=checkbox>` set
9388 delete config
.$input
;
9398 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.getInputElement = function () {
9400 return $( '<unused>' );
9406 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.getValue = function () {
9407 var value
= this.$element
.find( '.oo-ui-checkboxInputWidget .oo-ui-inputWidget-input:checked' )
9408 .toArray().map( function ( el
) { return el
.value
; } );
9409 if ( this.value
!== value
) {
9410 this.setValue( value
);
9418 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.setValue = function ( value
) {
9419 value
= this.cleanUpValue( value
);
9420 this.checkboxMultiselectWidget
.selectItemsByData( value
);
9421 OO
.ui
.CheckboxMultiselectInputWidget
.parent
.prototype.setValue
.call( this, value
);
9426 * Clean up incoming value.
9428 * @param {string[]} value Original value
9429 * @return {string[]} Cleaned up value
9431 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.cleanUpValue = function ( value
) {
9434 if ( !Array
.isArray( value
) ) {
9437 for ( i
= 0; i
< value
.length
; i
++ ) {
9439 OO
.ui
.CheckboxMultiselectInputWidget
.parent
.prototype.cleanUpValue
.call( this, value
[ i
] );
9440 // Remove options that we don't have here
9441 if ( !this.checkboxMultiselectWidget
.getItemFromData( singleValue
) ) {
9444 cleanValue
.push( singleValue
);
9452 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.setDisabled = function ( state
) {
9453 this.checkboxMultiselectWidget
.setDisabled( state
);
9454 OO
.ui
.CheckboxMultiselectInputWidget
.parent
.prototype.setDisabled
.call( this, state
);
9459 * Set the options available for this input.
9461 * @param {Object[]} options Array of menu options in the format `{ data: …, label: …, disabled: … }`
9464 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.setOptions = function ( options
) {
9467 // Rebuild the checkboxMultiselectWidget menu
9468 this.checkboxMultiselectWidget
9470 .addItems( options
.map( function ( opt
) {
9471 var optValue
, item
, optDisabled
;
9473 OO
.ui
.CheckboxMultiselectInputWidget
.parent
.prototype.cleanUpValue
.call( widget
, opt
.data
);
9474 optDisabled
= opt
.disabled
!== undefined ? opt
.disabled
: false;
9475 item
= new OO
.ui
.CheckboxMultioptionWidget( {
9477 label
: opt
.label
!== undefined ? opt
.label
: optValue
,
9478 disabled
: optDisabled
9480 // Set the 'name' and 'value' for form submission
9481 item
.checkbox
.$input
.attr( 'name', widget
.inputName
);
9482 item
.checkbox
.setValue( optValue
);
9486 // Re-set the value, checking the checkboxes as needed.
9487 // This will also get rid of any stale options that we just removed.
9488 this.setValue( this.getValue() );
9496 OO
.ui
.CheckboxMultiselectInputWidget
.prototype.focus = function () {
9497 this.checkboxMultiselectWidget
.focus();
9502 * TextInputWidgets, like HTML text inputs, can be configured with options that customize the
9503 * size of the field as well as its presentation. In addition, these widgets can be configured
9504 * with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators}, an optional
9505 * validation-pattern (used to determine if an input value is valid or not) and an input filter,
9506 * which modifies incoming values rather than validating them.
9507 * Please see the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
9509 * This widget can be used inside an HTML form, such as a OO.ui.FormLayout.
9512 * // Example of a text input widget
9513 * var textInput = new OO.ui.TextInputWidget( {
9514 * value: 'Text input'
9516 * $( 'body' ).append( textInput.$element );
9518 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
9521 * @extends OO.ui.InputWidget
9522 * @mixins OO.ui.mixin.IconElement
9523 * @mixins OO.ui.mixin.IndicatorElement
9524 * @mixins OO.ui.mixin.PendingElement
9525 * @mixins OO.ui.mixin.LabelElement
9528 * @param {Object} [config] Configuration options
9529 * @cfg {string} [type='text'] The value of the HTML `type` attribute: 'text', 'password'
9530 * 'email', 'url' or 'number'.
9531 * @cfg {string} [placeholder] Placeholder text
9532 * @cfg {boolean} [autofocus=false] Use an HTML `autofocus` attribute to
9533 * instruct the browser to focus this widget.
9534 * @cfg {boolean} [readOnly=false] Prevent changes to the value of the text input.
9535 * @cfg {number} [maxLength] Maximum number of characters allowed in the input.
9536 * @cfg {string} [labelPosition='after'] The position of the inline label relative to that of
9537 * the value or placeholder text: `'before'` or `'after'`
9538 * @cfg {boolean} [required=false] Mark the field as required. Implies `indicator: 'required'`.
9539 * @cfg {boolean} [autocomplete=true] Should the browser support autocomplete for this field
9540 * @cfg {RegExp|Function|string} [validate] Validation pattern: when string, a symbolic name of a
9541 * pattern defined by the class: 'non-empty' (the value cannot be an empty string) or 'integer'
9542 * (the value must contain only numbers); when RegExp, a regular expression that must match the
9543 * value for it to be considered valid; when Function, a function receiving the value as parameter
9544 * that must return true, or promise resolving to true, for it to be considered valid.
9546 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
9547 // Configuration initialization
9548 config
= $.extend( {
9550 labelPosition
: 'after'
9553 if ( config
.multiline
) {
9554 OO
.ui
.warnDeprecation( 'TextInputWidget: config.multiline is deprecated. Use the MultilineTextInputWidget instead. See T130434.' );
9555 return new OO
.ui
.MultilineTextInputWidget( config
);
9558 // Parent constructor
9559 OO
.ui
.TextInputWidget
.parent
.call( this, config
);
9561 // Mixin constructors
9562 OO
.ui
.mixin
.IconElement
.call( this, config
);
9563 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
9564 OO
.ui
.mixin
.PendingElement
.call( this, $.extend( {}, config
, { $pending
: this.$input
} ) );
9565 OO
.ui
.mixin
.LabelElement
.call( this, config
);
9568 this.type
= this.getSaneType( config
);
9569 this.readOnly
= false;
9570 this.required
= false;
9571 this.validate
= null;
9572 this.styleHeight
= null;
9573 this.scrollWidth
= null;
9575 this.setValidation( config
.validate
);
9576 this.setLabelPosition( config
.labelPosition
);
9580 keypress
: this.onKeyPress
.bind( this ),
9581 blur
: this.onBlur
.bind( this ),
9582 focus
: this.onFocus
.bind( this )
9584 this.$icon
.on( 'mousedown', this.onIconMouseDown
.bind( this ) );
9585 this.$indicator
.on( 'mousedown', this.onIndicatorMouseDown
.bind( this ) );
9586 this.on( 'labelChange', this.updatePosition
.bind( this ) );
9587 this.on( 'change', OO
.ui
.debounce( this.onDebouncedChange
.bind( this ), 250 ) );
9591 .addClass( 'oo-ui-textInputWidget oo-ui-textInputWidget-type-' + this.type
)
9592 .append( this.$icon
, this.$indicator
);
9593 this.setReadOnly( !!config
.readOnly
);
9594 this.setRequired( !!config
.required
);
9595 if ( config
.placeholder
!== undefined ) {
9596 this.$input
.attr( 'placeholder', config
.placeholder
);
9598 if ( config
.maxLength
!== undefined ) {
9599 this.$input
.attr( 'maxlength', config
.maxLength
);
9601 if ( config
.autofocus
) {
9602 this.$input
.attr( 'autofocus', 'autofocus' );
9604 if ( config
.autocomplete
=== false ) {
9605 this.$input
.attr( 'autocomplete', 'off' );
9606 // Turning off autocompletion also disables "form caching" when the user navigates to a
9607 // different page and then clicks "Back". Re-enable it when leaving. Borrowed from jQuery UI.
9609 beforeunload: function () {
9610 this.$input
.removeAttr( 'autocomplete' );
9612 pageshow: function () {
9613 // Browsers don't seem to actually fire this event on "Back", they instead just reload the
9614 // whole page... it shouldn't hurt, though.
9615 this.$input
.attr( 'autocomplete', 'off' );
9620 this.isWaitingToBeAttached
= true;
9621 this.installParentChangeDetector();
9627 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
9628 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.mixin
.IconElement
);
9629 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.mixin
.IndicatorElement
);
9630 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.mixin
.PendingElement
);
9631 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.mixin
.LabelElement
);
9633 /* Static Properties */
9635 OO
.ui
.TextInputWidget
.static.validationPatterns
= {
9640 /* Static Methods */
9645 OO
.ui
.TextInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
9646 var state
= OO
.ui
.TextInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
9653 * An `enter` event is emitted when the user presses 'enter' inside the text box.
9661 * Handle icon mouse down events.
9664 * @param {jQuery.Event} e Mouse down event
9666 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
9667 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
9674 * Handle indicator mouse down events.
9677 * @param {jQuery.Event} e Mouse down event
9679 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
9680 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
9687 * Handle key press events.
9690 * @param {jQuery.Event} e Key press event
9691 * @fires enter If enter key is pressed
9693 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
9694 if ( e
.which
=== OO
.ui
.Keys
.ENTER
) {
9695 this.emit( 'enter', e
);
9700 * Handle blur events.
9703 * @param {jQuery.Event} e Blur event
9705 OO
.ui
.TextInputWidget
.prototype.onBlur = function () {
9706 this.setValidityFlag();
9710 * Handle focus events.
9713 * @param {jQuery.Event} e Focus event
9715 OO
.ui
.TextInputWidget
.prototype.onFocus = function () {
9716 if ( this.isWaitingToBeAttached
) {
9717 // If we've received focus, then we must be attached to the document, and if
9718 // isWaitingToBeAttached is still true, that means the handler never fired. Fire it now.
9719 this.onElementAttach();
9721 this.setValidityFlag( true );
9725 * Handle element attach events.
9728 * @param {jQuery.Event} e Element attach event
9730 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
9731 this.isWaitingToBeAttached
= false;
9732 // Any previously calculated size is now probably invalid if we reattached elsewhere
9733 this.valCache
= null;
9734 this.positionLabel();
9738 * Handle debounced change events.
9740 * @param {string} value
9743 OO
.ui
.TextInputWidget
.prototype.onDebouncedChange = function () {
9744 this.setValidityFlag();
9748 * Check if the input is {@link #readOnly read-only}.
9752 OO
.ui
.TextInputWidget
.prototype.isReadOnly = function () {
9753 return this.readOnly
;
9757 * Set the {@link #readOnly read-only} state of the input.
9759 * @param {boolean} state Make input read-only
9762 OO
.ui
.TextInputWidget
.prototype.setReadOnly = function ( state
) {
9763 this.readOnly
= !!state
;
9764 this.$input
.prop( 'readOnly', this.readOnly
);
9769 * Check if the input is {@link #required required}.
9773 OO
.ui
.TextInputWidget
.prototype.isRequired = function () {
9774 return this.required
;
9778 * Set the {@link #required required} state of the input.
9780 * @param {boolean} state Make input required
9783 OO
.ui
.TextInputWidget
.prototype.setRequired = function ( state
) {
9784 this.required
= !!state
;
9785 if ( this.required
) {
9787 .prop( 'required', true )
9788 .attr( 'aria-required', 'true' );
9789 if ( this.getIndicator() === null ) {
9790 this.setIndicator( 'required' );
9794 .prop( 'required', false )
9795 .removeAttr( 'aria-required' );
9796 if ( this.getIndicator() === 'required' ) {
9797 this.setIndicator( null );
9804 * Support function for making #onElementAttach work across browsers.
9806 * This whole function could be replaced with one line of code using the DOMNodeInsertedIntoDocument
9807 * event, but it's not supported by Firefox and allegedly deprecated, so we only use it as fallback.
9809 * Due to MutationObserver performance woes, #onElementAttach is only somewhat reliably called the
9810 * first time that the element gets attached to the documented.
9812 OO
.ui
.TextInputWidget
.prototype.installParentChangeDetector = function () {
9813 var mutationObserver
, onRemove
, topmostNode
, fakeParentNode
,
9814 MutationObserver
= window
.MutationObserver
|| window
.WebKitMutationObserver
|| window
.MozMutationObserver
,
9817 if ( MutationObserver
) {
9818 // The new way. If only it wasn't so ugly.
9820 if ( this.isElementAttached() ) {
9821 // Widget is attached already, do nothing. This breaks the functionality of this function when
9822 // the widget is detached and reattached. Alas, doing this correctly with MutationObserver
9823 // would require observation of the whole document, which would hurt performance of other,
9824 // more important code.
9828 // Find topmost node in the tree
9829 topmostNode
= this.$element
[ 0 ];
9830 while ( topmostNode
.parentNode
) {
9831 topmostNode
= topmostNode
.parentNode
;
9834 // We have no way to detect the $element being attached somewhere without observing the entire
9835 // DOM with subtree modifications, which would hurt performance. So we cheat: we hook to the
9836 // parent node of $element, and instead detect when $element is removed from it (and thus
9837 // probably attached somewhere else). If there is no parent, we create a "fake" one. If it
9838 // doesn't get attached, we end up back here and create the parent.
9840 mutationObserver
= new MutationObserver( function ( mutations
) {
9841 var i
, j
, removedNodes
;
9842 for ( i
= 0; i
< mutations
.length
; i
++ ) {
9843 removedNodes
= mutations
[ i
].removedNodes
;
9844 for ( j
= 0; j
< removedNodes
.length
; j
++ ) {
9845 if ( removedNodes
[ j
] === topmostNode
) {
9846 setTimeout( onRemove
, 0 );
9853 onRemove = function () {
9854 // If the node was attached somewhere else, report it
9855 if ( widget
.isElementAttached() ) {
9856 widget
.onElementAttach();
9858 mutationObserver
.disconnect();
9859 widget
.installParentChangeDetector();
9862 // Create a fake parent and observe it
9863 fakeParentNode
= $( '<div>' ).append( topmostNode
)[ 0 ];
9864 mutationObserver
.observe( fakeParentNode
, { childList
: true } );
9866 // Using the DOMNodeInsertedIntoDocument event is much nicer and less magical, and works for
9867 // detachment and reattachment, but it's not supported by Firefox and allegedly deprecated.
9868 this.$element
.on( 'DOMNodeInsertedIntoDocument', this.onElementAttach
.bind( this ) );
9876 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9877 if ( this.getSaneType( config
) === 'number' ) {
9878 return $( '<input>' )
9879 .attr( 'step', 'any' )
9880 .attr( 'type', 'number' );
9882 return $( '<input>' ).attr( 'type', this.getSaneType( config
) );
9887 * Get sanitized value for 'type' for given config.
9889 * @param {Object} config Configuration options
9890 * @return {string|null}
9893 OO
.ui
.TextInputWidget
.prototype.getSaneType = function ( config
) {
9894 var allowedTypes
= [
9901 return allowedTypes
.indexOf( config
.type
) !== -1 ? config
.type
: 'text';
9905 * Focus the input and select a specified range within the text.
9907 * @param {number} from Select from offset
9908 * @param {number} [to] Select to offset, defaults to from
9911 OO
.ui
.TextInputWidget
.prototype.selectRange = function ( from, to
) {
9912 var isBackwards
, start
, end
,
9913 input
= this.$input
[ 0 ];
9917 isBackwards
= to
< from;
9918 start
= isBackwards
? to
: from;
9919 end
= isBackwards
? from : to
;
9924 input
.setSelectionRange( start
, end
, isBackwards
? 'backward' : 'forward' );
9926 // IE throws an exception if you call setSelectionRange on a unattached DOM node.
9927 // Rather than expensively check if the input is attached every time, just check
9928 // if it was the cause of an error being thrown. If not, rethrow the error.
9929 if ( this.getElementDocument().body
.contains( input
) ) {
9937 * Get an object describing the current selection range in a directional manner
9939 * @return {Object} Object containing 'from' and 'to' offsets
9941 OO
.ui
.TextInputWidget
.prototype.getRange = function () {
9942 var input
= this.$input
[ 0 ],
9943 start
= input
.selectionStart
,
9944 end
= input
.selectionEnd
,
9945 isBackwards
= input
.selectionDirection
=== 'backward';
9948 from: isBackwards
? end
: start
,
9949 to
: isBackwards
? start
: end
9954 * Get the length of the text input value.
9956 * This could differ from the length of #getValue if the
9957 * value gets filtered
9959 * @return {number} Input length
9961 OO
.ui
.TextInputWidget
.prototype.getInputLength = function () {
9962 return this.$input
[ 0 ].value
.length
;
9966 * Focus the input and select the entire text.
9970 OO
.ui
.TextInputWidget
.prototype.select = function () {
9971 return this.selectRange( 0, this.getInputLength() );
9975 * Focus the input and move the cursor to the start.
9979 OO
.ui
.TextInputWidget
.prototype.moveCursorToStart = function () {
9980 return this.selectRange( 0 );
9984 * Focus the input and move the cursor to the end.
9988 OO
.ui
.TextInputWidget
.prototype.moveCursorToEnd = function () {
9989 return this.selectRange( this.getInputLength() );
9993 * Insert new content into the input.
9995 * @param {string} content Content to be inserted
9998 OO
.ui
.TextInputWidget
.prototype.insertContent = function ( content
) {
10000 range
= this.getRange(),
10001 value
= this.getValue();
10003 start
= Math
.min( range
.from, range
.to
);
10004 end
= Math
.max( range
.from, range
.to
);
10006 this.setValue( value
.slice( 0, start
) + content
+ value
.slice( end
) );
10007 this.selectRange( start
+ content
.length
);
10012 * Insert new content either side of a selection.
10014 * @param {string} pre Content to be inserted before the selection
10015 * @param {string} post Content to be inserted after the selection
10018 OO
.ui
.TextInputWidget
.prototype.encapsulateContent = function ( pre
, post
) {
10020 range
= this.getRange(),
10021 offset
= pre
.length
;
10023 start
= Math
.min( range
.from, range
.to
);
10024 end
= Math
.max( range
.from, range
.to
);
10026 this.selectRange( start
).insertContent( pre
);
10027 this.selectRange( offset
+ end
).insertContent( post
);
10029 this.selectRange( offset
+ start
, offset
+ end
);
10034 * Set the validation pattern.
10036 * The validation pattern is either a regular expression, a function, or the symbolic name of a
10037 * pattern defined by the class: 'non-empty' (the value cannot be an empty string) or 'integer' (the
10038 * value must contain only numbers).
10040 * @param {RegExp|Function|string|null} validate Regular expression, function, or the symbolic name
10041 * of a pattern (either ‘integer’ or ‘non-empty’) defined by the class.
10043 OO
.ui
.TextInputWidget
.prototype.setValidation = function ( validate
) {
10044 if ( validate
instanceof RegExp
|| validate
instanceof Function
) {
10045 this.validate
= validate
;
10047 this.validate
= this.constructor.static.validationPatterns
[ validate
] || /.*/;
10052 * Sets the 'invalid' flag appropriately.
10054 * @param {boolean} [isValid] Optionally override validation result
10056 OO
.ui
.TextInputWidget
.prototype.setValidityFlag = function ( isValid
) {
10058 setFlag = function ( valid
) {
10060 widget
.$input
.attr( 'aria-invalid', 'true' );
10062 widget
.$input
.removeAttr( 'aria-invalid' );
10064 widget
.setFlags( { invalid
: !valid
} );
10067 if ( isValid
!== undefined ) {
10068 setFlag( isValid
);
10070 this.getValidity().then( function () {
10079 * Get the validity of current value.
10081 * This method returns a promise that resolves if the value is valid and rejects if
10082 * it isn't. Uses the {@link #validate validation pattern} to check for validity.
10084 * @return {jQuery.Promise} A promise that resolves if the value is valid, rejects if not.
10086 OO
.ui
.TextInputWidget
.prototype.getValidity = function () {
10089 function rejectOrResolve( valid
) {
10091 return $.Deferred().resolve().promise();
10093 return $.Deferred().reject().promise();
10097 // Check browser validity and reject if it is invalid
10099 this.$input
[ 0 ].checkValidity
!== undefined &&
10100 this.$input
[ 0 ].checkValidity() === false
10102 return rejectOrResolve( false );
10105 // Run our checks if the browser thinks the field is valid
10106 if ( this.validate
instanceof Function
) {
10107 result
= this.validate( this.getValue() );
10108 if ( result
&& $.isFunction( result
.promise
) ) {
10109 return result
.promise().then( function ( valid
) {
10110 return rejectOrResolve( valid
);
10113 return rejectOrResolve( result
);
10116 return rejectOrResolve( this.getValue().match( this.validate
) );
10121 * Set the position of the inline label relative to that of the value: `‘before’` or `‘after’`.
10123 * @param {string} labelPosition Label position, 'before' or 'after'
10126 OO
.ui
.TextInputWidget
.prototype.setLabelPosition = function ( labelPosition
) {
10127 this.labelPosition
= labelPosition
;
10128 if ( this.label
) {
10129 // If there is no label and we only change the position, #updatePosition is a no-op,
10130 // but it takes really a lot of work to do nothing.
10131 this.updatePosition();
10137 * Update the position of the inline label.
10139 * This method is called by #setLabelPosition, and can also be called on its own if
10140 * something causes the label to be mispositioned.
10144 OO
.ui
.TextInputWidget
.prototype.updatePosition = function () {
10145 var after
= this.labelPosition
=== 'after';
10148 .toggleClass( 'oo-ui-textInputWidget-labelPosition-after', !!this.label
&& after
)
10149 .toggleClass( 'oo-ui-textInputWidget-labelPosition-before', !!this.label
&& !after
);
10151 this.valCache
= null;
10152 this.scrollWidth
= null;
10153 this.positionLabel();
10159 * Position the label by setting the correct padding on the input.
10164 OO
.ui
.TextInputWidget
.prototype.positionLabel = function () {
10165 var after
, rtl
, property
, newCss
;
10167 if ( this.isWaitingToBeAttached
) {
10168 // #onElementAttach will be called soon, which calls this method
10173 'padding-right': '',
10177 if ( this.label
) {
10178 this.$element
.append( this.$label
);
10180 this.$label
.detach();
10181 // Clear old values if present
10182 this.$input
.css( newCss
);
10186 after
= this.labelPosition
=== 'after';
10187 rtl
= this.$element
.css( 'direction' ) === 'rtl';
10188 property
= after
=== rtl
? 'padding-left' : 'padding-right';
10190 newCss
[ property
] = this.$label
.outerWidth( true ) + ( after
? this.scrollWidth
: 0 );
10191 // We have to clear the padding on the other side, in case the element direction changed
10192 this.$input
.css( newCss
);
10200 OO
.ui
.TextInputWidget
.prototype.restorePreInfuseState = function ( state
) {
10201 OO
.ui
.TextInputWidget
.parent
.prototype.restorePreInfuseState
.call( this, state
);
10202 if ( state
.scrollTop
!== undefined ) {
10203 this.$input
.scrollTop( state
.scrollTop
);
10209 * @extends OO.ui.TextInputWidget
10212 * @param {Object} [config] Configuration options
10214 OO
.ui
.SearchInputWidget
= function OoUiSearchInputWidget( config
) {
10215 config
= $.extend( {
10219 // Set type to text so that TextInputWidget doesn't
10220 // get stuck in an infinite loop.
10221 config
.type
= 'text';
10223 // Parent constructor
10224 OO
.ui
.SearchInputWidget
.parent
.call( this, config
);
10227 this.connect( this, {
10232 this.$element
.addClass( 'oo-ui-textInputWidget-type-search' );
10233 this.updateSearchIndicator();
10234 this.connect( this, {
10235 disable
: 'onDisable'
10241 OO
.inheritClass( OO
.ui
.SearchInputWidget
, OO
.ui
.TextInputWidget
);
10249 OO
.ui
.SearchInputWidget
.prototype.getInputElement = function () {
10250 return $( '<input>' ).attr( 'type', 'search' );
10256 OO
.ui
.SearchInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
10257 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
10258 // Clear the text field
10259 this.setValue( '' );
10266 * Update the 'clear' indicator displayed on type: 'search' text
10267 * fields, hiding it when the field is already empty or when it's not
10270 OO
.ui
.SearchInputWidget
.prototype.updateSearchIndicator = function () {
10271 if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) {
10272 this.setIndicator( null );
10274 this.setIndicator( 'clear' );
10279 * Handle change events.
10283 OO
.ui
.SearchInputWidget
.prototype.onChange = function () {
10284 this.updateSearchIndicator();
10288 * Handle disable events.
10290 * @param {boolean} disabled Element is disabled
10293 OO
.ui
.SearchInputWidget
.prototype.onDisable = function () {
10294 this.updateSearchIndicator();
10300 OO
.ui
.SearchInputWidget
.prototype.setReadOnly = function ( state
) {
10301 OO
.ui
.SearchInputWidget
.parent
.prototype.setReadOnly
.call( this, state
);
10302 this.updateSearchIndicator();
10308 * @extends OO.ui.TextInputWidget
10311 * @param {Object} [config] Configuration options
10312 * @cfg {number} [rows] Number of visible lines in textarea. If used with `autosize`,
10313 * specifies minimum number of rows to display.
10314 * @cfg {string} [labelPosition='after'] The position of the inline label relative to that of
10315 * @cfg {boolean} [autosize=false] Automatically resize the text input to fit its content.
10316 * Use the #maxRows config to specify a maximum number of displayed rows.
10317 * @cfg {boolean} [maxRows] Maximum number of rows to display when #autosize is set to true.
10318 * Defaults to the maximum of `10` and `2 * rows`, or `10` if `rows` isn't provided.
10320 OO
.ui
.MultilineTextInputWidget
= function OoUiMultilineTextInputWidget( config
) {
10321 config
= $.extend( {
10324 config
.multiline
= false;
10325 // Parent constructor
10326 OO
.ui
.MultilineTextInputWidget
.parent
.call( this, config
);
10329 this.multiline
= true;
10330 this.autosize
= !!config
.autosize
;
10331 this.minRows
= config
.rows
!== undefined ? config
.rows
: '';
10332 this.maxRows
= config
.maxRows
|| Math
.max( 2 * ( this.minRows
|| 0 ), 10 );
10334 // Clone for resizing
10335 if ( this.autosize
) {
10336 this.$clone
= this.$input
10338 .insertAfter( this.$input
)
10339 .attr( 'aria-hidden', 'true' )
10340 .addClass( 'oo-ui-element-hidden' );
10344 this.connect( this, {
10349 if ( this.multiline
&& config
.rows
) {
10350 this.$input
.attr( 'rows', config
.rows
);
10352 if ( this.autosize
) {
10353 this.isWaitingToBeAttached
= true;
10354 this.installParentChangeDetector();
10360 OO
.inheritClass( OO
.ui
.MultilineTextInputWidget
, OO
.ui
.TextInputWidget
);
10362 /* Static Methods */
10367 OO
.ui
.MultilineTextInputWidget
.static.gatherPreInfuseState = function ( node
, config
) {
10368 var state
= OO
.ui
.MultilineTextInputWidget
.parent
.static.gatherPreInfuseState( node
, config
);
10369 state
.scrollTop
= config
.$input
.scrollTop();
10378 OO
.ui
.MultilineTextInputWidget
.prototype.onElementAttach = function () {
10379 OO
.ui
.MultilineTextInputWidget
.parent
.prototype.onElementAttach
.call( this );
10384 * Handle change events.
10388 OO
.ui
.MultilineTextInputWidget
.prototype.onChange = function () {
10395 OO
.ui
.MultilineTextInputWidget
.prototype.updatePosition = function () {
10396 OO
.ui
.MultilineTextInputWidget
.parent
.prototype.updatePosition
.call( this );
10401 * Override TextInputWidget so it doesn't emit the 'enter' event.
10404 * @param {jQuery.Event} e Key press event
10406 OO
.ui
.MultilineTextInputWidget
.prototype.onKeyPress = function () {
10411 * Automatically adjust the size of the text input.
10413 * This only affects multiline inputs that are {@link #autosize autosized}.
10418 OO
.ui
.MultilineTextInputWidget
.prototype.adjustSize = function () {
10419 var scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, measurementError
,
10420 idealHeight
, newHeight
, scrollWidth
, property
;
10422 if ( this.$input
.val() !== this.valCache
) {
10423 if ( this.autosize
) {
10425 .val( this.$input
.val() )
10426 .attr( 'rows', this.minRows
)
10427 // Set inline height property to 0 to measure scroll height
10428 .css( 'height', 0 );
10430 this.$clone
.removeClass( 'oo-ui-element-hidden' );
10432 this.valCache
= this.$input
.val();
10434 scrollHeight
= this.$clone
[ 0 ].scrollHeight
;
10436 // Remove inline height property to measure natural heights
10437 this.$clone
.css( 'height', '' );
10438 innerHeight
= this.$clone
.innerHeight();
10439 outerHeight
= this.$clone
.outerHeight();
10441 // Measure max rows height
10443 .attr( 'rows', this.maxRows
)
10444 .css( 'height', 'auto' )
10446 maxInnerHeight
= this.$clone
.innerHeight();
10448 // Difference between reported innerHeight and scrollHeight with no scrollbars present.
10449 // This is sometimes non-zero on Blink-based browsers, depending on zoom level.
10450 measurementError
= maxInnerHeight
- this.$clone
[ 0 ].scrollHeight
;
10451 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
+ measurementError
);
10453 this.$clone
.addClass( 'oo-ui-element-hidden' );
10455 // Only apply inline height when expansion beyond natural height is needed
10456 // Use the difference between the inner and outer height as a buffer
10457 newHeight
= idealHeight
> innerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : '';
10458 if ( newHeight
!== this.styleHeight
) {
10459 this.$input
.css( 'height', newHeight
);
10460 this.styleHeight
= newHeight
;
10461 this.emit( 'resize' );
10464 scrollWidth
= this.$input
[ 0 ].offsetWidth
- this.$input
[ 0 ].clientWidth
;
10465 if ( scrollWidth
!== this.scrollWidth
) {
10466 property
= this.$element
.css( 'direction' ) === 'rtl' ? 'left' : 'right';
10468 this.$label
.css( { right
: '', left
: '' } );
10469 this.$indicator
.css( { right
: '', left
: '' } );
10471 if ( scrollWidth
) {
10472 this.$indicator
.css( property
, scrollWidth
);
10473 if ( this.labelPosition
=== 'after' ) {
10474 this.$label
.css( property
, scrollWidth
);
10478 this.scrollWidth
= scrollWidth
;
10479 this.positionLabel();
10489 OO
.ui
.MultilineTextInputWidget
.prototype.getInputElement = function () {
10490 return $( '<textarea>' );
10494 * Check if the input supports multiple lines.
10496 * @return {boolean}
10498 OO
.ui
.MultilineTextInputWidget
.prototype.isMultiline = function () {
10499 return !!this.multiline
;
10503 * Check if the input automatically adjusts its size.
10505 * @return {boolean}
10507 OO
.ui
.MultilineTextInputWidget
.prototype.isAutosizing = function () {
10508 return !!this.autosize
;
10512 * ComboBoxInputWidgets combine a {@link OO.ui.TextInputWidget text input} (where a value
10513 * can be entered manually) and a {@link OO.ui.MenuSelectWidget menu of options} (from which
10514 * a value can be chosen instead). Users can choose options from the combo box in one of two ways:
10516 * - by typing a value in the text input field. If the value exactly matches the value of a menu
10517 * option, that option will appear to be selected.
10518 * - by choosing a value from the menu. The value of the chosen option will then appear in the text
10521 * After the user chooses an option, its `data` will be used as a new value for the widget.
10522 * A `label` also can be specified for each option: if given, it will be shown instead of the
10523 * `data` in the dropdown menu.
10525 * This widget can be used inside an HTML form, such as a OO.ui.FormLayout.
10527 * For more information about menus and options, please see the [OOjs UI documentation on MediaWiki][1].
10530 * // Example: A ComboBoxInputWidget.
10531 * var comboBox = new OO.ui.ComboBoxInputWidget( {
10532 * value: 'Option 1',
10534 * { data: 'Option 1' },
10535 * { data: 'Option 2' },
10536 * { data: 'Option 3' }
10539 * $( 'body' ).append( comboBox.$element );
10542 * // Example: A ComboBoxInputWidget with additional option labels.
10543 * var comboBox = new OO.ui.ComboBoxInputWidget( {
10544 * value: 'Option 1',
10547 * data: 'Option 1',
10548 * label: 'Option One'
10551 * data: 'Option 2',
10552 * label: 'Option Two'
10555 * data: 'Option 3',
10556 * label: 'Option Three'
10560 * $( 'body' ).append( comboBox.$element );
10562 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Selects_and_Options#Menu_selects_and_options
10565 * @extends OO.ui.TextInputWidget
10568 * @param {Object} [config] Configuration options
10569 * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
10570 * @cfg {Object} [menu] Configuration options to pass to the {@link OO.ui.MenuSelectWidget menu select widget}.
10571 * @cfg {jQuery} [$overlay] Render the menu into a separate layer. This configuration is useful in cases where
10572 * the expanded menu is larger than its containing `<div>`. The specified overlay layer is usually on top of the
10573 * containing `<div>` and has a larger area. By default, the menu uses relative positioning.
10574 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
10576 OO
.ui
.ComboBoxInputWidget
= function OoUiComboBoxInputWidget( config
) {
10577 // Configuration initialization
10578 config
= $.extend( {
10579 autocomplete
: false
10582 // ComboBoxInputWidget shouldn't support `multiline`
10583 config
.multiline
= false;
10585 // See InputWidget#reusePreInfuseDOM about `config.$input`
10586 if ( config
.$input
) {
10587 config
.$input
.removeAttr( 'list' );
10590 // Parent constructor
10591 OO
.ui
.ComboBoxInputWidget
.parent
.call( this, config
);
10594 this.$overlay
= config
.$overlay
|| this.$element
;
10595 this.dropdownButton
= new OO
.ui
.ButtonWidget( {
10596 classes
: [ 'oo-ui-comboBoxInputWidget-dropdownButton' ],
10598 disabled
: this.disabled
10600 this.menu
= new OO
.ui
.MenuSelectWidget( $.extend(
10604 $floatableContainer
: this.$element
,
10605 disabled
: this.isDisabled()
10611 this.connect( this, {
10612 change
: 'onInputChange',
10613 enter
: 'onInputEnter'
10615 this.dropdownButton
.connect( this, {
10616 click
: 'onDropdownButtonClick'
10618 this.menu
.connect( this, {
10619 choose
: 'onMenuChoose',
10620 add
: 'onMenuItemsChange',
10621 remove
: 'onMenuItemsChange'
10625 this.$input
.attr( {
10627 'aria-owns': this.menu
.getElementId(),
10628 'aria-autocomplete': 'list'
10630 // Do not override options set via config.menu.items
10631 if ( config
.options
!== undefined ) {
10632 this.setOptions( config
.options
);
10634 this.$field
= $( '<div>' )
10635 .addClass( 'oo-ui-comboBoxInputWidget-field' )
10636 .append( this.$input
, this.dropdownButton
.$element
);
10638 .addClass( 'oo-ui-comboBoxInputWidget' )
10639 .append( this.$field
);
10640 this.$overlay
.append( this.menu
.$element
);
10641 this.onMenuItemsChange();
10646 OO
.inheritClass( OO
.ui
.ComboBoxInputWidget
, OO
.ui
.TextInputWidget
);
10651 * Get the combobox's menu.
10653 * @return {OO.ui.MenuSelectWidget} Menu widget
10655 OO
.ui
.ComboBoxInputWidget
.prototype.getMenu = function () {
10660 * Get the combobox's text input widget.
10662 * @return {OO.ui.TextInputWidget} Text input widget
10664 OO
.ui
.ComboBoxInputWidget
.prototype.getInput = function () {
10669 * Handle input change events.
10672 * @param {string} value New value
10674 OO
.ui
.ComboBoxInputWidget
.prototype.onInputChange = function ( value
) {
10675 var match
= this.menu
.getItemFromData( value
);
10677 this.menu
.selectItem( match
);
10678 if ( this.menu
.getHighlightedItem() ) {
10679 this.menu
.highlightItem( match
);
10682 if ( !this.isDisabled() ) {
10683 this.menu
.toggle( true );
10688 * Handle input enter events.
10692 OO
.ui
.ComboBoxInputWidget
.prototype.onInputEnter = function () {
10693 if ( !this.isDisabled() ) {
10694 this.menu
.toggle( false );
10699 * Handle button click events.
10703 OO
.ui
.ComboBoxInputWidget
.prototype.onDropdownButtonClick = function () {
10704 this.menu
.toggle();
10709 * Handle menu choose events.
10712 * @param {OO.ui.OptionWidget} item Chosen item
10714 OO
.ui
.ComboBoxInputWidget
.prototype.onMenuChoose = function ( item
) {
10715 this.setValue( item
.getData() );
10719 * Handle menu item change events.
10723 OO
.ui
.ComboBoxInputWidget
.prototype.onMenuItemsChange = function () {
10724 var match
= this.menu
.getItemFromData( this.getValue() );
10725 this.menu
.selectItem( match
);
10726 if ( this.menu
.getHighlightedItem() ) {
10727 this.menu
.highlightItem( match
);
10729 this.$element
.toggleClass( 'oo-ui-comboBoxInputWidget-empty', this.menu
.isEmpty() );
10735 OO
.ui
.ComboBoxInputWidget
.prototype.setDisabled = function ( disabled
) {
10737 OO
.ui
.ComboBoxInputWidget
.parent
.prototype.setDisabled
.call( this, disabled
);
10739 if ( this.dropdownButton
) {
10740 this.dropdownButton
.setDisabled( this.isDisabled() );
10743 this.menu
.setDisabled( this.isDisabled() );
10750 * Set the options available for this input.
10752 * @param {Object[]} options Array of menu options in the format `{ data: …, label: … }`
10755 OO
.ui
.ComboBoxInputWidget
.prototype.setOptions = function ( options
) {
10758 .addItems( options
.map( function ( opt
) {
10759 return new OO
.ui
.MenuOptionWidget( {
10761 label
: opt
.label
!== undefined ? opt
.label
: opt
.data
10769 * FieldLayouts are used with OO.ui.FieldsetLayout. Each FieldLayout requires a field-widget,
10770 * which is a widget that is specified by reference before any optional configuration settings.
10772 * Field layouts can be configured with help text and/or labels. Labels are aligned in one of four ways:
10774 * - **left**: The label is placed before the field-widget and aligned with the left margin.
10775 * A left-alignment is used for forms with many fields.
10776 * - **right**: The label is placed before the field-widget and aligned to the right margin.
10777 * A right-alignment is used for long but familiar forms which users tab through,
10778 * verifying the current field with a quick glance at the label.
10779 * - **top**: The label is placed above the field-widget. A top-alignment is used for brief forms
10780 * that users fill out from top to bottom.
10781 * - **inline**: The label is placed after the field-widget and aligned to the left.
10782 * An inline-alignment is best used with checkboxes or radio buttons.
10784 * Help text is accessed via a help icon that appears in the upper right corner of the rendered field layout.
10785 * Please see the [OOjs UI documentation on MediaWiki] [1] for examples and more information.
10787 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Layouts/Fields_and_Fieldsets
10790 * @extends OO.ui.Layout
10791 * @mixins OO.ui.mixin.LabelElement
10792 * @mixins OO.ui.mixin.TitledElement
10795 * @param {OO.ui.Widget} fieldWidget Field widget
10796 * @param {Object} [config] Configuration options
10797 * @cfg {string} [align='left'] Alignment of the label: 'left', 'right', 'top' or 'inline'
10798 * @cfg {Array} [errors] Error messages about the widget, which will be displayed below the widget.
10799 * The array may contain strings or OO.ui.HtmlSnippet instances.
10800 * @cfg {Array} [notices] Notices about the widget, which will be displayed below the widget.
10801 * The array may contain strings or OO.ui.HtmlSnippet instances.
10802 * @cfg {string|OO.ui.HtmlSnippet} [help] Help text. When help text is specified, a "help" icon will appear
10803 * in the upper-right corner of the rendered field; clicking it will display the text in a popup.
10804 * For important messages, you are advised to use `notices`, as they are always shown.
10805 * @cfg {jQuery} [$overlay] Passed to OO.ui.PopupButtonWidget for help popup, if `help` is given.
10806 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
10808 * @throws {Error} An error is thrown if no widget is specified
10810 OO
.ui
.FieldLayout
= function OoUiFieldLayout( fieldWidget
, config
) {
10811 // Allow passing positional parameters inside the config object
10812 if ( OO
.isPlainObject( fieldWidget
) && config
=== undefined ) {
10813 config
= fieldWidget
;
10814 fieldWidget
= config
.fieldWidget
;
10817 // Make sure we have required constructor arguments
10818 if ( fieldWidget
=== undefined ) {
10819 throw new Error( 'Widget not found' );
10822 // Configuration initialization
10823 config
= $.extend( { align
: 'left' }, config
);
10825 // Parent constructor
10826 OO
.ui
.FieldLayout
.parent
.call( this, config
);
10828 // Mixin constructors
10829 OO
.ui
.mixin
.LabelElement
.call( this, $.extend( {}, config
, {
10830 $label
: $( '<label>' )
10832 OO
.ui
.mixin
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
10835 this.fieldWidget
= fieldWidget
;
10838 this.$field
= this.isFieldInline() ? $( '<span>' ) : $( '<div>' );
10839 this.$messages
= $( '<ul>' );
10840 this.$header
= $( '<span>' );
10841 this.$body
= $( '<div>' );
10843 if ( config
.help
) {
10844 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
10845 $overlay
: config
.$overlay
,
10849 classes
: [ 'oo-ui-fieldLayout-help' ],
10853 if ( config
.help
instanceof OO
.ui
.HtmlSnippet
) {
10854 this.popupButtonWidget
.getPopup().$body
.html( config
.help
.toString() );
10856 this.popupButtonWidget
.getPopup().$body
.text( config
.help
);
10858 this.$help
= this.popupButtonWidget
.$element
;
10860 this.$help
= $( [] );
10864 this.fieldWidget
.connect( this, { disable
: 'onFieldDisable' } );
10867 if ( config
.help
) {
10868 // Set the 'aria-describedby' attribute on the fieldWidget
10869 // Preference given to an input or a button
10871 this.fieldWidget
.$input
||
10872 this.fieldWidget
.$button
||
10873 this.fieldWidget
.$element
10875 'aria-describedby',
10876 this.popupButtonWidget
.getPopup().getBodyId()
10879 if ( this.fieldWidget
.getInputId() ) {
10880 this.$label
.attr( 'for', this.fieldWidget
.getInputId() );
10882 this.$label
.on( 'click', function () {
10883 this.fieldWidget
.simulateLabelClick();
10888 .addClass( 'oo-ui-fieldLayout' )
10889 .toggleClass( 'oo-ui-fieldLayout-disabled', this.fieldWidget
.isDisabled() )
10890 .append( this.$body
);
10891 this.$body
.addClass( 'oo-ui-fieldLayout-body' );
10892 this.$header
.addClass( 'oo-ui-fieldLayout-header' );
10893 this.$messages
.addClass( 'oo-ui-fieldLayout-messages' );
10895 .addClass( 'oo-ui-fieldLayout-field' )
10896 .append( this.fieldWidget
.$element
);
10898 this.setErrors( config
.errors
|| [] );
10899 this.setNotices( config
.notices
|| [] );
10900 this.setAlignment( config
.align
);
10901 // Call this again to take into account the widget's accessKey
10902 this.updateTitle();
10907 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
10908 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.mixin
.LabelElement
);
10909 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.mixin
.TitledElement
);
10914 * Handle field disable events.
10917 * @param {boolean} value Field is disabled
10919 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
10920 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
10924 * Get the widget contained by the field.
10926 * @return {OO.ui.Widget} Field widget
10928 OO
.ui
.FieldLayout
.prototype.getField = function () {
10929 return this.fieldWidget
;
10933 * Return `true` if the given field widget can be used with `'inline'` alignment (see
10934 * #setAlignment). Return `false` if it can't or if this can't be determined.
10936 * @return {boolean}
10938 OO
.ui
.FieldLayout
.prototype.isFieldInline = function () {
10939 // This is very simplistic, but should be good enough.
10940 return this.getField().$element
.prop( 'tagName' ).toLowerCase() === 'span';
10945 * @param {string} kind 'error' or 'notice'
10946 * @param {string|OO.ui.HtmlSnippet} text
10949 OO
.ui
.FieldLayout
.prototype.makeMessage = function ( kind
, text
) {
10950 var $listItem
, $icon
, message
;
10951 $listItem
= $( '<li>' );
10952 if ( kind
=== 'error' ) {
10953 $icon
= new OO
.ui
.IconWidget( { icon
: 'alert', flags
: [ 'warning' ] } ).$element
;
10954 $listItem
.attr( 'role', 'alert' );
10955 } else if ( kind
=== 'notice' ) {
10956 $icon
= new OO
.ui
.IconWidget( { icon
: 'info' } ).$element
;
10960 message
= new OO
.ui
.LabelWidget( { label
: text
} );
10962 .append( $icon
, message
.$element
)
10963 .addClass( 'oo-ui-fieldLayout-messages-' + kind
);
10968 * Set the field alignment mode.
10971 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
10974 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
10975 if ( value
!== this.align
) {
10976 // Default to 'left'
10977 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
10981 if ( value
=== 'inline' && !this.isFieldInline() ) {
10984 // Reorder elements
10985 if ( value
=== 'top' ) {
10986 this.$header
.append( this.$label
, this.$help
);
10987 this.$body
.append( this.$header
, this.$field
);
10988 } else if ( value
=== 'inline' ) {
10989 this.$header
.append( this.$label
, this.$help
);
10990 this.$body
.append( this.$field
, this.$header
);
10992 this.$header
.append( this.$label
);
10993 this.$body
.append( this.$header
, this.$help
, this.$field
);
10995 // Set classes. The following classes can be used here:
10996 // * oo-ui-fieldLayout-align-left
10997 // * oo-ui-fieldLayout-align-right
10998 // * oo-ui-fieldLayout-align-top
10999 // * oo-ui-fieldLayout-align-inline
11000 if ( this.align
) {
11001 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
11003 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + value
);
11004 this.align
= value
;
11011 * Set the list of error messages.
11013 * @param {Array} errors Error messages about the widget, which will be displayed below the widget.
11014 * The array may contain strings or OO.ui.HtmlSnippet instances.
11017 OO
.ui
.FieldLayout
.prototype.setErrors = function ( errors
) {
11018 this.errors
= errors
.slice();
11019 this.updateMessages();
11024 * Set the list of notice messages.
11026 * @param {Array} notices Notices about the widget, which will be displayed below the widget.
11027 * The array may contain strings or OO.ui.HtmlSnippet instances.
11030 OO
.ui
.FieldLayout
.prototype.setNotices = function ( notices
) {
11031 this.notices
= notices
.slice();
11032 this.updateMessages();
11037 * Update the rendering of error and notice messages.
11041 OO
.ui
.FieldLayout
.prototype.updateMessages = function () {
11043 this.$messages
.empty();
11045 if ( this.errors
.length
|| this.notices
.length
) {
11046 this.$body
.after( this.$messages
);
11048 this.$messages
.remove();
11052 for ( i
= 0; i
< this.notices
.length
; i
++ ) {
11053 this.$messages
.append( this.makeMessage( 'notice', this.notices
[ i
] ) );
11055 for ( i
= 0; i
< this.errors
.length
; i
++ ) {
11056 this.$messages
.append( this.makeMessage( 'error', this.errors
[ i
] ) );
11061 * Include information about the widget's accessKey in our title. TitledElement calls this method.
11062 * (This is a bit of a hack.)
11065 * @param {string} title Tooltip label for 'title' attribute
11068 OO
.ui
.FieldLayout
.prototype.formatTitleWithAccessKey = function ( title
) {
11069 if ( this.fieldWidget
&& this.fieldWidget
.formatTitleWithAccessKey
) {
11070 return this.fieldWidget
.formatTitleWithAccessKey( title
);
11076 * ActionFieldLayouts are used with OO.ui.FieldsetLayout. The layout consists of a field-widget, a button,
11077 * and an optional label and/or help text. The field-widget (e.g., a {@link OO.ui.TextInputWidget TextInputWidget}),
11078 * is required and is specified before any optional configuration settings.
11080 * Labels can be aligned in one of four ways:
11082 * - **left**: The label is placed before the field-widget and aligned with the left margin.
11083 * A left-alignment is used for forms with many fields.
11084 * - **right**: The label is placed before the field-widget and aligned to the right margin.
11085 * A right-alignment is used for long but familiar forms which users tab through,
11086 * verifying the current field with a quick glance at the label.
11087 * - **top**: The label is placed above the field-widget. A top-alignment is used for brief forms
11088 * that users fill out from top to bottom.
11089 * - **inline**: The label is placed after the field-widget and aligned to the left.
11090 * An inline-alignment is best used with checkboxes or radio buttons.
11092 * Help text is accessed via a help icon that appears in the upper right corner of the rendered field layout when help
11093 * text is specified.
11096 * // Example of an ActionFieldLayout
11097 * var actionFieldLayout = new OO.ui.ActionFieldLayout(
11098 * new OO.ui.TextInputWidget( {
11099 * placeholder: 'Field widget'
11101 * new OO.ui.ButtonWidget( {
11105 * label: 'An ActionFieldLayout. This label is aligned top',
11107 * help: 'This is help text'
11111 * $( 'body' ).append( actionFieldLayout.$element );
11114 * @extends OO.ui.FieldLayout
11117 * @param {OO.ui.Widget} fieldWidget Field widget
11118 * @param {OO.ui.ButtonWidget} buttonWidget Button widget
11119 * @param {Object} config
11121 OO
.ui
.ActionFieldLayout
= function OoUiActionFieldLayout( fieldWidget
, buttonWidget
, config
) {
11122 // Allow passing positional parameters inside the config object
11123 if ( OO
.isPlainObject( fieldWidget
) && config
=== undefined ) {
11124 config
= fieldWidget
;
11125 fieldWidget
= config
.fieldWidget
;
11126 buttonWidget
= config
.buttonWidget
;
11129 // Parent constructor
11130 OO
.ui
.ActionFieldLayout
.parent
.call( this, fieldWidget
, config
);
11133 this.buttonWidget
= buttonWidget
;
11134 this.$button
= $( '<span>' );
11135 this.$input
= this.isFieldInline() ? $( '<span>' ) : $( '<div>' );
11139 .addClass( 'oo-ui-actionFieldLayout' );
11141 .addClass( 'oo-ui-actionFieldLayout-button' )
11142 .append( this.buttonWidget
.$element
);
11144 .addClass( 'oo-ui-actionFieldLayout-input' )
11145 .append( this.fieldWidget
.$element
);
11147 .append( this.$input
, this.$button
);
11152 OO
.inheritClass( OO
.ui
.ActionFieldLayout
, OO
.ui
.FieldLayout
);
11155 * FieldsetLayouts are composed of one or more {@link OO.ui.FieldLayout FieldLayouts},
11156 * which each contain an individual widget and, optionally, a label. Each Fieldset can be
11157 * configured with a label as well. For more information and examples,
11158 * please see the [OOjs UI documentation on MediaWiki][1].
11161 * // Example of a fieldset layout
11162 * var input1 = new OO.ui.TextInputWidget( {
11163 * placeholder: 'A text input field'
11166 * var input2 = new OO.ui.TextInputWidget( {
11167 * placeholder: 'A text input field'
11170 * var fieldset = new OO.ui.FieldsetLayout( {
11171 * label: 'Example of a fieldset layout'
11174 * fieldset.addItems( [
11175 * new OO.ui.FieldLayout( input1, {
11176 * label: 'Field One'
11178 * new OO.ui.FieldLayout( input2, {
11179 * label: 'Field Two'
11182 * $( 'body' ).append( fieldset.$element );
11184 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Layouts/Fields_and_Fieldsets
11187 * @extends OO.ui.Layout
11188 * @mixins OO.ui.mixin.IconElement
11189 * @mixins OO.ui.mixin.LabelElement
11190 * @mixins OO.ui.mixin.GroupElement
11193 * @param {Object} [config] Configuration options
11194 * @cfg {OO.ui.FieldLayout[]} [items] An array of fields to add to the fieldset. See OO.ui.FieldLayout for more information about fields.
11195 * @cfg {string|OO.ui.HtmlSnippet} [help] Help text. When help text is specified, a "help" icon will appear
11196 * in the upper-right corner of the rendered field; clicking it will display the text in a popup.
11197 * For important messages, you are advised to use `notices`, as they are always shown.
11198 * @cfg {jQuery} [$overlay] Passed to OO.ui.PopupButtonWidget for help popup, if `help` is given.
11199 * See <https://www.mediawiki.org/wiki/OOjs_UI/Concepts#Overlays>.
11201 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
11202 // Configuration initialization
11203 config
= config
|| {};
11205 // Parent constructor
11206 OO
.ui
.FieldsetLayout
.parent
.call( this, config
);
11208 // Mixin constructors
11209 OO
.ui
.mixin
.IconElement
.call( this, config
);
11210 OO
.ui
.mixin
.LabelElement
.call( this, config
);
11211 OO
.ui
.mixin
.GroupElement
.call( this, config
);
11214 this.$header
= $( '<legend>' );
11215 if ( config
.help
) {
11216 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
11217 $overlay
: config
.$overlay
,
11221 classes
: [ 'oo-ui-fieldsetLayout-help' ],
11225 if ( config
.help
instanceof OO
.ui
.HtmlSnippet
) {
11226 this.popupButtonWidget
.getPopup().$body
.html( config
.help
.toString() );
11228 this.popupButtonWidget
.getPopup().$body
.text( config
.help
);
11230 this.$help
= this.popupButtonWidget
.$element
;
11232 this.$help
= $( [] );
11237 .addClass( 'oo-ui-fieldsetLayout-header' )
11238 .append( this.$icon
, this.$label
, this.$help
);
11239 this.$group
.addClass( 'oo-ui-fieldsetLayout-group' );
11241 .addClass( 'oo-ui-fieldsetLayout' )
11242 .prepend( this.$header
, this.$group
);
11243 if ( Array
.isArray( config
.items
) ) {
11244 this.addItems( config
.items
);
11250 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
11251 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.mixin
.IconElement
);
11252 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.mixin
.LabelElement
);
11253 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.mixin
.GroupElement
);
11255 /* Static Properties */
11261 OO
.ui
.FieldsetLayout
.static.tagName
= 'fieldset';
11264 * FormLayouts are used to wrap {@link OO.ui.FieldsetLayout FieldsetLayouts} when you intend to use browser-based
11265 * form submission for the fields instead of handling them in JavaScript. Form layouts can be configured with an
11266 * HTML form action, an encoding type, and a method using the #action, #enctype, and #method configs, respectively.
11267 * See the [OOjs UI documentation on MediaWiki] [1] for more information and examples.
11269 * Only widgets from the {@link OO.ui.InputWidget InputWidget} family support form submission. It
11270 * includes standard form elements like {@link OO.ui.CheckboxInputWidget checkboxes}, {@link
11271 * OO.ui.RadioInputWidget radio buttons} and {@link OO.ui.TextInputWidget text fields}, as well as
11272 * some fancier controls. Some controls have both regular and InputWidget variants, for example
11273 * OO.ui.DropdownWidget and OO.ui.DropdownInputWidget – only the latter support form submission and
11274 * often have simplified APIs to match the capabilities of HTML forms.
11275 * See the [OOjs UI Inputs documentation on MediaWiki] [2] for more information about InputWidgets.
11277 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Layouts/Forms
11278 * [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
11281 * // Example of a form layout that wraps a fieldset layout
11282 * var input1 = new OO.ui.TextInputWidget( {
11283 * placeholder: 'Username'
11285 * var input2 = new OO.ui.TextInputWidget( {
11286 * placeholder: 'Password',
11289 * var submit = new OO.ui.ButtonInputWidget( {
11293 * var fieldset = new OO.ui.FieldsetLayout( {
11294 * label: 'A form layout'
11296 * fieldset.addItems( [
11297 * new OO.ui.FieldLayout( input1, {
11298 * label: 'Username',
11301 * new OO.ui.FieldLayout( input2, {
11302 * label: 'Password',
11305 * new OO.ui.FieldLayout( submit )
11307 * var form = new OO.ui.FormLayout( {
11308 * items: [ fieldset ],
11309 * action: '/api/formhandler',
11312 * $( 'body' ).append( form.$element );
11315 * @extends OO.ui.Layout
11316 * @mixins OO.ui.mixin.GroupElement
11319 * @param {Object} [config] Configuration options
11320 * @cfg {string} [method] HTML form `method` attribute
11321 * @cfg {string} [action] HTML form `action` attribute
11322 * @cfg {string} [enctype] HTML form `enctype` attribute
11323 * @cfg {OO.ui.FieldsetLayout[]} [items] Fieldset layouts to add to the form layout.
11325 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
11328 // Configuration initialization
11329 config
= config
|| {};
11331 // Parent constructor
11332 OO
.ui
.FormLayout
.parent
.call( this, config
);
11334 // Mixin constructors
11335 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
11338 this.$element
.on( 'submit', this.onFormSubmit
.bind( this ) );
11340 // Make sure the action is safe
11341 action
= config
.action
;
11342 if ( action
!== undefined && !OO
.ui
.isSafeUrl( action
) ) {
11343 action
= './' + action
;
11348 .addClass( 'oo-ui-formLayout' )
11350 method
: config
.method
,
11352 enctype
: config
.enctype
11354 if ( Array
.isArray( config
.items
) ) {
11355 this.addItems( config
.items
);
11361 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
11362 OO
.mixinClass( OO
.ui
.FormLayout
, OO
.ui
.mixin
.GroupElement
);
11367 * A 'submit' event is emitted when the form is submitted.
11372 /* Static Properties */
11378 OO
.ui
.FormLayout
.static.tagName
= 'form';
11383 * Handle form submit events.
11386 * @param {jQuery.Event} e Submit event
11389 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
11390 if ( this.emit( 'submit' ) ) {
11396 * PanelLayouts expand to cover the entire area of their parent. They can be configured with scrolling, padding,
11397 * and a frame, and are often used together with {@link OO.ui.StackLayout StackLayouts}.
11400 * // Example of a panel layout
11401 * var panel = new OO.ui.PanelLayout( {
11405 * $content: $( '<p>A panel layout with padding and a frame.</p>' )
11407 * $( 'body' ).append( panel.$element );
11410 * @extends OO.ui.Layout
11413 * @param {Object} [config] Configuration options
11414 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
11415 * @cfg {boolean} [padded=false] Add padding between the content and the edges of the panel.
11416 * @cfg {boolean} [expanded=true] Expand the panel to fill the entire parent element.
11417 * @cfg {boolean} [framed=false] Render the panel with a frame to visually separate it from outside content.
11419 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
11420 // Configuration initialization
11421 config
= $.extend( {
11428 // Parent constructor
11429 OO
.ui
.PanelLayout
.parent
.call( this, config
);
11432 this.$element
.addClass( 'oo-ui-panelLayout' );
11433 if ( config
.scrollable
) {
11434 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
11436 if ( config
.padded
) {
11437 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
11439 if ( config
.expanded
) {
11440 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
11442 if ( config
.framed
) {
11443 this.$element
.addClass( 'oo-ui-panelLayout-framed' );
11449 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
11454 * Focus the panel layout
11456 * The default implementation just focuses the first focusable element in the panel
11458 OO
.ui
.PanelLayout
.prototype.focus = function () {
11459 OO
.ui
.findFocusable( this.$element
).focus();
11463 * HorizontalLayout arranges its contents in a single line (using `display: inline-block` for its
11464 * items), with small margins between them. Convenient when you need to put a number of block-level
11465 * widgets on a single line next to each other.
11467 * Note that inline elements, such as OO.ui.ButtonWidgets, do not need this wrapper.
11470 * // HorizontalLayout with a text input and a label
11471 * var layout = new OO.ui.HorizontalLayout( {
11473 * new OO.ui.LabelWidget( { label: 'Label' } ),
11474 * new OO.ui.TextInputWidget( { value: 'Text' } )
11477 * $( 'body' ).append( layout.$element );
11480 * @extends OO.ui.Layout
11481 * @mixins OO.ui.mixin.GroupElement
11484 * @param {Object} [config] Configuration options
11485 * @cfg {OO.ui.Widget[]|OO.ui.Layout[]} [items] Widgets or other layouts to add to the layout.
11487 OO
.ui
.HorizontalLayout
= function OoUiHorizontalLayout( config
) {
11488 // Configuration initialization
11489 config
= config
|| {};
11491 // Parent constructor
11492 OO
.ui
.HorizontalLayout
.parent
.call( this, config
);
11494 // Mixin constructors
11495 OO
.ui
.mixin
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
11498 this.$element
.addClass( 'oo-ui-horizontalLayout' );
11499 if ( Array
.isArray( config
.items
) ) {
11500 this.addItems( config
.items
);
11506 OO
.inheritClass( OO
.ui
.HorizontalLayout
, OO
.ui
.Layout
);
11507 OO
.mixinClass( OO
.ui
.HorizontalLayout
, OO
.ui
.mixin
.GroupElement
);
11511 //# sourceMappingURL=oojs-ui-core.js.map