app.js 7.9 KB

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