2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
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
) {
91 success: function(result
, settings
) {
94 error: function(result
, settings
) {
95 $().crmError(result
.error_message
, ts('Error'));
98 callBack: function(result
, settings
) {
99 if (result
.is_error
== 1) {
100 return settings
.error
.call(this, result
, settings
);
102 return settings
.success
.call(this, result
, settings
);
104 ajaxURL
: 'civicrm/ajax/rest'
106 action
= action
.toLowerCase();
107 // Default success handler
113 settings
.success = function() {
114 CRM
.alert('', ts('Saved'), 'success');
119 settings
.success = function() {
120 CRM
.alert('', ts('Removed'), 'success');
127 json
: JSON
.stringify(params
)
129 // Pass copy of settings into closure to preserve its value during multiple requests
132 url
: stg
.ajaxURL
.indexOf('http') === 0 ? stg
.ajaxURL
: CRM
.url(stg
.ajaxURL
),
135 type
: action
.indexOf('get') < 0 ? 'POST' : 'GET',
136 success: function(result
) {
137 stg
.callBack
.call(stg
.context
, result
, stg
);
140 })($.extend({}, settings
, options
));
143 // Backwards compatible with jQuery fn
144 $.fn
.crmAPI = function(entity
, action
, params
, options
) {
145 console
&& console
.log
&& console
.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.');
146 return CRM
.api
.call(this, entity
, action
, params
, options
);
150 * FIXME: This function is not documented, is not used anywhere in the codebase, and doesn't
151 * clearly differentiate field elements which store the "contact id" versus the "contact label".
152 * My guess is that it's designed more for "quick-search" and less for "CRUD forms".
158 $.fn
.crmAutocomplete = function (params
, options
) {
159 if (typeof params
== 'undefined') params
= {};
160 if (typeof options
== 'undefined') options
= {};
161 params
= $().extend({
169 options
= $().extend({}, {
171 skip
: ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'],
172 result: function(data
){
175 formatItem: function(data
,i
,max
,value
,term
){
178 if ($.inArray (attr
, options
.skip
) == -1 && data
[attr
]) {
179 tmp
.push(data
[attr
]);
182 return tmp
.join(' :: ');
184 parse: function (data
){
185 var acd
= new Array();
186 for(cid
in data
.values
){
187 delete data
.values
[cid
]["data"];// to be removed once quicksearch doesn't return data
188 acd
.push({ data
:data
.values
[cid
], value
:data
.values
[cid
].sort_name
, result
:data
.values
[cid
].sort_name
});
197 var contactUrl
= CRM
.url('civicrm/ajax/rest', params
);
199 return this.each(function() {
201 if (typeof $.fn
.autocomplete
!= 'function')
202 $.fn
.autocomplete
= cj
.fn
.autocomplete
;//to work around the fubar cj
204 extraP
[options
.field
] = function () {return $(selector
).val();};
205 $(this).autocomplete( contactUrl
, {
207 formatItem: function(data
,i
,max
,value
,term
){
208 return options
.formatItem(data
,i
,max
,value
,term
);
210 parse: function(data
){ return options
.parse(data
);},
211 width
: options
.width
,
215 minChars
:options
.minChars
,
217 }).result(function(event
, data
, formatted
) {
218 options
.result(data
);