source: Dev/branches/rest-dojo-ui/client/rft/widgets/MultipleChoiceWidget.js @ 417

Last change on this file since 417 was 417, checked in by hendrikvanantwerpen, 12 years ago

Put all model dependent code in one place. More separation of general and domain code.

File size: 3.5 KB
Line 
1define(['dojo/_base/declare','dojo/_base/array','dojo/_base/lang',
2    'dojo/dom-construct','dijit/registry','dijit/_WidgetBase',
3    'dijit/_TemplatedMixin','dijit/_WidgetsInTemplateMixin','dijit/_Container',
4    'dojo/text!./templates/MultipleChoiceWidget.html',
5    'dijit/form/RadioButton','dijit/form/CheckBox','dijit/form/TextBox','dijit/form/Button'],
6    function(declare,array,lang,domConstruct,registry,_WidgetBase,_TemplatedMixin,
7        _WidgetsInTemplateMixin,_Container,
8        templateString,RadioButton,CheckBox,TextBox,Button){
9        return declare([_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,_Container],{
10            templateString: templateString,
11            multiple: true,
12            baseClass: 'rftMultipleChoiceWidget',
13           
14            allowMultiple: false,
15            name: '',
16            value: null,
17           
18            _widgetCache: null,
19            constuctor: function() {
20                this.inherited(arguments);
21                this.value = [];
22                this._widgetCache = {};
23            },
24            _setValueAttr: function(value) {
25                this.reset();
26                this.value = value;
27                array.forEach(this.value,lang.hitch(this,function(val){
28                    this._addRow(val);
29                }));
30            },
31            _getValueAttr: function() {
32                var value = [];
33                registry.findWidgets(this.domNode).forEach(function(w){
34                    w.isInstanceOf(TextBox) && value.push(w.get('value'));
35                });
36                this.value = value;
37                return value;
38            },
39            reset: function() {
40                this._removeWidgetsBelowNode(this.rows);
41                domConstruct.empty(this.rows);
42            },
43            _onAddRow: function() {
44                this._addRow();
45            },
46            _addRow: function(value,tagRow,pos) {
47                var row = domConstruct.create('div',{'class':'row'});
48                if ( this.allowMultiple ) {
49                    new CheckBox({
50                        name:this.name,
51                        'class':'rowBox',
52                        disabled: this.mode == 'edit'
53                    }).placeAt(row);
54                } else {
55                    new RadioButton({
56                        name:this.name,
57                        'class':'rowBox',
58                        disabled: this.mode == 'edit'
59                    }).placeAt(row);
60                }
61                new TextBox({
62                    name:this.name,
63                    value:value || '',
64                    'class':'rowText'
65                }).placeAt(row);
66                new Button({
67                    label:'-',
68                    'class':'rowBtn',
69                    onClick:lang.hitch(this,'_removeNodeWithWidgets',row)
70                }).placeAt(row);
71                new Button({
72                    label:'+',
73                    'class':'rowBtn',
74                    onClick:lang.hitch(this,'_addRow','',row,'before')
75                }).placeAt(row);
76                domConstruct.place(row,tagRow || this.rows,pos||'last');
77            },
78            _removeNodeWithWidgets: function(node) {
79                this._removeWidgetsBelowNode(node);
80                domConstruct.destroy(node);
81            },
82            _removeWidgetsBelowNode: function(node) {
83                registry.findWidgets(node).forEach(function(w){
84                    w.destroyRecursive();
85                });
86            },
87            _onRowChange: function(row) {
88               
89            }
90        });
91    });
Note: See TracBrowser for help on using the repository browser.