app.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. API.query('getClusters').done(function(data) {
  27. app.clusters(data);
  28. });
  29. self.search = new tagsearch();
  30. self.views = {
  31. tables: new DataTableViewModel({columns:['Table Name', 'Enabled'], el: 'views.tables', reload: function(callback) {
  32. var d_self = this;
  33. d_self.items.removeAll();
  34. API.queryCluster("getTableList").done(function(data) {
  35. d_self.items.removeAll(); //need to remove again before callback executes
  36. for(q=0; q<data.length; q++) {
  37. d_self.items.push(new TableDataRow(data[q]));
  38. }
  39. d_self._el.find('a[data-row-selector=true]').jHueRowSelector();
  40. if(callback!=null)
  41. callback();
  42. });
  43. }}),
  44. tabledata: new SmartViewModel({el: 'views.tabledata', reload: function(callback) //move inside SmartViewModel class?
  45. {
  46. var t_self = this;
  47. function getColumnFamilies() {
  48. var cols = [];
  49. var cfs = t_self.columnFamilies();
  50. for(var i=0; i<cfs.length; i++) {
  51. if(cfs[i].enabled()) {
  52. cols.push(cfs[i].name);
  53. }
  54. }
  55. return cols;
  56. }
  57. API.queryTable("getRowQuerySet", JSON.stringify(getColumnFamilies()), ko.toJSON(t_self.querySet())).done(function(data) {
  58. if(data.length > 0) {
  59. var keys = Object.keys(data);
  60. var items = [];
  61. for(var i=0; i<keys.length; i++) {
  62. var row = new SmartViewDataRow({items: [], row:data[keys[i]].row, reload: function(options) {
  63. var self = this;
  64. options = (options == null) ? {} : options;
  65. options = ko.utils.extend({
  66. callback: function(data){},
  67. columns: getColumnFamilies()
  68. }, options);
  69. API.queryTable("getRow", JSON.stringify(options.columns), prepForTransport(self.row)).done(function(data) {
  70. self.setItems(data.columns);
  71. callback(data);
  72. self.isLoading(false);
  73. });
  74. }});
  75. row.setItems(data[keys[i]].columns);
  76. items.push(row);
  77. }
  78. t_self.items(items);
  79. }
  80. if(typeof(callback) === 'function')
  81. callback();
  82. $('*[data-toggle="tooltip"]').tooltip();
  83. });
  84. }})
  85. };
  86. }
  87. var app = new AppViewModel();
  88. ko.applyBindings(app);
  89. //routing
  90. routed = false;
  91. routie({
  92. ':cluster/:table/query/:query': function(cluster, table, query) {
  93. logGA('query_table');
  94. $.totalStorage('hbase_cluster', cluster);
  95. app.station('table');
  96. app.search.cur_input(query);
  97. Router.setTable(cluster, table);
  98. resetElements();
  99. Views.render('dataview');
  100. app.views.tabledata._reloadcfs(function(){
  101. app.search.evaluate();
  102. app.views.tabledata.searchQuery(query);
  103. });
  104. routed = true;
  105. },
  106. ':cluster/:table': function(cluster, table) {
  107. logGA('view_table');
  108. $.totalStorage('hbase_cluster', cluster);
  109. Router.setTable(cluster, table);
  110. resetSearch();
  111. resetElements();
  112. app.station('table');
  113. Views.render('dataview');
  114. routed = true;
  115. },
  116. ':cluster': function(cluster) {
  117. logGA('view_cluster');
  118. $.totalStorage('hbase_cluster', cluster);
  119. app.station('cluster');
  120. app.cluster(cluster);
  121. app.pageTitle(cluster);
  122. Views.render('clusterview');
  123. resetSearch();
  124. resetElements();
  125. app.views.tabledata.name('');
  126. app.views.tables.reload();
  127. routed = true;
  128. },
  129. 'error': function() {
  130. logGA('error');
  131. routed = true;
  132. },
  133. '': function(){
  134. var redirect = app.clusters.subscribe(function(data) {
  135. if ($.totalStorage('hbase_cluster') != null) {
  136. routie($.totalStorage('hbase_cluster'));
  137. }
  138. else {
  139. routie(data[0].name);
  140. }
  141. redirect.dispose();
  142. });
  143. resetElements();
  144. routed = true;
  145. },
  146. '*': function() {
  147. logGA('');
  148. if(!routed)
  149. history.back();
  150. routed = false;
  151. }
  152. });
  153. $.fn.renderElement = function(data){utils.renderElement($(this,data))};
  154. $.fn.showIndicator = function() {
  155. $(this).addClass('isLoading');
  156. }
  157. $.fn.hideIndicator = function() {
  158. $(this).removeClass('isLoading');
  159. }
  160. $.fn.toggleIndicator = function() {
  161. $(this).toggleClass('isLoading');
  162. }
  163. function bindSubmit() {
  164. var self = this;
  165. var data = [];
  166. var hash_cache = {};
  167. $(this).find('.controls > input, .controls > textarea, .controls > ul input').not('input[type=submit]').each(function() {
  168. if($(this).hasClass('ignore'))
  169. return;
  170. var use_post = $(this).data('use-post');
  171. var submitVal = null;
  172. if($(this).data('subscribe')) {
  173. var target = $($(this).data('subscribe'));
  174. switch(target[0].tagName) {
  175. case "UL":
  176. var serialized = {};
  177. target.find('li').each(function() {
  178. serialized[$(this).find('input')[0].value] = $(this).find('input')[1].value;
  179. });
  180. submitVal = serialized;
  181. use_post = true;
  182. break;
  183. }
  184. }
  185. else if($(this).hasClass('serializeHash')) {
  186. var target = $(this).attr('name');
  187. if(!hash_cache[target])
  188. hash_cache[target] = {};
  189. hash_cache[target][$(this).data(key)] = $(this).val();
  190. }
  191. else {
  192. submitVal = $(this).val();
  193. //change reload next
  194. }
  195. if(submitVal) {
  196. if(use_post)
  197. submitVal = "hbase-post-key-" + JSON.stringify(submitVal);
  198. else
  199. submitVal = prepForTransport(submitVal);
  200. data.push(submitVal);
  201. }
  202. });
  203. $(this).find('input[type=submit]').addClass('disabled').showIndicator();
  204. var ui = app.focusModel();
  205. if(ui)
  206. ui.isLoading(true);
  207. API.queryArray($(this).attr('action'), data).complete(function() {
  208. $(self).find('input[type=submit]').removeClass('disabled').hideIndicator();
  209. if(ui)
  210. ui.isLoading(false);
  211. }).success(function() {
  212. $(self).modal('hide');
  213. if(ui)
  214. app.focusModel().reload();
  215. });
  216. return false;
  217. }
  218. $('form.ajaxSubmit').submit(bindSubmit).on('hidden', function() {
  219. $(this).trigger('reset');
  220. });
  221. $('a.action_addColumn').click(function() {
  222. $(this).parent().find("ul").append("<li><input type=\"text\" name=\"table_columns\" placeholder = \"family_name\"/></li>")
  223. });