Commit | Line | Data |
---|---|---|
1fe97a01 | 1 | /** |
99d24adf CW |
2 | * Copyright (C) 2012 Xavier Dutoit |
3 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
4 | * | |
5 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Structure+convention+for+automagic+edit+in+place | |
6 | */ | |
6a488035 | 7 | (function($) { |
d4838efc CW |
8 | $.fn.crmEditableEntity = function() { |
9 | var | |
10 | el = this[0], | |
11 | ret = {}, | |
12 | $row = this.first().closest('.crm-entity'); | |
4f1c81da | 13 | ret.entity = $row.data('entity') || $row[0].id.split('-')[0]; |
14 | ret.id = $row.data('id') || $row[0].id.split('-')[1]; | |
2d221ef7 | 15 | ret.action = $row.data('action') || 'setvalue'; |
4f1c81da | 16 | |
d4838efc CW |
17 | if (!ret.entity || !ret.id) { |
18 | return false; | |
19 | } | |
60158442 | 20 | $('.crm-editable, [data-field]', $row).each(function() { |
d4838efc CW |
21 | var fieldName = $(this).data('field') || this.className.match(/crmf-(\S*)/)[1]; |
22 | if (fieldName) { | |
23 | ret[fieldName] = $(this).text(); | |
24 | if (this === el) { | |
25 | ret.field = fieldName; | |
6a488035 | 26 | } |
6a488035 | 27 | } |
d4838efc CW |
28 | }); |
29 | return ret; | |
30 | }; | |
6a488035 | 31 | |
99d24adf CW |
32 | $.fn.crmEditable = function(options) { |
33 | var checkable = function() { | |
34 | $(this).change(function() { | |
35 | var info = $(this).crmEditableEntity(); | |
36 | if (!info.field) { | |
37 | return false; | |
38 | } | |
39 | var checked = $(this).is(':checked'); | |
40 | var params = { | |
41 | sequential: 1, | |
42 | id: info.id, | |
43 | field: info.field, | |
44 | value: checked ? 1 : 0 | |
45 | }; | |
2d221ef7 | 46 | CRM.api(info.entity, info.action, params, { |
99d24adf CW |
47 | context: this, |
48 | error: function(data) { | |
49 | editableSettings.error.call(this, info.entity, info.field, checked, data); | |
50 | }, | |
51 | success: function(data) { | |
52 | editableSettings.success.call(this, info.entity, info.field, checked, data); | |
d4838efc | 53 | } |
6a488035 | 54 | }); |
99d24adf CW |
55 | }); |
56 | }; | |
57 | ||
58 | var defaults = { | |
59 | form: {}, | |
60 | callBack: function(data) { | |
61 | if (data.is_error) { | |
62 | editableSettings.error.call(this, data); | |
63 | } else { | |
64 | return editableSettings.success.call(this, data); | |
6a488035 | 65 | } |
99d24adf CW |
66 | }, |
67 | error: function(entity, field, value, data) { | |
68 | $(this).crmError(data.error_message, ts('Error')); | |
69 | $(this).removeClass('crm-editable-saving'); | |
70 | }, | |
e03e8aef | 71 | success: function(entity, field, value, data, settings) { |
6a488035 | 72 | var $i = $(this); |
e11b84ce | 73 | CRM.status(ts('Saved')); |
99d24adf | 74 | $i.removeClass('crm-editable-saving crm-error'); |
e03e8aef | 75 | value = value === '' ? settings.placeholder : value; |
99d24adf CW |
76 | $i.html(value); |
77 | } | |
78 | }; | |
6a488035 | 79 | |
99d24adf CW |
80 | var editableSettings = $.extend({}, defaults, options); |
81 | return this.each(function() { | |
82 | var $i = $(this); | |
83 | var fieldName = ""; | |
6a488035 | 84 | |
99d24adf CW |
85 | if (this.nodeName == "INPUT" && this.type == "checkbox") { |
86 | checkable.call(this, this); | |
87 | return; | |
88 | } | |
6a488035 | 89 | |
99d24adf CW |
90 | var settings = { |
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, ''); | |
6a488035 | 95 | } |
99d24adf CW |
96 | }; |
97 | if ($i.data('placeholder')) { | |
98 | settings.placeholder = $i.data('placeholder'); | |
99 | } else { | |
100 | settings.placeholder = '<span class="crm-editable-placeholder">Click to edit</span>'; | |
101 | } | |
102 | if ($i.data('tooltip')) { | |
103 | settings.placeholder = $i.data('tooltip') | |
104 | } else { | |
105 | settings.tooltip = 'Click to edit...'; | |
106 | } | |
107 | if ($i.data('type')) { | |
108 | settings.type = $i.data('type'); | |
109 | settings.onblur = 'submit'; | |
110 | } | |
111 | if ($i.data('options')) { | |
112 | settings.data = $i.data('options'); | |
113 | } | |
114 | if (settings.type == 'textarea') { | |
115 | $i.addClass('crm-editable-textarea-enabled'); | |
116 | } | |
117 | else { | |
118 | $i.addClass('crm-editable-enabled'); | |
119 | } | |
120 | ||
121 | $i.editable(function(value, settings) { | |
122 | $i.addClass('crm-editable-saving'); | |
123 | var | |
124 | info = $i.crmEditableEntity(), | |
125 | params = {}, | |
2d221ef7 | 126 | action = $i.data('action') || info.action; |
99d24adf CW |
127 | if (!info.field) { |
128 | return false; | |
6a488035 | 129 | } |
99d24adf CW |
130 | if (info.id && info.id !== 'new') { |
131 | params.id = info.id; | |
6a488035 | 132 | } |
99d24adf CW |
133 | if (action === 'setvalue') { |
134 | params.field = info.field; | |
135 | params.value = value; | |
6a488035 | 136 | } |
99d24adf CW |
137 | else { |
138 | params[info.field] = value; | |
6a488035 | 139 | } |
99d24adf CW |
140 | CRM.api(info.entity, action, params, { |
141 | context: this, | |
142 | error: function(data) { | |
143 | editableSettings.error.call(this, info.entity, info.field, value, data); | |
6a488035 | 144 | }, |
99d24adf CW |
145 | success: function(data) { |
146 | if ($i.data('options')) { | |
147 | value = $i.data('options')[value]; | |
6a488035 | 148 | } |
99d24adf | 149 | $i.trigger('crmFormSuccess'); |
e03e8aef | 150 | editableSettings.success.call(this, info.entity, info.field, value, data, settings); |
6a488035 | 151 | } |
99d24adf CW |
152 | }); |
153 | }, settings); | |
6a488035 TO |
154 | }); |
155 | }; | |
156 | ||
157 | })(jQuery); |