ko.layout.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) {
  49. var self = this;
  50. self.widgets = ko.observableArray(widgets);
  51. self.addWidget = function (widget) {
  52. self.widgets.push(widget);
  53. };
  54. self.move = function (from, to) {
  55. try {
  56. vm.columns()[to].addRow(self);
  57. vm.columns()[from].rows.remove(self);
  58. }
  59. catch (exception) {
  60. }
  61. }
  62. self.moveDown = function (col, row) {
  63. var _i = col.rows().indexOf(row);
  64. if (_i < col.rows().length - 1) {
  65. var _arr = col.rows();
  66. col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
  67. }
  68. }
  69. self.moveUp = function (col, row) {
  70. var _i = col.rows().indexOf(row);
  71. if (_i >= 1) {
  72. var _arr = col.rows();
  73. col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
  74. }
  75. }
  76. self.remove = function (col, row) {
  77. $.each(self.widgets(), function (i, widget) {
  78. vm.removeWidget(widget);
  79. });
  80. col.rows.remove(row);
  81. }
  82. self.autosizeWidgets = function () {
  83. $.each(self.widgets(), function (i, widget) {
  84. widget.size(Math.floor(12 / self.widgets().length));
  85. });
  86. }
  87. }
  88. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  89. var Widget = function (params) {
  90. var self = this;
  91. self.size = ko.observable(params.size).extend({ numeric: 0 });
  92. self.name = ko.observable(params.name);
  93. self.id = ko.observable(params.id);
  94. self.widgetType = ko.observable(typeof params.widgetType != "undefined" && params.widgetType != null ? params.widgetType : "empty-widget");
  95. self.properties = ko.observable(typeof params.properties != "undefined" && params.properties != null ? params.properties : {});
  96. self.offset = ko.observable(typeof params.offset != "undefined" && params.offset != null ? params.offset : 0).extend({ numeric: 0 });
  97. self.isLoading = ko.observable(typeof params.loading != "undefined" && params.loading != null ? params.loading : false);
  98. self.klass = ko.computed(function () {
  99. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  100. });
  101. self.expand = function () {
  102. self.size(self.size() + 1);
  103. $("#wdg_" + self.id()).trigger("resize");
  104. }
  105. self.compress = function () {
  106. self.size(self.size() - 1);
  107. $("#wdg_" + self.id()).trigger("resize");
  108. }
  109. self.moveLeft = function () {
  110. self.offset(self.offset() - 1);
  111. }
  112. self.moveRight = function () {
  113. self.offset(self.offset() + 1);
  114. }
  115. self.remove = function (row, widget) {
  116. if (params.vm != null) {
  117. params.vm.removeWidget(widget);
  118. }
  119. row.widgets.remove(widget);
  120. }
  121. };
  122. Widget.prototype.clone = function () {
  123. return new Widget({
  124. size: this.size(),
  125. id: UUID(),
  126. name: this.name(),
  127. widgetType: this.widgetType()
  128. });
  129. };
  130. function fullLayout(vm) {
  131. setLayout([12], vm);
  132. }
  133. function oneSixthLeftLayout(vm) {
  134. setLayout([2, 10], vm);
  135. }
  136. function oneFourthLeftLayout(vm) {
  137. setLayout([3, 9], vm);
  138. }
  139. function oneThirdLeftLayout(vm) {
  140. setLayout([4, 8], vm);
  141. }
  142. function halfHalfLayout(vm) {
  143. setLayout([6, 6], vm);
  144. }
  145. function oneThirdRightLayout(vm) {
  146. setLayout([8, 4], vm);
  147. }
  148. function oneFourthRightLayout(vm) {
  149. setLayout([9, 3], vm);
  150. }
  151. function oneSixthRightLayout(vm) {
  152. setLayout([10, 2], vm);
  153. }
  154. function setLayout(colSizes, vm) {
  155. // Save previous widgets
  156. var _allRows = [];
  157. $(vm.columns()).each(function (cnt, col) {
  158. var _tRows = [];
  159. $(col.rows()).each(function (icnt, row) {
  160. if (row.widgets().length > 0) {
  161. _tRows.push(row);
  162. }
  163. });
  164. _allRows = _allRows.concat(_tRows);
  165. });
  166. var _cols = [];
  167. var _highestCol = {
  168. idx: -1,
  169. size: -1
  170. };
  171. $(colSizes).each(function (cnt, size) {
  172. _cols.push(new Column(size, []));
  173. if (size > _highestCol.size) {
  174. _highestCol.idx = cnt;
  175. _highestCol.size = size;
  176. }
  177. });
  178. if (_allRows.length > 0 && _highestCol.idx > -1) {
  179. _cols[_highestCol.idx].rows(_allRows);
  180. }
  181. $(_cols).each(function (cnt, col) {
  182. if (col.rows().length == 0) {
  183. col.rows([new Row([], vm)]);
  184. }
  185. });
  186. vm.columns(_cols);
  187. $(document).trigger("setLayout");
  188. }