workflow-editor.ko.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 magicLayout(vm) {
  17. loadLayout(vm, vm.initial.layout);
  18. $(document).trigger("magicLayout");
  19. }
  20. function loadLayout(viewModel, json_layout) {
  21. var _columns = [];
  22. $(json_layout).each(function (cnt, json_col) {
  23. var _rows = [];
  24. $(json_col.rows).each(function (rcnt, json_row) {
  25. var row = new Row([], viewModel);
  26. $(json_row.widgets).each(function (wcnt, widget) {
  27. row.addWidget(new Widget({
  28. size:widget.size,
  29. id: widget.id,
  30. name: widget.name,
  31. widgetType: widget.widgetType,
  32. properties: widget.properties,
  33. offset: widget.offset,
  34. loading: true,
  35. vm: viewModel
  36. }));
  37. });
  38. _rows.push(row);
  39. });
  40. var column = new Column(json_col.size, _rows);
  41. _columns = _columns.concat(column);
  42. });
  43. viewModel.columns(_columns);
  44. }
  45. // End dashboard lib
  46. var Node = function (node) {
  47. var self = this;
  48. var type = typeof node.widgetType != "undefined" ? node.widgetType : node.type;
  49. self.id = ko.observable(typeof node.id != "undefined" && node.id != null ? node.id : UUID());
  50. self.name = ko.observable(typeof node.name != "undefined" && node.name != null ? node.name : "");
  51. self.type = ko.observable(typeof type != "undefined" && type != null ? type : "");
  52. self.properties = ko.mapping.fromJS(typeof node.properties != "undefined" && node.properties != null ? node.properties : {});
  53. self.children = ko.mapping.fromJS(typeof node.children != "undefined" && node.children != null ? node.children : []);
  54. }
  55. var Workflow = function (vm, workflow) {
  56. var self = this;
  57. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  58. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  59. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  60. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  61. self.nodes = ko.observableArray([]);
  62. self.loadNodes = function(workflow) {
  63. var nodes = []
  64. $.each(workflow.nodes, function(index, node) {
  65. var _node = new Node(node);
  66. nodes.push(_node);
  67. });
  68. self.nodes(nodes)
  69. }
  70. self.addNode = function(widget) {
  71. // Todo get parent cell, link nodes...
  72. //if (self.nodes().length == 0) {
  73. var node = new Node(ko.mapping.toJS(widget));
  74. node.children().push({'to': '33430f0f-ebfa-c3ec-f237-3e77efa03d0a'})
  75. self.nodes.push(node);
  76. self.nodes()[0].children.removeAll();
  77. self.nodes()[0].children().push({'to': node.id()});
  78. //}
  79. }
  80. self.getNodeById = function (node_id) {
  81. var _node = null;
  82. $.each(self.nodes(), function (index, node) {
  83. if (node.id() == node_id) {
  84. _node = node;
  85. return false;
  86. }
  87. });
  88. return _node;
  89. }
  90. }
  91. var WorkflowEditorViewModel = function (layout_json, workflow_json) {
  92. var self = this;
  93. self.isEditing = ko.observable(true);
  94. self.toggleEditing = function () {
  95. self.isEditing(! self.isEditing());
  96. };
  97. self.columns = ko.observable([]);
  98. self.previewColumns = ko.observable("");
  99. self.workflow = new Workflow(self, workflow_json);
  100. self.inited = ko.observable(self.columns().length > 0);
  101. self.init = function(callback) {
  102. loadLayout(self, layout_json);
  103. self.workflow.loadNodes(workflow_json);
  104. }
  105. self.getWidgetById = function (widget_id) {
  106. var _widget = null;
  107. $.each(self.columns(), function (i, col) {
  108. $.each(col.rows(), function (j, row) {
  109. $.each(row.widgets(), function (z, widget) {
  110. if (widget.id() == widget_id){
  111. _widget = widget;
  112. }
  113. });
  114. });
  115. });
  116. return _widget;
  117. }
  118. self.save = function () {
  119. $.post("/oozie/editor/workflow/save/", {
  120. "layout": ko.mapping.toJSON(self.columns),
  121. "workflow": ko.mapping.toJSON(self.workflow)
  122. }, function (data) {
  123. if (data.status == 0) {
  124. self.workflow.id(data.id);
  125. $(document).trigger("info", data.message);
  126. if (window.location.search.indexOf("workflow") == -1) {
  127. window.location.hash = '#workflow=' + data.id;
  128. }
  129. }
  130. else {
  131. $(document).trigger("error", data.message);
  132. }
  133. }).fail(function (xhr, textStatus, errorThrown) {
  134. $(document).trigger("error", xhr.responseText);
  135. });
  136. };
  137. self.gen_xml = function () {
  138. $.post("/oozie/editor/workflow/gen_xml/", {
  139. "layout": ko.mapping.toJSON(self.columns),
  140. "workflow": ko.mapping.toJSON(self.workflow)
  141. }, function (data) {
  142. if (data.status == 0) {
  143. alert(data.xml);
  144. }
  145. else {
  146. $(document).trigger("error", data.message);
  147. }
  148. }).fail(function (xhr, textStatus, errorThrown) {
  149. $(document).trigger("error", xhr.responseText);
  150. });
  151. };
  152. function bareWidgetBuilder(name, type){
  153. return new Widget({
  154. size: 12,
  155. id: UUID(),
  156. name: name,
  157. widgetType: type
  158. });
  159. }
  160. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  161. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  162. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  163. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  164. };