ko.layout.js 6.7 KB

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