app.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. app.views.tables.reload();
  113. routed = true;
  114. },
  115. 'error': function() {
  116. logGA('error');
  117. routed = true;
  118. },
  119. '': function(){
  120. var redirect = app.clusters.subscribe(function(data) {
  121. routie(data[0].name);
  122. redirect.dispose();
  123. });
  124. routed = true;
  125. },
  126. '*': function() {
  127. logGA();
  128. resetElements();
  129. if(!routed)
  130. history.back();
  131. routed = false;
  132. }
  133. });
  134. $.fn.renderElement = function(data){utils.renderElement($(this,data))};
  135. $.fn.showIndicator = function() {
  136. $(this).addClass('isLoading');
  137. }
  138. $.fn.hideIndicator = function() {
  139. $(this).removeClass('isLoading');
  140. }
  141. $.fn.toggleIndicator = function() {
  142. $(this).toggleClass('isLoading');
  143. }
  144. function bindSubmit() {
  145. var self = this;
  146. var data = [];
  147. var hash_cache = {};
  148. $(this).find('.controls > input, .controls > textarea, .controls > ul input').not('input[type=submit]').each(function() {
  149. if($(this).hasClass('ignore'))
  150. return;
  151. var use_post = $(this).data('use-post');
  152. var submitVal = null;
  153. if($(this).data('subscribe')) {
  154. var target = $($(this).data('subscribe'));
  155. switch(target[0].tagName) {
  156. case "UL":
  157. var serialized = {};
  158. target.find('li').each(function() {
  159. serialized[$(this).find('input')[0].value] = $(this).find('input')[1].value;
  160. });
  161. submitVal = JSON.stringify(serialized);
  162. use_post = true;
  163. break;
  164. }
  165. }
  166. else if($(this).hasClass('serializeHash')) {
  167. var target = $(this).attr('name');
  168. if(!hash_cache[target])
  169. hash_cache[target] = {};
  170. hash_cache[target][$(this).data(key)] = $(this).val();
  171. }
  172. else {
  173. submitVal = $(this).val();
  174. //change reload next
  175. }
  176. if(submitVal) {
  177. if(use_post)
  178. submitVal = "hbase-post-key-" + JSON.stringify(submitVal);
  179. else
  180. submitVal = prepForTransport(submitVal);
  181. data.push(submitVal);
  182. }
  183. });
  184. $(this).find('input[type=submit]').addClass('disabled').showIndicator();
  185. var ui = app.focusModel();
  186. if(ui)
  187. ui.isLoading(true);
  188. API.queryArray($(this).attr('action'), data).complete(function() {
  189. $(self).find('input[type=submit]').removeClass('disabled').hideIndicator();
  190. if(ui)
  191. ui.isLoading(false);
  192. }).success(function() {
  193. if(ui)
  194. app.focusModel().reload();
  195. $(self).modal('hide').trigger('reset');
  196. });
  197. return false;
  198. }
  199. $('form.ajaxSubmit').submit(bindSubmit);
  200. $('a.action_addColumn').click(function() {
  201. $(this).parent().find("ul").append("<li><input type=\"text\" name=\"table_columns\" placeholder = \"family:column_name\"/></li>")
  202. });
  203. $('a.action_addColumnValue').click(function() {
  204. $(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>")
  205. });
  206. var konami = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];
  207. var konami_index = 0;
  208. $(window).keydown(function(ev) {
  209. if(ev.keyCode == konami[konami_index])
  210. konami_index++;
  211. else
  212. konami_index = 0;
  213. if(konami_index >= konami.length)
  214. 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");
  215. });