workflow-editor.ko.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. }
  88. var Workflow = function (vm, workflow) {
  89. var self = this;
  90. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  91. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  92. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  93. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  94. self.nodes = ko.observableArray([]);
  95. self.loadNodes = function(workflow) {
  96. var nodes = []
  97. $.each(workflow.nodes, function(index, node) {
  98. var _node = new Node(node);
  99. nodes.push(_node);
  100. });
  101. self.nodes(nodes)
  102. }
  103. self.newNode = function(widget) {
  104. $.ajax({
  105. type: "POST",
  106. url: "/oozie/editor/workflow/new_node/",
  107. data: {
  108. "workflow": ko.mapping.toJSON(workflow),
  109. "node": ko.mapping.toJSON(widget)
  110. },
  111. success: function (data) {
  112. if (data.status == 0) {
  113. viewModel.addActionProperties(data.properties);
  114. viewModel.addActionWorkflows(data.workflows);
  115. }
  116. },
  117. async: false
  118. });
  119. };
  120. self.addNode = function(widget) {
  121. // Todo get parent cell, link nodes... when we have the new layout
  122. $.post("/oozie/editor/workflow/add_node/", {
  123. "workflow": ko.mapping.toJSON(workflow),
  124. "node": ko.mapping.toJSON(widget),
  125. "properties": ko.mapping.toJSON(viewModel.addActionProperties()),
  126. "subworkflow": viewModel.selectedSubWorkflow() ? ko.mapping.toJSON(viewModel.selectedSubWorkflow()) : '{}',
  127. }, function (data) {
  128. if (data.status == 0) {
  129. var _node = ko.mapping.toJS(widget);
  130. _node.properties = data.properties;
  131. _node.name = data.name;
  132. var node = new Node(_node);
  133. node.children().push({'to': '33430f0f-ebfa-c3ec-f237-3e77efa03d0a'}) // Link to child
  134. // Add to list of nodes
  135. var end = self.nodes.pop();
  136. self.nodes.push(node);
  137. self.nodes.push(end);
  138. self.nodes()[0].children.removeAll(); // Parent link to new node
  139. self.nodes()[0].children().push({'to': node.id()});
  140. } else {
  141. $(document).trigger("error", data.message);
  142. }
  143. }).fail(function (xhr, textStatus, errorThrown) {
  144. $(document).trigger("error", xhr.responseText);
  145. });
  146. };
  147. self.getNodeById = function (node_id) {
  148. var _node = null;
  149. $.each(self.nodes(), function (index, node) {
  150. if (node.id() == node_id) {
  151. _node = node;
  152. return false;
  153. }
  154. });
  155. return _node;
  156. }
  157. }
  158. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json) {
  159. var self = this;
  160. self.isNested = ko.observable(true);
  161. self.isEditing = ko.observable(true);
  162. self.toggleEditing = function () {
  163. self.isEditing(! self.isEditing());
  164. };
  165. self.columns = ko.observable([]);
  166. self.previewColumns = ko.observable("");
  167. self.workflow = new Workflow(self, workflow_json);
  168. self.credentials = ko.mapping.fromJSON(credentials_json);
  169. self.inited = ko.observable(self.columns().length > 0);
  170. self.init = function(callback) {
  171. loadLayout(self, layout_json);
  172. self.workflow.loadNodes(workflow_json);
  173. }
  174. self.depen = ko.observableArray(workflow_json.dependencies);
  175. self.addActionProperties = ko.observableArray([]);
  176. self.addActionWorkflows = ko.observableArray([]);
  177. self.selectedSubWorkflow = ko.observable();
  178. self.currentlyDraggedWidget = null;
  179. self.isDragging = ko.observable(false);
  180. self.setCurrentDraggedWidget = function (widget) {
  181. self.currentlyDraggedWidget = widget;
  182. }
  183. self.addDraggedWidget = function (row, atBeginning) {
  184. if (self.currentlyDraggedWidget != null) {
  185. var _w = new Widget({
  186. size: self.currentlyDraggedWidget.size(),
  187. id: UUID(),
  188. name: self.currentlyDraggedWidget.name(),
  189. widgetType: self.currentlyDraggedWidget.widgetType(),
  190. properties: self.currentlyDraggedWidget.properties(),
  191. offset: self.currentlyDraggedWidget.offset(),
  192. loading: true,
  193. vm: self
  194. });
  195. var _col = row.addEmptyColumn(atBeginning);
  196. var _row = new Row([_w], self);
  197. _col.addRow(_row);
  198. self.currentlyDraggedWidget = null;
  199. return _w;
  200. }
  201. }
  202. self.getWidgetById = function (widget_id) {
  203. var _widget = null;
  204. $.each(self.columns(), function (i, col) {
  205. $.each(col.rows(), function (j, row) {
  206. $.each(row.widgets(), function (z, widget) {
  207. if (widget.id() == widget_id){
  208. _widget = widget;
  209. return false;
  210. }
  211. });
  212. });
  213. });
  214. return _widget;
  215. }
  216. self.removeWidget = function (widget_json) {
  217. self.removeWidgetById(widget_json.id());
  218. }
  219. self.removeWidgetById = function (widget_id) {
  220. $.each(self.columns(), function (i, col) {
  221. $.each(col.rows(), function (j, row) {
  222. $.each(row.widgets(), function (z, widget) {
  223. if (widget.id() == widget_id){
  224. row.widgets.remove(widget);
  225. }
  226. });
  227. });
  228. });
  229. }
  230. self.save = function () {
  231. $.post("/oozie/editor/workflow/save/", {
  232. "layout": ko.mapping.toJSON(self.columns),
  233. "workflow": ko.mapping.toJSON(self.workflow)
  234. }, function (data) {
  235. if (data.status == 0) {
  236. self.workflow.id(data.id);
  237. $(document).trigger("info", data.message);
  238. if (window.location.search.indexOf("workflow") == -1) {
  239. window.location.hash = '#workflow=' + data.id;
  240. }
  241. }
  242. else {
  243. $(document).trigger("error", data.message);
  244. }
  245. }).fail(function (xhr, textStatus, errorThrown) {
  246. $(document).trigger("error", xhr.responseText);
  247. });
  248. };
  249. self.gen_xml = function () {
  250. $.post("/oozie/editor/workflow/gen_xml/", {
  251. "layout": ko.mapping.toJSON(self.columns),
  252. "workflow": ko.mapping.toJSON(self.workflow)
  253. }, function (data) {
  254. if (data.status == 0) {
  255. alert(data.xml);
  256. }
  257. else {
  258. $(document).trigger("error", data.message);
  259. }
  260. }).fail(function (xhr, textStatus, errorThrown) {
  261. $(document).trigger("error", xhr.responseText);
  262. });
  263. };
  264. self.showSubmitPopup = function () {
  265. // If self.workflow.id() == null, need to save wf for now
  266. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  267. }, function (data) {
  268. $(document).trigger("showSubmitPopup", data);
  269. }).fail(function (xhr, textStatus, errorThrown) {
  270. $(document).trigger("error", xhr.responseText);
  271. });
  272. };
  273. self.removeWidgetById = function (widget_id) {
  274. $.each(self.columns(), function (i, col) {
  275. $.each(col.rows(), function (j, row) {
  276. $.each(row.widgets(), function (z, widget) {
  277. if (widget.id() == widget_id){
  278. row.widgets.remove(widget);
  279. }
  280. });
  281. });
  282. });
  283. }
  284. function bareWidgetBuilder(name, type){
  285. return new Widget({
  286. size: 12,
  287. id: UUID(),
  288. name: name,
  289. widgetType: type
  290. });
  291. }
  292. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  293. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  294. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  295. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  296. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  297. self.draggableForkNode = ko.observable(bareWidgetBuilder("Fork", "fork-widget"));
  298. self.draggableDecisionNode = ko.observable(bareWidgetBuilder("Decision", "decision-widget"));
  299. self.draggableStopNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  300. };