File:
0.01.1a/js/search.js (
View as Code)
1: /* -------------------------------------------------------------
2: This file is part of FreeDESK
3:
4: FreeDESK is (C) Copyright 2012 David Cutting
5:
6: FreeDESK is free software: you can redistribute it and/or modify
7: it under the terms of the GNU General Public License as published by
8: the Free Software Foundation, either version 3 of the License, or
9: (at your option) any later version.
10:
11: FreeDESK is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with FreeDESK. If not, see www.gnu.org/licenses
18:
19: For more information see www.purplepixie.org/freedesk/
20: -------------------------------------------------------------- */
21:
22: /**
23: * FreeDESK entity search client-side code
24: **/
25:
26: function FreeDESK_Search()
27: {
28: this.entity = ""; // search entity
29:
30: this.searchformid = "entitysearch"; // id of search form
31:
32: this.displayid = "searchresults"; // id of search results container
33:
34: this.autosid = true; // Automatically add the DESK.sid
35:
36: this.autoentity = true; // Automatically add the entity
37:
38: this.autoreset = true; // Reset on every search call
39:
40: this.automode = true; // automatically set entity_search mode
41:
42: this.callback = null; // parent form callback function
43:
44: this.callbackOnSingle = false; // call callback if only one result
45:
46: this.data = "";
47:
48: // Add an item to the data string
49: this.add = function(name, value)
50: {
51: if (value == undefined)
52: var value = "";
53:
54: this.data += (this.data.length > 0 ? "&" : ""); // add & if required
55:
56: this.data += escape(name).replace(/\+/g, "%2B") + "=";
57:
58: this.data += escape(value).replace(/\+/g, "%2B");
59: }
60:
61: // Add a string to the data string (pre-escaped), optional flag add & if required (default true)
62: this.addString = function(str, autoAmp)
63: {
64: if ( autoAmp == undefined || autoAmp )
65: this.data += (this.data.length > 0 ? "&" : "");
66:
67: this.data += str;
68: }
69:
70: // Reset
71: this.reset = function()
72: {
73: this.data = "";
74: }
75:
76:
77: // Load a Request List to the Main Pane
78: this.search = function(start, results)
79: {
80: if (this.autoreset)
81: this.reset();
82:
83: if(this.autoentity)
84: this.add("entity", this.entity);
85:
86: if (this.automode)
87: this.add("mode", "entity_search");
88:
89: if (start == undefined)
90: start = 0;
91: if (results == undefined)
92: results = 30;
93: var sr = new ServerRequest();
94:
95: // Load the data from the form
96: this.addString(DESK.formToQuery(this.searchformid));
97:
98: this.add("start", start);
99: this.add("limit", results);
100:
101: if (this.autosid)
102: this.add("sid", DESK.sid);
103:
104:
105: sr.callback = DESKSearch.searchResults;
106: sr.url = "api.php";
107:
108: sr.Post(this.data);
109: }
110:
111: // Display a request list in the main pane
112: this.searchResults = function(xml)
113: {
114: if (DESK.isError(xml))
115: {
116: alert(DESK.getError(xml));
117: return ;
118: }
119: var table = document.createElement("table"); // table for results
120: table.border=0;
121: table.width="100%";
122: table.className="searchList";
123:
124: var container = document.getElementById(DESKSearch.displayid);
125:
126: if (container.hasChildNodes())
127: {
128: while(container.childNodes.length >= 1)
129: container.removeChild(container.firstChild);
130: }
131:
132: container.appendChild(table);
133:
134: var meta = xml.getElementsByTagName("meta")[0];
135:
136: var start = parseInt(meta.getElementsByTagName("start")[0].firstChild.nodeValue);
137: var limit = parseInt(meta.getElementsByTagName("limit")[0].firstChild.nodeValue);
138: var count = parseInt(meta.getElementsByTagName("count")[0].firstChild.nodeValue);
139: var results = xml.getElementsByTagName("entity").length;
140: var keyfield = (meta.getElementsByTagName("keyfield")[0].textContent==undefined) ?
141: meta.getElementsByTagName("keyfield")[0].firstChild.nodeValue : meta.getElementsByTagName("keyfield")[0].textContent ;
142:
143: var display = "Displaying results "+(start+1)+" to "+(start+results)+" of "+count;
144: var dispdivtop = document.createElement("div");
145: var dispdivbot = document.createElement("div");
146: dispdivtop.innerHTML = display;
147: dispdivbot.innerHTML = display;
148: container.appendChild(dispdivtop);
149:
150: container.appendChild(table);
151:
152: var entities = xml.getElementsByTagName("entity");
153:
154: var fieldcount = 0;
155: var rowcount = 0;
156: var callbacknow = false;
157:
158: if (DESKSearch.callback != null && entities.length==1)
159: callbacknow=true;
160:
161: for (var i=0; i162: {
163: var keyfieldval = "";
164: var entity = entities[i];
165: var row = table.insertRow(table.getElementsByTagName("tr").length);
166: row.className="row"+rowcount;
167: if ( ++rowcount > 1)
168: rowcount = 0;
169: var fields = entity.getElementsByTagName("field");
170:
171: fieldcount=0;
172:
173: for (var z=0; z174: {
175: var id = fields[z].attributes.getNamedItem("id").value;
176: var cell = row.insertCell(z);
177: var data = (fields[z].textContent == undefined) ? fields[z].firstChild.nodeValue : fields[z].textContent;
178: if (id == keyfield)
179: {
180: keyfieldval = data;
181: if (DESKSearch.callback != null)
182: {
183: data = ""+keyfieldval+"";
184: if (callbacknow)
185: {
186: DESKSearch.callback(keyfieldval);
187: window.close();
188: }
189: }
190: }
191: cell.innerHTML = data;
192: ++fieldcount;
193: }
194:
195: var edit = "Edit";
196: var cell = row.insertCell(-1);
197: cell.innerHTML = edit;
198: }
199:
200: var prevCell = " ";
201:
202: if (start>0)
203: {
204: prevCell="<< Previous";
205: }
206:
207: var nextCell = " ";
208:
209: if ( (start+limit) < count )
210: {
211: nextCell=">> Next";
212: }
213:
214: var spanWidth = fieldcount-1; // allowing for the edit
215:
216: var row = table.insertRow(0);
217: var rowb = table.insertRow(-1);
218: var cell = row.insertCell(0);
219: var cellb = rowb.insertCell(0);
220: cell.innerHTML = prevCell;
221: cellb.innerHTML = prevCell;
222:
223: for (var i=0; i224: {
225: cell = row.insertCell(i+1);
226: cellb = rowb.insertCell(i+1);
227: cell.innerHTML = " ";
228: cellb.innerHTML = " ";
229: }
230: cell=row.insertCell(spanWidth+1);
231: cellb=rowb.insertCell(spanWidth+1);
232:
233: cell.innerHTML = nextCell;
234: cellb.innerHTML = nextCell;
235:
236: container.appendChild(dispdivbot);
237:
238: }
239:
240: }
241:
242:
243: var DESKSearch = new FreeDESK_Search();
244:
245: