CRM-15295 - Show 'new item' after creating a container
[civicrm-core.git] / templates / CRM / Custom / Form / ContactReference.tpl
index 2372f9ad6260d6c450bafa3b59bdcf8724d1f20c..44f7f001a40d37a743e65e99f6e7e5093d601d9d 100644 (file)
@@ -1,8 +1,8 @@
 {*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
 *}
+{* Js needed to initialize custom field of type ContactReference *}
 {literal}
 <script type="text/javascript">
-cj( function( ) {
-    var url       = "{/literal}{$customUrls.$element_name}{literal}";
-    var custom    = "{/literal}#{$element_name|replace:']':''|replace:'[':'_'}{literal}";
-    var custom_id = "{/literal}input[name=\"{$element_name|cat:'_id'|regex_replace:'/\]_id$/':'_id]'}\"]{literal}";
+  CRM.$(function($) {
+    var $field = $("{/literal}#{$element_name|replace:']':''|replace:'[':'_'}{literal}");
 
-    var customObj   = cj(custom);
-    var customIdObj = cj(custom_id);
-
-    if ( !customObj.hasClass('ac_input') ) {
-        customObj.autocomplete( url,
-            { width : 250, selectFirst : false, elementId: custom,  matchContains: true, formatResult: {/literal}validate{$element_name|replace:']':''|replace:'[':'_'|replace:'-':'_'}{literal}, max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal}
-            }).result(
-                function(event, data) {
-                  cj(custom_id).val(data[1]);
-                }
-        );
-        customObj.click( function( ) {
-            customIdObj.val('');
-      });
-     }
+    $field.crmSelect2({
+      placeholder: {/literal}'{ts escape="js"}- select contact -{/ts}'{literal},
+      minimumInputLength: 1,
+      ajax: {
+        url: {/literal}"{$customUrls.$element_name}"{literal},
+        data: function(term) {
+          return {term: term};
+        },
+        results: function(response) {
+          return {results: response};
+        }
+      },
+      initSelection: function($el, callback) {
+        callback($el.data('entity-value'));
+      }
+    });
 });
-
-function validate{/literal}{$element_name|replace:']':''|replace:'[':'_'|replace:'-':'_'}{literal}( Data, position ) {
-  if ( Data[1] == 'error' ) {
-    cj(this.elementId).parent().append("<span id='"+ (this.elementId).substr(1) +"_error' class='hiddenElement messages crm-error'>" + "{/literal}{ts escape='js'}Invalid parameters for contact search.{/ts}{literal}" + "</span>");
-    cj(this.elementId + '_error').fadeIn(800).fadeOut(5000, function( ){ cj(this).remove(); });
-    Data[1] = '';
-  }
-}
 </script>
 {/literal}