MDL-62491 navigation: Update to tree.js to allow callbacks.
authorAdrian Greeve <abgreeve@gmail.com>
Tue, 2 Oct 2018 05:02:30 +0000 (13:02 +0800)
committerAdrian Greeve <abgreeve@gmail.com>
Mon, 8 Oct 2018 01:00:08 +0000 (09:00 +0800)
lib/amd/build/tree.min.js
lib/amd/src/tree.js

index 8d52c61..077b92c 100644 (file)
Binary files a/lib/amd/build/tree.min.js and b/lib/amd/build/tree.min.js differ
index 0938056..24fd7da 100644 (file)
@@ -70,6 +70,10 @@ define(['jquery'], function($) {
         this.bindEventHandlers();
     };
 
+    Tree.prototype.registerEnterCallback = function(callback) {
+        this.enterCallback = callback;
+    };
+
     /**
      * Find all visible tree items and save a cache of them on the tree object.
      *
@@ -136,7 +140,13 @@ define(['jquery'], function($) {
      * @returns {bool}
      */
     Tree.prototype.getGroupFromItem = function(item) {
-        return this.treeRoot.find('#' + item.attr('aria-owns')) || item.children('[role=group]');
+        var ariaowns = this.treeRoot.find('#' + item.attr('aria-owns'));
+        var plain = item.children('[role=group]');
+        if (ariaowns.length > plain.length) {
+            return ariaowns;
+        } else {
+            return plain;
+        }
     };
 
     /**
@@ -371,7 +381,12 @@ define(['jquery'], function($) {
             case this.keys.enter: {
                 var links = item.children('a').length ? item.children('a') : item.children().not(SELECTORS.GROUP).find('a');
                 if (links.length) {
-                    window.location.href = links.first().attr('href');
+                    // See if we have a callback.
+                    if (typeof this.enterCallback === 'function') {
+                        this.enterCallback(item);
+                    } else {
+                        window.location.href = links.first().attr('href');
+                    }
                 } else if (this.isGroupItem(item)) {
                     this.toggleGroup(item, true);
                 }