1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.widget.RadioGroup");
|
|
|
14 |
dojo.require("dojo.lang.common");
|
|
|
15 |
dojo.require("dojo.event.browser");
|
|
|
16 |
dojo.require("dojo.html.selection");
|
|
|
17 |
dojo.require("dojo.widget.*");
|
|
|
18 |
dojo.require("dojo.widget.HtmlWidget");
|
|
|
19 |
dojo.widget.defineWidget("dojo.widget.RadioGroup", dojo.widget.HtmlWidget, function () {
|
|
|
20 |
this.selectedItem = null;
|
|
|
21 |
this.items = [];
|
|
|
22 |
this.selected = [];
|
|
|
23 |
this.groupCssClass = "radioGroup";
|
|
|
24 |
this.selectedCssClass = "selected";
|
|
|
25 |
this.itemContentCssClass = "itemContent";
|
|
|
26 |
}, {isContainer:false, templatePath:null, templateCssPath:null, postCreate:function () {
|
|
|
27 |
this._parseStructure();
|
|
|
28 |
dojo.html.addClass(this.domNode, this.groupCssClass);
|
|
|
29 |
this._setupChildren();
|
|
|
30 |
dojo.event.browser.addListener(this.domNode, "onclick", dojo.lang.hitch(this, "onSelect"));
|
|
|
31 |
if (this.selectedItem) {
|
|
|
32 |
this._selectItem(this.selectedItem);
|
|
|
33 |
}
|
|
|
34 |
}, _parseStructure:function () {
|
|
|
35 |
if (this.domNode.tagName.toLowerCase() != "ul" && this.domNode.tagName.toLowerCase() != "ol") {
|
|
|
36 |
dojo.raise("RadioGroup: Expected ul or ol content.");
|
|
|
37 |
return;
|
|
|
38 |
}
|
|
|
39 |
this.items = [];
|
|
|
40 |
var nl = this.domNode.getElementsByTagName("li");
|
|
|
41 |
for (var i = 0; i < nl.length; i++) {
|
|
|
42 |
if (nl[i].parentNode == this.domNode) {
|
|
|
43 |
this.items.push(nl[i]);
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
}, add:function (node) {
|
|
|
47 |
if (node.parentNode != this.domNode) {
|
|
|
48 |
this.domNode.appendChild(node);
|
|
|
49 |
}
|
|
|
50 |
this.items.push(node);
|
|
|
51 |
this._setup(node);
|
|
|
52 |
}, remove:function (node) {
|
|
|
53 |
var idx = -1;
|
|
|
54 |
for (var i = 0; i < this.items.length; i++) {
|
|
|
55 |
if (this.items[i] == node) {
|
|
|
56 |
idx = i;
|
|
|
57 |
break;
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
if (idx < 0) {
|
|
|
61 |
return;
|
|
|
62 |
}
|
|
|
63 |
this.items.splice(idx, 1);
|
|
|
64 |
node.parentNode.removeChild(node);
|
|
|
65 |
}, clear:function () {
|
|
|
66 |
for (var i = 0; i < this.items.length; i++) {
|
|
|
67 |
this.domNode.removeChild(this.items[i]);
|
|
|
68 |
}
|
|
|
69 |
this.items = [];
|
|
|
70 |
}, clearSelections:function () {
|
|
|
71 |
for (var i = 0; i < this.items.length; i++) {
|
|
|
72 |
dojo.html.removeClass(this.items[i], this.selectedCssClass);
|
|
|
73 |
}
|
|
|
74 |
this.selectedItem = null;
|
|
|
75 |
}, _setup:function (node) {
|
|
|
76 |
var span = document.createElement("span");
|
|
|
77 |
dojo.html.disableSelection(span);
|
|
|
78 |
dojo.html.addClass(span, this.itemContentCssClass);
|
|
|
79 |
dojo.dom.moveChildren(node, span);
|
|
|
80 |
node.appendChild(span);
|
|
|
81 |
if (this.selected.length > 0) {
|
|
|
82 |
var uid = dojo.html.getAttribute(node, "id");
|
|
|
83 |
if (uid && uid == this.selected) {
|
|
|
84 |
this.selectedItem = node;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
dojo.event.browser.addListener(node, "onclick", dojo.lang.hitch(this, "onItemSelect"));
|
|
|
88 |
if (dojo.html.hasAttribute(node, "onitemselect")) {
|
|
|
89 |
var tn = dojo.lang.nameAnonFunc(new Function(dojo.html.getAttribute(node, "onitemselect")), this);
|
|
|
90 |
dojo.event.browser.addListener(node, "onclick", dojo.lang.hitch(this, tn));
|
|
|
91 |
}
|
|
|
92 |
}, _setupChildren:function () {
|
|
|
93 |
for (var i = 0; i < this.items.length; i++) {
|
|
|
94 |
this._setup(this.items[i]);
|
|
|
95 |
}
|
|
|
96 |
}, _selectItem:function (node, event, nofire) {
|
|
|
97 |
if (this.selectedItem) {
|
|
|
98 |
dojo.html.removeClass(this.selectedItem, this.selectedCssClass);
|
|
|
99 |
}
|
|
|
100 |
this.selectedItem = node;
|
|
|
101 |
dojo.html.addClass(this.selectedItem, this.selectedCssClass);
|
|
|
102 |
if (!dj_undef("currentTarget", event)) {
|
|
|
103 |
return;
|
|
|
104 |
}
|
|
|
105 |
if (!nofire) {
|
|
|
106 |
if (dojo.render.html.ie) {
|
|
|
107 |
this.selectedItem.fireEvent("onclick");
|
|
|
108 |
} else {
|
|
|
109 |
var e = document.createEvent("MouseEvents");
|
|
|
110 |
e.initEvent("click", true, false);
|
|
|
111 |
this.selectedItem.dispatchEvent(e);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}, getValue:function () {
|
|
|
115 |
return this.selectedItem;
|
|
|
116 |
}, onSelect:function (e) {
|
|
|
117 |
}, onItemSelect:function (e) {
|
|
|
118 |
if (!dj_undef("currentTarget", e)) {
|
|
|
119 |
this._selectItem(e.currentTarget, e);
|
|
|
120 |
}
|
|
|
121 |
}});
|
|
|
122 |
|