jquery.hiveautocomplete.js 12 KB

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