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

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

Big cleanup of the question content.

  • Replaced old list implementations with a new one that behaves like a form widget.
  • All question content is now in separate widgets, not in the factory itself.
  • Added form and widget validation for question editing.
File size: 4.8 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        appendItems: function(items) {
95            this.source.insertNodes(false,items);
96        },
97        appendItem: function(item) {
98            this.source.insertNodes(false,[item]);
99        },
100        removeItem: function(key) {
101            array.forEach(array.filter(this.source.getAllNodes(), function(node){
102                return node.id === key;
103            }), lang.hitch(this, "_destroyNodeOrWidget"));
104            this.source.delItem(key);
105        },
106        getChildren: function() {
107            return array.filter(array.map(this.source.getAllNodes(), function(node){
108                return registry.byNode(node);
109            }),function(widget){ return widget !== null; });
110        },
111        clear: function() {
112            array.forEach(this.source.getAllNodes(),
113                          lang.hitch(this, "_destroyNodeOrWidget"));
114            this.source.clearItems();
115        },
116        _destroyNodeOrWidget: function(node) {
117            var widget = registry.byNode(node);
118            if ( widget ) {
119                widget.destroyRecursive();
120            } else {
121                domConstruct.destroy(node);
122            }
123        },
124        validate: function() {
125            return array.every(array.map(this.source.getAllNodes(),function(node){
126                var widget = registry.byNode(node);
127                return !widget || widget.disabled || !widget.validate || widget.validate();
128            }), function(item){ return item; });
129        },
130        destroy: function() {
131            this.source.destroy();
132            this.inherited(arguments);
133        }
134    });
135});
Note: See TracBrowser for help on using the repository browser.