bundle-editor.ko.js 5.0 KB

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