bundle-editor.ko.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var Bundle = function (vm, bundle) {
  17. var self = this;
  18. self.id = ko.observable(typeof bundle.id != "undefined" && bundle.id != null ? bundle.id : null);
  19. self.uuid = ko.observable(typeof bundle.uuid != "undefined" && bundle.uuid != null ? bundle.uuid : UUID());
  20. self.name = ko.observable(typeof bundle.name != "undefined" && bundle.name != null ? bundle.name : "");
  21. self.coordinators = ko.mapping.fromJS(typeof bundle.coordinators != "undefined" && bundle.coordinators != null ? bundle.coordinators : []);
  22. self.properties = ko.mapping.fromJS(typeof bundle.properties != "undefined" && bundle.properties != null ? bundle.properties : {});
  23. self.tracker = new ChangeTracker(self); // from ko.common-dashboard.js
  24. self.isDirty = ko.computed(function () {
  25. return self.tracker().somethingHasChanged();
  26. });
  27. self.addCoordinator = function (coordinator_uuid) {
  28. self.getCoordinatorParameters(coordinator_uuid);
  29. };
  30. self.getCoordinatorParameters = function (uuid) {
  31. $.get("/oozie/editor/coordinator/parameters/", {
  32. "uuid": uuid,
  33. }, function (data) {
  34. var _var = {
  35. 'coordinator': uuid,
  36. 'properties': data.parameters
  37. };
  38. self.coordinators.push(ko.mapping.fromJS(_var));
  39. }).fail(function (xhr, textStatus, errorThrown) {
  40. $(document).trigger("error", xhr.responseText);
  41. });
  42. };
  43. }
  44. var BundleEditorViewModel = function (bundle_json, coordinators_json, can_edit_json) {
  45. var self = this;
  46. self.canEdit = ko.mapping.fromJS(can_edit_json);
  47. self.isEditing = ko.observable(bundle_json.id == null);
  48. self.isEditing.subscribe(function (newVal) {
  49. $(document).trigger("editingToggled");
  50. });
  51. self.toggleEditing = function () {
  52. self.isEditing(!self.isEditing());
  53. };
  54. self.bundle = new Bundle(self, bundle_json);
  55. self.coordinators = ko.mapping.fromJS(coordinators_json);
  56. self.coordinatorModalFilter = ko.observable("");
  57. self.filteredModalCoordinators = ko.computed(function () {
  58. var _filter = self.coordinatorModalFilter().toLowerCase();
  59. if (!_filter) {
  60. return self.coordinators();
  61. }
  62. else {
  63. return ko.utils.arrayFilter(self.coordinators(), function (coord) {
  64. return coord.name().toLowerCase().indexOf(_filter.toLowerCase()) > -1;
  65. });
  66. }
  67. }, self);
  68. self.getCoordinatorById = function (uuid) {
  69. var _coords = ko.utils.arrayFilter(self.coordinators(), function (coord) {
  70. return coord.uuid() == uuid;
  71. });
  72. if (_coords.length > 0) {
  73. return _coords[0];
  74. }
  75. return null;
  76. }
  77. self.addBundledCoordinator = function (uuid) {
  78. self.bundle.addCoordinator(uuid);
  79. };
  80. self.save = function () {
  81. $.post("/oozie/editor/bundle/save/", {
  82. "bundle": ko.mapping.toJSON(self.bundle)
  83. }, function (data) {
  84. if (data.status == 0) {
  85. self.bundle.id(data.id);
  86. self.bundle.tracker().markCurrentStateAsClean();
  87. $(document).trigger("info", data.message);
  88. if (window.location.search.indexOf("bundle") == -1) {
  89. window.location.hash = '#bundle=' + data.id;
  90. }
  91. }
  92. else {
  93. $(document).trigger("error", data.message);
  94. }
  95. }).fail(function (xhr, textStatus, errorThrown) {
  96. $(document).trigger("error", xhr.responseText);
  97. });
  98. };
  99. self.showSubmitPopup = function () {
  100. // If self.bundle.id() == null, need to save wf for now
  101. $(".jHueNotify").hide();
  102. if (!self.bundle.isDirty()) {
  103. logGA('submit');
  104. $.get("/oozie/editor/bundle/submit/" + self.bundle.id(), {
  105. }, function (data) {
  106. $(document).trigger("showSubmitPopup", data);
  107. }).fail(function (xhr, textStatus, errorThrown) {
  108. $(document).trigger("error", xhr.responseText);
  109. });
  110. }
  111. };
  112. };
  113. function logGA(page) {
  114. if (typeof trackOnGA == 'function') {
  115. trackOnGA('oozie/editor/bundle/' + page);
  116. }
  117. }