ko.layout.js 8.7 KB

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