jquery.hiveautocomplete.js 12 KB

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