base.js 3.9 KB

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