]> git.argeo.org Git - gpl/argeo-slc.git/blob - QueriesView.js
5a8dd840f6982c38857d718b1539215b03758d50
[gpl/argeo-slc.git] / QueriesView.js
1 qx.Class.define("org.argeo.jcr.ria.views.QueriesView", {
2 extend : qx.ui.container.Composite,
3 implement : [org.argeo.ria.components.IView],
4
5 properties : {
6 /**
7 * The commands definition Map that will be automatically added and wired to the menubar and toolbar.
8 * See {@link org.argeo.ria.event.CommandsManager#definitions} for the keys to use for defining commands.
9 */
10 commands : {
11 init : {
12 "remove_query" : {
13 label : "Remove",
14 icon : "org.argeo.slc.ria/media-playback-start.png",
15 shortcut : null,
16 enabled : true,
17 menu : "Queries",
18 toolbar : null,
19 callback : function(e) {
20 var selection = this.tree.getSelection();
21 if(!selection.length) return;
22 var treeNode = selection[0];
23 treeNode.getParent().remove(treeNode);
24 },
25 selectionChange : function(viewId, selection){
26 this.setEnabled(false);
27 if(selection && selection.length && !selection[0].getJcrNode){
28 this.setEnabled(true);
29 }
30 }
31 }
32 }
33 },
34 viewSelection : {
35 nullable:false,
36 check:"org.argeo.ria.components.ViewSelection"
37 },
38 instanceId : {
39 init:"queriesView",
40 event : "changeInstanceId"
41 },
42 instanceLabel : {
43 init:"Queries",
44 event : "changeInstanceLabel"
45 },
46 dataModel : {
47
48 }
49 },
50
51 construct : function(){
52 this.base(arguments);
53 },
54
55 members : {
56 /**
57 * The implementation should contain the GUI initialisation.
58 * This is the role of the manager to actually add the graphical component to the pane,
59 * so it's not necessary to do it here.
60 * @param viewPane {org.argeo.ria.components.ViewPane} The pane manager
61 * @param data {Mixed} Any object or data passed by the initiator of the view
62 * @return {Boolean}
63 */
64 init : function(viewPane, dataModel){
65 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
66 this.setLayout(new qx.ui.layout.VBox());
67 this.setDataModel(dataModel);
68
69 this.radio = new qx.ui.form.RadioButtonGroup(new qx.ui.layout.HBox(5));
70 var xPath = new qx.ui.form.RadioButton("XPath");
71 xPath.setModel("xpath");
72 var sql = new qx.ui.form.RadioButton("SQL");
73 sql.setModel("sql");
74 this.radio.add(xPath);
75 this.radio.add(sql);
76
77 var topLayout = new qx.ui.container.Composite(new qx.ui.layout.HBox(5));
78 topLayout.add(new qx.ui.basic.Label("Query (Ctrl+Enter to submit):"));
79 topLayout.add(new qx.ui.core.Spacer(), {flex:1});
80 topLayout.add(this.radio);
81 topLayout.setPadding(0,2,2,2);
82 this.add(topLayout);
83
84 this.textarea = new qx.ui.form.TextArea();
85 this.textarea.setHeight(60);
86 this.textarea.setMarginBottom(5);
87 this.add(this.textarea);
88 this.textarea.addListener("keypress", function(e){
89 if(e.getKeyIdentifier() == "Enter" && e.isCtrlOrCommandPressed()){
90 this._submitQuery();
91 }
92 }, this);
93
94
95 var resLabel = new qx.ui.basic.Label("Results");
96 resLabel.setPadding(0, 2, 2, 2);
97 this.add(resLabel);
98
99 this.tree = new qx.ui.tree.Tree();
100 this.add(this.tree, {flex:1});
101 },
102 /**
103 * The implementation should contain the real data loading (i.o. query...)
104 * @return {Boolean}
105 */
106 load : function(){
107 var dataModel = this.getDataModel();
108
109 this.treeBase = new qx.ui.tree.TreeFolder("Queries");
110 this.tree.setRoot(this.treeBase);
111 this.tree.setHideRoot(true);
112 this.treeBase.setOpen(true);
113
114 this.tree.addListener("changeSelection", function(e){
115 var sel = this.tree.getSelection();
116 var selection = [];
117 var viewSelection = this.getViewSelection();
118 viewSelection.clear();
119 for(var i=0;i<sel.length;i++){
120 if(sel[i].getJcrNode){
121 selection.push(sel[i].getJcrNode());
122 }
123 viewSelection.addNode(sel[i]);
124 }
125 this.getDataModel().setSelectionWithSource(selection, this);
126 }, this);
127 dataModel.addListener("changeSelection", function(e){
128 if(this.getDataModel().getSelectionSource() == this) return;
129 var selection = this.getDataModel().getSelection();
130 // Arbitrary : for the moment, external select can only apply
131 // to children of the current selection
132 var crtSel = this.tree.getSelection();
133 if(!crtSel.length || !selection.length) return;
134 var crtTreeSel = crtSel[0];
135 if(!crtTreeSel.getJcrNode) return;
136 if(selection[0].getParent() && crtTreeSel.getJcrNode().getPath() == selection[0].getParent().getPath()){
137 crtTreeSel.setOpen(true);
138 var crtChildren =crtTreeSel.getChildren();
139 for(var i=0;i<crtChildren.length;i++){
140 if(crtChildren[i].getJcrNode().getPath() == selection[0].getPath()){
141 this.tree.setSelection([crtChildren[i]]);
142 return;
143 }
144 }
145 }else if(crtTreeSel.getParent() && crtTreeSel.getJcrNode().getParent().getPath() == selection[0].getPath()){
146 this.tree.setSelection([crtTreeSel.getParent()]);
147 }
148
149 }, this);
150 this.tree.setContextMenu(org.argeo.ria.event.CommandsManager
151 .getInstance().createMenuFromIds(["open", "remove_query"]));
152
153 },
154
155 _submitQuery : function(){
156 var query = this.textarea.getValue();
157 var language = this.radio.getModelSelection()[0];
158 var src = "/org.argeo.slc.webapp/queryJcrNodes.jcr?language="+language+"&statement="+query;
159 var conn = new org.argeo.ria.remote.Request(src, "GET", "application/json");
160 conn.addListener("completed", function(response){
161 var json = response.getContent();
162 this._addQueryResult(language, query, json);
163 }, this);
164 conn.send();
165 },
166
167 _addQueryResult : function(language, query, results){
168 var treeQuery = new qx.ui.tree.TreeFolder((language=="xpath"?"XPath":"SQL") + " query : '"+query+"' ("+results.length+")");
169 this.treeBase.add(treeQuery);
170 var realRoot = this.getDataModel().getRootNode();
171 var provider = realRoot.getNodeProvider();
172 for(var i=0;i<results.length;i++){
173 var child = new org.argeo.jcr.ria.model.Node(results[i], provider, true);
174 child.setPath(results[i]);
175 var childTree = new org.argeo.jcr.ria.views.JcrTreeFolder(child);
176 treeQuery.add(childTree);
177 }
178 },
179
180 /**
181 * Whether this component is already contained in a scroller (return false) or not (return true).
182 * @return {Boolean}
183 */
184 addScroll : function(){
185 return false;
186 },
187 /**
188 * Called at destruction time
189 * Perform all the clean operations (stopping polling queries, etc.)
190 */
191 close : function(){
192
193 }
194
195 }
196 });