ko.common-dashboard.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. function s4() {
  17. return Math.floor((1 + Math.random()) * 0x10000)
  18. .toString(16)
  19. .substring(1);
  20. }
  21. function UUID() {
  22. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  23. }
  24. var Column = function (size, rows) {
  25. var self = this;
  26. self.rowPrototype = Row;
  27. self.id = ko.observable(UUID());
  28. self.size = ko.observable(size);
  29. self.rows = ko.observableArray(rows);
  30. self.drops = ko.observableArray(["temp"]);
  31. self.klass = ko.computed(function () {
  32. return "card card-home card-column span" + self.size();
  33. });
  34. self.percWidth = ko.observable();
  35. self.addEmptyRow = function (atBeginning, atIndex) {
  36. return self.addRow(null, atBeginning, atIndex);
  37. };
  38. self.addRow = function (row, atBeginning, atIndex) {
  39. if (typeof row == "undefined" || row == null) {
  40. row = new self.rowPrototype([], viewModel); // Hacky but needed when a new row is deleted
  41. }
  42. if (typeof atIndex != "undefined" && atIndex != null) {
  43. self.rows.splice(atIndex, 0, row);
  44. }
  45. else {
  46. if (typeof atBeginning == "undefined" || atBeginning == null || !atBeginning) {
  47. self.rows.push(row);
  48. }
  49. else {
  50. self.rows.unshift(row);
  51. }
  52. }
  53. return row;
  54. };
  55. }
  56. var Row = function (widgets, vm, columns) {
  57. var self = this;
  58. self.columnPrototype = Column;
  59. self.id = ko.observable(UUID());
  60. self.widgets = ko.observableArray(widgets);
  61. self.columns = ko.observableArray(columns ? columns : []);
  62. self.columns.subscribe(function(val){
  63. self.columns().forEach(function(col){
  64. col.percWidth((100 - self.columns().length * 0.5) / self.columns().length);
  65. });
  66. });
  67. self.addWidget = function (widget) {
  68. self.widgets.push(widget);
  69. };
  70. self.addEmptyColumn = function (atBeginning) {
  71. if (self.columns().length == 0){
  72. var _col = self.addColumn(null, atBeginning);
  73. if (self.widgets().length > 0){
  74. var _row = _col.addEmptyRow();
  75. self.widgets().forEach(function(widget){
  76. _row.addWidget(widget);
  77. });
  78. self.widgets([]);
  79. }
  80. }
  81. return self.addColumn(null, atBeginning);
  82. };
  83. self.addColumn = function (column, atBeginning) {
  84. if (typeof column == "undefined" || column == null) {
  85. var _size = Math.max(1, Math.floor(12 / (self.columns().length + 1)));
  86. column = new self.columnPrototype(_size, []); // Hacky but needed when a new row is deleted
  87. self.columns().forEach(function(col){
  88. col.size(_size);
  89. });
  90. }
  91. if (typeof atBeginning == "undefined" || atBeginning == null || ! atBeginning) {
  92. self.columns.push(column);
  93. }
  94. else {
  95. self.columns.unshift(column);
  96. }
  97. return column;
  98. };
  99. self.move = function (from, to) {
  100. try {
  101. vm.columns()[to].addRow(self);
  102. vm.columns()[from].rows.remove(self);
  103. }
  104. catch (exception) {
  105. }
  106. }
  107. self.moveDown = function (col, row) {
  108. var _i = col.rows().indexOf(row);
  109. if (_i < col.rows().length - 1) {
  110. var _arr = col.rows();
  111. col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
  112. }
  113. }
  114. self.moveUp = function (col, row) {
  115. var _i = col.rows().indexOf(row);
  116. if (_i >= 1) {
  117. var _arr = col.rows();
  118. col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
  119. }
  120. }
  121. self.remove = function (col, row) {
  122. $.each(self.columns(), function (i, column) {
  123. $.each(column.rows(), function (j, row) {
  124. $.each(row.widgets(), function (k, widget) {
  125. vm.removeWidget(widget);
  126. });
  127. });
  128. });
  129. $.each(self.widgets(), function (i, widget) {
  130. vm.removeWidget(widget);
  131. });
  132. col.rows.remove(row);
  133. }
  134. self.autosizeWidgets = function () {
  135. $.each(self.widgets(), function (i, widget) {
  136. widget.size(Math.floor(12 / self.widgets().length));
  137. });
  138. }
  139. }
  140. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  141. var Widget = function (params) {
  142. var self = this;
  143. self.extend = function() {
  144. return self;
  145. }
  146. self.size = ko.observable(params.size).extend({ numeric: 0 });
  147. self.name = ko.observable(params.name);
  148. self.id = ko.observable(params.id);
  149. self.widgetType = ko.observable(typeof params.widgetType != "undefined" && params.widgetType != null ? params.widgetType : "empty-widget");
  150. self.properties = ko.observable(typeof params.properties != "undefined" && params.properties != null ? params.properties : {});
  151. self.offset = ko.observable(typeof params.offset != "undefined" && params.offset != null ? params.offset : 0).extend({ numeric: 0 });
  152. self.isLoading = ko.observable(typeof params.loading != "undefined" && params.loading != null ? params.loading : false);
  153. self.klass = ko.computed(function () {
  154. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  155. });
  156. self.expand = function () {
  157. self.size(self.size() + 1);
  158. $("#wdg_" + self.id()).trigger("resize");
  159. }
  160. self.compress = function () {
  161. self.size(self.size() - 1);
  162. $("#wdg_" + self.id()).trigger("resize");
  163. }
  164. self.moveLeft = function () {
  165. self.offset(self.offset() - 1);
  166. }
  167. self.moveRight = function () {
  168. self.offset(self.offset() + 1);
  169. }
  170. self.remove = function (row, widget) {
  171. if (params.vm != null) {
  172. params.vm.removeWidget(widget);
  173. }
  174. row.widgets.remove(widget);
  175. }
  176. };
  177. Widget.prototype.clone = function () {
  178. return new Widget({
  179. size: this.size(),
  180. id: UUID(),
  181. name: this.name(),
  182. widgetType: this.widgetType()
  183. });
  184. };
  185. function fullLayout(vm) {
  186. setLayout([12], vm);
  187. }
  188. function oneSixthLeftLayout(vm) {
  189. setLayout([2, 10], vm);
  190. }
  191. function oneFourthLeftLayout(vm) {
  192. setLayout([3, 9], vm);
  193. }
  194. function oneThirdLeftLayout(vm) {
  195. setLayout([4, 8], vm);
  196. }
  197. function halfHalfLayout(vm) {
  198. setLayout([6, 6], vm);
  199. }
  200. function oneThirdRightLayout(vm) {
  201. setLayout([8, 4], vm);
  202. }
  203. function oneFourthRightLayout(vm) {
  204. setLayout([9, 3], vm);
  205. }
  206. function oneSixthRightLayout(vm) {
  207. setLayout([10, 2], vm);
  208. }
  209. function setLayout(colSizes, vm) {
  210. // Save previous widgets
  211. var _allRows = [];
  212. $(vm.columns()).each(function (cnt, col) {
  213. var _tRows = [];
  214. $(col.rows()).each(function (icnt, row) {
  215. if (row.widgets().length > 0 || (typeof vm.isNested != "undefined" && vm.isNested())) {
  216. _tRows.push(row);
  217. }
  218. });
  219. _allRows = _allRows.concat(_tRows);
  220. });
  221. var _cols = [];
  222. var _highestCol = {
  223. idx: -1,
  224. size: -1
  225. };
  226. $(colSizes).each(function (cnt, size) {
  227. _cols.push(new Column(size, []));
  228. if (size > _highestCol.size) {
  229. _highestCol.idx = cnt;
  230. _highestCol.size = size;
  231. }
  232. });
  233. if (_allRows.length > 0 && _highestCol.idx > -1) {
  234. _cols[_highestCol.idx].rows(_allRows);
  235. }
  236. $(_cols).each(function (cnt, col) {
  237. if (col.rows().length == 0) {
  238. col.rows([new Row([], vm)]);
  239. }
  240. });
  241. vm.columns(_cols);
  242. $(document).trigger("setLayout");
  243. }