source: Dev/trunk/client/qed/widgets/ListWidget.js @ 442

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

Enable validation on all fields of question.

File size: 5.4 KB
Line 
1define([
2    "dijit/_Container",
3    "dijit/_WidgetBase",
4    "dijit/registry",
5    "dojo/_base/array",
6    "dojo/_base/declare",
7    "dojo/_base/lang",
8    "dojo/dnd/Source",
9    "dojo/dnd/common",
10    "dojo/dom-construct"
11], function(_Container, _WidgetBase, registry, array, declare, lang, Source, dnd, domConstruct) {
12    return declare([_WidgetBase,_Container],{
13        name: "",
14        value: null,
15        multiple: true,
16        type: "text",
17        /* container : dojo/dnd/Container or subclass
18         * determine which container to use for the list
19         */
20        container: Source,
21        buildRendering: function() {
22            this.inherited(arguments);
23
24            var sourceParams = {};
25            var paramsToInherit = [
26                "singular",
27                "creator",
28                "skipForm",
29                "dropParent",
30                "isSource",
31                "autoSync",
32                "copyOnly",
33                "delay",
34                "horizontal",
35                "selfCopy",
36                "selfAccept",
37                "withHandles",
38                "generateText"
39            ];
40            array.forEach(paramsToInherit, function(param){
41                if ( typeof this[param] !== "undefined" ) {
42                    sourceParams[param] = this[param];
43                }
44            },this);
45            lang.mixin(sourceParams, {
46                accept: [this.type],
47                creator: lang.hitch(this, "creator"),
48                dropParent: this.containerNode
49            });
50            this.source = new this.container(this.domNode,sourceParams);
51        },
52        creator: function(item, hint) {
53            var id = dnd.getUniqueId();
54            var nodeOrWidget = null;
55            if ( hint === "avatar" ) {
56                if ( this.createAvatar ) {
57                    nodeOrWidget = this.createAvatar(id,item);
58                } else {
59                    return this.source.defaultCreator(item, hint);
60                }
61            } else {
62                if ( this.createListElement ) {
63                    nodeOrWidget = this.createListElement(id,item);
64                } else {
65                    return this.source.defaultCreator(item, hint);
66                }
67            }
68            var node = nodeOrWidget.domNode ? nodeOrWidget.domNode : nodeOrWidget;
69            if ( hint !== "avatar" && node.id !== id ) {
70                console.warn("Node id '%s' not equal to generated id '%s'. Is this intended?", node.id, id);
71            }
72            return {
73                data: item,
74                type: [this.type],
75                node: node
76            };
77        },
78        createAvatar: null, /*function(id,item){},*/
79        createListElement: null, /* function(id,item){},*/
80        _getValueAttr: function() {
81            return array.map(this.source.getAllNodes(),function(node){
82                var widget = registry.byNode(node);
83                if ( widget && "value" in widget ) {
84                    return widget.get('value');
85                } else {
86                    return this.source.getItem(node.id).data;
87                }
88            },this);
89        },
90        _setValueAttr: function(value) {
91            this.clear();
92            this.appendItems(value || []);
93        },
94        _setDisabledAttr: function(value) {
95            this._set("disabled", value);
96            array.forEach(this.getChildren(), function(child){
97                child.set("disabled", value);
98            });
99        },
100        _setReadOnlyAttr: function(value) {
101            this._set("readOnly", value);
102            array.forEach(this.getChildren(), function(child){
103                child.set("readOnly", value);
104            });
105        },
106        focus: function() {
107            var children = this.getChildren();
108            if ( children.length > 0 ) {
109                children[0].focus();
110            }
111        },
112        appendItems: function(items) {
113            this.source.insertNodes(false,items);
114        },
115        appendItem: function(item) {
116            this.source.insertNodes(false,[item]);
117        },
118        removeItem: function(key) {
119            array.forEach(array.filter(this.source.getAllNodes(), function(node){
120                return node.id === key;
121            }), lang.hitch(this, "_destroyNodeOrWidget"));
122            this.source.delItem(key);
123        },
124        getChildren: function() {
125            return array.filter(array.map(this.source.getAllNodes(), function(node){
126                return registry.byNode(node);
127            }),function(widget){ return widget !== null; });
128        },
129        clear: function() {
130            array.forEach(this.source.getAllNodes(),
131                          lang.hitch(this, "_destroyNodeOrWidget"));
132            this.source.clearItems();
133        },
134        _destroyNodeOrWidget: function(node) {
135            var widget = registry.byNode(node);
136            if ( widget ) {
137                widget.destroyRecursive();
138            } else {
139                domConstruct.destroy(node);
140            }
141        },
142        validate: function() {
143            return array.every(array.map(this.source.getAllNodes(),function(node){
144                var widget = registry.byNode(node);
145                return !widget || widget.disabled || !widget.validate || widget.validate();
146            }), function(item){ return item; });
147        },
148        destroy: function() {
149            this.source.destroy();
150            this.inherited(arguments);
151        }
152    });
153});
Note: See TracBrowser for help on using the repository browser.