jquery.hiveautocomplete.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 = "jHueHiveAutocomplete",
  22. defaults = {
  23. serverType: "HIVE",
  24. home: "/",
  25. skipColumns: false,
  26. onEnter: function () {
  27. },
  28. onBlur: function () {
  29. },
  30. onPathChange: function () {
  31. },
  32. smartTooltip: "",
  33. smartTooltipThreshold: 10 // needs 10 up/down or click actions and no tab to activate the smart tooltip
  34. };
  35. function Plugin(element, options) {
  36. this.element = element;
  37. this.options = $.extend({}, defaults, options);
  38. this._defaults = defaults;
  39. this._name = pluginName;
  40. this.init();
  41. }
  42. Plugin.prototype.init = function () {
  43. var _this = this;
  44. var _el = $(_this.element);
  45. // creates autocomplete popover
  46. if ($("#jHueHiveAutocomplete").length == 0) {
  47. $("<div>").attr("id", "jHueHiveAutocomplete").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"><ul class="unstyled"></ul></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. showHiveAutocomplete(function () {
  78. var path = _el.val();
  79. if (path.indexOf(".") > -1) {
  80. path = path.substr(path.lastIndexOf(".") + 1);
  81. }
  82. guessHivePath(path);
  83. });
  84. }
  85. });
  86. function smartTooltipMaker() {
  87. if (_this.options.smartTooltip != "" && typeof $.totalStorage != "undefined" && $.totalStorage("jHueHiveAutocompleteTooltip") != -1) {
  88. var cnt = 0;
  89. if ($.totalStorage("jHueHiveAutocompleteTooltip") != null) {
  90. cnt = $.totalStorage("jHueHiveAutocompleteTooltip") + 1;
  91. }
  92. $.totalStorage("jHueHiveAutocompleteTooltip", 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("jHueHiveAutocompleteTooltip", -1);
  104. }
  105. }
  106. }
  107. $(window).on("scroll", function(){
  108. $("#jHueHiveAutocomplete").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, [17, 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. $("#jHueHiveAutocomplete ul li").show();
  122. if (path != ""){
  123. $("#jHueHiveAutocomplete ul li").each(function () {
  124. if ($(this).text().trim().indexOf(path) != 0) {
  125. $(this).hide();
  126. }
  127. });
  128. }
  129. }, 500);
  130. }
  131. if (e.keyCode == 38) {
  132. if (_hdfsAutocompleteSelectedIndex <= 0) {
  133. _hdfsAutocompleteSelectedIndex = $("#jHueHiveAutocomplete ul li:visible").length - 1;
  134. }
  135. else {
  136. _hdfsAutocompleteSelectedIndex--;
  137. }
  138. }
  139. if (e.keyCode == 40) {
  140. if (_hdfsAutocompleteSelectedIndex == $("#jHueHiveAutocomplete ul li:visible").length - 1) {
  141. _hdfsAutocompleteSelectedIndex = 0;
  142. }
  143. else {
  144. _hdfsAutocompleteSelectedIndex++;
  145. }
  146. }
  147. if (e.keyCode == 38 || e.keyCode == 40) {
  148. smartTooltipMaker();
  149. $("#jHueHiveAutocomplete ul li").removeClass("active");
  150. $("#jHueHiveAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).addClass("active");
  151. $("#jHueHiveAutocomplete .popover-content").scrollTop($("#jHueHiveAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).prevAll().length * $("#jHueHiveAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).outerHeight());
  152. }
  153. if ((e.keyCode == 32 && e.ctrlKey) || e.keyCode == 191) {
  154. smartTooltipMaker();
  155. showHiveAutocomplete();
  156. }
  157. if (e.keyCode == 13) {
  158. if (_hdfsAutocompleteSelectedIndex > -1) {
  159. $("#jHueHiveAutocomplete ul li:visible").eq(_hdfsAutocompleteSelectedIndex).click();
  160. }
  161. else {
  162. _this.options.onEnter($(this));
  163. }
  164. $("#jHueHiveAutocomplete").hide();
  165. _hdfsAutocompleteSelectedIndex = -1;
  166. }
  167. });
  168. var _pauseBlur = false;
  169. _el.blur(function () {
  170. if (!_pauseBlur) {
  171. $(document.body).off("contextmenu");
  172. $("#jHueHiveAutocomplete").hide();
  173. _this.options.onBlur();
  174. }
  175. });
  176. var BASE_PATH = "/beeswax/api/autocomplete/";
  177. if (_this.options.serverType == "IMPALA"){
  178. BASE_PATH = "/impala/api/autocomplete/";
  179. }
  180. var _currentFiles = [];
  181. function showHiveAutocomplete(callback) {
  182. var path = _el.val();
  183. var autocompleteUrl = BASE_PATH;
  184. if (path != "" && path.indexOf(".") == -1) {
  185. path = "";
  186. }
  187. if (path != "" && path.lastIndexOf(".") != path.length - 1) {
  188. path = path.substring(0, _el.val().lastIndexOf("."));
  189. }
  190. autocompleteUrl += path.replace(/\./g, "/");
  191. $.getJSON(autocompleteUrl, function (data) {
  192. if (data.error == null) {
  193. _currentFiles = [];
  194. var _ico = "";
  195. var _iterable = [];
  196. var _isSkipColumns = false;
  197. if (data.databases != null){ // it's a db
  198. _iterable = data.databases;
  199. _ico = "fa-database";
  200. }
  201. else if (data.tables != null) { // it's a table
  202. _iterable = data.tables;
  203. _ico = "fa-table";
  204. }
  205. else {
  206. if (! _this.options.skipColumns) {
  207. _iterable = data.columns;
  208. _ico = "fa-columns";
  209. }
  210. else {
  211. _isSkipColumns = true;
  212. }
  213. }
  214. if (! _isSkipColumns){
  215. $(_iterable).each(function (cnt, item) {
  216. _currentFiles.push('<li class="hiveAutocompleteItem" data-value="' + item + '"><i class="fa '+ _ico +'"></i> ' + item + '</li>');
  217. });
  218. $("#jHueHiveAutocomplete").css("top", _el.offset().top + _el.outerHeight() - 1).css("left", _el.offset().left).width(_el.outerWidth() - 4);
  219. $("#jHueHiveAutocomplete").find("ul").empty().html(_currentFiles.join(""));
  220. $("#jHueHiveAutocomplete").find("li").on("click", function (e) {
  221. smartTooltipMaker();
  222. e.preventDefault();
  223. var item = $(this).text().trim();
  224. var path = autocompleteUrl.substring(BASE_PATH.length);
  225. if ($(this).html().indexOf("database") > -1){
  226. _el.val(item + ".");
  227. _this.options.onPathChange(_el.val());
  228. showHiveAutocomplete();
  229. }
  230. if ($(this).html().indexOf("table") > -1){
  231. if (_el.val().indexOf(".") > -1){
  232. if (_el.val().match(/\./gi).length == 1){
  233. _el.val(_el.val().substring(0, _el.val().lastIndexOf(".") + 1) + item);
  234. }
  235. else {
  236. _el.val(_el.val().substring(0, _el.val().indexOf(".") + 1) + item);
  237. }
  238. }
  239. else {
  240. _el.val(_el.val() + item);
  241. }
  242. if (! _this.options.skipColumns){
  243. _el.val(_el.val() + ".");
  244. }
  245. _this.options.onPathChange(_el.val());
  246. if (! _this.options.skipColumns) {
  247. showHiveAutocomplete();
  248. }
  249. }
  250. if ($(this).html().indexOf("columns") > -1){
  251. if (_el.val().match(/\./gi).length > 1){
  252. _el.val(_el.val().substring(0, _el.val().lastIndexOf(".") + 1) + item);
  253. }
  254. else {
  255. _el.val(_el.val() + "." + item);
  256. }
  257. $("#jHueHiveAutocomplete").hide();
  258. _hdfsAutocompleteSelectedIndex = -1;
  259. _this.options.onEnter(_el);
  260. }
  261. });
  262. $("#jHueHiveAutocomplete").show();
  263. window.setTimeout(function(){
  264. setHueBreadcrumbCaretAtEnd(_this.element);
  265. }, 100)
  266. if ("undefined" != typeof callback) {
  267. callback();
  268. }
  269. }
  270. }
  271. });
  272. }
  273. $(document).on("mouseenter", ".hiveAutocompleteItem", function () {
  274. _pauseBlur = true;
  275. });
  276. $(document).on("mouseout", ".hiveAutocompleteItem", function () {
  277. _pauseBlur = false;
  278. })
  279. function guessHivePath(lastChars) {
  280. var possibleMatches = [];
  281. for (var i = 0; i < _currentFiles.length; i++) {
  282. if (($(_currentFiles[i]).text().trim().indexOf(lastChars) == 0 || lastChars == "") && $(_currentFiles[i]).text().trim() != "..") {
  283. possibleMatches.push(_currentFiles[i]);
  284. }
  285. }
  286. if (possibleMatches.length == 1) {
  287. _el.val(_el.val() + $(possibleMatches[0]).text().trim().substr(lastChars.length));
  288. if ($(possibleMatches[0]).html().indexOf("folder") > -1) {
  289. _el.val(_el.val() + "/");
  290. showHiveAutocomplete();
  291. }
  292. }
  293. else if (possibleMatches.length > 1) {
  294. // finds the longest common prefix
  295. var possibleMatchesPlain = [];
  296. for (var z = 0; z < possibleMatches.length; z++) {
  297. possibleMatchesPlain.push($(possibleMatches[z]).text().trim());
  298. }
  299. var arr = possibleMatchesPlain.slice(0).sort(),
  300. word1 = arr[0], word2 = arr[arr.length - 1],
  301. j = 0;
  302. while (word1.charAt(j) == word2.charAt(j))++j;
  303. var match = word1.substring(0, j);
  304. _el.val(_el.val() + match.substr(lastChars.length));
  305. }
  306. }
  307. };
  308. Plugin.prototype.setOptions = function (options) {
  309. this.options = $.extend({}, defaults, options);
  310. };
  311. $.fn[pluginName] = function (options) {
  312. return this.each(function () {
  313. if (!$.data(this, 'plugin_' + pluginName)) {
  314. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  315. }
  316. });
  317. }
  318. $[pluginName] = function (options) {
  319. if (typeof console != "undefined") {
  320. console.warn("$(elem).jHueHiveAutocomplete() is a preferred call method.");
  321. }
  322. $(options.element).jHueHiveAutocomplete(options);
  323. };
  324. })(jQuery, window, document);