app.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 AppViewModel = function() {
  17. var self = this;
  18. self.station = ko.observable("");
  19. self.pageTitle = ko.observable("");
  20. self.focusModel = ko.observable();
  21. self.cluster = ko.observable("");
  22. self.cluster.subscribe(function() {
  23. app.views.tabledata.name('');
  24. });
  25. self.clusters = ko.observableArray();
  26. self.clusterNames = ko.computed(function() {
  27. return ko.utils.arrayMap(self.clusters(), function (cluster_config) {
  28. return cluster_config.name;
  29. });
  30. });
  31. self.search = new tagsearch();
  32. self.views = {
  33. tables: new DataTableViewModel({columns:['Table Name', 'Enabled'], el: 'views.tables', reload: function(callback) {
  34. var d_self = this;
  35. d_self.items.removeAll();
  36. API.queryCluster("getTableList").done(function(data) {
  37. d_self.items.removeAll(); //need to remove again before callback executes
  38. for(q=0; q<data.length; q++) {
  39. d_self.items.push(new TableDataRow(data[q]));
  40. }
  41. d_self._el.find('a[data-row-selector=true]').jHueRowSelector();
  42. if(callback!=null)
  43. callback();
  44. });
  45. }}),
  46. tabledata: new SmartViewModel({'canWrite': canWrite, el: 'views.tabledata', reload: function(callback) //move inside SmartViewModel class?
  47. {
  48. var t_self = this;
  49. function getColumnFamilies() {
  50. var cols = [];
  51. var cfs = t_self.columnFamilies();
  52. for(var i=0; i<cfs.length; i++) {
  53. if(cfs[i].enabled()) {
  54. cols.push(cfs[i].name);
  55. }
  56. }
  57. return cols;
  58. }
  59. API.queryTable("getRowQuerySet", JSON.stringify(getColumnFamilies()), ko.toJSON(t_self.querySet())).done(function(data) {
  60. if(data.length > 0) {
  61. var keys = Object.keys(data);
  62. var items = [];
  63. for(var i=0; i<keys.length; i++) {
  64. var row = new SmartViewDataRow({'canWrite': canWrite, items: [], row:data[keys[i]].row, reload: function(options) {
  65. var self = this;
  66. options = (options == null) ? {} : options;
  67. options = ko.utils.extend({
  68. callback: function(data){},
  69. columns: getColumnFamilies()
  70. }, options);
  71. API.queryTable("getRow", JSON.stringify(options.columns), prepForTransport(self.row)).done(function(data) {
  72. self.setItems(data.columns);
  73. callback(data);
  74. self.isLoading(false);
  75. });
  76. }});
  77. row.setItems(data[keys[i]].columns);
  78. items.push(row);
  79. }
  80. t_self.items(items);
  81. }
  82. if(typeof(callback) === 'function')
  83. callback();
  84. $('*[data-toggle="tooltip"]').tooltip();
  85. });
  86. }})
  87. };
  88. self.initialize = function() {
  89. return API.query('getClusters').done(function(data) {
  90. app.clusters(data);
  91. });
  92. };
  93. };
  94. var app = new AppViewModel();
  95. ko.applyBindings(app);
  96. //routing
  97. routed = false;
  98. app.initialize().done(function() {
  99. routie({
  100. ':cluster/:table/query': function(cluster, table) {
  101. routie(cluster + '/' + table);
  102. },
  103. ':cluster/:table/query/:query': function(cluster, table, query) {
  104. logGA('query_table');
  105. $.totalStorage('hbase_cluster', cluster);
  106. app.station('table');
  107. app.search.cur_input(query);
  108. Router.setTable(cluster, table);
  109. resetElements();
  110. Views.render('dataview');
  111. app.views.tabledata._reloadcfs(function(){
  112. app.search.evaluate();
  113. app.views.tabledata.searchQuery(query);
  114. });
  115. routed = true;
  116. },
  117. ':cluster/:table': function(cluster, table) {
  118. logGA('view_table');
  119. $.totalStorage('hbase_cluster', cluster);
  120. Router.setTable(cluster, table);
  121. resetSearch();
  122. resetElements();
  123. app.station('table');
  124. Views.render('dataview');
  125. routed = true;
  126. },
  127. ':cluster': function(cluster) {
  128. if ($.inArray(cluster, app.clusterNames()) == -1) {
  129. routie('');
  130. } else {
  131. logGA('view_cluster');
  132. $.totalStorage('hbase_cluster', cluster);
  133. app.station('cluster');
  134. app.cluster(cluster);
  135. app.pageTitle(cluster);
  136. Views.render('clusterview');
  137. resetSearch();
  138. resetElements();
  139. app.views.tabledata.name('');
  140. app.views.tables.reload();
  141. routed = true;
  142. }
  143. resetElements();
  144. routed = true;
  145. },
  146. 'error': function() {
  147. logGA('error');
  148. routed = true;
  149. },
  150. '': function(){
  151. var cluster = $.totalStorage('hbase_cluster');
  152. if (cluster != null && $.inArray(cluster, app.clusterNames()) > -1) {
  153. routie(cluster);
  154. } else {
  155. routie(app.clusterNames()[0]);
  156. }
  157. resetElements();
  158. routed = true;
  159. },
  160. '*': function() {
  161. logGA('');
  162. if(!routed)
  163. history.back();
  164. routed = false;
  165. }
  166. });
  167. });
  168. $.fn.renderElement = function(data){utils.renderElement($(this,data))};
  169. $.fn.showIndicator = function() {
  170. $(this).addClass('isLoading');
  171. }
  172. $.fn.hideIndicator = function() {
  173. $(this).removeClass('isLoading');
  174. }
  175. $.fn.toggleIndicator = function() {
  176. $(this).toggleClass('isLoading');
  177. }
  178. function bindSubmit() {
  179. var self = this;
  180. var data = [];
  181. var hash_cache = {};
  182. if ($(this).attr("id") == "new_table_modal"){
  183. var _cols = [];
  184. $(this).find(".columns li.column").each(function(cnt, column){
  185. var _props = {
  186. name: $(column).find("input[name='table_columns']").val()
  187. };
  188. $(column).find(".columnProperties li").each(function(icnt, property){
  189. if (! $(property).hasClass("columnPropertyEmpty")) {
  190. _props[$(property).find("select").val()] = $(property).find("input[name='table_columns_property_value']").val();
  191. }
  192. });
  193. _cols.push({
  194. properties: _props
  195. });
  196. });
  197. data = [
  198. $(this).find("input[name='cluster']").val(),
  199. $(this).find("input[name='tableName']").val(),
  200. JSON.stringify(_cols)
  201. ]
  202. }
  203. else {
  204. $(this).find('.controls > input, .controls > textarea, .controls > ul input').not('input[type=submit]').each(function() {
  205. if($(this).hasClass('ignore'))
  206. return;
  207. var use_post = $(this).data('use-post');
  208. var submitVal = null;
  209. if($(this).data('subscribe')) {
  210. var target = $($(this).data('subscribe'));
  211. switch(target[0].tagName) {
  212. case "UL":
  213. var serialized = {};
  214. target.find('li').each(function() {
  215. serialized[$(this).find('input')[0].value] = $(this).find('input')[1].value;
  216. });
  217. submitVal = serialized;
  218. use_post = true;
  219. break;
  220. }
  221. }
  222. else if($(this).hasClass('serializeHash')) {
  223. var target = $(this).attr('name');
  224. if(!hash_cache[target])
  225. hash_cache[target] = {};
  226. hash_cache[target][$(this).data(key)] = $(this).val();
  227. }
  228. else {
  229. submitVal = $(this).val();
  230. //change reload next
  231. }
  232. if(submitVal) {
  233. if(use_post) {
  234. submitVal = "hbase-post-key-" + JSON.stringify(submitVal);
  235. } else {
  236. submitVal = prepForTransport(submitVal);
  237. }
  238. data.push(submitVal);
  239. }
  240. });
  241. }
  242. $(this).find('input[type=submit]').addClass('disabled').showIndicator();
  243. var ui = app.focusModel();
  244. if(ui)
  245. ui.isLoading(true);
  246. API.queryArray($(this).attr('action'), data).complete(function() {
  247. $(self).find('input[type=submit]').removeClass('disabled').hideIndicator();
  248. if(ui)
  249. ui.isLoading(false);
  250. }).success(function() {
  251. $(self).modal('hide');
  252. if(ui)
  253. app.focusModel().reload();
  254. });
  255. return false;
  256. }
  257. $('form.ajaxSubmit').submit(bindSubmit).on('hidden', function() {
  258. $(this).trigger('reset');
  259. });
  260. var prepareNewTableForm = function () {
  261. $("#new_table_modal .modal-body ul").empty();
  262. addColumnToNewTableForm();
  263. }
  264. var addColumnToNewTableForm = function() {
  265. var $li = $("<li>").addClass("column").css("marginBottom", "10px").html($("#columnTemplate").html());
  266. $li.find("ul").html($("#columnPropertyEmptyTemplate").html());
  267. $li.appendTo($("#new_table_modal .modal-body ul.columns"));
  268. }
  269. var addColumnPropertyToColumn = function (col){
  270. var $li = $("<li>").addClass("columnProperty").css("marginBottom", "5px").html($("#columnPropertyTemplate").html());
  271. $li.find("select").on("change", function(){
  272. $li.find("[name='table_columns_property_value']").attr("placeholder", $(this).find("option:selected").data("default"));
  273. });
  274. $li.appendTo(col.find("ul"));
  275. }
  276. $(document).on("click", "a.action_addColumn", function() {
  277. addColumnToNewTableForm();
  278. });
  279. $(document).on("click", "a.action_removeColumn", function() {
  280. $(this).parents("li").remove();
  281. });
  282. $(document).on("click", "a.action_addColumnProperty", function() {
  283. addColumnPropertyToColumn($(this).parents(".column"));
  284. $(this).parents(".column").find(".columnPropertyEmpty").remove();
  285. });
  286. $(document).on("click", "a.action_removeColumnProperty", function() {
  287. var _col = $(this).parents(".column");
  288. _col.find(".columnPropertyEmpty").remove();
  289. $(this).parent().remove();
  290. if (_col.find("li").length == 0){
  291. _col.find("ul").html($("#columnPropertyEmptyTemplate").html());
  292. }
  293. });