Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | /* |
2 | +--------------------------------------------------------------------+ | |
3 | | CiviCRM version 4.3 | | |
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 | */ | |
87 | CRM.api = function(entity, action, params, options) { | |
88 | // Default settings | |
e32d748a CW |
89 | var json = false, |
90 | settings = { | |
91 | context: null, | |
6a488035 TO |
92 | success: function(result, settings) { |
93 | return true; | |
94 | }, | |
95 | error: function(result, settings) { | |
96 | $().crmError(result.error_message, ts('Error')); | |
97 | return false; | |
98 | }, | |
99 | callBack: function(result, settings) { | |
100 | if (result.is_error == 1) { | |
101 | return settings.error.call(this, result, settings); | |
102 | } | |
103 | return settings.success.call(this, result, settings); | |
104 | }, | |
105 | ajaxURL: 'civicrm/ajax/rest', | |
106 | }; | |
107 | action = action.toLowerCase(); | |
108 | // Default success handler | |
109 | switch (action) { | |
110 | case "update": | |
111 | case "create": | |
112 | case "setvalue": | |
113 | case "replace": | |
114 | settings.success = function() { | |
115 | CRM.alert('', ts('Saved'), 'success'); | |
116 | return true; | |
117 | }; | |
118 | break; | |
119 | case "delete": | |
120 | settings.success = function() { | |
121 | CRM.alert('', ts('Removed'), 'success'); | |
122 | return true; | |
123 | }; | |
124 | } | |
6a488035 TO |
125 | for (var i in params) { |
126 | if (i.slice(0, 4) == 'api.' || typeof(params[i]) == 'Object') { | |
127 | json = true; | |
128 | break; | |
129 | } | |
130 | } | |
131 | if (json) { | |
132 | params = { | |
133 | entity: entity, | |
134 | action: action, | |
135 | json: JSON.stringify(params) | |
136 | }; | |
137 | } | |
138 | else { | |
139 | params.entity = entity; | |
140 | params.action = action; | |
141 | params.json = 1; | |
142 | } | |
143 | // Pass copy of settings into closure to preserve its value during multiple requests | |
e32d748a | 144 | (function(stg) { |
6a488035 TO |
145 | $.ajax({ |
146 | url: stg.ajaxURL.indexOf('http') === 0 ? stg.ajaxURL : CRM.url(stg.ajaxURL), | |
147 | dataType: 'json', | |
148 | data: params, | |
149 | type: action.indexOf('get') < 0 ? 'POST' : 'GET', | |
150 | success: function(result) { | |
e32d748a | 151 | stg.callBack.call(stg.context, result, stg); |
6a488035 TO |
152 | } |
153 | }); | |
e32d748a | 154 | })($.extend({}, settings, options)); |
6a488035 TO |
155 | }; |
156 | ||
157 | // Backwards compatible with jQuery fn | |
158 | $.fn.crmAPI = function(entity, action, params, options) { | |
159 | console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.'); | |
fbc3a95a | 160 | return CRM.api.call(this, entity, action, params, options); |
6a488035 TO |
161 | }; |
162 | ||
54cb3deb TO |
163 | /** |
164 | * FIXME: This function is not documented, is not used anywhere in the codebase, and doesn't | |
165 | * clearly differentiate field elements which store the "contact id" versus the "contact label". | |
166 | * My guess is that it's designed more for "quick-search" and less for "CRUD forms". | |
167 | * | |
168 | * @param params | |
169 | * @param options | |
170 | * @return {*} | |
171 | */ | |
6a488035 TO |
172 | $.fn.crmAutocomplete = function (params, options) { |
173 | if (typeof params == 'undefined') params = {}; | |
174 | if (typeof options == 'undefined') options = {}; | |
e32d748a | 175 | params = $().extend({ |
6a488035 TO |
176 | rowCount:35, |
177 | json:1, | |
178 | entity:'Contact', | |
e32d748a | 179 | action:'getquick', |
6a488035 TO |
180 | sequential:1 |
181 | }, params); | |
182 | ||
183 | options = $().extend({}, { | |
184 | field :'name', | |
185 | skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'], | |
186 | result: function(data){ | |
187 | return false; | |
188 | }, | |
189 | formatItem: function(data,i,max,value,term){ | |
190 | var tmp = []; | |
191 | for (attr in data) { | |
192 | if ($.inArray (attr, options.skip) == -1 && data[attr]) { | |
193 | tmp.push(data[attr]); | |
194 | } | |
195 | } | |
196 | return tmp.join(' :: '); | |
197 | }, | |
198 | parse: function (data){ | |
199 | var acd = new Array(); | |
200 | for(cid in data.values){ | |
201 | delete data.values[cid]["data"];// to be removed once quicksearch doesn't return data | |
202 | acd.push({ data:data.values[cid], value:data.values[cid].sort_name, result:data.values[cid].sort_name }); | |
203 | } | |
204 | return acd; | |
205 | }, | |
206 | delay:100, | |
207 | width:250, | |
208 | minChars:1 | |
209 | }, options | |
210 | ); | |
211 | var contactUrl = CRM.url('civicrm/ajax/rest', params); | |
212 | ||
213 | return this.each(function() { | |
214 | var selector = this; | |
215 | if (typeof $.fn.autocomplete != 'function') | |
216 | $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj | |
217 | var extraP = {}; | |
218 | extraP [options.field] = function () {return $(selector).val();}; | |
219 | $(this).autocomplete( contactUrl, { | |
220 | extraParams:extraP, | |
221 | formatItem: function(data,i,max,value,term){ | |
222 | return options.formatItem(data,i,max,value,term); | |
223 | }, | |
224 | parse: function(data){ return options.parse(data);}, | |
225 | width: options.width, | |
226 | delay:options.delay, | |
227 | max:25, | |
228 | dataType:'json', | |
229 | minChars:options.minChars, | |
230 | selectFirst: true | |
231 | }).result(function(event, data, formatted) { | |
232 | options.result(data); | |
233 | }); | |
234 | }); | |
235 | } | |
236 | ||
237 | })(jQuery, CRM); |