2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.3 |
4 +--------------------------------------------------------------------+
5 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
25 * Copyright (C) 2009-2010 Xavier Dutoit
26 * Licensed to CiviCRM under the Academic Free License version 3.0.
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();
37 var tplURL
= '/civicrm/example?placeholder';
38 var urlInitted
= false;
39 CRM
.url = function (p
, params
) {
46 console
&& console
.log
&& console
.log('Warning: CRM.url called before initialization');
48 params
= params
|| '';
49 var frag
= p
.split ('?');
50 var url
= tplURL
.replace("civicrm/example", frag
[0]);
52 if (typeof(params
) == 'string') {
53 url
= url
.replace("placeholder", params
);
56 url
= url
.replace("placeholder", $.param(params
));
59 url
+= (url
.indexOf('?') === (url
.length
- 1) ? '' : '&') + frag
[1];
61 // remove trailing "?"
62 if (url
.indexOf('?') === (url
.length
- 1)) {
63 url
= url
.slice(0, (url
.length
- 1));
68 // Backwards compatible with jQuery fn
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
);
76 $.fn
.crmURL = function () {
77 return this.each(function() {
79 this.href
= CRM
.url(this.href
);
87 CRM
.api = function(entity
, action
, params
, options
) {
92 success: function(result
, settings
) {
95 error: function(result
, settings
) {
96 $().crmError(result
.error_message
, ts('Error'));
99 callBack: function(result
, settings
) {
100 if (result
.is_error
== 1) {
101 return settings
.error
.call(this, result
, settings
);
103 return settings
.success
.call(this, result
, settings
);
105 ajaxURL
: 'civicrm/ajax/rest',
107 action
= action
.toLowerCase();
108 // Default success handler
114 settings
.success = function() {
115 CRM
.alert('', ts('Saved'), 'success');
120 settings
.success = function() {
121 CRM
.alert('', ts('Removed'), 'success');
125 for (var i
in params
) {
126 if (i
.slice(0, 4) == 'api.' || typeof(params
[i
]) == 'Object') {
135 json
: JSON
.stringify(params
)
139 params
.entity
= entity
;
140 params
.action
= action
;
143 // Pass copy of settings into closure to preserve its value during multiple requests
146 url
: stg
.ajaxURL
.indexOf('http') === 0 ? stg
.ajaxURL
: CRM
.url(stg
.ajaxURL
),
149 type
: action
.indexOf('get') < 0 ? 'POST' : 'GET',
150 success: function(result
) {
151 stg
.callBack
.call(stg
.context
, result
, stg
);
154 })($.extend({}, settings
, options
));
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.');
160 return CRM
.api
.call(this, entity
, action
, params
, options
);
163 $.fn
.crmAutocomplete = function (params
, options
) {
164 if (typeof params
== 'undefined') params
= {};
165 if (typeof options
== 'undefined') options
= {};
166 params
= $().extend({
174 options
= $().extend({}, {
176 skip
: ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'],
177 result: function(data
){
180 formatItem: function(data
,i
,max
,value
,term
){
183 if ($.inArray (attr
, options
.skip
) == -1 && data
[attr
]) {
184 tmp
.push(data
[attr
]);
187 return tmp
.join(' :: ');
189 parse: function (data
){
190 var acd
= new Array();
191 for(cid
in data
.values
){
192 delete data
.values
[cid
]["data"];// to be removed once quicksearch doesn't return data
193 acd
.push({ data
:data
.values
[cid
], value
:data
.values
[cid
].sort_name
, result
:data
.values
[cid
].sort_name
});
202 var contactUrl
= CRM
.url('civicrm/ajax/rest', params
);
204 return this.each(function() {
206 if (typeof $.fn
.autocomplete
!= 'function')
207 $.fn
.autocomplete
= cj
.fn
.autocomplete
;//to work around the fubar cj
209 extraP
[options
.field
] = function () {return $(selector
).val();};
210 $(this).autocomplete( contactUrl
, {
212 formatItem: function(data
,i
,max
,value
,term
){
213 return options
.formatItem(data
,i
,max
,value
,term
);
215 parse: function(data
){ return options
.parse(data
);},
216 width
: options
.width
,
220 minChars
:options
.minChars
,
222 }).result(function(event
, data
, formatted
) {
223 options
.result(data
);