Subversion Repositories Applications.papyrus

Rev

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