Add basic support for embedded types

For example, given:
```javascript
{
  {
    id: 'container',
    type: 'schema',
    resourceFields: {
      restart: {
        type: 'restartPolicy'
      }
    }
  },

  {
    id: 'restartPolicy',
    type: 'schema',
    resourceFields: {
      name: {
        type: 'string'
      },

      maximumRetryCount: {
        type: 'int',
      }
    }
  }
}
```

the `restart` field will now show up as a textarea that you can put JSON in.
It would be nicer if it looked up the properties of a restartPolicy and allowed
you to enter them directly, but nesting complex types is not a terribly simple
change so this will provide a way to set embedded types in the meantime.
This commit is contained in:
Vincent Fiduccia 2015-01-16 14:59:24 -07:00
parent 613e2d8f2a
commit bb2a818a8f
3 changed files with 106 additions and 94 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.DS_Store
node_modules
npm-debug.log
bower_components
dist
tmp

View File

@ -1,6 +1,6 @@
{
"name": "api-ui",
"version": "1.0.0",
"version": "1.0.1",
"description": "Embedded UI for any service that implements the Rancher API spec",
"contributors": [
"Go Daddy Operating Company, LLC. (http://godaddy.com)",
@ -14,7 +14,7 @@
"url": "https://github.com/rancherio/api-ui.git"
},
"scripts": {
"start": "./scripts/server",
"start": "./scripts/serve",
"build": "./scripts/build"
},
"devDependencies": {

View File

@ -472,7 +472,6 @@ HTMLApi.prototype.actionLoad = function(name, obj, body)
// The description of the input and output for this action
var actionSchema = (isCollection ? objSchema.collectionActions[name] : objSchema.resourceActions[name]);
// The schema for the input
var actionInput = {};
if ( actionSchema.input )
@ -1349,12 +1348,25 @@ HTMLApi.prototype._flattenFields = function(mode,schema,data)
HTMLApi.prototype._flattenField = function(mode, name, field, data, depth)
{
if ( (mode != 'update') && (mode != 'action') && !(mode == 'create' && field.create) )
{
// This field is not set/changeable
return null;
}
depth = depth || 0;
var type = field._typeList[depth];
if ( mode == 'update' || (mode == 'create' && field.create) || (mode == 'action') )
var isEmbedded = false;
if ( ['string','password','float','int','date','blob','boolean','enum','reference','array','map'].indexOf(type) === -1 && this.getSchema(type) )
{
// Show embedded types as JSON, until proper support for embedded types is added.
type = 'json';
isEmbedded = true;
}
// The value input's name
var formFieldName = name;
@ -1374,7 +1386,7 @@ HTMLApi.prototype._flattenField = function(mode, name, field, data, depth)
formFieldName += '.value{}';
}
if ( subType == 'json' )
if ( subType == 'json' || (isEmbedded && i === depth) )
{
formFieldName += '.json{}';
}
@ -1399,7 +1411,7 @@ HTMLApi.prototype._flattenField = function(mode, name, field, data, depth)
var displayType = field._typeList[ field._typeList.length - 1];
var parentType = field._typeList[ field._typeList.length - 2];
if ( parentType && parentType == 'reference' )
if ( isEmbedded || (parentType && parentType == 'reference') )
{
var link = null;
if ( field.referenceCollection )
@ -1416,8 +1428,10 @@ HTMLApi.prototype._flattenField = function(mode, name, field, data, depth)
}
if ( link )
{
displayType = '<a tabindex="-1" href="' + link + '" target="_blank">' + displayType + '</a>';
}
}
for ( var i = field._typeList.length - 2 ; i >= depth ; i-- )
{
@ -1457,9 +1471,6 @@ HTMLApi.prototype._flattenField = function(mode, name, field, data, depth)
}
return row;
}
return null;
}
@ -1751,7 +1762,7 @@ HTMLApi.prototype.subAdd = function(button, name)
children: [field]
}
var html = Handlebars.templates['field.hbs'](par);
var html = Handlebars.partials['field.hbs'](par);
// html = '<div><input type="button" onclick="htmlapi.subRemove(this);" value="-">' + html + '</div>';
$(button).before(html);