jquery.hiveautocomplete.js 13 KB

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