app.js 7.6 KB

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