Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250165

Change subject: FieldLayout: Allow setting errors and notices dynamically
......................................................................

FieldLayout: Allow setting errors and notices dynamically

This adds two new methods to FieldLayout: #setErrors and #setNotices,
corresponding to configuration options 'errors' and 'notices'.

This commit only implements them for JS and not PHP, because they're
much less useful there and I am lazy.

Change-Id: I1b06899fb8d704ed60ca3e81c372ed9fc58b5733
---
M src/layouts/FieldLayout.js
1 file changed, 55 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/65/250165/1

diff --git a/src/layouts/FieldLayout.js b/src/layouts/FieldLayout.js
index 1e88149..01a2f3e 100644
--- a/src/layouts/FieldLayout.js
+++ b/src/layouts/FieldLayout.js
@@ -38,7 +38,7 @@
  * @throws {Error} An error is thrown if no widget is specified
  */
 OO.ui.FieldLayout = function OoUiFieldLayout( fieldWidget, config ) {
-       var hasInputWidget, div, i;
+       var hasInputWidget, div;
 
        // Allow passing positional parameters inside the config object
        if ( OO.isPlainObject( fieldWidget ) && config === undefined ) {
@@ -65,8 +65,8 @@
 
        // Properties
        this.fieldWidget = fieldWidget;
-       this.errors = config.errors || [];
-       this.notices = config.notices || [];
+       this.errors = [];
+       this.notices = [];
        this.$field = $( '<div>' );
        this.$messages = $( '<ul>' );
        this.$body = $( '<' + ( hasInputWidget ? 'label' : 'div' ) + '>' );
@@ -102,9 +102,6 @@
        this.$element
                .addClass( 'oo-ui-fieldLayout' )
                .append( this.$help, this.$body );
-       if ( this.errors.length || this.notices.length ) {
-               this.$element.append( this.$messages );
-       }
        this.$body.addClass( 'oo-ui-fieldLayout-body' );
        this.$messages.addClass( 'oo-ui-fieldLayout-messages' );
        this.$field
@@ -112,13 +109,8 @@
                .toggleClass( 'oo-ui-fieldLayout-disable', 
this.fieldWidget.isDisabled() )
                .append( this.fieldWidget.$element );
 
-       for ( i = 0; i < this.notices.length; i++ ) {
-               this.$messages.append( this.makeMessage( 'notice', 
this.notices[ i ] ) );
-       }
-       for ( i = 0; i < this.errors.length; i++ ) {
-               this.$messages.append( this.makeMessage( 'error', this.errors[ 
i ] ) );
-       }
-
+       this.setErrors( config.errors || [] );
+       this.setNotices( config.notices || [] );
        this.setAlignment( config.align );
 };
 
@@ -215,3 +207,53 @@
 
        return this;
 };
+
+/**
+ * Set the list of error messages.
+ *
+ * @param {Array} errors Error messages about the widget, which will be 
displayed below the widget.
+ *  The array may contain strings or OO.ui.HtmlSnippet instances.
+ * @chainable
+ */
+OO.ui.FieldLayout.prototype.setErrors = function ( errors ) {
+       this.errors = errors.slice();
+       this.updateMessages();
+       return this;
+};
+
+/**
+ * Set the list of notice messages.
+ *
+ * @param {Array} notices Notices about the widget, which will be displayed 
below the widget.
+ *  The array may contain strings or OO.ui.HtmlSnippet instances.
+ * @chainable
+ */
+OO.ui.FieldLayout.prototype.setNotices = function ( notices ) {
+       this.notices = notices.slice();
+       this.updateMessages();
+       return this;
+};
+
+/**
+ * Update the rendering of error and notice messages.
+ *
+ * @private
+ */
+OO.ui.FieldLayout.prototype.updateMessages = function () {
+       var i;
+       this.$messages.empty();
+
+       if ( this.errors.length || this.notices.length ) {
+               this.$body.after( this.$messages );
+       } else {
+               this.$messages.remove();
+               return;
+       }
+
+       for ( i = 0; i < this.notices.length; i++ ) {
+               this.$messages.append( this.makeMessage( 'notice', 
this.notices[ i ] ) );
+       }
+       for ( i = 0; i < this.errors.length; i++ ) {
+               this.$messages.append( this.makeMessage( 'error', this.errors[ 
i ] ) );
+       }
+};

-- 
To view, visit https://gerrit.wikimedia.org/r/250165
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1b06899fb8d704ed60ca3e81c372ed9fc58b5733
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to