base.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 != null && 'complete' in call)
  67. call.complete(callback);
  68. else
  69. self.isLoading(false);
  70. }
  71. };
  72. self.batchSelectedAlias = function(actionAlias) {
  73. self.batchSelected(function() {
  74. return this[actionAlias]();
  75. });
  76. };
  77. self.enableSelected = function() {
  78. self.batchSelected(function() {
  79. return this.enable();
  80. });
  81. };
  82. self.disableSelected = function() {
  83. confirm("Confirm Disable", "Disable these tables?", function() {
  84. self.batchSelected(function() {
  85. return this.disable();
  86. });
  87. });
  88. };
  89. self.dropSelected = function() {
  90. confirm("Confirm Delete", "Are you sure you want to delete the selected items? (WARNING: This cannot be undone!)", function() {
  91. self.batchSelected(function() {
  92. return this.drop(true);
  93. });
  94. });
  95. };
  96. self.reload = function(callback){
  97. self.items.removeAll();
  98. self.isLoading(true);
  99. options.reload.apply(self,[function() {
  100. self.isLoading(false);
  101. if(callback!=null)
  102. callback();
  103. self.sortDropDown.sort();
  104. }]);
  105. };
  106. self.searchQuery = ko.observable("");
  107. self.isLoading = ko.observable(false);
  108. };
  109. var DataRow = function(options) {
  110. var self = this;
  111. ko.utils.extend(self,options); //applies options on itself
  112. BaseModel.apply(self,[options]);
  113. self.isSelected = ko.observable(false);
  114. self.select = function(){self.isSelected(!self.isSelected());};
  115. };