workflow-editor.ko.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. ko.bindingHandlers.droppable = {
  17. init: function(element, valueAccessor) {
  18. var _dropElement = $(element);
  19. var _options = valueAccessor();
  20. if (_options.enabled){
  21. var _dropOptions = {
  22. hoverClass: 'drop-target-highlight',
  23. drop: _options.onDrop
  24. };
  25. _dropElement.droppable(_dropOptions);
  26. }
  27. }
  28. };
  29. function magicLayout(vm) {
  30. loadLayout(vm, vm.initial.layout);
  31. $(document).trigger("magicLayout");
  32. }
  33. function loadLayout(viewModel, json_layout) {
  34. var _columns = [];
  35. $(json_layout).each(function (cnt, json_col) {
  36. var _rows = [];
  37. $(json_col.rows).each(function (rcnt, json_row) {
  38. var row = new Row([], viewModel);
  39. $(json_row.widgets).each(function (wcnt, widget) {
  40. row.addWidget(new Widget({
  41. size:widget.size,
  42. id: widget.id,
  43. name: widget.name,
  44. widgetType: widget.widgetType,
  45. properties: widget.properties,
  46. offset: widget.offset,
  47. loading: true,
  48. vm: viewModel
  49. }));
  50. });
  51. $(json_row.columns).each(function (ccnt, column) {
  52. var _irows = [];
  53. $(column.rows).each(function (ircnt, json_irow) {
  54. var _irow = new Row([], viewModel);
  55. $(json_irow.widgets).each(function (iwcnt, iwidget) {
  56. _irow.addWidget(new Widget({
  57. size:iwidget.size,
  58. id: iwidget.id,
  59. name: iwidget.name,
  60. widgetType: iwidget.widgetType,
  61. properties: iwidget.properties,
  62. offset: iwidget.offset,
  63. loading: true,
  64. vm: viewModel
  65. }));
  66. });
  67. _irows.push(_irow);
  68. });
  69. row.addColumn(new Column(column.size, _irows));
  70. });
  71. _rows.push(row);
  72. });
  73. var column = new Column(json_col.size, _rows);
  74. _columns = _columns.concat(column);
  75. });
  76. viewModel.columns(_columns);
  77. }
  78. // End dashboard lib
  79. var Node = function (node) {
  80. var self = this;
  81. var type = typeof node.widgetType != "undefined" ? node.widgetType : node.type;
  82. self.id = ko.observable(typeof node.id != "undefined" && node.id != null ? node.id : UUID());
  83. self.name = ko.observable(typeof node.name != "undefined" && node.name != null ? node.name : "");
  84. self.type = ko.observable(typeof type != "undefined" && type != null ? type : "");
  85. self.properties = ko.mapping.fromJS(typeof node.properties != "undefined" && node.properties != null ? node.properties : {});
  86. self.children = ko.mapping.fromJS(typeof node.children != "undefined" && node.children != null ? node.children : []);
  87. self.get_link = function(name) {
  88. var _link = null;
  89. $.each(self.children(), function(index, link) {
  90. if (name in link) {
  91. _link = link;
  92. return false;
  93. }
  94. });
  95. return _link;
  96. }
  97. self.set_link = function(name, node_id) {
  98. var _link = self.get_link(name);
  99. if (_link == null) {
  100. _link = {}
  101. _link[name] = node_id;
  102. self.children.push(_link);
  103. } else {
  104. _link[name] = node_id;
  105. }
  106. }
  107. self.remove_link = function(name, child) {
  108. var _link = null;
  109. $.each(self.children(), function(index, link) {
  110. var _l = ko.mapping.toJS(link);
  111. if (name in _l && _l[name] == child) {
  112. _link = link;
  113. return false;
  114. }
  115. });
  116. if (_link != null) {
  117. self.children.remove(_link);
  118. }
  119. }
  120. }
  121. var Workflow = function (vm, workflow) {
  122. var self = this;
  123. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  124. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  125. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  126. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  127. self.nodes = ko.observableArray([]);
  128. self.linkMapping = ko.computed(function() {
  129. var mapping = {};
  130. $.each(self.nodes(), function(index, node) {
  131. var links = []
  132. $.each(node.children(), function(index, link) {
  133. if ('to' in link) {
  134. links.push(link['to']);
  135. }
  136. });
  137. mapping[node.id()] = links
  138. });
  139. return mapping;
  140. });
  141. self.linkMapping.subscribe(function(newVal){
  142. $(document).trigger("drawArrows");
  143. });
  144. self.loadNodes = function(workflow) {
  145. var nodes = []
  146. $.each(workflow.nodes, function(index, node) {
  147. var _node = new Node(node);
  148. nodes.push(_node);
  149. });
  150. self.nodes(nodes)
  151. }
  152. self.newNode = function(widget) {
  153. $.ajax({
  154. type: "POST",
  155. url: "/oozie/editor/workflow/new_node/",
  156. data: {
  157. "workflow": ko.mapping.toJSON(workflow),
  158. "node": ko.mapping.toJSON(widget)
  159. },
  160. success: function (data) {
  161. if (data.status == 0) {
  162. viewModel.addActionProperties(data.properties);
  163. viewModel.addActionWorkflows(data.workflows);
  164. }
  165. },
  166. async: false
  167. });
  168. };
  169. self.addNode = function(widget) {
  170. // Todo get parent cell, link nodes... when we have the new layout
  171. $.post("/oozie/editor/workflow/add_node/", {
  172. "workflow": ko.mapping.toJSON(workflow),
  173. "node": ko.mapping.toJSON(widget),
  174. "properties": ko.mapping.toJSON(viewModel.addActionProperties()),
  175. "subworkflow": viewModel.selectedSubWorkflow() ? ko.mapping.toJSON(viewModel.selectedSubWorkflow()) : '{}',
  176. }, function (data) {
  177. if (data.status == 0) {
  178. var _node = ko.mapping.toJS(widget);
  179. _node.properties = data.properties;
  180. _node.name = data.name;
  181. var node = new Node(_node);
  182. // Add to list of nodes
  183. var end = self.nodes.pop();
  184. self.nodes.push(node);
  185. self.nodes.push(end);
  186. // if node != kill node
  187. // Added to the side ?
  188. if (vm.currentlyCreatingFork) {
  189. var parentWidget = vm.getWidgetPredecessor(node.id());
  190. if (self.getNodeById(parentWidget.id()) == null) { // New fork
  191. var fork = new Node(vm.currentlyCreatedFork);
  192. var join = new Node(vm.currentlyCreatedJoin);
  193. var forkParent = self.getNodeById(vm.getWidgetPredecessor(parentWidget.id()).id());
  194. var afterParentId = ko.mapping.toJS(forkParent.get_link('to')).to;
  195. var afterParent = self.getNodeById(afterParentId);
  196. fork.children.push({'to': afterParentId});
  197. fork.children.push({'to': node.id()});
  198. forkParent.get_link('to')['to'] = fork.id();
  199. join.set_link('to', afterParent.get_link('to')['to']);
  200. afterParent.set_link('to', join.id());
  201. node.set_link('to', join.id());
  202. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  203. var end = self.nodes.pop();
  204. self.nodes.push(fork);
  205. self.nodes.push(join);
  206. self.nodes.push(end);
  207. // Regular node
  208. // Join node
  209. } else {
  210. // Just add to existing fork
  211. var join = vm.getWidgetSuccessor(node.id());
  212. node.set_link('to', join.id());
  213. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  214. self.getNodeById(parentWidget.id()).children.push({'to': node.id()});
  215. }
  216. } else {
  217. var parentWidget = vm.getWidgetPredecessor(node.id());
  218. var parent = self.getNodeById(parentWidget.id());
  219. if (parentWidget.widgetType() == 'start-widget') {
  220. // Star node link to new node
  221. parent.set_link('to', node.id());
  222. // Link to end
  223. node.set_link('to', '33430f0f-ebfa-c3ec-f237-3e77efa03d0a');
  224. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  225. } if (parentWidget.widgetType() == 'fork-widget') {
  226. var child = vm.getWidgetSuccessor(node.id());
  227. parent.remove_link('to', child.id());
  228. parent.children.push({'to': node.id()});
  229. node.set_link('to', child.id());
  230. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  231. } else {
  232. // Parent is regular node
  233. node.set_link('to', parent.get_link('to')['to']);
  234. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  235. parent.set_link('to', node.id());
  236. }
  237. // Todo decision/join
  238. }
  239. vm.currentlyCreatingFork = false;
  240. } else {
  241. $(document).trigger("error", data.message);
  242. }
  243. }).fail(function (xhr, textStatus, errorThrown) {
  244. $(document).trigger("error", xhr.responseText);
  245. });
  246. };
  247. self.removeNode = function(node_id) {
  248. var node = self.getNodeById(node_id);
  249. var parentWidget = vm.getWidgetPredecessor(node_id); // Use smarter self.getParents if action node with multi parents
  250. var parent = self.getNodeById(parentWidget.id());
  251. var childLink = node.get_link('to');
  252. var childId = ko.mapping.toJS(childLink)['to'];
  253. parent.remove_link('to', node_id);
  254. parent.children.push({'to': childId});
  255. self.nodes.remove(node);
  256. // If need to remove fork
  257. if (parentWidget.widgetType() == 'fork-widget') {
  258. var fork = parent;
  259. var join = self.getNodeById(childId);
  260. if (join.type() == 'join-widget') {
  261. if (fork.children().length == 2) {
  262. // Link top to above and delete fork
  263. var forkParent = self.getParents(fork.id());
  264. forkParent.set_link('to', ko.mapping.toJS(fork.get_link('to'))['to']);
  265. self.nodes.remove(fork);
  266. // Link bottom to child of join
  267. var beboreJoin = self.getParents(childId);
  268. var joinChildId = ko.mapping.toJS(join.get_link('to'))['to'];
  269. beboreJoin.set_link('to', joinChildId);
  270. self.nodes.remove(join);
  271. } else {
  272. parent.remove_link('to', childId);
  273. }
  274. }
  275. }
  276. };
  277. self.getParents = function(node_id) { // Only one for now
  278. var _node = null;
  279. $.each(self.nodes(), function (index, node) {
  280. $.each(node.children(), function(index, link) {
  281. var _link = ko.mapping.toJS(link);
  282. if ('to' in _link && _link.to == node_id) {
  283. _node = node;
  284. return false;
  285. }
  286. })
  287. });
  288. return _node;
  289. }
  290. self.getNodeById = function (node_id) {
  291. var _node = null;
  292. $.each(self.nodes(), function (index, node) {
  293. if (node.id() == node_id) {
  294. _node = node;
  295. return false;
  296. }
  297. });
  298. return _node;
  299. };
  300. }
  301. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json) {
  302. var self = this;
  303. self.isNested = ko.observable(true);
  304. self.isEditing = ko.observable(true);
  305. self.isEditing.subscribe(function(newVal){
  306. $(document).trigger("editingToggled");
  307. });
  308. self.toggleEditing = function () {
  309. self.isEditing(! self.isEditing());
  310. };
  311. self.columns = ko.observable([]);
  312. self.previewColumns = ko.observable("");
  313. self.workflow = new Workflow(self, workflow_json);
  314. self.credentials = ko.mapping.fromJSON(credentials_json);
  315. self.inited = ko.observable(self.columns().length > 0);
  316. self.init = function(callback) {
  317. loadLayout(self, layout_json);
  318. self.workflow.loadNodes(workflow_json);
  319. }
  320. self.depen = ko.observableArray(workflow_json.dependencies);
  321. self.addActionProperties = ko.observableArray([]);
  322. self.addActionWorkflows = ko.observableArray([]);
  323. self.selectedSubWorkflow = ko.observable();
  324. self.currentlyDraggedWidget = null;
  325. self.currentlyCreatingFork = false;
  326. self.currentlyCreatedFork = null;
  327. self.currentlyCreatedJoin = null;
  328. self.isDragging = ko.observable(false);
  329. self.setCurrentDraggedWidget = function (widget) {
  330. self.currentlyDraggedWidget = widget;
  331. }
  332. self.addDraggedWidget = function (row, atBeginning) {
  333. if (self.currentlyDraggedWidget != null) {
  334. var _parentCol = self.getRowParentColumn(row.id());
  335. var _rowIdx = 0;
  336. $.each(_parentCol.rows(), function (i, irow) {
  337. if (irow.id() == row.id()) {
  338. _rowIdx = i;
  339. }
  340. });
  341. var _newRow = _parentCol.addEmptyRow(false, _rowIdx);
  342. var _w = new Widget({
  343. size: self.currentlyDraggedWidget.size(),
  344. id: UUID(),
  345. name: self.currentlyDraggedWidget.name(),
  346. widgetType: self.currentlyDraggedWidget.widgetType(),
  347. properties: self.currentlyDraggedWidget.properties(),
  348. offset: self.currentlyDraggedWidget.offset(),
  349. loading: true,
  350. vm: self
  351. });
  352. _newRow.widgets([_w]);
  353. return _w;
  354. }
  355. }
  356. self.addSideDraggedWidget = function (row, atBeginning) {
  357. if (self.currentlyDraggedWidget != null) {
  358. var _parentCol = self.getRowParentColumn(row.id());
  359. var _rowIdx = 0;
  360. $.each(_parentCol.rows(), function (i, irow) {
  361. if (irow.id() == row.id()) {
  362. _rowIdx = i;
  363. }
  364. });
  365. var _addForkAndJoin = (row.columns().length == 0);
  366. if (_addForkAndJoin){
  367. var _forkRow = _parentCol.addEmptyRow(false, _rowIdx);
  368. var _id = UUID();
  369. var _fork = new Widget({
  370. size: 12,
  371. id: _id,
  372. name: 'fork' + '-' + _id.slice(0, 4),
  373. widgetType: "fork-widget",
  374. properties: {},
  375. offset: 0,
  376. loading: true,
  377. vm: self
  378. });
  379. _forkRow.widgets([_fork]);
  380. }
  381. var _w = new Widget({
  382. size: self.currentlyDraggedWidget.size(),
  383. id: UUID(),
  384. name: self.currentlyDraggedWidget.name(),
  385. widgetType: self.currentlyDraggedWidget.widgetType(),
  386. properties: self.currentlyDraggedWidget.properties(),
  387. offset: self.currentlyDraggedWidget.offset(),
  388. loading: true,
  389. vm: self
  390. });
  391. var _col = row.addEmptyColumn(atBeginning);
  392. var _row = new Row([_w], self);
  393. _col.addRow(_row);
  394. if (_addForkAndJoin) {
  395. var _joinRow = _parentCol.addEmptyRow(false, _rowIdx + 2);
  396. var _id = UUID();
  397. var _join = new Widget({
  398. size: 12,
  399. id: _id,
  400. name: "join" + '-' + _id.slice(0, 4),
  401. widgetType: "join-widget",
  402. properties: {},
  403. offset: 0,
  404. loading: true,
  405. vm: self
  406. });
  407. _joinRow.widgets([_join]);
  408. self.currentlyCreatedFork = ko.mapping.toJS(_fork);
  409. self.currentlyCreatedJoin = ko.mapping.toJS(_join);
  410. }
  411. self.currentlyCreatingFork = true;
  412. self.currentlyDraggedWidget = null;
  413. return _w;
  414. }
  415. }
  416. self.getWidgetById = function (widget_id) {
  417. var _widget = null;
  418. $.each(self.columns(), function (i, col) {
  419. $.each(col.rows(), function (j, row) {
  420. $.each(row.widgets(), function (z, widget) {
  421. if (widget.id() == widget_id){
  422. _widget = widget;
  423. return false;
  424. }
  425. });
  426. });
  427. });
  428. return _widget;
  429. }
  430. self.removeWidget = function (widget_json) {
  431. self.workflow.removeNode(widget_json.id());
  432. self.removeWidgetById(widget_json.id());
  433. }
  434. self.removeWidgetById = function (widget_id) {
  435. $.each(self.columns(), function (i, col) {
  436. self.deeplyRemoveWidgetById(widget_id, col, self)
  437. });
  438. }
  439. self.deeplyRemoveWidgetById = function (widget_id, col, parent) {
  440. if (col) {
  441. $.each(col.rows(), function (j, row) {
  442. if (row && row.widgets()){
  443. $.each(row.widgets(), function (z, widget) {
  444. if (widget.id() == widget_id) {
  445. row.widgets.remove(widget);
  446. col.rows.remove(row);
  447. }
  448. });
  449. }
  450. if (row && row.columns()) {
  451. $.each(row.columns(), function (i, icol) {
  452. self.deeplyRemoveWidgetById(widget_id, icol, row);
  453. });
  454. }
  455. });
  456. if (col.rows().length == 0) {
  457. parent.columns.remove(col);
  458. if (parent.columns().length > 1) {
  459. var _size = Math.max(1, Math.floor(12 / (parent.columns().length)));
  460. parent.columns().forEach(function (icol) {
  461. icol.size(_size);
  462. });
  463. }
  464. else {
  465. var _rows = parent.columns()[0].rows();
  466. var _parentRows = self.getRowParentColumn(parent.id()).rows;
  467. var _prevRowIdx = -1;
  468. for (var i = 0; i < _parentRows().length; i++) {
  469. if (_parentRows()[i].id() == parent.id()) {
  470. break;
  471. }
  472. _prevRowIdx = i;
  473. }
  474. if (_prevRowIdx > -1 && _parentRows()[_prevRowIdx].widgets().length > 0 && _parentRows()[_prevRowIdx].widgets()[0].widgetType() == "fork-widget"){
  475. _parentRows.remove(_parentRows()[_prevRowIdx]);
  476. _parentRows.remove(_parentRows()[_prevRowIdx+1]);
  477. }
  478. for (var i=0;i<_rows.length;i++){
  479. if (i==0){
  480. parent.widgets(_rows[i].widgets());
  481. }
  482. else {
  483. _parentRows.push(_rows[i]);
  484. }
  485. }
  486. parent.columns([]);
  487. }
  488. }
  489. }
  490. }
  491. self.getWidgetRelative = function (widget_id, isPredecessor) {
  492. var _row = self.getWidgetParentRow(widget_id);
  493. var _col = self.getRowParentColumn(_row.id());
  494. var _nextRow = null;
  495. for (var i = 0; i < _col.rows().length; i++) {
  496. if (_col.rows()[i].id() == _row.id()) {
  497. if (!isPredecessor && _col.rows().length >= i + 1) {
  498. _nextRow = _col.rows()[i + 1];
  499. }
  500. break;
  501. }
  502. _nextRow = _col.rows()[i];
  503. }
  504. if (_nextRow != null) {
  505. return _nextRow.widgets()[0];
  506. }
  507. else {
  508. var _parentRow = self.getColumnParentRow(_col.id());
  509. if (_parentRow) {
  510. var _parentColumn = self.getRowParentColumn(_parentRow.id());
  511. var _nextParentRow = null;
  512. for (var i = 0; i < _parentColumn.rows().length; i++) {
  513. if (_parentColumn.rows()[i].id() == _parentRow.id()) {
  514. if (!isPredecessor && _parentColumn.rows().length >= i + 1) {
  515. _nextParentRow = _parentColumn.rows()[i + 1];
  516. }
  517. break;
  518. }
  519. _nextParentRow = _parentColumn.rows()[i];
  520. }
  521. if (_nextParentRow != null) {
  522. return _nextParentRow.widgets()[0];
  523. }
  524. }
  525. }
  526. return null;
  527. }
  528. self.getWidgetPredecessor = function (widget_id) {
  529. return self.getWidgetRelative(widget_id, true);
  530. }
  531. self.getWidgetSuccessor = function (widget_id) {
  532. return self.getWidgetRelative(widget_id, false);
  533. }
  534. self.isRowAfterFork = function (row) {
  535. var _parentColumn = self.getRowParentColumn(row.id());
  536. var _prevRow = null;
  537. for (var i = 0; i < _parentColumn.rows().length; i++) {
  538. var _currentRow = _parentColumn.rows()[i];
  539. if (_currentRow.id() == row.id()){
  540. break;
  541. }
  542. _prevRow = _currentRow;
  543. }
  544. if (_prevRow != null){
  545. return _prevRow.widgets().length > 0 && _prevRow.widgets()[0].widgetType() == "fork-widget";
  546. }
  547. return false;
  548. }
  549. self.isRowBeforeJoin = function (row) {
  550. return row.widgets().length > 0 && row.widgets()[0].widgetType() == "join-widget";
  551. }
  552. self.getWidgetParentRow = function (widget_id) {
  553. var _row = null;
  554. for (var i = 0; i < self.columns().length; i++) {
  555. _row = self.traverseColumnForWidget(widget_id, self.columns()[i]);
  556. if (_row != null) {
  557. break;
  558. }
  559. }
  560. return _row;
  561. }
  562. self.getRowParentColumn = function (row_id) {
  563. var _column = null;
  564. for (var i = 0; i < self.columns().length; i++) {
  565. _column = self.traverseColumnForColumn(row_id, self.columns()[i]);
  566. }
  567. return _column;
  568. }
  569. self.getColumnParentRow = function (col_id) {
  570. var _row = null;
  571. for (var i = 0; i < self.columns().length; i++) {
  572. _row = self.traverseColumnForRow(col_id, self.columns()[i]);
  573. if (_row != null) {
  574. break;
  575. }
  576. }
  577. return _row;
  578. }
  579. self.getRowParentRow = function (row_id) {
  580. var _col = self.getRowParentColumn(row_id);
  581. if (_col != null) {
  582. return self.getColumnParentRow(_col.id());
  583. }
  584. }
  585. self.traverseColumnForColumn = function (row_id, col) {
  586. var _column = null;
  587. if (col) {
  588. for (var j = 0; j < col.rows().length; j++) {
  589. var row = col.rows()[j];
  590. if (row.id() == row_id) {
  591. _column = col;
  592. break;
  593. }
  594. for (var z = 0; z < row.columns().length; z++) {
  595. _column = self.traverseColumnForColumn(row_id, row.columns()[z]);
  596. if (_column != null) {
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. return _column;
  603. }
  604. self.traverseColumnForRow = function (col_id, col) {
  605. var _row = null;
  606. if (col) {
  607. for (var j = 0; j < col.rows().length; j++) {
  608. var row = col.rows()[j];
  609. for (var z = 0; z < row.columns().length; z++) {
  610. var _col = row.columns()[z];
  611. if (_col.id() == col_id) {
  612. _row = row;
  613. }
  614. else {
  615. _row = self.traverseColumnForRow(col_id, _col);
  616. }
  617. if (_row != null) {
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. return _row;
  624. }
  625. self.traverseColumnForWidget = function (widget_id, col) {
  626. var _row = null;
  627. if (col) {
  628. for (var j = 0; j < col.rows().length; j++) {
  629. var row = col.rows()[j];
  630. for (var z = 0; z < row.widgets().length; z++) {
  631. var widget = row.widgets()[z];
  632. if (widget.id() == widget_id) {
  633. _row = row;
  634. break;
  635. }
  636. }
  637. if (_row != null) {
  638. break;
  639. }
  640. for (var z = 0; z < row.columns().length; z++) {
  641. _row = self.traverseColumnForWidget(widget_id, row.columns()[z]);
  642. if (_row != null) {
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. return _row;
  649. }
  650. self.save = function () {
  651. $.post("/oozie/editor/workflow/save/", {
  652. "layout": ko.mapping.toJSON(self.columns),
  653. "workflow": ko.mapping.toJSON(self.workflow)
  654. }, function (data) {
  655. if (data.status == 0) {
  656. self.workflow.id(data.id);
  657. $(document).trigger("info", data.message);
  658. if (window.location.search.indexOf("workflow") == -1) {
  659. window.location.hash = '#workflow=' + data.id;
  660. }
  661. }
  662. else {
  663. $(document).trigger("error", data.message);
  664. }
  665. }).fail(function (xhr, textStatus, errorThrown) {
  666. $(document).trigger("error", xhr.responseText);
  667. });
  668. };
  669. self.gen_xml = function () {
  670. $.post("/oozie/editor/workflow/gen_xml/", {
  671. "layout": ko.mapping.toJSON(self.columns),
  672. "workflow": ko.mapping.toJSON(self.workflow)
  673. }, function (data) {
  674. if (data.status == 0) {
  675. console.log(data.xml);
  676. }
  677. else {
  678. $(document).trigger("error", data.message);
  679. }
  680. }).fail(function (xhr, textStatus, errorThrown) {
  681. $(document).trigger("error", xhr.responseText);
  682. });
  683. };
  684. self.showSubmitPopup = function () {
  685. // If self.workflow.id() == null, need to save wf for now
  686. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  687. }, function (data) {
  688. $(document).trigger("showSubmitPopup", data);
  689. }).fail(function (xhr, textStatus, errorThrown) {
  690. $(document).trigger("error", xhr.responseText);
  691. });
  692. };
  693. function bareWidgetBuilder(name, type){
  694. return new Widget({
  695. size: 12,
  696. id: UUID(),
  697. name: name,
  698. widgetType: type
  699. });
  700. }
  701. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  702. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  703. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  704. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  705. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  706. self.draggableStopNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  707. };