OOUI/Creating interfaces programmatically/id

Pustaka OOUI menyediakan blok pembangun untuk membangun antarmuka pengguna berorientasi objek. Blok ini mencakup banyak widget dan tata letak yang siap digunakan, serta elemen dasar yang dapat disusun untuk membangun sesuatu sesuai keinginan Anda. Sistem yang kompleks, seperti penjendelaan dan bilah perkakas, juga disediakan, dan dapat diperluas dan disusun agar sesuai dengan kebutuhan aplikasi Anda.

User interfaces are created programmatically in OOUI, which abstracts away HTML markup entirely. This technique allows the markup to change, so long as the APIs that generate the markup remain stable. Markup may change when extending a class to support a new feature, or when fixing a bug to increase compatibility.

Everything in OOUI that the user can see is built using elements, which are composed together to form discrete parts of a user interface, and also to bring those parts together. By maximising element reuse, visual, behavioural, and API consistency is easier to maintain.



Memulai
If you are not yet familiar with how interfaces are created with OOUI, this section contains a quick tutorial on how to build a widget using the library.

Creating a widget involves two basic steps:


 * 1) The widget is created and configured
 * 2) The widget is added to the DOM

Note that OOUI objects must be added to the DOM before they are visible to the user. Developers writing code should account for the fact that a widget may or may not be attached to the DOM, and will never be attached to the DOM during the execution of the constructor. Even after an element has been attached, it may later become detached. You can check if the element is currently attached using the  method.

Remember to include the required libraries jQuery and Special:MyLanguage/OOjs as well as the relevant style information (one of the OOUI themes, currently WikimediaUI as default or Apex as alternative) and the OOUI library itself.



Membuat dan mengonfigurasi widget
Each widget is created and configured with either a configuration object that is passed to the widget constructor or by using methods to set the configurations (see example below). Many of the library’s methods are chainable, as illustrated in the example.

Once the widget has been created and configured, it must be attached to the DOM in order to be visible to the user. Use the jQuery  method to insert the new widget into the DOM using its   property.

The following example demonstrates how to create and append a simple  that, when clicked, will open “https://www.mediawiki.org” in a new window. is configured using a config object. is configured using the widget’s methods:



Note that some get methods (e.g. or   ) should not be chained. These methods may return ‘null’, and trying to call another method on ‘null’ will cause an error.



Menghubungkan event handler ke widget
Widgets emit events when they are interacted with or changed in an interesting way. These events can be listened to by connecting event handlers to the widget. The below example uses an event handler to display an alert when the button is clicked. When the button is clicked, its label will be reset to read ‘Do not click me!’ via the button’s setLabel method.

Event handling is provided by the EventEmitter class, which is “mixed in” to all widgets. For more information about EventEmitter, please see OOjs primer.



Memahami mixins
The concept of mixins is fundamental to the library, and many of the library’s elements can be customised via mixin functionality. For example, one can easily add an icon to a button widget by taking advantage of the fact that the  class is mixed in to the   class.



Mixin classes can be used to create and add new elements to a widget (such as the icon in the above example) or to add new behaviours (such as new methods for handling styling flags or titles).

There are three kinds of element mixins, each identified with a naming convention:

Behaviour mixins are identified with an ”able" suffix (e.g.,, etc.) Note that though a mixin class can create and initialise an object, it will not append it to the DOM. This must be done manually if you wish to manipulate the object directly.
 * Content &mdash; Content mixins generate elements by default (e.g., groups, icons, indicators and labels). The names of content mixins have no suffix (e.g.,,  , etc.)
 * Attribute &mdash; Attribute mixins operate on  by default (e.g., flags and titles). The names of attribute mixins have an "ed" suffix (e.g., ,  , etc.)
 * Behaviour &mdash; Behaviour mixins operate on  by default (e.g., clippable).

It is not uncommon to see widgets composed of multiple mixins: for example, the  class utilises   and   mixins (and a few others). One can easily see which classes have been mixed and are available in the code-level documentation for each class. Mixin classes will always be displayed in the upper right of the documentation screen for each class. In addition, each configuration option notes the name of the class that defines it.