2 * Copyright (C) 2012 Xavier Dutoit
3 * Licensed to CiviCRM under the Academic Free License version 3.0.
5 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Structure+convention+for+automagic+edit+in+place
8 $.fn
.crmEditableEntity = function() {
12 $row
= this.first().closest('.crm-entity');
13 ret
.entity
= $row
.data('entity') || $row
[0].id
.split('-')[0];
14 ret
.id
= $row
.data('id') || $row
[0].id
.split('-')[1];
15 ret
.action
= $row
.data('action') || 'setvalue';
17 if (!ret
.entity
|| !ret
.id
) {
20 $('.crm-editable, [data-field]', $row
).each(function() {
21 var fieldName
= $(this).data('field') || this.className
.match(/crmf-(\S*)/)[1];
23 ret
[fieldName
] = $(this).text();
25 ret
.field
= fieldName
;
32 $.fn
.crmEditable = function(options
) {
33 var checkable = function() {
34 $(this).change(function() {
35 var info
= $(this).crmEditableEntity();
39 var checked
= $(this).is(':checked');
44 value
: checked
? 1 : 0
46 CRM
.api(info
.entity
, info
.action
, params
, {
48 error: function(data
) {
49 editableSettings
.error
.call(this, info
.entity
, info
.field
, checked
, data
);
51 success: function(data
) {
52 editableSettings
.success
.call(this, info
.entity
, info
.field
, checked
, data
);
60 callBack: function(data
) {
62 editableSettings
.error
.call(this, data
);
64 return editableSettings
.success
.call(this, data
);
67 error: function(entity
, field
, value
, data
) {
68 $(this).crmError(data
.error_message
, ts('Error'));
69 $(this).removeClass('crm-editable-saving');
71 success: function(entity
, field
, value
, data
, settings
) {
73 CRM
.status(ts('Saved'));
74 $i
.removeClass('crm-editable-saving crm-error');
75 value
= value
=== '' ? settings
.placeholder
: value
;
80 var editableSettings
= $.extend({}, defaults
, options
);
81 return this.each(function() {
85 if (this.nodeName
== "INPUT" && this.type
== "checkbox") {
86 checkable
.call(this, this);
91 tooltip
: 'Click to edit...',
92 placeholder
: '<span class="crm-editable-placeholder">Click to edit</span>',
93 data: function(value
, settings
) {
94 return value
.replace(/<(?:.|\n)*?>/gm, '');
97 if ($i
.data('placeholder')) {
98 settings
.placeholder
= $i
.data('placeholder');
100 settings
.placeholder
= '<span class="crm-editable-placeholder">Click to edit</span>';
102 if ($i
.data('tooltip')) {
103 settings
.placeholder
= $i
.data('tooltip')
105 settings
.tooltip
= 'Click to edit...';
107 if ($i
.data('type')) {
108 settings
.type
= $i
.data('type');
109 settings
.onblur
= 'submit';
111 if ($i
.data('options')) {
112 settings
.data
= $i
.data('options');
114 if (settings
.type
== 'textarea') {
115 $i
.addClass('crm-editable-textarea-enabled');
118 $i
.addClass('crm-editable-enabled');
121 $i
.editable(function(value
, settings
) {
122 $i
.addClass('crm-editable-saving');
124 info
= $i
.crmEditableEntity(),
126 action
= $i
.data('action') || info
.action
;
130 if (info
.id
&& info
.id
!== 'new') {
133 if (action
=== 'setvalue') {
134 params
.field
= info
.field
;
135 params
.value
= value
;
138 params
[info
.field
] = value
;
140 CRM
.api(info
.entity
, action
, params
, {
142 error: function(data
) {
143 editableSettings
.error
.call(this, info
.entity
, info
.field
, value
, data
);
145 success: function(data
) {
146 if ($i
.data('options')) {
147 value
= $i
.data('options')[value
];
149 $i
.trigger('crmFormSuccess');
150 editableSettings
.success
.call(this, info
.entity
, info
.field
, value
, data
, settings
);