workflow-editor.ko.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_json) {
  47. var self = this;
  48. self.id = ko.observable(typeof node_json.id != "undefined" && node_json.id != null ? node_json.id : UUID());
  49. self.name = ko.observable(typeof node_json.name != "undefined" && node_json.name != null ? node_json.name : "");
  50. }
  51. var Workflow = function (vm, workflow) {
  52. var self = this;
  53. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : UUID());
  54. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  55. self.nodes = ko.observableArray([]);
  56. self.addNode = function(widget) {
  57. if (self.nodes().length == 0) {
  58. var node = new Node(ko.mapping.toJS(widget));
  59. self.nodes.push(node);
  60. }
  61. }
  62. self.getNodeById = function (node_id) {
  63. var _node = null;
  64. $.each(self.nodes(), function (index, node) {
  65. if (node.id() == node_id) {
  66. _node = node;
  67. return false;
  68. }
  69. });
  70. return _node;
  71. }
  72. }
  73. var WorkflowEditorViewModel = function (layout_json, workflow_json) {
  74. var self = this;
  75. self.isEditing = ko.observable(true);
  76. self.toggleEditing = function () {
  77. self.isEditing(! self.isEditing());
  78. };
  79. self.columns = ko.observable([]);
  80. self.previewColumns = ko.observable("");
  81. self.workflow = new Workflow(self, workflow_json);
  82. self.inited = ko.observable(self.columns().length > 0);
  83. self.init = function(callback) {
  84. loadLayout(self, layout_json);
  85. }
  86. self.getWidgetById = function (widget_id) {
  87. var _widget = null;
  88. $.each(self.columns(), function (i, col) {
  89. $.each(col.rows(), function (j, row) {
  90. $.each(row.widgets(), function (z, widget) {
  91. if (widget.id() == widget_id){
  92. _widget = widget;
  93. }
  94. });
  95. });
  96. });
  97. return _widget;
  98. }
  99. self.save = function () {
  100. $.post("/impala/dashboard/save", {
  101. "dashboard": ko.mapping.toJSON(self.dashboard),
  102. "layout": ko.mapping.toJSON(self.columns)
  103. }, function (data) {
  104. if (data.status == 0) {
  105. self.dashboard.id(data.id);
  106. $(document).trigger("info", data.message);
  107. if (window.location.search.indexOf("dashboard") == -1) {
  108. window.location.hash = '#dashboard=' + data.id;
  109. }
  110. }
  111. else {
  112. $(document).trigger("error", data.message);
  113. }
  114. }).fail(function (xhr, textStatus, errorThrown) {
  115. $(document).trigger("error", xhr.responseText);
  116. });
  117. };
  118. function bareWidgetBuilder(name, type){
  119. return new Widget({
  120. size: 12,
  121. id: UUID(),
  122. name: name,
  123. widgetType: type
  124. });
  125. }
  126. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  127. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  128. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  129. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  130. };