ko.layout.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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([]);
  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. }
  83. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  84. var Widget = function (params) {
  85. var self = this;
  86. self.size = ko.observable(params.size).extend({ numeric: 0 });
  87. self.name = ko.observable(params.name);
  88. self.id = ko.observable(params.id);
  89. self.widgetType = ko.observable(typeof params.widgetType != "undefined" && params.widgetType != null ? params.widgetType : "empty-widget");
  90. self.properties = ko.observable(typeof params.properties != "undefined" && params.properties != null ? params.properties : {});
  91. self.offset = ko.observable(typeof params.offset != "undefined" && params.offset != null ? params.offset : 0).extend({ numeric: 0 });
  92. self.isLoading = ko.observable(typeof params.loading != "undefined" && params.loading != null ? params.loading : false);
  93. self.klass = ko.computed(function () {
  94. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  95. });
  96. self.expand = function () {
  97. self.size(self.size() + 1);
  98. $("#wdg_" + self.id()).trigger("resize");
  99. }
  100. self.compress = function () {
  101. self.size(self.size() - 1);
  102. $("#wdg_" + self.id()).trigger("resize");
  103. }
  104. self.moveLeft = function () {
  105. self.offset(self.offset() - 1);
  106. }
  107. self.moveRight = function () {
  108. self.offset(self.offset() + 1);
  109. }
  110. self.remove = function (row, widget) {
  111. if (params.vm != null) {
  112. params.vm.removeWidget(widget);
  113. }
  114. row.widgets.remove(widget);
  115. }
  116. };
  117. Widget.prototype.clone = function () {
  118. return new Widget({
  119. size: this.size(),
  120. id: UUID(),
  121. name: this.name(),
  122. widgetType: this.widgetType()
  123. });
  124. };
  125. function fullLayout(vm) {
  126. setLayout([12], vm);
  127. }
  128. function oneSixthLeftLayout(vm) {
  129. setLayout([2, 10], vm);
  130. }
  131. function oneFourthLeftLayout(vm) {
  132. setLayout([3, 9], vm);
  133. }
  134. function oneThirdLeftLayout(vm) {
  135. setLayout([4, 8], vm);
  136. }
  137. function halfHalfLayout(vm) {
  138. setLayout([6, 6], vm);
  139. }
  140. function oneThirdRightLayout(vm) {
  141. setLayout([8, 4], vm);
  142. }
  143. function oneFourthRightLayout(vm) {
  144. setLayout([9, 3], vm);
  145. }
  146. function oneSixthRightLayout(vm) {
  147. setLayout([10, 2], vm);
  148. }
  149. function setLayout(colSizes, vm) {
  150. // Save previous widgets
  151. var _allRows = [];
  152. $(vm.columns()).each(function (cnt, col) {
  153. var _tRows = [];
  154. $(col.rows()).each(function (icnt, row) {
  155. if (row.widgets().length > 0) {
  156. _tRows.push(row);
  157. }
  158. });
  159. _allRows = _allRows.concat(_tRows);
  160. });
  161. var _cols = [];
  162. var _highestCol = {
  163. idx: -1,
  164. size: -1
  165. };
  166. $(colSizes).each(function (cnt, size) {
  167. _cols.push(new Column(size, []));
  168. if (size > _highestCol.size) {
  169. _highestCol.idx = cnt;
  170. _highestCol.size = size;
  171. }
  172. });
  173. if (_allRows.length > 0 && _highestCol.idx > -1) {
  174. _cols[_highestCol.idx].rows(_allRows);
  175. }
  176. $(_cols).each(function (cnt, col) {
  177. if (col.rows().length == 0) {
  178. col.rows([new Row([], vm)]);
  179. }
  180. });
  181. vm.columns(_cols);
  182. $(document).trigger("setLayout");
  183. }