base.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 BaseModel = function() {
  17. }
  18. var ListViewModel = function(options) {
  19. var self = this, _defaults = {
  20. items: [],
  21. reload: function() {
  22. },
  23. sortFields: {}
  24. };
  25. options = ko.utils.extend(_defaults,options);
  26. BaseModel.apply(this,[options]);
  27. self.items = ko.observableArray(options.items);
  28. self.sortDropDown = new SortDropDownView({sortFields: options.sortFields, target: self.items});
  29. self.selectAll = function(){
  30. for(t=0; t<self.items().length; t++)
  31. self.items()[t].isSelected(true);
  32. return self;
  33. };
  34. self.deselectAll = function() {
  35. for(q=0; q<self.items().length; q++)
  36. self.items()[q].isSelected(false);
  37. return self;
  38. };
  39. self.toggleSelectAll = function() {
  40. if(self.selected().length != self.items().length)
  41. return self.selectAll();
  42. return self.deselectAll();
  43. };
  44. self.selected = function(){
  45. var acc = [];
  46. var items = self.items();
  47. for(i=0; i<items.length; i++) {
  48. if(items[i].isSelected())
  49. acc.push(items[i]);
  50. }
  51. return acc;
  52. };
  53. self.batchSelected = function(action) {
  54. var selected = self.selected();
  55. var batchCount = 0;
  56. for(q=0; q<selected.length; q++) {
  57. self.isLoading(true);
  58. var call = action.apply(selected[q], arguments);
  59. var callback = function() {
  60. batchCount++;
  61. if(batchCount >= selected.length) {
  62. self.reload();
  63. self.isLoading(false);
  64. }
  65. };
  66. if(call === true) {
  67. callback();
  68. } else if (call != null && 'complete' in call) {
  69. call.complete(callback);
  70. } else {
  71. self.isLoading(false);
  72. }
  73. }
  74. };
  75. self.batchSelectedAlias = function(actionAlias) {
  76. self.batchSelected(function() {
  77. return this[actionAlias]();
  78. });
  79. };
  80. self.enableSelected = function() {
  81. self.batchSelected(function() {
  82. return this.enable();
  83. });
  84. };
  85. self.disableSelected = function() {
  86. confirm("Confirm Disable", "Disable these tables?", function() {
  87. self.batchSelected(function() {
  88. return this.disable();
  89. });
  90. });
  91. };
  92. self.dropSelected = function() {
  93. confirm("Confirm Delete", "Are you sure you want to drop the selected items? (WARNING: This cannot be undone!)", function() {
  94. self.batchSelected(function() {
  95. var s = this;
  96. if(s.enabled()) {
  97. self.isLoading(true);
  98. return s.disable(function() {
  99. s.drop(true);
  100. });
  101. } else {
  102. return s.drop(true);
  103. }
  104. });
  105. });
  106. };
  107. self.reload = function(callback){
  108. self.items.removeAll();
  109. self.isLoading(true);
  110. options.reload.apply(self,[function() {
  111. self.isLoading(false);
  112. if(callback!=null)
  113. callback();
  114. self.sortDropDown.sort();
  115. }]);
  116. };
  117. self.searchQuery = ko.observable("");
  118. self.isLoading = ko.observable(false);
  119. };
  120. var DataRow = function(options) {
  121. var self = this;
  122. ko.utils.extend(self,options); //applies options on itself
  123. BaseModel.apply(self,[options]);
  124. self.isSelected = ko.observable(false);
  125. self.select = function(){self.isSelected(!self.isSelected());};
  126. };