Sunday, July 28, 2013

Troubleshooting JSON UI Generation: nested properties

I was asked why the following piece of Metawidget code returns an unusual result:

<!DOCTYPE html>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <script type="text/javascript">
      var person = {
         name: "Homer Simpson",
         age: 40,
         retired: false
      };
      </script>
   </head>
   <body>
      <div id="metawidget"></div>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
            inspector: new metawidget.inspector.CompositeInspector( [
               new metawidget.inspector.PropertyTypeInspector(),
               function( toInspect, type, names ) {
                  return {
                     properties: {
                        last_name: {
                           type:"object",
                           required:false,
                           properties: {
                              prefix: {
                                 type:"string",
                                 required:false
                              },
                              suffix: {
                                 type:"number",
                                 required:false
                              }
                           }
                        }
                     }
                  };
               }

            ] )
         } );
         mw.toInspect = person;
         mw.buildWidgets();
      </script>
   </body>
</html>

The developer was trying to add a custom Inspector to declare a new property last_name. This should be rendered along with his normal JSON object (which contains name, age and retired). But the result looks strange:

Let's explain what Metawidget is doing. Metawidget uses its Inspectors to retrieve metadata about which properties to render. Then it iterates over those properties and chooses appropriate widgets for them. For simple types, like string and number, Metawidget chooses native widgets like input type="text" and input type="number". But for complex types, such as object, it creates a nested Metawidget and recurses into it.

For this to work, the Inspectors have to play along. They cannot return the same metadata for the nested Metawidget as for the top-level Metawidget. If they do, we'll just recurse infinitely. This is what we're seeing. Actually you can see Metawidget helpfully caps the recursions so that the browser doesn't crash completely!

To fix this, our custom Inspector has to understand what it's being asked for and return appropriate metadata. It can use the names array to do this. A complete example:

<!DOCTYPE html>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <script type="text/javascript">
         var person = {
            name: "Homer Simpson",
            age: 40,
            retired: false
         };
      </script>
   </head>
   <body>
      <div id="metawidget"></div>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
            inspector: new metawidget.inspector.CompositeInspector( [
               new metawidget.inspector.PropertyTypeInspector(),
               function( toInspect, type, names ) {
                  if ( names === undefined ) {
                     return {
                        properties: {
                           last_name: {
                              type:"object",
                              required:false,
                           }
                        }
                     };
                  }


                  return {
                     type:"object",
                     required:false,
                     properties: {
                        prefix: {
                           type:"string",
                           required:false
                        },
                        suffix: {
                           type:"number",
                           required:false
                        }
                     }
                  }
               }
            ] )
         } );
         mw.toInspect = person;
         mw.buildWidgets();
      </script>
   </body>
</html>

This will correctly render:

This is the 'native' way of doing things. Another way of thinking of it is: the 'native' result from an Inspector cannot contain nested properties. Of course, for more complex use cases your Inspector will have to consider which names it's being asked for and act accordingly.

However Metawidget can make things a little easier. It comes with a JsonSchemaInspector which understands how to navigate JSON Schemas for nested properties. So you can simply do:

<!DOCTYPE html>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <script type="text/javascript">
         var person = {
            name: "Homer Simpson",
            age: 40,
            retired: false
         };
      </script>
   </head>
   <body>
      <div id="metawidget"></div>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
            inspector: new metawidget.inspector.CompositeInspector( [
               new metawidget.inspector.PropertyTypeInspector(),
               new metawidget.inspector.JsonSchemaInspector( {
                  properties: {
                     last_name: {
                        type:"object",
                        required:false,
                        properties: {
                           prefix: {
                              type:"string",
                              required:false
                           },
                           suffix: {
                              type:"number",
                              required:false
                           }
                        }
                     }
                  }
               } )

            ] )
         } );
         mw.toInspect = person;
         mw.buildWidgets();
      </script>
   </body>
</html>

Hope that helps!

Saturday, July 27, 2013

JSON UI Generator: Array Support

I've been asked to blog about array support in Metawidget. Displaying and manipulating arrays is fraught with personal choices about UI design. For example: is item deletion done by a right-click menu? Or a delete icon on the end of each row? Or at the start of each row? Is addition done using a blank row at the end of the table? Or as a footer row? And so on. It's the same problem we encountered for server-side rendering.

Because of this, Metawidget only goes so far out-of-the-box. metawidget.widgetbuilder.HtmlWidgetBuilder will render an array as a read-only table, but no further. Here's a complete example:

<!DOCTYPE HTML>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <style>
         #metawidget {
            border: 1px solid #cccccc;
            width: 250px;
            border-radius: 10px;
            padding: 10px;
            margin: 50px auto;
         }
      </style>
   </head>
   <body>
      <div id="metawidget"/>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ));
         mw.toInspect = {
            firstname: 'Homer',
            surname: 'Simpson',
            age: 36,
            family: [ {
               id: 0,
               firstname: 'Marge',
               surname: 'Simpson'
            }, {
               id: 1,
               firstname: 'Bart',
               surname: 'Simpson'
            } ]

         };
         mw.buildWidgets();
      </script>
   </body>
</html>

This will render:

For custom use cases, you can create your own WidgetBuilder and chain it together with the original ones using metawidget.widgetbuilder.CompositeWidgetBuilder. Then your own WidgetBuilder can handle special cases (like arrays) and 'fall back' to the original WidgetBuilders for everything else. Remember Metawidget only strives to automate what you'd normally do manually. Metawidget is not trying to be a big new framework. So the general approach is a) work out how to do something manually; b) write a WidgetBuilder to automate it.

As a shortcut, the existing metawidget.widgetbuilder.HtmlWidgetBuilder also has some methods you can override like createTable and addColumn. Here's a complete example:

<!DOCTYPE HTML>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <style>
         #metawidget {
            border: 1px solid #cccccc;
            width: 350px;
            border-radius: 10px;
            padding: 10px;
            margin: 50px auto;
         }
      </style>
   </head>
   <body>
      <div id="metawidget"/>
      <script type="text/javascript">
         var _myWidgetBuilder = new metawidget.widgetbuilder.HtmlWidgetBuilder();
         var _superAddRow = _myWidgetBuilder.addRow;
         _myWidgetBuilder.addRow = function( tbody, value, columnAttributes, mw ) {

            var tr = _superAddRow.call( this, tbody, value, columnAttributes, mw );
            var anchor = document.createElement( 'a' );
            anchor.setAttribute( 'href', '#' );
            anchor.setAttribute( 'onclick', 'this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode )' );
            anchor.innerHTML = 'Delete';
            var td = document.createElement( 'td' );
            td.appendChild( anchor );
            tr.appendChild( td );

            return tr;
         };

         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
            widgetBuilder: _myWidgetBuilder
         } );
         mw.toInspect = {
            firstname: 'Homer',
            surname: 'Simpson',
            age: 36,
            family: [ {
               id: 0,
               firstname: 'Marge',
               surname: 'Simpson'
            }, {
               id: 1,
               firstname: 'Bart',
               surname: 'Simpson'
            } ]
         };
         mw.buildWidgets();
      </script>
   </body>
</html>

UPDATE: signature of addRow has changed in newer releases of Metawidget to be addRow( tbody, value, row, columnAttributes, elementName, attributes, mw ). Please update the function declaration and .call lines appropriately.

This will render:

Of course, you may also need fine-grained control over what columns you display, and in what order. For this, you can use JSON Schema to describe the schema of your array items. Here's a complete example:

<!DOCTYPE HTML>
<html>
   <head>
      <script src="http://metawidget.org/js/3.5/metawidget-core.min.js"></script>
      <style>
         #metawidget {
            border: 1px solid #cccccc;
            width: 350px;
            border-radius: 10px;
            padding: 10px;
            margin: 50px auto;
         }
      </style>
   </head>
   <body>
      <div id="metawidget"/>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
            inspector: new metawidget.inspector.CompositeInspector( [
               new metawidget.inspector.PropertyTypeInspector(),
               new metawidget.inspector.JsonSchemaInspector( {
                  properties: {
                     family: {
                        items: {
                           properties: {
                              id: {
                                 hidden: true
                              },
                              employer: {
                                 type: 'string'
                              }
                           }
                        }
                     }
                  }
               } )

            ] )
         } );
         mw.toInspect = {
            firstname: 'Homer',
            surname: 'Simpson',
            age: 36,
            family: [ {
               id: 0,
               firstname: 'Marge',
               surname: 'Simpson'
            }, {
               id: 1,
               firstname: 'Bart',
               surname: 'Simpson'
            } ]
         };
         mw.buildWidgets();
      </script>
   </body>
</html>

This will render:

Note the JSON Schema is being combined with the inspection results from PropertyTypeInspector, so you don't have to re-specify columns like firstname and surname, or attributes like the type of id.

Feedback welcome!

Wednesday, July 24, 2013

Generate UI from JSON

I received some feedback that my previous minimal post was a little too minimal. So here's an ever-so-slightly extended version.

It shows how to use Metawidget as a lightweight, no-fuss solution to quickly render JSON objects on the client. This extended version also saves the JSON object back again. Here's the entire code:

<!DOCTYPE HTML>
<html>
   <head>
      <script src="http://metawidget.org/js/3.4/metawidget-core.min.js"></script>
      <style>
         #metawidget {
            border: 1px solid #cccccc;
            width: 250px;
            border-radius: 10px;
            padding: 10px;
            margin: 50px auto;
         }
         #metawidget button {
            display: block;
            margin: 10px auto 0px;
         }
      </style>
   </head>
   <body>
      <div id="metawidget">
         <button onclick="save()">Save</button>
      </div>
      <script type="text/javascript">
         var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ));
         mw.toInspect = {
            firstname: 'Homer',
            surname: 'Simpson',
            age: 36
         };
         mw.buildWidgets();
         function save() {
            mw.getWidgetProcessor(
               function( widgetProcessor ) {
                  return widgetProcessor instanceof metawidget.widgetprocessor.SimpleBindingProcessor;
               }
            ).save( mw );
            console.log( mw.toInspect );
         }

      </script>
   </body>
</html>

This will handily render:

Clicking the Save button will update the original JSON object, and print the results to the console.

Of course Metawidget can extend this further to support form validation, alternate layouts, third-party components, and much more. To see how, the best place to start is the tutorial.