Problem/Motivation

Currently, the autocomplete derived form Drupal's misc/autocomplete.js is such that when the user hits the enter key, the search is immediately submitted. This causes issues with certain kinds of CJK key entry, which requires the compilation of radicals / syllables into character sets and require enter as a confirmation. This causes the form to be immediately submitted with a potentially undesired query.

Proposed resolution

Modify the javascript behavior to recognize when the field contains the set of characters that indicate a CJK style input, use appropriate key detect to account for the enter-term completion and ajax autocomplete.

Comments

angrytoast’s picture

Patch attached:

iamEAP’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Needs review » Needs work

Thanks, @angrytoast! Testing indicates the patch is functional. A number of code style issues, though. Should be a quick cleanup.

For reference, Drupal JavaScript coding standards.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -1,4 +1,17 @@
+  /* ¶
+    CJK regex patterns

Adds whitespace.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -1,4 +1,17 @@
+  ¶
+  var CJK_FLAG = false,
+      CJK_REGEX = /[\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F\u3200-\u32FF\uA960-\uA97F\uD7B0-\uD7FF\uFF00-\uFFEF\u4E00-\u9FFF]+?/g;
+      ¶
+  var S_STR = null;

More whitespace; though not in the official standards, it might be good to keep consistent code style on variable declarations. Perhaps a new "var" per line?

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -18,6 +31,13 @@
+    ¶
+    // check if the last two characters contain a CJK symbol, using 2 chars
+    // to account for syllable input
+    var lcharFlag = input.value.charAt( input.value.length-2 ).match( CJK_REGEX );

More whitespace; also comments should start with a capital letter and end with a period.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -18,6 +31,13 @@
+    CJK_FLAG = ( CJK_REGEX.test( input.value ) ) ? true : false;

Code style issue: remove all spaces from within control structures.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -34,6 +54,13 @@
+        if( CJK_FLAG && input.value.length > 0 ) {

Whitespace within control structures again. Also there should be a space between the control structure and the boolean phrase. e.g. if (...) as opposed to if(...)

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -34,6 +54,13 @@
+          return false;
+        } else {
+          this.hidePopup(e.keyCode);

Also, "else" phrases should be on a new line, below the closing brace of the "if"

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -34,6 +54,13 @@
+        ¶

Adds whitespace.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -44,10 +71,12 @@
+        if ( !CJK_FLAG && input.value.length > 0 && lcharFlag === null ){

Another instance of whitespace within control structures.

+++ b/html/sites/all/modules/google_appliance_suggest/google_appliance_suggest.jsundefined
@@ -44,10 +71,12 @@
+        } else {

Another instance of the "else" starting on the same line as the closing "if" brace.

angrytoast’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB

Updated with coding style fixes:

iamEAP’s picture

Status: Needs review » Needs work

Ah, patch doesn't apply. You'll need to make the patch from the project root / repo, as opposed to your Drupal root. Otherwise, looks good.

See git instructions and/or Creating Drupal patches with git.

angrytoast’s picture

Status: Needs work » Needs review
StatusFileSize
new1.96 KB

Regenerated patch with drupal git repo structure:

iamEAP’s picture

Status: Needs review » Fixed

Committed fbe1431

Thanks for your contribution!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.