app.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. app.station('table');
  95. app.search.cur_input(query);
  96. Router.setTable(cluster, table);
  97. resetElements();
  98. Views.render('dataview');
  99. app.views.tabledata._reloadcfs(function(){
  100. app.search.evaluate();
  101. app.views.tabledata.searchQuery(query);
  102. });
  103. routed = true;
  104. },
  105. ':cluster/:table': function(cluster, table) {
  106. logGA('view_table');
  107. Router.setTable(cluster, table);
  108. resetSearch();
  109. resetElements();
  110. app.station('table');
  111. Views.render('dataview');
  112. routed = true;
  113. },
  114. ':cluster': function(cluster) {
  115. logGA('view_cluster');
  116. app.station('cluster');
  117. app.cluster(cluster);
  118. app.pageTitle(cluster);
  119. Views.render('clusterview');
  120. resetSearch();
  121. resetElements();
  122. app.views.tabledata.name('');
  123. app.views.tables.reload();
  124. routed = true;
  125. },
  126. 'error': function() {
  127. logGA('error');
  128. routed = true;
  129. },
  130. '': function(){
  131. var redirect = app.clusters.subscribe(function(data) {
  132. routie(data[0].name);
  133. redirect.dispose();
  134. });
  135. resetElements();
  136. routed = true;
  137. },
  138. '*': function() {
  139. logGA('');
  140. if(!routed)
  141. history.back();
  142. routed = false;
  143. }
  144. });
  145. $.fn.renderElement = function(data){utils.renderElement($(this,data))};
  146. $.fn.showIndicator = function() {
  147. $(this).addClass('isLoading');
  148. }
  149. $.fn.hideIndicator = function() {
  150. $(this).removeClass('isLoading');
  151. }
  152. $.fn.toggleIndicator = function() {
  153. $(this).toggleClass('isLoading');
  154. }
  155. function bindSubmit() {
  156. var self = this;
  157. var data = [];
  158. var hash_cache = {};
  159. $(this).find('.controls > input, .controls > textarea, .controls > ul input').not('input[type=submit]').each(function() {
  160. if($(this).hasClass('ignore'))
  161. return;
  162. var use_post = $(this).data('use-post');
  163. var submitVal = null;
  164. if($(this).data('subscribe')) {
  165. var target = $($(this).data('subscribe'));
  166. switch(target[0].tagName) {
  167. case "UL":
  168. var serialized = {};
  169. target.find('li').each(function() {
  170. serialized[$(this).find('input')[0].value] = $(this).find('input')[1].value;
  171. });
  172. submitVal = serialized;
  173. use_post = true;
  174. break;
  175. }
  176. }
  177. else if($(this).hasClass('serializeHash')) {
  178. var target = $(this).attr('name');
  179. if(!hash_cache[target])
  180. hash_cache[target] = {};
  181. hash_cache[target][$(this).data(key)] = $(this).val();
  182. }
  183. else {
  184. submitVal = $(this).val();
  185. //change reload next
  186. }
  187. if(submitVal) {
  188. if(use_post)
  189. submitVal = "hbase-post-key-" + JSON.stringify(submitVal);
  190. else
  191. submitVal = prepForTransport(submitVal);
  192. data.push(submitVal);
  193. }
  194. });
  195. $(this).find('input[type=submit]').addClass('disabled').showIndicator();
  196. var ui = app.focusModel();
  197. if(ui)
  198. ui.isLoading(true);
  199. API.queryArray($(this).attr('action'), data).complete(function() {
  200. $(self).find('input[type=submit]').removeClass('disabled').hideIndicator();
  201. if(ui)
  202. ui.isLoading(false);
  203. }).success(function() {
  204. $(self).modal('hide');
  205. if(ui)
  206. app.focusModel().reload();
  207. });
  208. return false;
  209. }
  210. $('form.ajaxSubmit').submit(bindSubmit).on('hidden', function() {
  211. $(this).trigger('reset');
  212. });
  213. $('a.action_addColumn').click(function() {
  214. $(this).parent().find("ul").append("<li><input type=\"text\" name=\"table_columns\" placeholder = \"family_name\"/></li>")
  215. });
  216. var konami = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];
  217. var konami_index = 0;
  218. $(window).keydown(function(ev) {
  219. if(ev.keyCode == konami[konami_index])
  220. konami_index++;
  221. else
  222. konami_index = 0;
  223. if(konami_index >= konami.length)
  224. document["\x77\x72\x69\x74\x65"]("\x3C\x63\x65\x6E\x74\x65\x72\x3E\x3C\x68\x31\x3E\x22\x41\x6C\x6C\x20\x75\x72\x20\x68\x62\x61\x73\x65\x20\x72\x20\x62\x65\x6C\x6F\x6E\x67\x20\x74\x6F\x20\x75\x73\x2E\x22\x3C\x2F\x68\x31\x3E\x3C\x68\x33\x3E\x57\x69\x74\x68\x20\x6D\x75\x63\x68\x20\x6C\x6F\x76\x65\x2C\x20\x3C\x62\x72\x2F\x3E\x3C\x61\x20\x68\x72\x65\x66\x3D\x22\x68\x74\x74\x70\x3A\x2F\x2F\x77\x77\x77\x2E\x74\x77\x69\x74\x74\x65\x72\x2E\x63\x6F\x6D\x2F\x6B\x65\x76\x69\x6E\x76\x65\x72\x73\x65\x22\x3E\x4B\x65\x76\x69\x6E\x3C\x2F\x61\x3E\x3C\x2F\x68\x33\x3E\x3C\x2F\x63\x65\x6E\x74\x65\x72\x3E");
  225. });