app.js 7.8 KB

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