workflow.utils.js 3.0 KB

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