ko.layout.js 8.2 KB

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