workflow.utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // open a modal window for editing a node
  17. function edit_node_modal(modal, workflow, node, save, cancel, template) {
  18. var backup = ko.mapping.toJS(node);
  19. normalize_model_fields(backup);
  20. modal.hide();
  21. modal.setTemplate(template || node.edit_template);
  22. // Provide node, readonly mode, and error link updater.
  23. // Kill node is manually added to list of nodes that users can select from.
  24. // Kill node is placed at the front of the list so that it is automatically selected.
  25. var context = {
  26. node: node,
  27. read_only: workflow.read_only(),
  28. nodes: ko.computed({
  29. read: function() {
  30. var arr = ko.utils.arrayFilter(workflow.registry.allNodes(), function(_node) {
  31. return _node.id() && _node.id() != node.id() && $.inArray(_node.node_type(), ['start']) == -1;
  32. });
  33. return arr;
  34. }
  35. }),
  36. error_node: ko.computed({
  37. read: function() {
  38. var error_child = node.getErrorChild();
  39. return (error_child) ? error_child.id() : null;
  40. },
  41. write: function(node_id) {
  42. var error_child = workflow.registry.get(node_id);
  43. if (error_child) {
  44. node.putErrorChild(error_child);
  45. }
  46. }
  47. })
  48. };
  49. modal.show(context);
  50. modal.recenter(280, 0);
  51. modal.addDecorations();
  52. var cancel_edit = cancel || function() {
  53. ko.mapping.fromJS(backup, node);
  54. modal.hide();
  55. // Prevent event propagation
  56. return false;
  57. };
  58. var try_save = save || function() {
  59. if (node.validate()) {
  60. workflow.is_dirty( true );
  61. modal.hide();
  62. }
  63. };
  64. $('.modal-backdrop').on('click', cancel_edit);
  65. modal.el.on('click', '.close', cancel_edit);
  66. modal.el.on('click', '.cancelButton', cancel_edit);
  67. modal.el.on('click', '.doneButton', try_save);
  68. modal.el.on('click', '.edit-node-link', function() {
  69. var link = ko.contextFor(this).$data;
  70. var parent = ko.contextFor(this).$parent;
  71. var node = parent.registry.get(link.child());
  72. cancel_edit();
  73. edit_node_modal(node);
  74. });
  75. }