app.js 7.8 KB

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