jquery.hdfsautocomplete.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue HDFS autocomplete plugin
  18. * augment a textbox into an HDFS autocomplete
  19. */
  20. (function ($, window, document, undefined) {
  21. var pluginName = "jHueHdfsAutocomplete",
  22. defaults = {
  23. home: "/",
  24. onEnter: function () {
  25. },
  26. onBlur: function () {
  27. },
  28. smartTooltip: "",
  29. smartTooltipThreshold: 10 // needs 10 up/down or click actions and no tab to activate the smart tooltip
  30. };
  31. function Plugin(element, options) {
  32. this.element = element;
  33. this.options = $.extend({}, defaults, options);
  34. this._defaults = defaults;
  35. this._name = pluginName;
  36. this.init();
  37. }
  38. Plugin.prototype.init = function () {
  39. var _this = this;
  40. var _el = $(_this.element);
  41. // creates autocomplete popover
  42. if ($("#jHueHdfsAutocomplete").length == 0) {
  43. $("<div>").attr("id", "jHueHdfsAutocomplete").addClass("popover")
  44. .addClass("bottom").attr("style", "position:absolute;display:none;max-width:1000px;z-index:33000")
  45. .html('<div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p><ul class="unstyled"></ul></p></div></div>')
  46. .appendTo($("body"));
  47. }
  48. function setHueBreadcrumbCaretAtEnd(element) {
  49. var elemLength = element.value.length;
  50. if (document.selection) {
  51. element.focus();
  52. var _oSel = document.selection.createRange();
  53. _oSel.moveStart('character', -elemLength);
  54. _oSel.moveStart('character', elemLength);
  55. _oSel.moveEnd('character', 0);
  56. _oSel.select();
  57. }
  58. else if (element.selectionStart || element.selectionStart == '0') {
  59. element.selectionStart = elemLength;
  60. element.selectionEnd = elemLength;
  61. element.focus();
  62. }
  63. }
  64. _el.focus(function () {
  65. $(document.body).on("contextmenu", function (e) {
  66. e.preventDefault(); // prevents native menu on FF for Mac from being shown
  67. });
  68. setHueBreadcrumbCaretAtEnd(_this.element);
  69. });
  70. _el.keydown(function (e) {
  71. if (e.keyCode == 9) {
  72. e.preventDefault();
  73. showHdfsAutocomplete(function () {
  74. var path = _el.val();
  75. if (path.indexOf("/") > -1) {
  76. path = path.substr(path.lastIndexOf("/") + 1);
  77. }
  78. guessHdfsPath(path);
  79. });
  80. }
  81. });
  82. function smartTooltipMaker() {
  83. if (_this.options.smartTooltip != "" && typeof $.totalStorage != "undefined" && $.totalStorage("jHueHdfsAutocompleteTooltip") != -1) {
  84. var cnt = 0;
  85. if ($.totalStorage("jHueHdfsAutocompleteTooltip") != null) {
  86. cnt = $.totalStorage("jHueHdfsAutocompleteTooltip") + 1;
  87. }
  88. $.totalStorage("jHueHdfsAutocompleteTooltip", cnt);
  89. if (cnt >= _this.options.smartTooltipThreshold) {
  90. _el.tooltip({
  91. animation: true,
  92. title: _this.options.smartTooltip,
  93. trigger: "manual",
  94. placement: "top"
  95. }).tooltip("show");
  96. window.setTimeout(function () {
  97. _el.tooltip("hide");
  98. }, 10000);
  99. $.totalStorage("jHueHdfsAutocompleteTooltip", -1);
  100. }
  101. }
  102. }
  103. var _hdfsAutocompleteSelectedIndex = -1;
  104. var _filterTimeout = -1;
  105. _el.keyup(function (e) {
  106. window.clearTimeout(_filterTimeout);
  107. if ($.inArray(e.keyCode, [38, 40, 13, 32, 191]) == -1) {
  108. _hdfsAutocompleteSelectedIndex = -1;
  109. _filterTimeout = window.setTimeout(function () {
  110. var path = _el.val();
  111. if (path.indexOf("/") > -1) {
  112. path = path.substr(path.lastIndexOf("/") + 1);
  113. }
  114. $("#jHueHdfsAutocomplete ul li").show();
  115. $("#jHueHdfsAutocomplete ul li").each(function () {
  116. if ($(this).text().trim().indexOf(path) != 0) {
  117. $(this).hide();
  118. }
  119. });
  120. }, 500);
  121. }
  122. if (e.keyCode == 38) {
  123. if (_hdfsAutocompleteSelectedIndex <= 0) {
  124. _hdfsAutocompleteSelectedIndex = $("#jHueHdfsAutocomplete ul li:visible").length - 1;
  125. }
  126. else {
  127. _hdfsAutocompleteSelectedIndex--;
  128. }
  129. }
  130. if (e.keyCode == 40) {
  131. if (_hdfsAutocompleteSelectedIndex == $("#jHueHdfsAutocomplete ul li:visible").length - 1) {
  132. _hdfsAutocompleteSelectedIndex = 0;
  133. }
  134. else {
  135. _hdfsAutocompleteSelectedIndex++;
  136. }
  137. }
  138. if (e.keyCode == 38 || e.keyCode == 40) {
  139. smartTooltipMaker();
  140. $("#jHueHdfsAutocomplete ul li").removeClass("active");
  141. $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).addClass("active");
  142. $("#jHueHdfsAutocomplete .popover-content").scrollTop($("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).prevAll().length * $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).outerHeight());
  143. }
  144. if ((e.keyCode == 32 && e.ctrlKey) || e.keyCode == 191) {
  145. smartTooltipMaker();
  146. showHdfsAutocomplete();
  147. }
  148. if (e.keyCode == 13) {
  149. if (_hdfsAutocompleteSelectedIndex > -1) {
  150. $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).click();
  151. }
  152. else {
  153. _this.options.onEnter($(this));
  154. }
  155. $("#jHueHdfsAutocomplete").hide();
  156. _hdfsAutocompleteSelectedIndex = -1;
  157. }
  158. });
  159. var _pauseBlur = false;
  160. _el.blur(function () {
  161. if (!_pauseBlur) {
  162. $(document.body).off("contextmenu");
  163. $("#jHueHdfsAutocomplete").hide();
  164. _this.options.onBlur();
  165. }
  166. });
  167. var BASE_PATH = "/filebrowser/view";
  168. var _currentFiles = [];
  169. function showHdfsAutocomplete(callback) {
  170. var path = _el.val();
  171. var autocompleteUrl = BASE_PATH;
  172. if (path.indexOf("/") == 0) {
  173. autocompleteUrl += path.substr(0, path.lastIndexOf("/"));
  174. }
  175. else if (path.indexOf("/") > 0) {
  176. autocompleteUrl += _this.options.home + path.substr(0, path.lastIndexOf("/"));
  177. }
  178. else {
  179. autocompleteUrl += _this.options.home;
  180. }
  181. $.getJSON(autocompleteUrl + "?pagesize=1000&format=json", function (data) {
  182. _currentFiles = [];
  183. if (data.error == null) {
  184. $(data.files).each(function (cnt, item) {
  185. if (item.name != ".") {
  186. var ico = "fa-file-o";
  187. if (item.type == "dir") {
  188. ico = "fa-folder";
  189. }
  190. _currentFiles.push('<li class="hdfsAutocompleteItem" data-value="' + item.name + '"><i class="fa ' + ico + '"></i> ' + item.name + '</li>');
  191. }
  192. });
  193. window.setTimeout(function () {
  194. $("#jHueHdfsAutocomplete").css("top", _el.offset().top + _el.outerHeight()).css("left", _el.offset().left).width(_el.width());
  195. $("#jHueHdfsAutocomplete").find("ul").empty().html(_currentFiles.join(""));
  196. $("#jHueHdfsAutocomplete").find("li").on("click", function (e) {
  197. smartTooltipMaker();
  198. e.preventDefault();
  199. var item = $(this).text().trim();
  200. var path = autocompleteUrl.substring(BASE_PATH.length);
  201. if (item == "..") { // one folder up
  202. _el.val(path.substring(0, path.lastIndexOf("/")));
  203. }
  204. else {
  205. _el.val(path + "/" + item);
  206. }
  207. if ($(this).html().indexOf("folder") > -1) {
  208. _el.val(_el.val() + "/");
  209. showHdfsAutocomplete();
  210. }
  211. else {
  212. _this.options.onEnter(_el);
  213. }
  214. });
  215. $("#jHueHdfsAutocomplete").show();
  216. setHueBreadcrumbCaretAtEnd(_this.element);
  217. if ("undefined" != typeof callback) {
  218. callback();
  219. }
  220. }, 100); // timeout for IE8
  221. }
  222. });
  223. }
  224. $(document).on("mouseenter", ".hdfsAutocompleteItem", function () {
  225. _pauseBlur = true;
  226. });
  227. $(document).on("mouseout", ".hdfsAutocompleteItem", function () {
  228. _pauseBlur = false;
  229. })
  230. function guessHdfsPath(lastChars) {
  231. var possibleMatches = [];
  232. for (var i = 0; i < _currentFiles.length; i++) {
  233. if (($(_currentFiles[i]).text().trim().indexOf(lastChars) == 0 || lastChars == "") && $(_currentFiles[i]).text().trim() != "..") {
  234. possibleMatches.push(_currentFiles[i]);
  235. }
  236. }
  237. if (possibleMatches.length == 1) {
  238. _el.val(_el.val() + $(possibleMatches[0]).text().trim().substr(lastChars.length));
  239. if ($(possibleMatches[0]).html().indexOf("folder") > -1) {
  240. _el.val(_el.val() + "/");
  241. showHdfsAutocomplete();
  242. }
  243. }
  244. else if (possibleMatches.length > 1) {
  245. // finds the longest common prefix
  246. var possibleMatchesPlain = [];
  247. for (var z = 0; z < possibleMatches.length; z++) {
  248. possibleMatchesPlain.push($(possibleMatches[z]).text().trim());
  249. }
  250. var arr = possibleMatchesPlain.slice(0).sort(),
  251. word1 = arr[0], word2 = arr[arr.length - 1],
  252. j = 0;
  253. while (word1.charAt(j) == word2.charAt(j))++j;
  254. var match = word1.substring(0, j);
  255. _el.val(_el.val() + match.substr(lastChars.length));
  256. }
  257. }
  258. };
  259. Plugin.prototype.setOptions = function (options) {
  260. this.options = $.extend({}, defaults, options);
  261. };
  262. $.fn[pluginName] = function (options) {
  263. return this.each(function () {
  264. if (!$.data(this, 'plugin_' + pluginName)) {
  265. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  266. }
  267. });
  268. }
  269. $[pluginName] = function (options) {
  270. if (typeof console != "undefined") {
  271. console.warn("$(elem).jHueHdfsAutocomplete() is a preferred call method.");
  272. }
  273. $(options.element).jHueHdfsAutocomplete(options);
  274. };
  275. })(jQuery, window, document);