5a920362 |
1 | /* |
2 | +--------------------------------------------------------------------+ |
3 | | CiviCRM version 4.2 | |
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 | |
30 | /* |
31 | TO BE VERIFIED |
32 | If you do not use clean urls on drupal, you have to define a variable to set the url of the server to be used for the rest |
33 | <script type="text/javascript"> |
34 | var options {ajaxURL:"{$config->userFrameworkResourceURL}"; |
35 | </script> |
36 | |
37 | |
38 | */ |
39 | |
40 | (function($){ |
41 | var defaults = { |
42 | success: function(result,settings){ |
43 | var successMsg = 'Saved <a href="#" id="closerestmsg">'+ settings.closetxt +'</a>'; |
44 | $(settings.msgbox).addClass('msgok').html( successMsg ).show(); |
45 | $("#closerestmsg").click(function(){$(settings.msgbox).fadeOut("slow");return false;}); |
46 | return true; |
47 | }, |
48 | error: function(result,settings){ |
49 | if ($(settings.msgbox).length>0) { |
50 | $(settings.msgbox).addClass('msgnok').html(result.error_message); |
51 | } else { |
52 | alert (result.error_message); |
53 | } |
54 | return false; |
55 | }, |
56 | callBack: function(result,settings){ |
57 | if (result.is_error == 1) { |
58 | return settings.error.call(this,result,settings); |
59 | return false; |
60 | } |
61 | return settings.success.call(this,result,settings); |
62 | }, |
63 | closetxt: "<div class='icon close-icon' title='Close'>[X]</div>", |
64 | ajaxURL: "/civicrm/ajax/rest", |
65 | msgbox: '#restmsg' |
66 | }; |
67 | |
68 | $.fn.crmAPI = function(entity,action,params,options) { |
69 | // params ['fnName'] = "civicrm/"+entity+"/"+action; |
70 | params ['entity'] = entity; |
71 | params ['action'] = action; |
72 | params ['json'] = 1; |
73 | var settings = $.extend({}, defaults, options); |
74 | $(settings.msgbox).removeClass('msgok').removeClass('msgnok').html(""); |
75 | $.ajax({ |
76 | url: settings.ajaxURL, |
77 | dataType: 'json', |
78 | data: params, |
79 | type:'POST', |
80 | context:this, |
81 | success: function(result) { |
82 | settings.callBack.call(this,result,settings); |
83 | } |
84 | }); |
85 | }; |
86 | |
87 | $.fn.crmAutocomplete = function (params,options) { |
88 | if (typeof params == 'undefined') params = {}; |
89 | if (typeof options == 'undefined') options = {}; |
90 | params = $().extend( { |
91 | rowCount:35, |
92 | json:1, |
93 | entity:'Contact', |
94 | action:'getquick', |
95 | sequential:1 |
96 | },params); |
97 | //'return':'sort_name,email' |
98 | |
99 | options = $().extend({}, { |
100 | field :'name', |
101 | skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'], |
102 | result: function(data){ |
103 | console.log(data); |
104 | return false; |
105 | }, |
106 | formatItem: function(data,i,max,value,term){ |
107 | var tmp = []; |
108 | for (attr in data) { |
109 | if ($.inArray (attr, options.skip) == -1 && data[attr]) { |
110 | tmp.push(data[attr]); |
111 | } |
112 | } |
113 | return tmp.join(' :: '); |
114 | }, |
115 | parse: function (data){ |
116 | var acd = new Array(); |
117 | for(cid in data.values){ |
118 | delete data.values[cid]["data"];// to be removed once quicksearch doesn't return data |
119 | acd.push({ data:data.values[cid], value:data.values[cid].sort_name, result:data.values[cid].sort_name }); |
120 | } |
121 | return acd; |
122 | }, |
123 | delay:100, |
124 | minChars:1 |
125 | },options |
126 | ); |
127 | var contactUrl = defaults.ajaxURL + "?"+ $.param(params); |
128 | |
129 | // contactUrl = contactUrl + "fnName=civicrm/contact/search&json=1&"; |
130 | //var contactUrl = "/civicrm/ajax/rest?fnName=civicrm/contact/search&json=1&return[sort_name]=1&return[email]&rowCount=25"; |
131 | |
132 | return this.each(function() { |
133 | var selector = this; |
134 | if (typeof $.fn.autocomplete != 'function') |
135 | $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj |
136 | var extraP = {}; |
137 | extraP [options.field] = function () {return $(selector).val();}; |
138 | $(this).autocomplete( contactUrl, { |
139 | extraParams:extraP, |
140 | formatItem: function(data,i,max,value,term){ |
141 | return options.formatItem(data,i,max,value,term); |
142 | }, |
143 | parse: function(data){ return options.parse(data);}, |
144 | width: 250, |
145 | delay:options.delay, |
146 | max:25, |
147 | dataType:'json', |
148 | minChars:options.minChars, |
149 | selectFirst: true |
150 | }).result(function(event, data, formatted) { |
151 | options.result(data); |
152 | }); |
153 | }); |
154 | } |
155 | |
156 | //display a message or (message=false) clear the notification |
157 | $.fn.crmNotification = function (message,type,item) { |
158 | if (message === false) { |
159 | if (typeof $.noty == "function") { |
160 | $.noty.closeAll(); |
161 | } |
162 | return; |
163 | } |
164 | item = typeof item !== 'undefined' ? item : null; |
165 | type = typeof type !== 'undefined' ? type : 'error'; |
166 | if (typeof $.noty == "function") |
167 | $.noty({"text":message,"layout":"top","type":type,"animateOpen":{"height":"toggle"},"animateClose":{"height":"toggle"},"speed":500,"timeout":false,"closeButton":true,"closeOnSelfClick":true,"closeOnSelfOver":true,"modal":false}); |
168 | else { |
169 | alert (message); |
170 | } |
171 | item && console && console.log && console.log (item); |
172 | } |
173 | |
174 | /* you need to init this function first: cj.crmURL ('init', '{crmURL p="civicrm/example" q="placeholder"}'); |
175 | * then you can call it almost like {crmAPI} but on the client side, eg: var url = cj.crmURL ('civicrm/contact/view', {reset:1,cid:42}); |
176 | * or $('a.crmURL').crmURL(); |
177 | */ |
178 | $.extend ({ 'crmURL': |
179 | function (p, params) { |
180 | if (p == "init") { |
181 | $(document).data('civicrm_templateURL',params); // storage and avoid polluting the global namespace |
182 | return; |
183 | } |
184 | var tplURL = $(document).data('civicrm_templateURL'); |
185 | if (!tplURL) { |
186 | console && console.log && console.log ("you need to init crmURL first"); |
187 | return; // should we alert() or set to drupal clean (/civicrm/bla/bla?param)? |
188 | } |
189 | var t= tplURL.replace("civicrm/example",p); |
190 | if (typeof(params)=='string') { |
191 | if (t[0]=="/") |
192 | t= t.substring(1); |
193 | return t.replace("placeholder",params); |
194 | } else |
195 | return t.replace("placeholder",$.param(params)); |
196 | |
197 | }}); |
198 | |
199 | $.fn.crmURL = function (templateURL) { // you don't need to set templateURL each time, if you have init it with cj.crmURL ('init'); |
200 | if (!templateURL && $(document).data('civicrm_templateURL')) |
201 | templateURL = $(document).data('civicrm_templateURL'); |
202 | return this.each(function() { |
203 | var $this = $(this); |
204 | if (this.href) { |
205 | var frag = $this.attr('href').split ('?'); |
206 | if (frag[1]) |
207 | this.href=$.crmURL (frag[0],frag[1]); |
208 | else |
209 | this.href=$.crmURL (frag[0]); |
210 | } |
211 | }); |
212 | }; |
213 | |
214 | })(jQuery); |
215 | //})(cj); |