workflow-editor.ko.js 24 KB

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