1c1dd91e |
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 |
89 | var json = false, |
90 | settings = { |
91 | context: null, |
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 | } |
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 |
144 | (function(stg) { |
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) { |
151 | stg.callBack.call(stg.context, result, stg); |
152 | } |
153 | }); |
154 | })($.extend({}, settings, options)); |
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.'); |
160 | return CRM.api.call(this, entity, action, params, options); |
161 | }; |
162 | |
163 | $.fn.crmAutocomplete = function (params, options) { |
164 | if (typeof params == 'undefined') params = {}; |
165 | if (typeof options == 'undefined') options = {}; |
166 | params = $().extend({ |
167 | rowCount:35, |
168 | json:1, |
169 | entity:'Contact', |
170 | action:'getquick', |
171 | sequential:1 |
172 | }, params); |
173 | |
174 | options = $().extend({}, { |
175 | field :'name', |
176 | skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'], |
177 | result: function(data){ |
178 | return false; |
179 | }, |
180 | formatItem: function(data,i,max,value,term){ |
181 | var tmp = []; |
182 | for (attr in data) { |
183 | if ($.inArray (attr, options.skip) == -1 && data[attr]) { |
184 | tmp.push(data[attr]); |
185 | } |
186 | } |
187 | return tmp.join(' :: '); |
188 | }, |
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 }); |
194 | } |
195 | return acd; |
196 | }, |
197 | delay:100, |
198 | width:250, |
199 | minChars:1 |
200 | }, options |
201 | ); |
202 | var contactUrl = CRM.url('civicrm/ajax/rest', params); |
203 | |
204 | return this.each(function() { |
205 | var selector = this; |
206 | if (typeof $.fn.autocomplete != 'function') |
207 | $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj |
208 | var extraP = {}; |
209 | extraP [options.field] = function () {return $(selector).val();}; |
210 | $(this).autocomplete( contactUrl, { |
211 | extraParams:extraP, |
212 | formatItem: function(data,i,max,value,term){ |
213 | return options.formatItem(data,i,max,value,term); |
214 | }, |
215 | parse: function(data){ return options.parse(data);}, |
216 | width: options.width, |
217 | delay:options.delay, |
218 | max:25, |
219 | dataType:'json', |
220 | minChars:options.minChars, |
221 | selectFirst: true |
222 | }).result(function(event, data, formatted) { |
223 | options.result(data); |
224 | }); |
225 | }); |
226 | } |
227 | |
228 | })(jQuery, CRM); |