]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/main/webapp/argeo-ria-src/class/org/argeo/ria/components/DynamicTreeFolder.js
Create Argeo SLC RIA project
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / main / webapp / argeo-ria-src / class / org / argeo / ria / components / DynamicTreeFolder.js
1 /**
2 * A "dynamic" implementation of the standard TreeFolder class.
3 *
4 */
5 qx.Class.define("org.argeo.ria.components.DynamicTreeFolder", {
6 extend : qx.ui.tree.TreeFolder,
7
8 properties : {
9 /**
10 * The current state of the folder, usually "empty" => "loading" => "loaded"
11 */
12 "state" : {
13 check : "String",
14 init : "empty",
15 apply : "_applyState"
16 },
17 /**
18 * String to display as a child node during loading
19 */
20 "loadingString" : {
21 check : "String",
22 init : "Loading..."
23 },
24 /**
25 * Function that will load the children of this folder
26 */
27 "loader" : {
28 check : "Function",
29 init : function(treeFolder){treeFolder.setLoaded();}
30 },
31 /**
32 * Optionnal data describing the "drag" behaviour of the created children.
33 * First level is "file" or "folder", and for each of them, supported keys are "type" and "action".
34 */
35 "dragData": {
36 check : "Map",
37 init : {}
38 }
39 },
40
41 /**
42 * Creates a new instance of DynamicTreeFolder
43 * @param label {String} Label of the folder
44 * @param loader {Function} Function that will load the children
45 * @param loadingString {String} String to display as a child node during loading
46 * @param dragData {Map} Optionnal data describing the "drag" behaviour of the created children.
47 */
48 construct : function(label, loader, loadingString, dragData){
49 this.base(arguments, label);
50 if(loader) this.setLoader(loader);
51 if(loadingString) this.setLoadingString(loadingString);
52 if(dragData) this.setDragData(dragData);
53 this.addListener("changeOpen", function(e){
54 if(e.getData() && this.getState() == "loading"){
55 this.load();
56 }
57 }, this);
58 this.setState("loading");
59 },
60
61 members : {
62 /**
63 * Add an item to the folder
64 * @param varargs {Mixed} One or many children to add
65 */
66 add : function(varargs){
67 this.base(arguments, varargs);
68 for (var i=0, l=arguments.length; i<l; i++)
69 {
70 var treeItem = arguments[i];
71 if(treeItem == this.loadingChild) continue;
72 this.appendDragData(treeItem);
73 }
74 },
75
76 /**
77 * If there is dragData set, init the drag behaviour of a child
78 * @param treeItem {qx.ui.tree.AbstractTreeItem} Newly created child
79 */
80 appendDragData : function(treeItem){
81 var dragData = this.getDragData();
82 var nodeTypeDetected = false;
83 if(qx.Class.isSubClassOf(qx.Class.getByName(treeItem.classname), qx.ui.tree.TreeFile) && dragData["file"]){
84 nodeTypeDetected = "file";
85 }
86 if(qx.Class.isSubClassOf(qx.Class.getByName(treeItem.classname), qx.ui.tree.TreeFolder) && dragData["folder"]){
87 nodeTypeDetected = "folder";
88 }
89 if(!nodeTypeDetected) return;
90
91 treeItem.setDraggable(true);
92 treeItem.addListener("dragstart", function(e){
93 if(dragData[nodeTypeDetected]["type"]){
94 for(var j=0;j<dragData[nodeTypeDetected]["type"].length;j++){
95 e.addType(dragData[nodeTypeDetected]["type"][j]);
96 }
97 }
98 if(dragData[nodeTypeDetected]["action"]){
99 for(var j=0;j<dragData[nodeTypeDetected]["action"].length;j++){
100 e.addAction(dragData[nodeTypeDetected]["action"][j]);
101 }
102 }
103 });
104
105 },
106
107 /**
108 * Set the state to "loaded"
109 */
110 setLoaded : function(){
111 this.setState("loaded");
112 },
113 /**
114 * Called when "state" is set to a new value
115 * @param state {String} the new state
116 */
117 _applyState : function(state){
118 if(state == "loaded"){
119 if(this.loadingChild){
120 this.remove(this.loadingChild);
121 delete this.loadingChild;
122 }
123 }else if(state == "loading" && !this.loadingChild){
124 this.addLoadingChild();
125 }
126 },
127 /**
128 * Create a temporary child with the loadingString label and add it.
129 */
130 addLoadingChild : function(){
131 this.loadingChild = new qx.ui.tree.TreeFile(this.getLoadingString());
132 this.add(this.loadingChild);
133 },
134 /**
135 * Call loader function.
136 */
137 load : function(){
138 var loaderFunc = this.getLoader();
139 loaderFunc(this);
140 },
141 /**
142 * Empty then call loader function.
143 */
144 reload : function(){
145 this.removeAll();
146 this.setState("loading");
147 this.load();
148 }
149 }
150 });