app.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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': function(cluster, table) {
  98. routie(cluster + '/' + table);
  99. },
  100. ':cluster/:table/query/:query': function(cluster, table, query) {
  101. logGA('query_table');
  102. $.totalStorage('hbase_cluster', cluster);
  103. app.station('table');
  104. app.search.cur_input(query);
  105. Router.setTable(cluster, table);
  106. resetElements();
  107. Views.render('dataview');
  108. app.views.tabledata._reloadcfs(function(){
  109. app.search.evaluate();
  110. app.views.tabledata.searchQuery(query);
  111. });
  112. routed = true;
  113. },
  114. ':cluster/:table': function(cluster, table) {
  115. logGA('view_table');
  116. $.totalStorage('hbase_cluster', cluster);
  117. Router.setTable(cluster, table);
  118. resetSearch();
  119. resetElements();
  120. app.station('table');
  121. Views.render('dataview');
  122. routed = true;
  123. },
  124. ':cluster': function(cluster) {
  125. var redirect = app.clusters.subscribe(function(data) {
  126. if ($.inArray(cluster, app.clusterNames()) == -1) {
  127. routie('');
  128. } else {
  129. logGA('view_cluster');
  130. $.totalStorage('hbase_cluster', cluster);
  131. app.station('cluster');
  132. app.cluster(cluster);
  133. app.pageTitle(cluster);
  134. Views.render('clusterview');
  135. resetSearch();
  136. resetElements();
  137. app.views.tabledata.name('');
  138. app.views.tables.reload();
  139. routed = true;
  140. }
  141. redirect.dispose();
  142. });
  143. resetElements();
  144. routed = true;
  145. },
  146. 'error': function() {
  147. logGA('error');
  148. routed = true;
  149. },
  150. '': function(){
  151. var redirect = app.clusters.subscribe(function(data) {
  152. var cluster = $.totalStorage('hbase_cluster');
  153. if (cluster != null && $.inArray(cluster, app.clusterNames()) > -1) {
  154. routie(cluster);
  155. }
  156. else {
  157. routie(data[0].name);
  158. }
  159. redirect.dispose();
  160. });
  161. resetElements();
  162. routed = true;
  163. },
  164. '*': function() {
  165. logGA('');
  166. if(!routed)
  167. history.back();
  168. routed = false;
  169. }
  170. });
  171. $.fn.renderElement = function(data){utils.renderElement($(this,data))};
  172. $.fn.showIndicator = function() {
  173. $(this).addClass('isLoading');
  174. }
  175. $.fn.hideIndicator = function() {
  176. $(this).removeClass('isLoading');
  177. }
  178. $.fn.toggleIndicator = function() {
  179. $(this).toggleClass('isLoading');
  180. }
  181. function bindSubmit() {
  182. var self = this;
  183. var data = [];
  184. var hash_cache = {};
  185. $(this).find('.controls > input, .controls > textarea, .controls > ul input').not('input[type=submit]').each(function() {
  186. if($(this).hasClass('ignore'))
  187. return;
  188. var use_post = $(this).data('use-post');
  189. var submitVal = null;
  190. if($(this).data('subscribe')) {
  191. var target = $($(this).data('subscribe'));
  192. switch(target[0].tagName) {
  193. case "UL":
  194. var serialized = {};
  195. target.find('li').each(function() {
  196. serialized[$(this).find('input')[0].value] = $(this).find('input')[1].value;
  197. });
  198. submitVal = serialized;
  199. use_post = true;
  200. break;
  201. }
  202. }
  203. else if($(this).hasClass('serializeHash')) {
  204. var target = $(this).attr('name');
  205. if(!hash_cache[target])
  206. hash_cache[target] = {};
  207. hash_cache[target][$(this).data(key)] = $(this).val();
  208. }
  209. else {
  210. submitVal = $(this).val();
  211. //change reload next
  212. }
  213. if(submitVal) {
  214. if(use_post)
  215. submitVal = "hbase-post-key-" + JSON.stringify(submitVal);
  216. else
  217. submitVal = prepForTransport(submitVal);
  218. data.push(submitVal);
  219. }
  220. });
  221. $(this).find('input[type=submit]').addClass('disabled').showIndicator();
  222. var ui = app.focusModel();
  223. if(ui)
  224. ui.isLoading(true);
  225. API.queryArray($(this).attr('action'), data).complete(function() {
  226. $(self).find('input[type=submit]').removeClass('disabled').hideIndicator();
  227. if(ui)
  228. ui.isLoading(false);
  229. }).success(function() {
  230. $(self).modal('hide');
  231. if(ui)
  232. app.focusModel().reload();
  233. });
  234. return false;
  235. }
  236. $('form.ajaxSubmit').submit(bindSubmit).on('hidden', function() {
  237. $(this).trigger('reset');
  238. });
  239. $('a.action_addColumn').click(function() {
  240. $(this).parent().find("ul").append("<li><input type=\"text\" name=\"table_columns\" placeholder = \"family_name\"/></li>")
  241. });