]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/quicksearch.js
Release 6.1.4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / quicksearch.js
1 /*********************************************************************************
2  * SugarCRM is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37
38
39 /**
40  * browse document for quickSearch fields
41  * Compatible ExtJS 1.1.1 and ExtJS 2.0
42  * parameter : noReload - if set to true, enableQS will enable only
43  *             the new sqsEnabled field on the page. If set to false
44  *             it will reload all the sqsEnabled fields.
45  */
46 function enableQS(noReload){
47         YAHOO.util.Event.onDOMReady(function(){
48         //Safety check.  If sqs_objects variable is null, we really can't do anything useful
49         if(typeof sqs_objects == 'undefined') {
50            return;
51         }
52         
53         var Dom = YAHOO.util.Dom;
54         
55         //Get all the fields where sqsEnabled is an attribue, these should be the input text fields for quicksearch
56         var qsFields = Dom.getElementsByClassName('sqsEnabled');
57         
58         //Now loop through all these fields and process them
59         for(qsField in qsFields){
60                 
61                 //Safety checks to skip processing of invalid entries
62                 if(typeof qsFields[qsField] == 'function' || typeof qsFields[qsField].id == 'undefined') {
63                    continue;
64                 }
65                 
66                 //Create the index we are using to search for the sqs_objects Array
67                 form_id = qsFields[qsField].form.getAttribute('id');
68                 
69                 //This is a special case where there is an element with id attribute value of "id"
70                 //In this case, we get the real_id attribute (occurs in modules/Import/tpls/step3.tpl only).
71                 if(typeof form_id == 'object' && qsFields[qsField].form.getAttribute('real_id')) {
72                         form_id = qsFields[qsField].form.getAttribute('real_id');
73                 }
74                 qs_index_id = form_id + '_' + qsFields[qsField].name;
75
76                 //Another safety check, if the sqs_objects entry is not defined, we can't do anything useful
77                 if(typeof sqs_objects[qs_index_id] == 'undefined') {
78                         qs_index_id = qsFields[qsField].name;
79                         if(typeof sqs_objects[qs_index_id] == 'undefined') {
80                                 continue;
81                         }
82                 }
83                 
84                 //Track if this field has already been processed.  The way the enableQS function is called
85                 //is a bit problematic in that it lends itself to a lot of duplicate processing
86                 if(QSProcessedFieldsArray[qs_index_id]) {
87                    continue;
88                 }                       
89                 
90                 //Store sqs_objects entry as a reference for convenience
91                 var qs_obj = sqs_objects[qs_index_id];
92                 //The loaded variable will be used to check whether or not the quick search field should be created
93             var loaded = false;   
94
95             if (!document.forms[qs_obj.form]) {
96                         continue;
97                 }
98             //Skip quicksearch fields that are readOnly or that are disabled since you can't search on them anyway
99             if (!document.forms[qs_obj.form].elements[qsFields[qsField].id].readOnly && qs_obj['disable'] != true) {
100                 combo_id = qs_obj.form + '_' + qsFields[qsField].id;
101                 if (Dom.get(combo_id + "_results")) {
102                         loaded = true
103                 }
104                 
105                 // if loaded == false, then we do the heavy lifting to re-create the quicksearch field
106                 if (!loaded) {
107                         QSProcessedFieldsArray[qs_index_id] = true;
108                         
109                         qsFields[qsField].form_id = form_id;
110                     
111                     var sqs = sqs_objects[qs_index_id];    
112                     
113                     //Initialize the result div
114                     var resultDiv = document.createElement('div');
115                     resultDiv.id = combo_id + "_results";
116                     Dom.insertAfter(resultDiv, qsFields[qsField]);
117                     
118                     //Add the module to the fields so we can read it from the response
119                     var fields = qs_obj.field_list.slice();
120                     fields[fields.length] = "module";
121                     
122                     //Create the DataSource for this QS
123                     var ds = new YAHOO.util.DataSource("index.php?", {
124                                         responseType: YAHOO.util.XHRDataSource.TYPE_JSON,
125                         responseSchema: {
126                                 resultsList: 'fields',
127                                     total: 'totalCount', 
128                                     fields: fields,
129                                     metaNode: "fields",
130                                     metaFields: {total: 'totalCount', fields:"fields"}
131                                         },
132                                         connMethodPost: true
133                             });
134                     
135                     // Don't force selection for search fields
136                     var forceSelect = !((qsFields[qsField].form && typeof(qsFields[qsField].form) == 'object' && qsFields[qsField].form.name == 'search_form')
137                                                         || qsFields[qsField].className.match('sqsNoAutofill') !=  null);
138                     
139                     //Finally Declare the Autocomplete
140                     var search = new YAHOO.widget.AutoComplete(qsFields[qsField], resultDiv, ds, {
141                         typeAhead: forceSelect,
142                                 forceSelection : forceSelect,
143                         fields: fields,
144                         sqs : sqs,
145                                                 animSpeed : 0.25,
146                         qs_obj: qs_obj,
147                         //YUI requires the data, even POST, to be URL encoded
148                         generateRequest : function(sQuery) {
149                                 var out = SUGAR.util.paramsToUrl({
150                                         to_pdf: 'true',
151                                     module: 'Home',
152                                     action: 'quicksearchQuery',
153                                     data: encodeURIComponent(YAHOO.lang.JSON.stringify(this.sqs)),
154                                     query: sQuery
155                                 });
156                                 return out;
157                             },
158                             //Method to fill in form fields with the returned results. 
159                             //Should be called on select, and must be called from the AC instance scope.
160                             setFields : function (data, filter) {
161                                 
162                                 for(var i in this.fields) {
163                                         
164                                         for (var key in this.qs_obj.field_list) {
165                                                 //Check that the field exists and matches the filter
166                                            if (this.fields[i] == this.qs_obj.field_list[key] && 
167                                                    document.forms[this.qs_obj.form].elements[this.qs_obj.populate_list[key]] &&
168                                                    this.qs_obj.populate_list[key].match(filter)) {
169                                                    document.forms[this.qs_obj.form].elements[this.qs_obj.populate_list[key]].value = data[i];
170                                            }
171                                        }
172                                 }
173                             },
174                             clearFields : function() {
175                                 for (var key in this.qs_obj.field_list) {
176                                     if (document.forms[this.qs_obj.form].elements[this.qs_obj.populate_list[key]]){
177                                             document.forms[this.qs_obj.form].elements[this.qs_obj.populate_list[key]].value = "";
178                                     }
179                                 }
180                                                         this.oldValue = "";
181                             }
182                     });
183                     
184                     if ( typeof(SUGAR.config.quicksearch_querydelay) != 'undefined' ) {
185                         search.queryDelay = SUGAR.config.quicksearch_querydelay;
186                     }
187                     
188                     //fill in the data fields on selection
189                     search.itemSelectEvent.subscribe(function(e, args){
190                         var data = args[2];
191                         var fields = this.fields;
192                         this.setFields(data, /\S/);
193                         
194                         //Handle special case where post_onblur_function is set    
195                         if (typeof(this.qs_obj['post_onblur_function']) != 'undefined') {
196                             collection_extended = new Array();
197                             for (var i in fields) {
198                                 for (var key in this.qs_obj.field_list) {
199                                     if (fields[i] == this.qs_obj.field_list[key]) {
200                                         collection_extended[this.qs_obj.field_list[key]] = data[i];
201                                     }
202                                 }
203                             }
204                             eval(this.qs_obj['post_onblur_function'] + '(collection_extended, this.qs_obj.id)');
205                         }
206                     });
207                                         
208                                         // We will get the old value firstly when the field lose focus.
209                     search.textboxFocusEvent.subscribe(function(){
210                         this.oldValue = this.getInputEl().value;
211                     });
212                     
213                     //If there is no change for this qucik search field , the value of it will not be cleared.
214                     search.selectionEnforceEvent.subscribe(function(e, args){
215                             if (this.oldValue != args[1]) {
216                                         this.clearFields();
217                             } else {
218                                 this.getInputEl().value = this.oldValue;
219                             }
220                     });
221                                         
222                                         search.dataReturnEvent.subscribe(function(e, args){
223                         //Selected the first returned value if a tab was done before results were returned
224                                                 if (this.getInputEl().value.length == 0 && args[2].length > 0) {
225                                                         var data = [];
226                                                         for(var key in this.qs_obj.field_list) {
227                                                                 data[data.length] = args[2][0][this.qs_obj.field_list[key]];
228                                                         }
229                                                         this.getInputEl().value = data[this.key];
230                                                         this.itemSelectEvent.fire(this, "", data);
231                                                 }
232                     });
233
234                     
235                     if (typeof QSFieldsArray[combo_id] == 'undefined' && qsFields[qsField].id) {
236                         QSFieldsArray[combo_id] = search;
237                     }
238                 }
239             }
240         }
241         });        
242 }
243
244 function registerSingleSmartInputListener(input) {
245     if ((c = input.className) && (c.indexOf("sqsEnabled") != -1)) {
246         enableQS(true);
247     }
248 }
249
250 if(typeof QSFieldsArray == 'undefined') {
251    QSFieldsArray = new Array();
252    QSProcessedFieldsArray = new Array();
253 }