Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.Repeater");
14
dojo.require("dojo.widget.HtmlWidget");
15
dojo.require("dojo.string");
16
dojo.require("dojo.event.*");
17
dojo.require("dojo.experimental");
18
dojo.experimental("dojo.widget.Repeater");
19
dojo.widget.defineWidget("dojo.widget.Repeater", dojo.widget.HtmlWidget, {name:"", rowTemplate:"", myObject:null, pattern:"", useDnd:false, isContainer:true, initialize:function (args, frag) {
20
	var node = this.getFragNodeRef(frag);
21
	node.removeAttribute("dojotype");
22
	this.setRow(dojo.string.trim(node.innerHTML), {});
23
	node.innerHTML = "";
24
	frag = null;
25
}, postCreate:function (args, frag) {
26
	if (this.useDnd) {
27
		dojo.require("dojo.dnd.*");
28
		var dnd = new dojo.dnd.HtmlDropTarget(this.domNode, [this.widgetId]);
29
	}
30
}, _reIndexRows:function () {
31
	for (var i = 0, len = this.domNode.childNodes.length; i < len; i++) {
32
		var elems = ["INPUT", "SELECT", "TEXTAREA"];
33
		for (var k = 0; k < elems.length; k++) {
34
			var list = this.domNode.childNodes[i].getElementsByTagName(elems[k]);
35
			for (var j = 0, len2 = list.length; j < len2; j++) {
36
				var name = list[j].name;
37
				var index = dojo.string.escape("regexp", this.pattern);
38
				index = index.replace(/(%\\\{index\\\})/g, "%{index}");
39
				var nameRegexp = dojo.string.substituteParams(index, {"index":"[0-9]*"});
40
				var newName = dojo.string.substituteParams(this.pattern, {"index":"" + i});
41
				var re = new RegExp(nameRegexp, "g");
42
				list[j].name = name.replace(re, newName);
43
			}
44
		}
45
	}
46
}, onDeleteRow:function (e) {
47
	var index = dojo.string.escape("regexp", this.pattern);
48
	index = index.replace(/%\\\{index\\\}/g, "%{index}");
49
	var nameRegexp = dojo.string.substituteParams(index, {"index":"([0-9]*)"});
50
	var re = new RegExp(nameRegexp, "g");
51
	this.deleteRow(re.exec(e.target.name)[1]);
52
}, hasRows:function () {
53
	if (this.domNode.childNodes.length > 0) {
54
		return true;
55
	}
56
	return false;
57
}, getRowCount:function () {
58
	return this.domNode.childNodes.length;
59
}, deleteRow:function (idx) {
60
	this.domNode.removeChild(this.domNode.childNodes[idx]);
61
	this._reIndexRows();
62
}, _changeRowPosition:function (e) {
63
	if (e.dragStatus == "dropFailure") {
64
		this.domNode.removeChild(e["dragSource"].domNode);
65
	} else {
66
		if (e.dragStatus == "dropSuccess") {
67
		}
68
	}
69
	this._reIndexRows();
70
}, setRow:function (template, myObject) {
71
	template = template.replace(/\%\{(index)\}/g, "0");
72
	this.rowTemplate = template;
73
	this.myObject = myObject;
74
}, getRow:function () {
75
	return this.rowTemplate;
76
}, _initRow:function (node) {
77
	if (typeof (node) == "number") {
78
		node = this.domNode.childNodes[node];
79
	}
80
	var elems = ["INPUT", "SELECT", "IMG"];
81
	for (var k = 0; k < elems.length; k++) {
82
		var list = node.getElementsByTagName(elems[k]);
83
		for (var i = 0, len = list.length; i < len; i++) {
84
			var child = list[i];
85
			if (child.nodeType != 1) {
86
				continue;
87
			}
88
			if (child.getAttribute("rowFunction") != null) {
89
				if (typeof (this.myObject[child.getAttribute("rowFunction")]) == "undefined") {
90
					dojo.debug("Function " + child.getAttribute("rowFunction") + " not found");
91
				} else {
92
					this.myObject[child.getAttribute("rowFunction")](child);
93
				}
94
			} else {
95
				if (child.getAttribute("rowAction") != null) {
96
					if (child.getAttribute("rowAction") == "delete") {
97
						child.name = dojo.string.substituteParams(this.pattern, {"index":"" + (this.getRowCount() - 1)});
98
						dojo.event.connect(child, "onclick", this, "onDeleteRow");
99
					}
100
				}
101
			}
102
		}
103
	}
104
}, onAddRow:function (e) {
105
}, addRow:function (doInit) {
106
	if (typeof (doInit) == "undefined") {
107
		doInit = true;
108
	}
109
	var node = document.createElement("span");
110
	node.innerHTML = this.getRow();
111
	if (node.childNodes.length == 1) {
112
		node = node.childNodes[0];
113
	}
114
	this.domNode.appendChild(node);
115
	var parser = new dojo.xml.Parse();
116
	var frag = parser.parseElement(node, null, true);
117
	dojo.widget.getParser().createSubComponents(frag, this);
118
	this._reIndexRows();
119
	if (doInit) {
120
		this._initRow(node);
121
	}
122
	if (this.useDnd) {
123
		node = new dojo.dnd.HtmlDragSource(node, this.widgetId);
124
		dojo.event.connect(node, "onDragEnd", this, "_changeRowPosition");
125
	}
126
	this.onAddRow(node);
127
}});
128