configuration
=============


a gform app is configured using the basic building blocks:

1. stores. provides access to data
2. schemas. provides schemas for entities
3. resource factories
4. views. Provides master and detail views for entities. The ui is generated from schemas.

## General

The configuration is a function that takes a very basic environment specific configuration and outputs 
a javascript object that is served to the AppFactory to generate the application widget. The application widget is then 
placed into the dom.


**environment specific configuration**

The environment specific configuration contains information about which stores an app should connect to. By customizing this configuration
the mongodb client can connect to different mongob db servers.
 
 
**app configuration function**

The app configuration function generates the stores, schemas and the widget configuration for the app. Usually
the environment specific configuration is used to customize the stores. The AppFactory reads the configuration and 
creates the AppContext which holds the schemas and stores and the widget. It also passes all widgets the AppContext.


## Configuration factories

The configuration is usually a javascript object with a single required property "factoryId". The AppFactory interprets the 
factoryId as am amd module id, requires it and instantiates it. It then calls `factory.create` and passes the configuration and the 
AppContext. 

The initialization is a multi-step process managed by the AppFactory:

- initialize the storeRegistry from `config.storeRegistry`. This task is delegated to the `factory/StoreRegistryFactory`. 
- The schemaRegistry is created by the `factory/SchemaRegistryFactory` from `config.schemaRegistry`. It has access to the storeRegistry to setup dynamic schema stores.
- Instantiate the AppContext with both registries. BorderContainerFactory instantiates the widget tree.
- Load components in `config.resourceFactories`. These will register their stores, schemas and views with the AppContext.
- BorderContainerFactory instantiates the widget tree and puts it in the dom.
- Start the router.



### StoreFactory

To configure a store a StoreFactory is used.

    {
        "factoryId": "cms/factory/StoreFactory",
        "storeClass": "cms/util/JsonRest", // the type of the Store
        "name": "/page",  // the id (name) of the store
        "templateStore": "/template", // the id of its templates' stores
        "instanceStore": "/page", // if the  entities in this store are templates, then instanceStore is the id of the store that contains the instances. Not present if templateStore is present.
        "idProperty": "identifier", // the id property
        "idType": "string", // the type of the id property, used to create the schema
        "typeProperty": "template", // the type property
        "target": "http://localhost:8080/entity/base/", // the base url
        "createEditorFactory": "cms/createBuilderEditorFactory", // the factory function of the EditorFactory. To create the editor for the entities
        "plainValueFactory": "cms/default/createTemplateValueFactory" // the function that creates default instances
    }


### SchemaRegistry

All type, schemas and templates are accessible through the SchemaRegistry.


    {
        "factoryId": "cms/factory/schema/SchemaRegistryFactory",
        "registryClass": "cms/SchemaRegistry",
        "stores": ["/template"], // stores that contains schemas
        "schemaGenerators": [
            {
                "factoryId": "cms/factory/schema/SchemaGenerator",
                "store": "/template"
            }
        ]
    }

Schemas are store or simply read from stores. Some Schema are generated by SchemaGenerators. These are usually meta schema, whose instances are schemas themselves and are stored in a store.

### ResourceFactories

Resource factories register stores and schemas that are defined externally. 

#### Static ResourceFactories

A static resource factory gets all information about stores and schemas at startup time.

Here is an example for accessing a mongoosejs schemas and models via baucis-gform:

    {
      "storeId": "/baucis",
      "factoryId": "gform-app/factory/ResourceFactory",
      "apiUrl": "http://localhost:3333/api/gform",
      "storeClass": "gform-app/util/BaucisStore",
      "idProperty": "_id",
      "createEditorFactory": "gform-app/baucis/createEditorFactory"
    }

#### Dynamic ResourceFactories

A dynamic resource factory provides a way to add new schemas an stores at runtime. It uses two static stores and any number 
of dynamic stores. The first store contains schemas.
The second store contains metadata about the dynamic stores and their schemas. 

Here is an example for mongodb (via mongomat server).


		{
			"factoryId": "cms/factory/DynamicResourceFactory",
			"storeClass": "cms/util/MongoRest",
			"baseUrl": "http://localhost:3000/collection/",
			"storeId": "/mdbcollection",
			"schemaStore":"/mdbschema",
			"idProperty":"_id"
		}

This resourceFactory adds MongoDb stores with the base url "http://localhost:3000/collection/".
 All schemas are stores in `${baseUrl}/mdbschema` the metadata is stored in `${baseUrl}/mdbcollection`. Each metadata
 document describes a mongodb collection and its schema or schemas and typeProperty.
 


### Views



The json structure of the views is identical to the resulting widget hierachy. Each widget is created by a Factory that takes a set of properties and maybe children.
The root widget is a borderContainer that supports children with different regions (top, left, right, center).

The following example creates a toolbar on top. A Preview in the center. The form for editing data on the right and views to browse entities and types on the left.
```json

            {
                    "factoryId": "cms/factory/ToolbarFactory", // creates a toolbar on top
                    "region": "top",
                    "children": [..]
                },
                {
                    "factoryId": "cms/factory/TabFactory", // create a tabContainer providing trees and grids to browse entities
                    "region": "left",
                    "splitter": true,
                    "width":"250px",
                    "children": [..]
                },
                {
                    "factoryId": "cms/factory/PreviewerFactory", // creates the previewer
                    "region": "center",
                    "splitter": true
                },
                {
                    "factoryId": "cms/factory/TabOpenerFactory", // creates the forms to edit entities. multiple forms can be opened in parallel.
                    "width": "40%",
                    "region": "right",
                    "splitter": true
                }
            }

```

#### Toolbar

The following tools are available in the Toolbar.

##### Search for entities

This tool consists of an autocomplete input field and a button. The button will open the selected entity.

    {
        "factoryId": "cms/factory/FindPageFactory",
        "storeId": "/page",  // the store to search in
        "label": "open", // label of the button
        "searchProperty":"url", // input is matched against the beginning of this property's values.
        "labelProperty":"url",  // the property to display (if left blank is identical to searchProperty
        "placeHolder":"find page .." // the placeHolder in the input field
     }

##### Create new entity

To create a new entity you must specify the entity store. If the entity store contains entities of a single type only, then a simple button
will be displayed:

    {
        "factoryId": "cms/factory/CreateFactory",
        "storeId": "/page",
        "label": "add entity"
    }

If the entity store contains entities of different types, then an autocomplete field to search for a type will be displayed:

    {
        "factoryId": "cms/factory/CreateFactory",
        "storeId": "/page", // store.templateSTore is the id of teh template store.
        "label": "+",
        "searchProperty": "name", // searchProperty in the template store
        "placeHolder": "find template .."
    }

##### Create new template

This is done just like "creating a new entity".


#### Edit and create Entities

Entities (which includes templates and types as well) are usually opened in a tab container. That makes it possible to have multiple open editor at a time.

It listens to the messages "/focus" and "/create".

    {
        "factoryId": "cms/factory/TabOpenerFactory"
    }

The actions that are available in the editor are defined in the EditorFactory.


#### Preview Entities

An entity can be associated with a template. To preview the rendered entity. There is currently one Previewer for Handlebars. It listens to
the messages "/create", "/focus", "/modify/update", "/modify/cancel".

    {
        "factoryId": "cms/factory/PreviewerFactory",
        "rendererClass": "cms/preview/handlebars/Renderer",
        "pageStore": "/page"
    }


#### Treeview

To create a tree view the store needs to provide a query with parameter `parent` returning the children.


    {
        "factoryId": "cms/factory/TreeFactory",
        "title": "tree",  // to display in a tab container
        "storeId": "/pagetree", // store id
        "labelAttribute": "name" // property to display
    }

#### Grid

To create a grid view the store needs to provide a query with parameter `parent` returning the children.


    {
        "factoryId": "cms/factory/GridFactory",
        "title": "template", // for diplay in tab container
        "storeId": "/template",
        "columns": [  // columns of the grid
                    {
                        "id": "name",
                        "field": "name",
                        "name": "name"
                    }
                   ]
    }

#### General layout container

#### border container

The root container is a borderContainer Its children must support the `region` property. Children may provide `width` and `height` properties. Also
the boolean property`splitter` can be set to make the border be draggable.

#### tab container

The tab container displays only one child at a time. Each child must provide a title property to display at the top.


    {
        "factoryId": "cms/factory/TabFactory",
        "children": [..]
     }






