Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | /* |
2 | +--------------------------------------------------------------------+ | |
232624b1 | 3 | | CiviCRM version 4.4 | |
6a488035 TO |
4 | +--------------------------------------------------------------------+ |
5 | | This file is a part of CiviCRM. | | |
6 | | | | |
7 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
8 | | under the terms of the GNU Affero General Public License | | |
9 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
10 | | | | |
11 | | CiviCRM is distributed in the hope that it will be useful, but | | |
12 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
14 | | See the GNU Affero General Public License for more details. | | |
15 | | | | |
16 | | You should have received a copy of the GNU Affero General Public | | |
17 | | License and the CiviCRM Licensing Exception along | | |
18 | | with this program; if not, contact CiviCRM LLC | | |
19 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
20 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
21 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
22 | +--------------------------------------------------------------------+ | |
23 | */ | |
24 | /* | |
25 | * Copyright (C) 2009-2010 Xavier Dutoit | |
26 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
27 | */ | |
28 | ||
29 | var CRM = CRM || {}; | |
30 | ||
31 | (function($, CRM) { | |
32 | /** | |
33 | * Almost like {crmURL} but on the client side | |
34 | * eg: var url = CRM.url('civicrm/contact/view', {reset:1,cid:42}); | |
35 | * or: $('a.my-link').crmURL(); | |
36 | */ | |
37 | var tplURL = '/civicrm/example?placeholder'; | |
38 | var urlInitted = false; | |
39 | CRM.url = function (p, params) { | |
40 | if (p == "init") { | |
41 | tplURL = params; | |
42 | urlInitted = true; | |
43 | return; | |
44 | } | |
45 | if (!urlInitted) { | |
46 | console && console.log && console.log('Warning: CRM.url called before initialization'); | |
47 | } | |
48 | params = params || ''; | |
49 | var frag = p.split ('?'); | |
50 | var url = tplURL.replace("civicrm/example", frag[0]); | |
51 | ||
52 | if (typeof(params) == 'string') { | |
53 | url = url.replace("placeholder", params); | |
54 | } | |
55 | else { | |
56 | url = url.replace("placeholder", $.param(params)); | |
57 | } | |
58 | if (frag[1]) { | |
59 | url += (url.indexOf('?') === (url.length - 1) ? '' : '&') + frag[1]; | |
60 | } | |
61 | // remove trailing "?" | |
62 | if (url.indexOf('?') === (url.length - 1)) { | |
63 | url = url.slice(0, (url.length - 1)); | |
64 | } | |
65 | return url; | |
66 | }; | |
67 | ||
68 | // Backwards compatible with jQuery fn | |
69 | $.extend ({'crmURL': | |
70 | function (p, params) { | |
71 | console && console.log && console.log('Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.'); | |
72 | return CRM.url(p, params); | |
73 | } | |
74 | }); | |
75 | ||
76 | $.fn.crmURL = function () { | |
77 | return this.each(function() { | |
78 | if (this.href) { | |
79 | this.href = CRM.url(this.href); | |
80 | } | |
81 | }); | |
82 | }; | |
83 | ||
84 | /** | |
85 | * AJAX api | |
86 | */ | |
86caa4d7 CW |
87 | CRM.api3 = function(entity, action, params) { |
88 | if (typeof(entity) === 'string') { | |
89 | params = { | |
90 | entity: entity, | |
91 | action: action.toLowerCase(), | |
92 | json: JSON.stringify(params || {}) | |
93 | }; | |
94 | } else { | |
95 | params = { | |
96 | entity: 'api3', | |
97 | action: 'call', | |
98 | json: JSON.stringify(entity) | |
99 | } | |
100 | } | |
101 | return $.ajax({ | |
102 | url: CRM.url('civicrm/ajax/rest'), | |
103 | dataType: 'json', | |
104 | data: params, | |
105 | type: params.action.indexOf('get') < 0 ? 'POST' : 'GET' | |
106 | }); | |
107 | }; | |
108 | ||
109 | /** | |
110 | * @deprecated | |
111 | * AJAX api | |
112 | */ | |
6a488035 TO |
113 | CRM.api = function(entity, action, params, options) { |
114 | // Default settings | |
c68358c8 | 115 | var settings = { |
e32d748a | 116 | context: null, |
6a488035 TO |
117 | success: function(result, settings) { |
118 | return true; | |
119 | }, | |
120 | error: function(result, settings) { | |
121 | $().crmError(result.error_message, ts('Error')); | |
122 | return false; | |
123 | }, | |
124 | callBack: function(result, settings) { | |
125 | if (result.is_error == 1) { | |
126 | return settings.error.call(this, result, settings); | |
127 | } | |
128 | return settings.success.call(this, result, settings); | |
129 | }, | |
c68358c8 | 130 | ajaxURL: 'civicrm/ajax/rest' |
6a488035 TO |
131 | }; |
132 | action = action.toLowerCase(); | |
133 | // Default success handler | |
134 | switch (action) { | |
135 | case "update": | |
136 | case "create": | |
137 | case "setvalue": | |
138 | case "replace": | |
139 | settings.success = function() { | |
140 | CRM.alert('', ts('Saved'), 'success'); | |
141 | return true; | |
142 | }; | |
143 | break; | |
144 | case "delete": | |
145 | settings.success = function() { | |
146 | CRM.alert('', ts('Removed'), 'success'); | |
147 | return true; | |
148 | }; | |
149 | } | |
c68358c8 TO |
150 | params = { |
151 | entity: entity, | |
152 | action: action, | |
153 | json: JSON.stringify(params) | |
154 | }; | |
6a488035 | 155 | // Pass copy of settings into closure to preserve its value during multiple requests |
e32d748a | 156 | (function(stg) { |
6a488035 TO |
157 | $.ajax({ |
158 | url: stg.ajaxURL.indexOf('http') === 0 ? stg.ajaxURL : CRM.url(stg.ajaxURL), | |
159 | dataType: 'json', | |
160 | data: params, | |
161 | type: action.indexOf('get') < 0 ? 'POST' : 'GET', | |
162 | success: function(result) { | |
e32d748a | 163 | stg.callBack.call(stg.context, result, stg); |
6a488035 TO |
164 | } |
165 | }); | |
e32d748a | 166 | })($.extend({}, settings, options)); |
6a488035 TO |
167 | }; |
168 | ||
169 | // Backwards compatible with jQuery fn | |
170 | $.fn.crmAPI = function(entity, action, params, options) { | |
171 | console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.'); | |
fbc3a95a | 172 | return CRM.api.call(this, entity, action, params, options); |
6a488035 TO |
173 | }; |
174 | ||
54cb3deb TO |
175 | /** |
176 | * FIXME: This function is not documented, is not used anywhere in the codebase, and doesn't | |
177 | * clearly differentiate field elements which store the "contact id" versus the "contact label". | |
178 | * My guess is that it's designed more for "quick-search" and less for "CRUD forms". | |
179 | * | |
180 | * @param params | |
181 | * @param options | |
182 | * @return {*} | |
183 | */ | |
6a488035 TO |
184 | $.fn.crmAutocomplete = function (params, options) { |
185 | if (typeof params == 'undefined') params = {}; | |
186 | if (typeof options == 'undefined') options = {}; | |
e32d748a | 187 | params = $().extend({ |
6a488035 TO |
188 | rowCount:35, |
189 | json:1, | |
190 | entity:'Contact', | |
e32d748a | 191 | action:'getquick', |
6a488035 TO |
192 | sequential:1 |
193 | }, params); | |
194 | ||
195 | options = $().extend({}, { | |
196 | field :'name', | |
197 | skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'], | |
198 | result: function(data){ | |
199 | return false; | |
200 | }, | |
201 | formatItem: function(data,i,max,value,term){ | |
202 | var tmp = []; | |
203 | for (attr in data) { | |
204 | if ($.inArray (attr, options.skip) == -1 && data[attr]) { | |
205 | tmp.push(data[attr]); | |
206 | } | |
207 | } | |
208 | return tmp.join(' :: '); | |
209 | }, | |
210 | parse: function (data){ | |
211 | var acd = new Array(); | |
212 | for(cid in data.values){ | |
213 | delete data.values[cid]["data"];// to be removed once quicksearch doesn't return data | |
214 | acd.push({ data:data.values[cid], value:data.values[cid].sort_name, result:data.values[cid].sort_name }); | |
215 | } | |
216 | return acd; | |
217 | }, | |
218 | delay:100, | |
219 | width:250, | |
220 | minChars:1 | |
221 | }, options | |
222 | ); | |
223 | var contactUrl = CRM.url('civicrm/ajax/rest', params); | |
224 | ||
225 | return this.each(function() { | |
226 | var selector = this; | |
227 | if (typeof $.fn.autocomplete != 'function') | |
228 | $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj | |
229 | var extraP = {}; | |
230 | extraP [options.field] = function () {return $(selector).val();}; | |
231 | $(this).autocomplete( contactUrl, { | |
232 | extraParams:extraP, | |
233 | formatItem: function(data,i,max,value,term){ | |
234 | return options.formatItem(data,i,max,value,term); | |
235 | }, | |
236 | parse: function(data){ return options.parse(data);}, | |
237 | width: options.width, | |
238 | delay:options.delay, | |
239 | max:25, | |
240 | dataType:'json', | |
241 | minChars:options.minChars, | |
242 | selectFirst: true | |
243 | }).result(function(event, data, formatted) { | |
244 | options.result(data); | |
245 | }); | |
246 | }); | |
247 | } | |
248 | ||
249 | })(jQuery, CRM); |