Finish migrating AlternateContactSelector.js to use entityRef
authorColeman Watts <coleman@civicrm.org>
Mon, 28 Jul 2014 14:50:20 +0000 (15:50 +0100)
committerColeman Watts <coleman@civicrm.org>
Mon, 28 Jul 2014 14:53:38 +0000 (15:53 +0100)
js/AlternateContactSelector.js [new file with mode: 0644]
js/AutoComplete.js [deleted file]

diff --git a/js/AlternateContactSelector.js b/js/AlternateContactSelector.js
new file mode 100644 (file)
index 0000000..31fc0f8
--- /dev/null
@@ -0,0 +1,24 @@
+CRM.$(function($) {
+  'use strict';
+
+  function assignAutoComplete(id_field, profileids) {
+    $('#' + id_field).on('change', function (event, data) {
+      var contactID = $(this).val();
+      CRM.api3('profile', 'get', {'profile_id': profileids, 'contact_id': contactID})
+        .done(function (result) {
+          $.each(result.values, function (id, value) {
+            $.each(value, function (fieldname, fieldvalue) {
+              $('#' + fieldname).val(fieldvalue);
+            });
+          });
+        }
+      );
+    });
+  }
+
+  $(CRM.form.autocompletes).each(function (index, autocomplete) {
+    assignAutoComplete(autocomplete.id_field, CRM.ids.profile || []);
+  });
+
+});
+
diff --git a/js/AutoComplete.js b/js/AutoComplete.js
deleted file mode 100644 (file)
index 7a1237e..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-CRM.$(function($) {
-  'use strict';
-
-  // Behind the scenes method deals with browser for setting cursor position
-  $.caretTo = function (el, index) {
-    if (el.createTextRange) {
-      var range = el.createTextRange();
-      range.move("character", index);
-      range.select();
-    }
-    else if (el.selectionStart != null) {
-      el.focus();
-      el.setSelectionRange(index, index);
-    }
-  };
-
-  /**
-   * Display a personalized message containing the contact's name
-   * and a variable from the server
-   */
-  function assignAutoComplete( id_field, profileids, autocomplete) {
-    if(profileids === undefined) {profileids = [];}
-
-    var customIdObj = $('#' + id_field);
-
-    customIdObj.on('change', function (event, data) {
-        var contactID = $(this).val();
-        var namefields = ['first_name', 'last_name', 'middle_name'];
-        CRM.api('profile', 'get', {'profile_id': profileids, 'contact_id': contactID}, {
-          success: function (result) {
-            $.each(result.values, function (id, value) {
-              $.each(value, function (fieldname, fieldvalue) {
-                $('#' + fieldname).val(fieldvalue);
-              });
-            });
-          }
-        });
-      }
-      )
-    }
-
-  /**
-   * Show or hide the autocomplete and change the text
-   */
-  function showHideAutoComplete(id_field, hidden_text, shown_text, profileids) {
-    $('#crm-contact-toggle-' + id_field).on('click', function(event) {
-      event.preventDefault();
-      $('#' + name_field).toggle();
-      if($('#' + name_field).is(":visible")) {
-        $('#crm-contact-toggle-text-'  + id_field).text(shown_text);
-      }
-      else{
-        $('#crm-contact-toggle-text-'  + id_field).text(hidden_text);
-        $('#' + id_field + ', #' + name_field).val('');
-        CRM.api('profile', 'get', {'profile_id' : profileids}, {
-          success: function(result) {
-            $.each(result.values, function (id, values){
-            $.each(values, function (fieldname, fieldvalue) {
-              $('#' + fieldname).val(fieldvalue);
-            });
-            });
-          }
-        });
-      }
-    });
-  }
-
-  var autocompletes = CRM.form.autocompletes;
-  $(autocompletes).each(function (index, autocomplete) {
-    assignAutoComplete(autocomplete.id_field, CRM.ids.profile, autocomplete);
-    }
-  );
-
-});
-