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 if (!ret
.entity
|| !ret
.id
) {
18 $('.crm-editable, [data-field]', $row
).each(function() {
19 var fieldName
= $(this).data('field') || this.className
.match(/crmf-(\S*)/)[1];
21 ret
[fieldName
] = $(this).text();
23 ret
.field
= fieldName
;
30 $.fn
.crmEditable = function(options
) {
31 var checkable = function() {
32 $(this).change(function() {
33 var info
= $(this).crmEditableEntity();
37 var checked
= $(this).is(':checked');
42 value
: checked
? 1 : 0
44 CRM
.api(info
.entity
, 'setvalue', params
, {
46 error: function(data
) {
47 editableSettings
.error
.call(this, info
.entity
, info
.field
, checked
, data
);
49 success: function(data
) {
50 editableSettings
.success
.call(this, info
.entity
, info
.field
, checked
, data
);
58 callBack: function(data
) {
60 editableSettings
.error
.call(this, data
);
62 return editableSettings
.success
.call(this, data
);
65 error: function(entity
, field
, value
, data
) {
66 $(this).crmError(data
.error_message
, ts('Error'));
67 $(this).removeClass('crm-editable-saving');
69 success: function(entity
, field
, value
, data
) {
71 CRM
.status(ts('Saved'));
72 $i
.removeClass('crm-editable-saving crm-error');
77 var editableSettings
= $.extend({}, defaults
, options
);
78 return this.each(function() {
82 if (this.nodeName
== "INPUT" && this.type
== "checkbox") {
83 checkable
.call(this, this);
88 tooltip
: 'Click to edit...',
89 placeholder
: '<span class="crm-editable-placeholder">Click to edit</span>',
90 data: function(value
, settings
) {
91 return value
.replace(/<(?:.|\n)*?>/gm, '');
94 if ($i
.data('placeholder')) {
95 settings
.placeholder
= $i
.data('placeholder');
97 settings
.placeholder
= '<span class="crm-editable-placeholder">Click to edit</span>';
99 if ($i
.data('tooltip')) {
100 settings
.placeholder
= $i
.data('tooltip')
102 settings
.tooltip
= 'Click to edit...';
104 if ($i
.data('type')) {
105 settings
.type
= $i
.data('type');
106 settings
.onblur
= 'submit';
108 if ($i
.data('options')) {
109 settings
.data
= $i
.data('options');
111 if (settings
.type
== 'textarea') {
112 $i
.addClass('crm-editable-textarea-enabled');
115 $i
.addClass('crm-editable-enabled');
118 $i
.editable(function(value
, settings
) {
119 $i
.addClass('crm-editable-saving');
121 info
= $i
.crmEditableEntity(),
123 action
= $i
.data('action') || 'setvalue';
127 if (info
.id
&& info
.id
!== 'new') {
130 if (action
=== 'setvalue') {
131 params
.field
= info
.field
;
132 params
.value
= value
;
135 params
[info
.field
] = value
;
137 CRM
.api(info
.entity
, action
, params
, {
139 error: function(data
) {
140 editableSettings
.error
.call(this, info
.entity
, info
.field
, value
, data
);
142 success: function(data
) {
143 if ($i
.data('options')) {
144 value
= $i
.data('options')[value
];
146 $i
.trigger('crmFormSuccess');
147 editableSettings
.success
.call(this, info
.entity
, info
.field
, value
, data
);