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) 2012 Xavier Dutoit | |
26 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
27 | * | |
28 | * | |
29 | * This offers two features: | |
30 | * - crmEditable() edit in place of a single field | |
31 | * (mostly a wrapper that binds jeditable features with the ajax api and replies on crm-entity crmf-{field} html conventions) | |
32 | * if you want to add an edit in place on a template: | |
33 | * - add a class crm-entity and id {EntityName}-{Entityid} higher in the dom | |
34 | * - add a class crm-editable and crmf-{FieldName} around the field (you can add a span if needed) | |
35 | * - add data-action=create if you need to specify the api action to call (default setvalue) | |
36 | * crmf- stands for crm field | |
37 | * - crmForm() | |
38 | * this embed a civicrm form and make it in place (load+ajaxForm) | |
39 | * to make it easier to customize the form (eg. hide a button...) it triggers a 'load' event on the form. you can then catch the load on your code (using the $('#id_of_the_form').on(function(){//do something | |
40 | */ | |
41 | ||
42 | ||
43 | (function($) { | |
44 | ||
45 | $.fn.crmEditable = function (options) { | |
46 | // for a jquery object (the crm-editable), find the entity name and id to apply the changes to | |
47 | // call result function(entity,id). The caller is responsible to use these params and do the needed | |
48 | var getEntityID = function (field,result) { | |
49 | var domid= $(field).closest('.crm-entity'); | |
50 | if (!domid) { | |
51 | console && console.log && console.log("Couldn't get the entity id. You need to set class='crm-entity' on a parent element of the field"); | |
52 | return false; | |
53 | } | |
54 | // trying to extract using the html5 data | |
55 | if (domid.data('entity')) { | |
56 | result (domid.data('entity'),domid.data('id')); | |
57 | return true; | |
58 | } | |
59 | domid=domid.attr('id'); | |
60 | if (!domid) { | |
61 | console && console.log && console.log("FATAL crm-editable: Couldn't get the entity id. You need to set class='crm-entity' id='{entityName}-{id}'"); | |
62 | return false; | |
63 | } | |
64 | var e=domid.match(/(\S*)-(\S*)/); | |
65 | if (!e) { | |
66 | console && console.log && console.log("Couldn't get the entity id. You need to set class='crm-entity' id='{entityName}-{id}'"); | |
67 | return false; | |
68 | } | |
69 | result(e[1],e[2]); | |
70 | return true; | |
71 | } | |
72 | // param in : a dom object that contains the field name as a class crmf-xxx | |
73 | var getFieldName = function (field) { | |
74 | if ($(field).data('field')) { | |
75 | return $(field).data('field'); | |
76 | } | |
77 | var fieldName=field.className.match(/crmf-(\S*)/)[1]; | |
78 | if (!fieldName) { | |
79 | console && console.log && console.log("Couldn't get the crm-editable field name to modify. You need to set crmf-{field_name} or data-{field_name}"); | |
80 | return false; | |
81 | } | |
82 | return fieldName; | |
83 | } | |
84 | ||
6a488035 TO |
85 | var checkable = function () { |
86 | $(this).change (function() { | |
87 | var params={sequential:1}; | |
88 | var entity = null; | |
89 | var checked = $(this).is(':checked'); | |
90 | if (!getEntityID (this,function (e,id) { | |
91 | entity=e; | |
92 | params.id = id; | |
93 | ||
94 | })) { return }; | |
95 | ||
96 | params['field']=getFieldName(this); | |
97 | if (!params['field']) | |
98 | return false; | |
99 | params['value']=checked?'1':'0';//seems that the ajax backend gets lost with boolean | |
100 | ||
101 | //CRM.api.call(this,entity,'create',params,{ create is still too buggy & perf | |
102 | CRM.api.call(this,entity,'setvalue',params,{ | |
103 | error: function (data) { | |
104 | editableSettings.error.call(this,entity,fieldName,checked,data); | |
105 | }, | |
106 | success: function (data) { | |
107 | editableSettings.success.call(this,entity,fieldName,checked,data); | |
108 | } | |
109 | }); | |
110 | }); | |
111 | }; | |
112 | ||
113 | var defaults = { | |
114 | form:{}, | |
115 | callBack:function(data){ | |
116 | if (data.is_error) { | |
117 | editableSettings.error.call (this,data); | |
118 | } else { | |
119 | return editableSettings.success.call (this,data); | |
120 | } | |
121 | }, | |
122 | error: function(entity,field,value,data) { | |
123 | $(this).crmError(data.error_message, ts('Error')); | |
124 | $(this).removeClass('crm-editable-saving'); | |
125 | }, | |
126 | success: function(entity,field,value,data) { | |
127 | var $i = $(this); | |
128 | CRM.alert('', ts('Saved'), 'success'); | |
129 | $i.removeClass ('crm-editable-saving crm-error'); | |
130 | $i.html(value); | |
131 | } | |
132 | } | |
133 | ||
134 | var editableSettings = $.extend({}, defaults, options); | |
135 | return this.each(function() { | |
136 | var $i = $(this); | |
137 | var fieldName = ""; | |
138 | ||
139 | if (this.nodeName == "INPUT" && this.type=="checkbox") { | |
140 | checkable.call(this,this); | |
141 | return; | |
142 | } | |
143 | ||
144 | if (this.nodeName = 'A') { | |
145 | if (this.className.indexOf('crmf-') == -1) { // it isn't a jeditable field | |
146 | var formSettings= $.extend({}, editableSettings.form , | |
147 | {source: $i.attr('href') | |
148 | ,success: function (result) { | |
149 | if ($i.hasClass('crm-dialog')) { | |
150 | $('.ui-dialog').dialog('close').remove(); | |
151 | } else | |
152 | $i.next().slideUp().remove(); | |
153 | $i.trigger('success',result); | |
154 | } | |
155 | }); | |
156 | var id= $i.closest('.crm-entity').attr('id'); | |
157 | if (id) { | |
158 | var e=id.match(/(\S*)-(\S*)/); | |
159 | if (!e) | |
160 | console && console.log && console.log("Couldn't get the entity id. You need to set class='crm-entity' id='{entityName}-{id}'"); | |
161 | formSettings.entity=e[1]; | |
162 | formSettings.id=e[2]; | |
163 | } | |
164 | if ($i.hasClass('crm-dialog')) { | |
165 | $i.click (function () { | |
166 | var $n=$('<div>Loading</div>').appendTo('body'); | |
167 | $n.dialog ({modal:true,width:500}); | |
168 | $n.crmForm (formSettings); | |
169 | return false; | |
170 | }); | |
171 | } else { | |
172 | $i.click (function () { | |
173 | var $n=$i.next(); | |
174 | if (!$n.hasClass('crm-target')) { | |
175 | $n=$i.after('<div class="crm-target"></div>').next(); | |
176 | } else { | |
177 | $n.slideToggle(); | |
178 | return false; | |
179 | }; | |
180 | $n.crmForm (formSettings); | |
181 | return false; | |
182 | }); | |
183 | } | |
184 | return; | |
185 | } | |
186 | } | |
187 | ||
188 | ||
189 | var settings = { | |
190 | tooltip : 'Click to edit...', | |
191 | placeholder : '<span class="crm-editable-placeholder">Click to edit</span>', | |
192 | data: function(value, settings) { | |
193 | return value.replace(/<(?:.|\n)*?>/gm, ''); | |
194 | } | |
195 | }; | |
196 | if ($i.data('placeholder')) { | |
197 | settings.placeholder = $i.data('placeholder'); | |
198 | } else { | |
199 | settings.placeholder = '<span class="crm-editable-placeholder">Click to edit</span>'; | |
200 | } | |
201 | if ($i.data('tooltip')) { | |
202 | settings.placeholder = $i.data('tooltip') | |
203 | } else { | |
204 | settings.tooltip = 'Click to edit...'; | |
205 | } | |
206 | if ($i.data('type')) { | |
207 | settings.type = $i.data('type'); | |
208 | settings.onblur = 'submit'; | |
209 | } | |
210 | if ($i.data('options')){ | |
211 | settings.data = $i.data('options'); | |
212 | } | |
213 | if(settings.type == 'textarea'){ | |
214 | $i.addClass ('crm-editable-textarea-enabled'); | |
215 | } | |
216 | else{ | |
217 | $i.addClass ('crm-editable-enabled'); | |
218 | } | |
219 | ||
220 | $i.editable(function(value,settings) { | |
221 | //$i.editable(function(value,editableSettings) { | |
222 | parent=$i.closest('.crm-entity'); | |
223 | if (!parent) { | |
224 | console && console.log && console.log("crm-editable: you need to define one parent element that has a class .crm-entity"); | |
225 | return; | |
226 | } | |
227 | ||
228 | $i.addClass ('crm-editable-saving'); | |
229 | var params = {}; | |
230 | var entity = null; | |
231 | params['field']=getFieldName(this); | |
232 | if (!params['field']) | |
233 | return false; | |
234 | params['value']=value; | |
235 | if (!getEntityID (this,function (e,id) { | |
236 | entity=e; | |
237 | params.id = id; | |
238 | })) {return;} | |
239 | ||
240 | if (params.id == "new") { | |
241 | params.id = ''; | |
242 | } | |
243 | ||
244 | if ($i.data('action')) { | |
245 | params[params['field']]=value;//format for create at least | |
246 | action=$i.data('action'); | |
247 | } else { | |
248 | action="setvalue"; | |
249 | } | |
250 | CRM.api.call(this,entity,action,params,{ | |
251 | error: function (data) { | |
252 | editableSettings.error.call(this,entity,fieldName,value,data); | |
253 | }, | |
254 | success: function (data) { | |
255 | if ($i.data('options')){ | |
256 | value = $i.data('options')[value]; | |
257 | } | |
258 | editableSettings.success.call(this,entity,fieldName,value,data); | |
259 | } | |
260 | }); | |
261 | },settings); | |
262 | }); | |
263 | } | |
264 | ||
265 | $.fn.crmForm = function (options ) { | |
266 | var settings = $.extend( { | |
267 | 'title':'', | |
268 | 'entity':'', | |
269 | 'action':'get', | |
270 | 'id':0, | |
271 | 'sequential':1, | |
272 | 'dialog': false, | |
273 | 'load' : function (target){}, | |
274 | 'success' : function (result) { | |
275 | $(this).html(ts('Saved')); | |
276 | } | |
277 | }, options); | |
278 | ||
279 | ||
280 | return this.each(function() { | |
281 | var formLoaded = function (target) { | |
282 | var $this =$(target); | |
283 | var destination="<input type='hidden' name='civicrmDestination' value='"+CRM.url('civicrm/ajax/rest',{ | |
284 | 'sequential':settings.sequential, | |
285 | 'json':'html', | |
286 | 'entity':settings.entity, | |
287 | 'action':settings.action, | |
288 | 'id':settings.id | |
289 | })+"' />"; | |
290 | $this.find('form').ajaxForm({ | |
291 | beforeSubmit :function () { | |
292 | $this.html("<div class='crm-editable-saving'>Saving...</div>"); | |
293 | return true; | |
294 | }, | |
295 | success:function(response) { | |
296 | if (response.indexOf('crm-error') >= 0) { // we got an error, re-display the page | |
297 | $this.html(response); | |
298 | formLoaded(target); | |
299 | } else { | |
300 | if (response[0] == '{') | |
301 | settings.success($.parseJSON (response)); | |
302 | else | |
303 | settings.success(response); | |
304 | } | |
305 | } | |
306 | }).append('<input type="hidden" name="snippet" value="1"/>'+destination).trigger('load'); | |
307 | ||
308 | settings.load(target); | |
309 | }; | |
310 | ||
311 | var $this = $(this); | |
312 | if (settings.source && settings.source.indexOf('snippet') == -1) { | |
313 | if (settings.source.indexOf('?') >= 0) | |
314 | settings.source = settings.source + "&snippet=1"; | |
315 | else | |
316 | settings.source = settings.source + "?snippet=1"; | |
317 | } | |
318 | ||
319 | ||
320 | $this.html ("Loading..."); | |
321 | if (settings.dialog) | |
322 | $this.dialog({width:'auto',minWidth:600}); | |
323 | $this.load (settings.source ,function (){formLoaded(this)}); | |
324 | ||
325 | }); | |
326 | }; | |
327 | ||
328 | })(jQuery); |