jquery.hdfsautocomplete.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. var _hdfsAutocompleteSelectedIndex = -1;
  111. var _filterTimeout = -1;
  112. _el.keyup(function (e) {
  113. window.clearTimeout(_filterTimeout);
  114. if ($.inArray(e.keyCode, [38, 40, 13, 32, 191]) == -1) {
  115. _hdfsAutocompleteSelectedIndex = -1;
  116. _filterTimeout = window.setTimeout(function () {
  117. var path = _el.val();
  118. if (path.indexOf("/") > -1) {
  119. path = path.substr(path.lastIndexOf("/") + 1);
  120. }
  121. $("#jHueHdfsAutocomplete ul li").show();
  122. $("#jHueHdfsAutocomplete ul li").each(function () {
  123. if ($(this).text().trim().indexOf(path) != 0) {
  124. $(this).hide();
  125. }
  126. });
  127. }, 500);
  128. }
  129. if (e.keyCode == 38) {
  130. if (_hdfsAutocompleteSelectedIndex <= 0) {
  131. _hdfsAutocompleteSelectedIndex = $("#jHueHdfsAutocomplete ul li:visible").length - 1;
  132. }
  133. else {
  134. _hdfsAutocompleteSelectedIndex--;
  135. }
  136. }
  137. if (e.keyCode == 40) {
  138. if (_hdfsAutocompleteSelectedIndex == $("#jHueHdfsAutocomplete ul li:visible").length - 1) {
  139. _hdfsAutocompleteSelectedIndex = 0;
  140. }
  141. else {
  142. _hdfsAutocompleteSelectedIndex++;
  143. }
  144. }
  145. if (e.keyCode == 38 || e.keyCode == 40) {
  146. smartTooltipMaker();
  147. $("#jHueHdfsAutocomplete ul li").removeClass("active");
  148. $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).addClass("active");
  149. $("#jHueHdfsAutocomplete .popover-content").scrollTop($("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).prevAll().length * $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).outerHeight());
  150. }
  151. if ((e.keyCode == 32 && e.ctrlKey) || e.keyCode == 191) {
  152. smartTooltipMaker();
  153. showHdfsAutocomplete();
  154. }
  155. if (e.keyCode == 13) {
  156. if (_hdfsAutocompleteSelectedIndex > -1) {
  157. $("#jHueHdfsAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).click();
  158. }
  159. else {
  160. _this.options.onEnter($(this));
  161. }
  162. $("#jHueHdfsAutocomplete").hide();
  163. _hdfsAutocompleteSelectedIndex = -1;
  164. }
  165. });
  166. if (_this.options.showOnFocus){
  167. _el.on("focus", function(){
  168. showHdfsAutocomplete();
  169. });
  170. }
  171. var _pauseBlur = false;
  172. _el.blur(function () {
  173. if (!_pauseBlur) {
  174. $(document.body).off("contextmenu");
  175. $("#jHueHdfsAutocomplete").hide();
  176. _this.options.onBlur();
  177. }
  178. });
  179. var BASE_PATH = "/filebrowser/view";
  180. var _currentFiles = [];
  181. function showHdfsAutocomplete(callback) {
  182. var path = _el.val();
  183. var autocompleteUrl = BASE_PATH;
  184. if (path.indexOf("/") == 0) {
  185. autocompleteUrl += path.substr(0, path.lastIndexOf("/"));
  186. }
  187. else if (path.indexOf("/") > 0) {
  188. autocompleteUrl += _this.options.home + path.substr(0, path.lastIndexOf("/"));
  189. }
  190. else {
  191. autocompleteUrl += _this.options.home;
  192. }
  193. $.getJSON(autocompleteUrl + "?pagesize=1000&format=json", function (data) {
  194. _currentFiles = [];
  195. if (data.error == null) {
  196. $(data.files).each(function (cnt, item) {
  197. if (item.name != ".") {
  198. var ico = "fa-file-o";
  199. if (item.type == "dir") {
  200. ico = "fa-folder";
  201. }
  202. _currentFiles.push('<li class="hdfsAutocompleteItem" data-value="' + item.name + '"><i class="fa ' + ico + '"></i> ' + item.name + '</li>');
  203. }
  204. });
  205. window.setTimeout(function () {
  206. $("#jHueHdfsAutocomplete").css("top", _el.offset().top + _el.outerHeight() - 1).css("left", _el.offset().left).width(_el.outerWidth() - 4);
  207. $("#jHueHdfsAutocomplete").find("ul").empty().html(_currentFiles.join(""));
  208. $("#jHueHdfsAutocomplete").find("li").on("click", function (e) {
  209. smartTooltipMaker();
  210. e.preventDefault();
  211. var item = $(this).text().trim();
  212. var path = autocompleteUrl.substring(BASE_PATH.length);
  213. if (item == "..") { // one folder up
  214. _el.val(path.substring(0, path.lastIndexOf("/")));
  215. }
  216. else {
  217. _el.val(path + "/" + item);
  218. }
  219. if ($(this).html().indexOf("folder") > -1) {
  220. _el.val(_el.val() + "/");
  221. _this.options.onPathChange(_el.val());
  222. showHdfsAutocomplete();
  223. }
  224. else {
  225. _this.options.onEnter(_el);
  226. }
  227. });
  228. $("#jHueHdfsAutocomplete").show();
  229. setHueBreadcrumbCaretAtEnd(_this.element);
  230. if ("undefined" != typeof callback) {
  231. callback();
  232. }
  233. }, 100); // timeout for IE8
  234. }
  235. });
  236. }
  237. $(document).on("mouseenter", ".hdfsAutocompleteItem", function () {
  238. _pauseBlur = true;
  239. });
  240. $(document).on("mouseout", ".hdfsAutocompleteItem", function () {
  241. _pauseBlur = false;
  242. })
  243. function guessHdfsPath(lastChars) {
  244. var possibleMatches = [];
  245. for (var i = 0; i < _currentFiles.length; i++) {
  246. if (($(_currentFiles[i]).text().trim().indexOf(lastChars) == 0 || lastChars == "") && $(_currentFiles[i]).text().trim() != "..") {
  247. possibleMatches.push(_currentFiles[i]);
  248. }
  249. }
  250. if (possibleMatches.length == 1) {
  251. _el.val(_el.val() + $(possibleMatches[0]).text().trim().substr(lastChars.length));
  252. if ($(possibleMatches[0]).html().indexOf("folder") > -1) {
  253. _el.val(_el.val() + "/");
  254. showHdfsAutocomplete();
  255. }
  256. }
  257. else if (possibleMatches.length > 1) {
  258. // finds the longest common prefix
  259. var possibleMatchesPlain = [];
  260. for (var z = 0; z < possibleMatches.length; z++) {
  261. possibleMatchesPlain.push($(possibleMatches[z]).text().trim());
  262. }
  263. var arr = possibleMatchesPlain.slice(0).sort(),
  264. word1 = arr[0], word2 = arr[arr.length - 1],
  265. j = 0;
  266. while (word1.charAt(j) == word2.charAt(j))++j;
  267. var match = word1.substring(0, j);
  268. _el.val(_el.val() + match.substr(lastChars.length));
  269. }
  270. }
  271. };
  272. Plugin.prototype.setOptions = function (options) {
  273. this.options = $.extend({}, defaults, options);
  274. };
  275. $.fn[pluginName] = function (options) {
  276. return this.each(function () {
  277. if (!$.data(this, 'plugin_' + pluginName)) {
  278. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  279. }
  280. });
  281. }
  282. $[pluginName] = function (options) {
  283. if (typeof console != "undefined") {
  284. console.warn("$(elem).jHueHdfsAutocomplete() is a preferred call method.");
  285. }
  286. $(options.element).jHueHdfsAutocomplete(options);
  287. };
  288. })(jQuery, window, document);