source: Dev/branches/rest-dojo-ui/client/dojox/mvc/Generate.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 5.6 KB
Line 
1define([
2        "dojo/_base/lang",
3        "dojo/_base/declare",
4        "./_Container",
5        "./Group",
6        "dijit/form/TextBox"
7], function(lang, declare, Container){
8        /*=====
9                Container = dojox.mvc._Container;
10                declare = dojo.declare;
11        =====*/
12
13        return declare("dojox.mvc.Generate", [Container], {
14                // summary:
15                //              A container that generates a view based on the data model its bound to.
16                //
17                // description:
18                //              A generate introspects its data binding and creates a view contained in
19                //              it that allows displaying the bound data. Child dijits or custom view
20                //              components inside it inherit their parent data binding context from it.
21       
22                // _counter: [private] Integer
23                //              A count maintained internally to always generate predictable widget
24                //              IDs in the view generated by this container.
25                _counter : 0,
26       
27                // defaultWidgetMapping: Object
28                //              The mapping of types to a widget class. Set widgetMapping to override this.
29                //     
30                _defaultWidgetMapping: {"String" : "dijit.form.TextBox"},
31       
32                // defaultClassMapping: Object
33                //              The mapping of class to use. Set classMapping to override this.
34                //     
35                _defaultClassMapping: {"Label" : "generate-label-cell", "String" : "generate-dijit-cell", "Heading" : "generate-heading", "Row" : "row"},
36       
37       
38                // defaultIdNameMapping: Object
39                //              The mapping of id and name to use. Set idNameMapping to override this. A count will be added to the id and name
40                //     
41                _defaultIdNameMapping: {"String" : "textbox_t"},
42               
43                ////////////////////// PRIVATE METHODS ////////////////////////
44       
45                _updateBinding: function(){
46                        // summary:
47                        //              Regenerate if the binding changes.
48                        this.inherited(arguments);
49                        this._buildContained();
50                },
51       
52                _buildContained: function(){
53                        // summary:
54                        //              Destroy any existing generated view, recreate it from scratch
55                        //              parse the new contents.
56                        // tags:
57                        //              private
58                        this._destroyBody();
59       
60                        this._counter = 0;
61                        this.srcNodeRef.innerHTML = this._generateBody(this.get("binding"));
62       
63                        this._createBody();
64                },
65       
66                _generateBody: function(binding, hideHeading){
67                        // summary:
68                        //              Generate the markup for the view associated with this generate
69                        //              container.
70                        //      binding:
71                        //              The associated data binding to generate a view for.
72                        //      hideHeading:
73                        //              Whether the property name should be displayed as a heading.
74                        // tags:
75                        //              private
76                        var body = "";
77                        for(var prop in binding){
78                                if(binding[prop] && lang.isFunction(binding[prop].toPlainObject)){
79                                        if(binding[prop].get(0)){
80                                                body += this._generateRepeat(binding[prop], prop);
81                                        }else if(binding[prop].value){
82                                                // TODO: Data types based widgets
83                                                body += this._generateTextBox(prop);
84                                        }else{
85                                                body += this._generateGroup(binding[prop], prop, hideHeading);
86                                        }
87                                }
88                        }
89                        return body;
90                },
91       
92                _generateRepeat: function(binding, repeatHeading){
93                        // summary:
94                        //              Generate a repeating model-bound view.
95                        //      binding:
96                        //              The bound node (a collection/array node) to generate a
97                        //              repeating UI/view for.
98                        //      repeatHeading:
99                        //              The heading to be used for this portion.
100                        // tags:
101                        //              private
102                        var headingClass = (this.classMapping && this.classMapping["Heading"]) ? this.classMapping["Heading"] : this._defaultClassMapping["Heading"];
103                        var repeat = '<div data-dojo-type="dojox.mvc.Group" data-dojo-props="ref: \'' + repeatHeading + '\'" + id="' + this.id + '_r' + this._counter++ + '">' +
104                                                 '<div class="' + headingClass + '\">' + repeatHeading + '</div>';
105                        repeat += this._generateBody(binding, true);
106                        repeat += '</div>';
107                        return repeat;
108                },
109               
110                _generateGroup: function(binding, groupHeading, hideHeading){
111                        // summary:
112                        //              Generate a hierarchical model-bound view.
113                        //      binding:
114                        //              The bound (intermediate) node to generate a hierarchical
115                        //              view portion for.
116                        //      groupHeading:
117                        //              The heading to be used for this portion.
118                        //      hideHeading:
119                        //              Whether the heading should be hidden for this portion.
120                        // tags:
121                        //              private
122                        var group = '<div data-dojo-type="dojox.mvc.Group" data-dojo-props="ref: \'' + groupHeading + '\'" + id="' + this.id + '_g' + this._counter++ + '">';
123                        if(!hideHeading){
124                                var headingClass = (this.classMapping && this.classMapping["Heading"]) ? this.classMapping["Heading"] : this._defaultClassMapping["Heading"];
125                                group += '<div class="' + headingClass + '\">' + groupHeading + '</div>';
126                        }
127                        group += this._generateBody(binding);
128                        group += '</div>';
129                        return group;
130                },
131       
132                _generateTextBox: function(prop){
133                        // summary:
134                        //              Produce a widget for a simple value.
135                        //      prop:
136                        //              The data model property name.
137                        // tags:
138                        //              private
139                        // TODO: Data type based widget generation / enhanced meta-data
140                        var idname = this.idNameMapping ? this.idNameMapping["String"] : this._defaultIdNameMapping["String"];
141                        idname = idname + this._counter++;
142                        var widClass = this.widgetMapping ? this.widgetMapping["String"] : this._defaultWidgetMapping["String"];
143                        var labelClass = (this.classMapping && this.classMapping["Label"]) ? this.classMapping["Label"] : this._defaultClassMapping["Label"];
144                        var stringClass = (this.classMapping && this.classMapping["String"]) ? this.classMapping["String"] : this._defaultClassMapping["String"];
145                        var rowClass = (this.classMapping && this.classMapping["Row"]) ? this.classMapping["Row"] : this._defaultClassMapping["Row"];
146                       
147                        return '<div class="' + rowClass + '\">' +
148                                        '<label class="' + labelClass + '\">' + prop + ':</label>' +
149                                        '<input class="' + stringClass + '\" data-dojo-type="' + widClass + '\" data-dojo-props="name: \'' + idname + "', ref: '" + prop + '\'" id="' +
150                                        idname + '\"></input>' +
151                                        '</div>';
152                }
153        });
154});
Note: See TracBrowser for help on using the repository browser.