ko.layout.js 8.0 KB

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