sqoop.autocomplete.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var autocomplete = (function($) {
  17. function cas(server_type, database, table, value) {
  18. var key = '';
  19. if (server_type) {
  20. if (database) {
  21. if (table) {
  22. key = 'sqoop_' + server_type + "_" + database + "_" + table;
  23. } else {
  24. key = 'sqoop_' + server_type + "_" + database;
  25. }
  26. } else {
  27. key = 'sqoop_' + server_type;
  28. }
  29. }
  30. var val = $.totalStorage(key) || [];
  31. if (value) {
  32. $.totalStorage(key, value);
  33. }
  34. return val;
  35. }
  36. var loading = {
  37. 'databases': false,
  38. 'tables': false,
  39. 'columns': false
  40. };
  41. return {
  42. 'databases': function(server_type) {
  43. if (!loading.databases && server_type) {
  44. loading.databases = true;
  45. $(document).trigger('fetch.databases', []);
  46. var request = {
  47. url: '/sqoop/api/autocomplete/databases/?server_type=' + server_type,
  48. dataType: 'json',
  49. type: 'GET',
  50. success: function(data) {
  51. var databases = cas(server_type, null, null, data.databases);
  52. $(document).trigger('fetched.databases', [databases]);
  53. },
  54. error: function() {},
  55. always: function() {
  56. loading.databases = false;
  57. }
  58. };
  59. $.ajax(request);
  60. }
  61. return cas(server_type);
  62. },
  63. 'tables': function(server_type, database) {
  64. if (!loading.tables && server_type && database) {
  65. loading.tables = true;
  66. $(document).trigger('fetch.tables', []);
  67. var request = {
  68. url: '/sqoop/api/autocomplete/databases/' + database + '/tables?server_type=' + server_type,
  69. dataType: 'json',
  70. type: 'GET',
  71. success: function(data) {
  72. var tables = cas(server_type, database, null, data.tables);
  73. $(document).trigger('fetched.tables', [tables]);
  74. },
  75. error: function() {},
  76. always: function() {
  77. loading.tables = false;
  78. }
  79. };
  80. $.ajax(request);
  81. }
  82. return cas(server_type, database);
  83. },
  84. 'columns': function(server_type, database, table) {
  85. if (!loading.columns && server_type && database && table) {
  86. loading.columns = true;
  87. $(document).trigger('fetch.columns', []);
  88. var request = {
  89. url: '/sqoop/api/autocomplete/databases/' + database + '/tables/' + table + '/columns?server_type=' + server_type,
  90. dataType: 'json',
  91. type: 'GET',
  92. success: function(data) {
  93. var columns = cas(server_type, database, table, data.columns);
  94. $(document).trigger('fetched.columns', [columns]);
  95. },
  96. error: function() {},
  97. always: function() {
  98. loading.columns = false;
  99. }
  100. };
  101. $.ajax(request);
  102. }
  103. return cas(server_type, database, table);
  104. }
  105. };
  106. })($);