jquery.hdfsautocomplete.js 11 KB

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