From d4838efc254aa0d4b5c432c6193d3a91b05e7b05 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 21 Dec 2013 22:01:02 -0800 Subject: [PATCH] CRM-14015 - Abstract entity/field extraction from crmEditable --- js/jquery/jquery.crmeditable.js | 135 +++++++++------------- templates/CRM/common/enableDisableApi.tpl | 40 ++----- 2 files changed, 66 insertions(+), 109 deletions(-) diff --git a/js/jquery/jquery.crmeditable.js b/js/jquery/jquery.crmeditable.js index e0c620dd92..539ef4e73a 100644 --- a/js/jquery/jquery.crmeditable.js +++ b/js/jquery/jquery.crmeditable.js @@ -18,65 +18,53 @@ (function($) { - $.fn.crmEditable = function (options) { - // for a jquery object (the crm-editable), find the entity name and id to apply the changes to - // call result function(entity,id). The caller is responsible to use these params and do the needed - var getEntityID = function (field,result) { - var domid= $(field).closest('.crm-entity'); - if (!domid) { - console && console.log && console.log("Couldn't get the entity id. You need to set class='crm-entity' on a parent element of the field"); - return false; - } - // trying to extract using the html5 data - if (domid.data('entity')) { - result (domid.data('entity'),domid.data('id')); - return true; - } - domid=domid.attr('id'); - if (!domid) { - console && console.log && console.log("FATAL crm-editable: Couldn't get the entity id. You need to set class='crm-entity' id='{entityName}-{id}'"); - return false; - } - var e=domid.match(/(\S*)-(\S*)/); - if (!e) { - console && console.log && console.log("Couldn't get the entity id. You need to set class='crm-entity' id='{entityName}-{id}'"); - return false; - } - result(e[1],e[2]); - return true; - } - // param in : a dom object that contains the field name as a class crmf-xxx - var getFieldName = function (field) { - var fieldName = $(field).data('field') || field.className.match(/crmf-(\S*)/)[1]; - if (!fieldName) { - console && console.log && console.log("Couldn't get the crm-editable field name to modify. You need to set crmf-{field_name} or data-{field_name}"); + $.fn.crmEditableEntity = function() { + var + el = this[0], + ret = {}, + $row = this.first().closest('.crm-entity'); + ret.entity = $row.data('entity'); + ret.id = $row.data('id'); + if (!ret.entity || !ret.id) { + ret.entity = $row[0].id.split('-')[0]; + ret.id = $row[0].id.split('-')[1]; + } + if (!ret.entity || !ret.id) { + return false; + } + $('.crm-editable', $row).each(function() { + var fieldName = $(this).data('field') || this.className.match(/crmf-(\S*)/)[1]; + if (fieldName) { + ret[fieldName] = $(this).text(); + if (this === el) { + ret.field = fieldName; } - return fieldName; } + }); + return ret; + }; + $.fn.crmEditable = function (options) { var checkable = function () { $(this).change (function() { - var params={sequential:1}; - var entity = null; - var checked = $(this).is(':checked'); - if (!getEntityID (this,function (e,id) { - entity=e; - params.id = id; - - })) { return }; - - params['field']=getFieldName(this); - if (!params['field']) + var info = $(this).crmEditableEntity(); + if (!info.field) { return false; - params['value']=checked?'1':'0';//seems that the ajax backend gets lost with boolean - - CRM.api(entity,'setvalue',params,{ + } + var checked = $(this).is(':checked'); + var params = { + sequential: 1, + id: info.id, + field: info.field, + value: checked ? 1 : 0 + }; + CRM.api(info.entity, 'setvalue', params, { context: this, error: function (data) { - editableSettings.error.call(this,entity,params.field,checked,data); + editableSettings.error.call(this, info.entity, info.field, checked, data); }, success: function (data) { - editableSettings.success.call(this,entity,params.field,checked,data); + editableSettings.success.call(this, info.entity, info.field, checked, data); } }); }); @@ -190,49 +178,34 @@ } $i.editable(function(value,settings) { - //$i.editable(function(value,editableSettings) { - parent=$i.closest('.crm-entity'); - if (!parent) { - console && console.log && console.log("crm-editable: you need to define one parent element that has a class .crm-entity"); - return; - } - $i.addClass ('crm-editable-saving'); - var params = {}; - var entity = null; - params['field']=getFieldName(this); - if (!params['field']) + var + info = $i.crmEditableEntity(), + params= {}, + action = $i.data('action') || 'setvalue'; + if (!info.field) { return false; - params['value']=value; - if (!getEntityID (this,function (e,id) { - entity=e; - params.id = id; - })) {return;} - - if (params.id == "new") { - params.id = ''; } - - if ($i.data('action')) { - var fieldName = params['field']; - delete params['field']; - delete params['value']; - - params[fieldName]=value;//format for create at least - action=$i.data('action'); - } else { - action="setvalue"; + if (info.id && info.id !== 'new') { + params.id = info.id; + } + if (action === 'setvalue') { + params.field = info.field; + params.value = value; + } + else { + params[info.field] = value; } - CRM.api(entity, action, params, { + CRM.api(info.entity, action, params, { context: this, error: function (data) { - editableSettings.error.call(this,entity,fieldName,value,data); + editableSettings.error.call(this, info.entity, info.field, value, data); }, success: function (data) { if ($i.data('options')){ value = $i.data('options')[value]; } - editableSettings.success.call(this,entity,fieldName,value,data); + editableSettings.success.call(this, info.entity, info.field, value, data); } }); },settings); diff --git a/templates/CRM/common/enableDisableApi.tpl b/templates/CRM/common/enableDisableApi.tpl index 649bf8fd9a..63e355d523 100644 --- a/templates/CRM/common/enableDisableApi.tpl +++ b/templates/CRM/common/enableDisableApi.tpl @@ -27,7 +27,7 @@ {literal}