jquery.hdfsautocomplete.js 11 KB

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