ko.common-dashboard.js 9.3 KB

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