From b48d66cecee3c449d05d252bfcfc10831a866d6c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 2 Jan 2015 10:25:02 -0500 Subject: [PATCH] CRM-15759 - support empty select option in crmEditable --- css/civicrm.css | 16 ++++++- js/jquery/jquery.crmeditable.js | 79 +++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/css/civicrm.css b/css/civicrm.css index 0d5d4685dd..6b7853e1fc 100644 --- a/css/civicrm.css +++ b/css/civicrm.css @@ -4071,10 +4071,15 @@ div.m ul#civicrm-menu, } .crm-container .crm-editable-enabled:hover { - border: 2px dashed lightgrey; + border: 2px dashed #d1d1d1; cursor: pointer; } +.crm-container .crm-editable-enabled.crm-editable-editing:hover { + border: 2px dashed transparent; + cursor: auto; +} + .crm-container span.crm-editable-textarea-enabled { display: inline-block !important; width: 96%; @@ -4084,10 +4089,16 @@ div.m ul#civicrm-menu, } .crm-container .crm-editable-placeholder { - background: url("../i/icons/jquery-ui-2786C2.png") -66px -114px no-repeat; + background: url("../i/icons/jquery-ui-3E3E3E.png") -66px -114px no-repeat; text-indent: -10000px; display: block; width: 12px; + opacity: .5; +} + +.crm-container .crm-editable-enabled:hover .crm-editable-placeholder { + background-image: url("../i/icons/jquery-ui-2786C2.png"); + opacity: 1; } .crm-container .crm-editable-saving { @@ -4111,6 +4122,7 @@ div.m ul#civicrm-menu, .crm-editable-form { margin: 0 !important; padding: 0 !important; + width: auto !important; position: relative; overflow: visible; } diff --git a/js/jquery/jquery.crmeditable.js b/js/jquery/jquery.crmeditable.js index 503adfa12c..201d893054 100644 --- a/js/jquery/jquery.crmeditable.js +++ b/js/jquery/jquery.crmeditable.js @@ -69,7 +69,7 @@ if ($i.data('refresh')) { CRM.refreshParent($i); } else { - $i.removeClass('crm-editable-saving crm-error'); + $i.removeClass('crm-editable-saving crm-error crm-editable-editing'); value = value === '' ? settings.placeholder : value; $i.html(value); } @@ -113,29 +113,8 @@ cancel: '', submit: '', cssclass: 'crm-editable-form', - data: function(value, settings) { - if ($i.data('type') == 'select' && !$i.data('options')) { - var result, - info = $i.crmEditableEntity(), - hash = info.entity + '.' + info.field, - params = { - field: info.field, - context: 'create' - }; - $i.data('optionsHashKey', hash); - if (optionsCache[hash]) { - return optionsCache[hash]; - } - $.ajax({ - url: CRM.url('civicrm/ajax/rest'), - data: {entity: info.entity, action: 'getoptions', json: JSON.stringify(params)}, - async: false, - success: function(data) {optionsCache[hash] = data.values;} - }); - return optionsCache[hash]; - } - return value.replace(/<(?:.|\n)*?>/gm, ''); - } + data: getData, + onreset: restoreContainer }; if ($i.data('type')) { settings.type = $i.data('type'); @@ -144,9 +123,6 @@ $i.data('options', {'0': ts('No'), '1': ts('Yes')}); } } - if ($i.data('options')) { - settings.data = $i.data('options'); - } if (settings.type == 'textarea') { $i.addClass('crm-editable-textarea-enabled'); } @@ -175,11 +151,11 @@ CRM.api3(info.entity, action, params, true) .done(function(data) { if ($el.data('options')) { - value = $el.data('options')[value]; + value = $el.data('options')[value] || ''; } else if ($el.data('optionsHashKey')) { var options = optionsCache[$el.data('optionsHashKey')]; - value = options ? options[value] : ''; + value = options && options[value] ? options[value] : ''; } $el.trigger('crmFormSuccess'); editableSettings.success.call($el[0], info.entity, info.field, value, data, settings); @@ -203,6 +179,51 @@ } }); }); + + function getData(value, settings) { + // Add css class to wrapper + // FIXME: This should be a response to an event instead of coupled with this function but jeditable 1.7.1 doesn't trigger any events :( + $i.addClass('crm-editable-editing'); + + if ($i.data('type') == 'select' || $i.data('type') == 'boolean') { + if ($i.data('options')) { + return formatOptions($i.data('options')); + } + var result, + info = $i.crmEditableEntity(), + hash = info.entity + '.' + info.field, + params = { + field: info.field, + context: 'create' + }; + $i.data('optionsHashKey', hash); + if (optionsCache[hash]) { + return formatOptions(optionsCache[hash]); + } + $.ajax({ + url: CRM.url('civicrm/ajax/rest'), + data: {entity: info.entity, action: 'getoptions', json: JSON.stringify(params)}, + async: false, // jeditable lacks support for async options lookup + success: function(data) {optionsCache[hash] = data.values;} + }); + return formatOptions(optionsCache[hash]); + + } + return value.replace(/<(?:.|\n)*?>/gm, ''); + } + + function formatOptions(options) { + if (typeof $i.data('emptyOption') === 'string') { + // Using 'null' because '' is broken in jeditable 1.7.1 + return $.extend({'null': $i.data('emptyOption')}, options); + } + return options; + } + + function restoreContainer() { + $i.removeClass('crm-editable-editing'); + } + }); }; -- 2.25.1