浏览代码

HUE-8153 [docs] Show expanded TOC tree by default and highlight search keyword

Enrico Berti 7 年之前
父节点
当前提交
e410494
共有 6 个文件被更改,包括 87 次插入4 次删除
  1. 1 0
      docs/admin-manual/manual.md
  2. 11 2
      docs/js/hue-docs.js
  3. 72 0
      docs/js/jquery.highlight.js
  4. 1 2
      docs/js/jquery.treed.js
  5. 1 0
      docs/sdk/sdk.md
  6. 1 0
      docs/user-guide/user-guide.md

+ 1 - 0
docs/admin-manual/manual.md

@@ -1258,6 +1258,7 @@ your bug reports.
 
 <script src="../js/jquery.min.js"></script>
 <script src="../js/jquery.treed.js"></script>
+<script src="../js/jquery.highlight.js"></script>
 <script src="../js/hue-docs.js"></script>
 
 

+ 11 - 2
docs/js/hue-docs.js

@@ -24,6 +24,15 @@ $(document).ready(function () {
     if (![13, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
       window.clearTimeout(tocTimeout);
       tocTimeout = window.setTimeout(function () {
+        $('.highlight').each(function () {
+          $(this).replaceWith(this.childNodes);
+        });
+        $('.toc li a').highlight($tocFilter.val(), {
+          bold: true,
+          class: "highlight",
+          ignoreCase: true,
+          wholeWord: false
+        });
         $('.toc li').each(function () {
           if ($(this).text().toLowerCase().indexOf($tocFilter.val().toLowerCase()) > -1) {
             $(this).show();
@@ -35,8 +44,8 @@ $(document).ready(function () {
       }, 300);
     }
   });
-  $(window).on('hashchange', function(){
+  $(window).on('hashchange', function () {
     $('.toc a.highlighted').removeClass('highlighted');
-    $('.toc a[href="'+ window.location.hash +'"]').addClass('highlighted');
+    $('.toc a[href="' + window.location.hash + '"]').addClass('highlighted');
   });
 });

+ 72 - 0
docs/js/jquery.highlight.js

@@ -0,0 +1,72 @@
+/**
+ * highlight 1.0.0
+ * Licensed under MIT
+ *
+ * Copyright (c) 2016 yjteam
+ * http://yjteam.co.kr
+ *
+ * GitHub Repositories
+ * https://github.com/yjseo29/highlight.js
+ */
+
+if (typeof jQuery === 'undefined') {
+  throw new Error('JavaScript requires jQuery')
+}
+
++function ($) {
+  'use strict';
+  var version = $.fn.jquery.split(' ')[0].split('.')
+  if ((version[0] < 2 && version[1] < 9) || (version[0] == 1 && version[1] == 9 && version[2] < 1)) {
+    throw new Error('JavaScript requires jQuery version 1.9.1 or higher')
+  }
+}(jQuery);
+
++function ($) {
+  $.fn.highlight = function (word, options) {
+    var option = $.extend({
+      background: "#ffff00",
+      color: "#000",
+      bold: false,
+      class: "",
+      ignoreCase: true,
+      wholeWord: true
+    }, options);
+    var findCnt = 0;
+
+    if (this.length == 0) {
+      throw new Error('Node was not found')
+    }
+
+    var $el = $('<span style="color:' + option.color + ';"></span>');
+    if (option.bold) {
+      $el.css("font-weight", "bold");
+    }
+    if (option.background != "") {
+      $el.css("background", option.background);
+    }
+    if (option.class != "") {
+      $el.addClass(option.class);
+    }
+
+    if (option.wholeWord) {
+      word = "\\b" + escapeRegExp(word) + "\\b";
+    }
+    var re = new RegExp(word, option.ignoreCase == true ? 'gi' : 'g');
+
+    this.each(function () {
+      var content = $(this).html();
+
+      $(this).html(content.replace(re, function (t) {
+        findCnt++;
+        $el.text(t);
+        return $el.get(0).outerHTML;
+      }));
+
+    });
+    return findCnt;
+
+    function escapeRegExp(string) {
+      return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
+    }
+  }
+}(jQuery);

+ 1 - 2
docs/js/jquery.treed.js

@@ -22,7 +22,7 @@ $.fn.extend({
     tree.addClass("tree");
     tree.find('li').has("ul").each(function () {
       var branch = $(this);
-      branch.prepend("<i class='indicator fa fa-fw " + closedClass + "'></i>");
+      branch.prepend("<i class='indicator fa fa-fw " + openedClass + "'></i>");
       branch.addClass('branch');
       branch.on('click', function (e) {
         if (this == e.target) {
@@ -35,7 +35,6 @@ $.fn.extend({
           });
         }
       });
-      branch.children().children().toggle();
     });
     tree.find('li').not(":has(ul)").each(function () {
       var branch = $(this);

+ 1 - 0
docs/sdk/sdk.md

@@ -1103,6 +1103,7 @@ and greatest in build technologies.
 
 <script src="../js/jquery.min.js"></script>
 <script src="../js/jquery.treed.js"></script>
+<script src="../js/jquery.highlight.js"></script>
 <script src="../js/hue-docs.js"></script>
 
 

+ 1 - 0
docs/user-guide/user-guide.md

@@ -1469,6 +1469,7 @@ A basic read only version is done [HUE-951](https://issues.cloudera.org/browse/H
 
 <script src="../js/jquery.min.js"></script>
 <script src="../js/jquery.treed.js"></script>
+<script src="../js/jquery.highlight.js"></script>
 <script src="../js/hue-docs.js"></script>
 
 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-37637545-1"></script>