base.js 3.6 KB

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