From d154efb928123cb5be3afb1f69aecfabae3edd34 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 18 Jul 2013 12:10:22 -0700 Subject: [PATCH] CRM-12923 - Move $.fn.crmContactField from rest.js to jquery.crmContactField.js ---------------------------------------- * CRM-12923: Create HTML prototype of "Job Position" UI http://issues.civicrm.org/jira/browse/CRM-12923 --- js/jquery/jquery.crmContactField.js | 85 +++++++++++++++++++++++++++++ js/rest.js | 82 ---------------------------- 2 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 js/jquery/jquery.crmContactField.js diff --git a/js/jquery/jquery.crmContactField.js b/js/jquery/jquery.crmContactField.js new file mode 100644 index 0000000000..9ba1645228 --- /dev/null +++ b/js/jquery/jquery.crmContactField.js @@ -0,0 +1,85 @@ +(function($, CRM) { + + /** + * Setup a contact widget for use in CRUD forms. The form element stores a contact ID + * but displays a name/label. + * + * Usage: + * + * + * + * Note: The given form element is the canonical representation of the selected contact-ID. + * To display/enter the contact by name, the contact-ID field will be hidden, and a helper + * widget will be inserted. The values will be synced between the two. + * + * Cases to consider/test: + * - When initializing, the read the contact-ID and set the contact-label + * - When unsetting(blanking) the contact-label, also unset (blank) the contact-ID + * - If third party code updates the hidden value, then one must trigger a 'change' + * event to update the visible widget. + */ + $.fn.crmContactField = function() { + return this.each(function(){ + var contactUrl = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'); + var hiddenEl = this; + var widgetEl = $(''); + + var activeContactId = null; + var setContactId = function(newContactId) { + if (newContactId != $(hiddenEl).val()) { + $(hiddenEl).val(newContactId); + $(hiddenEl).trigger('change'); + } + if (activeContactId != newContactId) { + activeContactId = newContactId; + + if (activeContactId) { + // lookup the name + $(widgetEl).css({visibility: 'hidden'}); // don't allow input during ajax + $.ajax({ + url : contactUrl + '&id=' + newContactId, + async : false, + success : function(html){ + var htmlText = html.split( '|' , 2); + $(widgetEl).val(htmlText[0]); + $(widgetEl).css({visibility: 'visible'}); + } + }); + } else { + // there is no name to lookup - just show a blank + $(widgetEl).val(''); + } + } + }; + + $(hiddenEl).after(widgetEl); + $(hiddenEl).hide(); + $(widgetEl).autocomplete(contactUrl, { + width: 200, + selectFirst: false, + minChars: 1, + matchContains: true, + delay: 400 + }).result(function(event, data) { + activeContactId = data[1]; + setContactId(activeContactId); + }).bind('change blur', function() { + if (! $(widgetEl).val()) { + activeContactId = ''; + setContactId(activeContactId); + } + }); + + $(hiddenEl).bind('change', function(){ + setContactId($(this).val()); + }); + setContactId($(hiddenEl).val()); + }); + }; + +})(jQuery, CRM); diff --git a/js/rest.js b/js/rest.js index 83b34a833d..f784d890a1 100644 --- a/js/rest.js +++ b/js/rest.js @@ -234,86 +234,4 @@ var CRM = CRM || {}; }); } - /** - * Setup a contact widget for use in CRUD forms. The form element stores a contact ID - * but displays a name/label. - * - * Usage: - * - * - * - * Note: The given form element is the canonical representation of the selected contact-ID. - * To display/enter the contact by name, the contact-ID field will be hidden, and a helper - * widget will be inserted. The values will be synced between the two. - * - * Cases to consider/test: - * - When initializing, the read the contact-ID and set the contact-label - * - When unsetting(blanking) the contact-label, also unset (blank) the contact-ID - * - If third party code updates the hidden value, then one must trigger a 'change' - * event to update the visible widget. - */ - $.fn.crmContactField = function() { - return this.each(function(){ - var contactUrl = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'); - var hiddenEl = this; - var widgetEl = $(''); - - var activeContactId = null; - var setContactId = function(newContactId) { - if (newContactId != $(hiddenEl).val()) { - $(hiddenEl).val(newContactId); - $(hiddenEl).trigger('change'); - } - if (activeContactId != newContactId) { - activeContactId = newContactId; - - if (activeContactId) { - // lookup the name - $(widgetEl).css({visibility: 'hidden'}); // don't allow input during ajax - $.ajax({ - url : contactUrl + '&id=' + newContactId, - async : false, - success : function(html){ - var htmlText = html.split( '|' , 2); - $(widgetEl).val(htmlText[0]); - $(widgetEl).css({visibility: 'visible'}); - } - }); - } else { - // there is no name to lookup - just show a blank - $(widgetEl).val(''); - } - } - }; - - $(hiddenEl).after(widgetEl); - $(hiddenEl).hide(); - $(widgetEl).autocomplete(contactUrl, { - width: 200, - selectFirst: false, - minChars: 1, - matchContains: true, - delay: 400 - }).result(function(event, data) { - activeContactId = data[1]; - setContactId(activeContactId); - }).bind('change blur', function() { - if (! $(widgetEl).val()) { - activeContactId = ''; - setContactId(activeContactId); - } - }); - - $(hiddenEl).bind('change', function(){ - setContactId($(this).val()); - }); - setContactId($(hiddenEl).val()); - }); - }; - })(jQuery, CRM); -- 2.25.1