2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
39 {if !isset($config.enable_autocomplete) || $config.enable_autocomplete==false}
41 <input type="hidden" id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}_multiselect"
42 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}_multiselect" value="true">
43 {multienum_to_array string={{sugarvar key='value' string=true}} default={{sugarvar key='default' string=true}} assign="values"}
44 <select id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}"
45 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}[]"
46 multiple="true" size='{{$displayParams.size|default:6}}' style="width:150" title='{{$vardef.help}}' tabindex="{{$tabindex}}" {{$displayParams.field}} >
47 {html_options options={{sugarvar key='options' string=true}} selected=$values}
52 {assign var="field_options" value={{sugarvar key='options' string="true"}} }
53 {capture name="field_val"}{{sugarvar key='value'}}{/capture}
54 {assign var="field_val" value=$smarty.capture.field_val}
55 {capture name="ac_key"}{{sugarvar key='name'}}{/capture}
56 {assign var="ac_key" value=$smarty.capture.ac_key}
58 {{if empty($vardef.autocomplete_ajax)}}
59 <input type="hidden" id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}_multiselect"
60 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}_multiselect" value="true">
61 {multienum_to_array string={{sugarvar key='value' string=true}} default={{sugarvar key='default' string=true}} assign="values"}
62 <select style='display:none' id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}"
63 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}[]"
64 multiple="true" size='{{$displayParams.size|default:6}}' style="width:150" title='{{$vardef.help}}' tabindex="{{$tabindex}}" {{$displayParams.field}}>
65 {html_options options={{sugarvar key='options' string=true}} selected=$values}
69 id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input"
70 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input"
72 type="text" style="vertical-align: top;">
75 id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}"
76 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}"
77 value="{{sugarvar key='value'}}">
80 id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input"
81 name="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input"
83 type="text" style="vertical-align: top;">
86 <span class="id-ff multiple">
87 <button type="button">
88 <img src="{sugar_getimagepath file="id-ff-down.png"}" id="{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-image">
91 id="btn-clear-{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input"
93 onclick="SUGAR.clearRelateField(this.form, '{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input', '{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}};');SUGAR.AutoComplete.{$ac_key}.inputNode.updateHidden()"><img src="{sugar_getimagepath file="id-ff-clear.png"}"></button>
98 SUGAR.AutoComplete.{/literal}{$ac_key}{literal} = [];
101 {{if empty($vardef.autocomplete_ajax)}}
103 YUI().use('datasource', 'datasource-jsonschema', function (Y) {
104 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.ds = new Y.DataSource.Function({
105 source: function (request) {
106 var selectElem = document.getElementById("{/literal}{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}{literal}");
108 for (i=0;i<selectElem.options.length;i++)
109 if (!(selectElem.options[i].value=='' && selectElem.options[i].innerHTML==''))
110 ret.push({'key':selectElem.options[i].value,'text':selectElem.options[i].innerHTML});
118 // Create a new YUI instance and populate it with the required modules.
119 YUI().use('datasource', 'datasource-jsonschema', function (Y) {
120 // DataSource is available and ready for use.
121 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.ds = new Y.DataSource.Get({
122 source: 'index.php?module=Accounts&action=ajaxautocomplete&to_pdf=1'
124 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.ds.plug(Y.Plugin.DataSourceJSONSchema, {
126 resultListLocator: "option_items",
127 resultFields: ["text", "key"],
136 YUI().use("autocomplete", "autocomplete-filters", "autocomplete-highlighters","node-event-simulate", function (Y) {
139 SUGAR.AutoComplete.{$ac_key}.inputNode = Y.one('#{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input');
140 SUGAR.AutoComplete.{$ac_key}.inputImage = Y.one('#{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-image');
141 SUGAR.AutoComplete.{$ac_key}.inputHidden = Y.one('#{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}');
143 {{if empty($vardef.autocomplete_ajax)}}
144 SUGAR.AutoComplete.{$ac_key}.minQLen = 0;
145 SUGAR.AutoComplete.{$ac_key}.queryDelay = 0;
146 SUGAR.AutoComplete.{$ac_key}.numOptions = {$field_options|@count};
147 if(SUGAR.AutoComplete.{$ac_key}.numOptions >= 300) {literal}{
149 SUGAR.AutoComplete.{$ac_key}.minQLen = 1;
150 SUGAR.AutoComplete.{$ac_key}.queryDelay = 200;
154 if(SUGAR.AutoComplete.{$ac_key}.numOptions >= 3000) {literal}{
156 SUGAR.AutoComplete.{$ac_key}.minQLen = 1;
157 SUGAR.AutoComplete.{$ac_key}.queryDelay = 500;
162 SUGAR.AutoComplete.{$ac_key}.minQLen = 1;
163 SUGAR.AutoComplete.{$ac_key}.queryDelay = 500;
166 {{if empty($vardef.autocomplete_ajax)}}
168 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.plug(Y.Plugin.AutoComplete, {
169 activateFirstItem: true,
170 allowTrailingDelimiter: true,
172 minQueryLength: SUGAR.AutoComplete.{$ac_key}.minQLen,
173 queryDelay: SUGAR.AutoComplete.{$ac_key}.queryDelay,
177 {{if !empty($vardef.autocomplete_ajax)}}
178 requestTemplate: '&options={{$vardef.autocomplete_options}}&q={literal}{query}{/literal}',
181 source: SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.ds,
183 resultTextLocator: 'text',
184 resultHighlighter: 'phraseMatch',
185 resultFilters: 'phraseMatch',
186 // Chain together a startsWith filter followed by a custom result filter
187 // that only displays tags that haven't already been selected.
188 resultFilters: ['phraseMatch', function (query, results) {
189 // Split the current input value into an array based on comma delimiters.
190 var selected = SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.get('value').split(/\s*,\s*/);
192 // Convert the array into a hash for faster lookups.
193 selected = Y.Array.hash(selected);
195 // Filter out any results that are already selected, then return the
196 // array of filtered results.
197 return Y.Array.filter(results, function (result) {
198 return !selected.hasOwnProperty(result.text);
202 {/literal}{{else}}{literal}
203 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.plug(Y.Plugin.AutoComplete, {
204 activateFirstItem: true,
205 allowTrailingDelimiter: true,
207 minQueryLength: SUGAR.AutoComplete.{$ac_key}.minQLen,
208 queryDelay: SUGAR.AutoComplete.{$ac_key}.queryDelay,
211 requestTemplate: '&options={{$vardef.autocomplete_options}}&q={literal}{query}{/literal}',
213 source: SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.ds,
215 resultTextLocator: 'text',
216 resultHighlighter: 'phraseMatch',
217 resultFilters: 'phraseMatch',
218 // Chain together a startsWith filter followed by a custom result filter
219 // that only displays tags that haven't already been selected.
220 resultFilters: ['phraseMatch', function (query, results) {
221 // Split the current input value into an array based on comma delimiters.
222 var selected = SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.get('value').split(/\s*,\s*/);
224 // Pop the last item off the array, since it represents the current query
225 // and we don't want to filter it out.
228 // Convert the array into a hash for faster lookups.
229 selected = Y.Array.hash(selected);
231 // Filter out any results that are already selected, then return the
232 // array of filtered results.
233 return Y.Array.filter(results, function (result) {
234 return !selected.hasOwnProperty(result.text);
238 {/literal}{{/if}}{literal}
239 if({/literal}SUGAR.AutoComplete.{$ac_key}.minQLen{literal} == 0){
240 // expand the dropdown options upon focus
241 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('focus', function () {
242 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.sendRequest('');
246 {{if empty($vardef.autocomplete_ajax)}}
247 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateHidden = function() {
248 var index_array = SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.get('value').split(/\s*,\s*/);
250 var selectElem = document.getElementById("{/literal}{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}{literal}");
253 for (i=0;i<selectElem.options.length;i++){
254 if (selectElem.options[i].selected)
255 oTable[selectElem.options[i].value] = true;
258 for (i=0;i<selectElem.options.length;i++){
259 selectElem.options[i].selected=false;
264 for (i=0;i<index_array.length;i++){
265 var key = index_array[i];
266 for (c=0;c<selectElem.options.length;c++)
267 if (selectElem.options[c].innerHTML == key){
268 selectElem.options[c].selected=true;
269 nTable[selectElem.options[c].value]=1;
273 //the following two loops check to see if the selected options are different from before.
274 //oTable holds the original select. nTable holds the new one
279 if (!oTable.hasOwnProperty(n))
280 fireEvent = true; //the options are different, fire the event
286 if (!nTable.hasOwnProperty(o))
287 fireEvent=true; //the options are different, fire the event
290 //if the selected options are different from before, fire the 'change' event
292 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('change');
296 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText = function() {
297 //get last option typed into the input widget
298 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.set(SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.get('value'));
299 var index_array = SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.get('value').split(/\s*,\s*/);
300 //create a string ret_string as a comma-delimited list of text from selectElem options.
301 var selectElem = document.getElementById("{/literal}{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}{literal}");
304 if (selectElem==null || selectElem.options == null)
307 for (i=0;i<selectElem.options.length;i++){
308 if (selectElem.options[i].selected && selectElem.options[i].innerHTML!=''){
309 ret_array.push(selectElem.options[i].innerHTML);
313 //index array - array from input
314 //ret array - array from select
316 var sorted_array = [];
317 var notsorted_array = [];
318 for (i=0;i<index_array.length;i++){
319 for (c=0;c<ret_array.length;c++){
320 if (ret_array[c]==index_array[i]){
321 sorted_array.push(ret_array[c]);
322 ret_array.splice(c,1);
326 ret_string = ret_array.concat(sorted_array).join(', ');
327 if (ret_string.match(/^\s*$/))
332 //update the input widget
333 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.set('value', ret_string);
336 function updateTextOnInterval(){
337 if (document.activeElement != document.getElementById("{/literal}{{if empty($displayParams.idName)}}{{sugarvar key='name'}}{{else}}{{$displayParams.idName}}{{/if}}-input{literal}"))
338 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText();
339 setTimeout(updateTextOnInterval,100);
342 updateTextOnInterval();
344 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateHidden = function() {
345 var index_array = SUGAR.MultiEnumAutoComplete.getMultiSelectKeysFromValues("{/literal}{{$vardef.autocomplete_options}}{literal}", SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.get('value'));
346 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.set('value', index_array.join("^,^"));
349 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText = function() {
350 var index_array = SUGAR.MultiEnumAutoComplete.getMultiSelectValuesFromKeys("{/literal}{{$vardef.autocomplete_options}}{literal}", SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.get('value'));
351 if(index_array.length < 1){
352 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.set('value', '');
355 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.set('value', index_array.join(", ") + ", ");
358 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText();
361 {{if empty($vardef.autocomplete_ajax)}}
362 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('click', function(e) {
363 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('click');
366 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('dblclick', function(e) {
367 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('dblclick');
370 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('focus', function(e) {
371 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('focus');
374 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('mouseup', function(e) {
375 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('mouseup');
378 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('mousedown', function(e) {
379 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('mousedown');
382 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('blur', function(e) {
383 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputHidden.simulate('blur');
387 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.on('blur', function () {
388 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateHidden();
389 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText();
392 // when they click on the arrow image, toggle the visibility of the options
393 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputImage.on('click', function () {
394 if({/literal}SUGAR.AutoComplete.{$ac_key}.minQLen{literal} == 0){
395 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.sendRequest('');
396 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.show();
399 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.focus();
403 if({/literal}SUGAR.AutoComplete.{$ac_key}.minQLen{literal} == 0){
404 // After a tag is selected, send an empty query to update the list of tags.
405 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.after('select', function () {
406 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.sendRequest('');
407 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.show();
408 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateHidden();
409 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText();
412 // After a tag is selected, send an empty query to update the list of tags.
413 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.ac.after('select', function () {
414 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateHidden();
415 SUGAR.AutoComplete.{/literal}{$ac_key}{literal}.inputNode.updateText();