From 9d0d4076bb265875d72d386014c31c20e34249d3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 1 Jan 2015 13:28:32 -0500 Subject: [PATCH] CRM-15759 - Improve support for crmEditable select options --- css/civicrm.css | 6 +-- js/jquery/jquery.crmeditable.js | 65 ++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/css/civicrm.css b/css/civicrm.css index 2d668b470c..0d5d4685dd 100644 --- a/css/civicrm.css +++ b/css/civicrm.css @@ -4067,12 +4067,10 @@ div.m ul#civicrm-menu, border: 2px dashed transparent; } .crm-container .crm-editable-textarea-enabled { - padding-left: 2px; - border: 2px dashed transparent; + white-space: normal; } -.crm-container .crm-editable-enabled:hover, -.crm-container .crm-editable-textarea-enabled:hover { +.crm-container .crm-editable-enabled:hover { border: 2px dashed lightgrey; cursor: pointer; } diff --git a/js/jquery/jquery.crmeditable.js b/js/jquery/jquery.crmeditable.js index 4cea8fb43c..503adfa12c 100644 --- a/js/jquery/jquery.crmeditable.js +++ b/js/jquery/jquery.crmeditable.js @@ -1,5 +1,10 @@ // https://civicrm.org/licensing (function($) { + // TODO: We'll need a way to clear this cache if options are edited. + // Maybe it should be stored in the CRM object so other parts of the app can use it. + // Note that if we do move it, we should also change the format of option lists to our standard sequential arrays + var optionsCache = {}; + /** * Helper fn to retrieve semantic data from markup */ @@ -31,8 +36,8 @@ * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Structure+convention+for+automagic+edit+in+place */ $.fn.crmEditable = function(options) { - var checkable = function() { - $(this).change(function() { + function checkable() { + $(this).off('.crmEditable').on('change.crmEditable', function() { var $el = $(this), info = $el.crmEditableEntity(); if (!info.field) { @@ -52,26 +57,22 @@ editableSettings.success.call($el[0], info.entity, info.field, checked, data); }); }); - }; + } var defaults = { - form: {}, - callBack: function(data) { - if (data.is_error) { - editableSettings.error.call(this, data); - } else { - return editableSettings.success.call(this, data); - } - }, error: function(entity, field, value, data) { $(this).crmError(data.error_message, ts('Error')); $(this).removeClass('crm-editable-saving'); }, success: function(entity, field, value, data, settings) { var $i = $(this); - $i.removeClass('crm-editable-saving crm-error'); - value = value === '' ? settings.placeholder : value; - $i.html(value); + if ($i.data('refresh')) { + CRM.refreshParent($i); + } else { + $i.removeClass('crm-editable-saving crm-error'); + value = value === '' ? settings.placeholder : value; + $i.html(value); + } } }; @@ -80,6 +81,10 @@ var $i, fieldName = ""; + if ($(this).hasClass('crm-editable-enabled')) { + return; + } + if (this.nodeName == "INPUT" && this.type == "checkbox") { checkable.call(this, this); return; @@ -109,11 +114,35 @@ 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, ''); } }; if ($i.data('type')) { settings.type = $i.data('type'); + if (settings.type == 'boolean') { + settings.type = 'select'; + $i.data('options', {'0': ts('No'), '1': ts('Yes')}); + } } if ($i.data('options')) { settings.data = $i.data('options'); @@ -121,9 +150,7 @@ if (settings.type == 'textarea') { $i.addClass('crm-editable-textarea-enabled'); } - else { - $i.addClass('crm-editable-enabled'); - } + $i.addClass('crm-editable-enabled'); $i.editable(function(value, settings) { $i.addClass('crm-editable-saving'); @@ -150,6 +177,10 @@ if ($el.data('options')) { value = $el.data('options')[value]; } + else if ($el.data('optionsHashKey')) { + var options = optionsCache[$el.data('optionsHashKey')]; + value = options ? options[value] : ''; + } $el.trigger('crmFormSuccess'); editableSettings.success.call($el[0], info.entity, info.field, value, data, settings); }) -- 2.25.1