sqoop.utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //// Get collections utils
  17. function fetcher_success(name, Node, options) {
  18. return function(data) {
  19. switch(data.status) {
  20. case -1:
  21. $(document).trigger('connection_error.' + name, [options, data])
  22. break;
  23. case 0:
  24. var nodes = [];
  25. $.each(data[name], function(index, model_dict) {
  26. var node = new Node({modelDict: model_dict});
  27. nodes.push(node);
  28. });
  29. $(document).trigger('loaded.' + name, [nodes, options]);
  30. break;
  31. default:
  32. case 1:
  33. $(document).trigger('load_error.' + name, [options, data]);
  34. break;
  35. }
  36. };
  37. }
  38. //// KO utils
  39. ko.bindingHandlers.routie = {
  40. init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  41. $(element).click(function() {
  42. var obj = ko.utils.unwrapObservable(valueAccessor());
  43. var url = null;
  44. var bubble = false;
  45. if ($.isPlainObject(obj)) {
  46. url = obj.url;
  47. bubble = !!obj.bubble;
  48. } else {
  49. url = obj;
  50. }
  51. routie(url);
  52. return bubble;
  53. });
  54. }
  55. };
  56. ko.bindingHandlers.editableText = {
  57. init: function(element, valueAccessor) {
  58. $(element).on('blur', function() {
  59. var observable = valueAccessor();
  60. observable( $(this).text() );
  61. });
  62. },
  63. update: function(element, valueAccessor) {
  64. var value = ko.utils.unwrapObservable(valueAccessor());
  65. $(element).text(value);
  66. }
  67. };
  68. ko.bindingHandlers.clickValue = {
  69. init: function(element, valueAccessor) {
  70. var value = ko.utils.unwrapObservable(valueAccessor());
  71. if ($(element).val() == value) {
  72. $(element).click();
  73. }
  74. $(element).on('click', function() {
  75. var observable = valueAccessor();
  76. observable( $(this).val() );
  77. });
  78. },
  79. update: function(element, valueAccessor) {
  80. var value = ko.utils.unwrapObservable(valueAccessor());
  81. if ($(element).val() == value) {
  82. $(element).click();
  83. }
  84. }
  85. };
  86. ko.sqoop = {
  87. 'fixModel' : function(root) {
  88. function parsePropertyName(propertyName) {
  89. var properties = [];
  90. if (propertyName) {
  91. // get object keys in form of array
  92. var propertyNames = propertyName.split('.');
  93. // add indexes to array
  94. $.each(propertyNames, function(index, property) {
  95. if (property.indexOf(']') !== -1) {
  96. var left = property.indexOf('[');
  97. var right = property.indexOf(']');
  98. var index = parseInt(property.substring(left+1, right));
  99. properties.push(property.substring(0, left), index);
  100. } else {
  101. properties.push(property);
  102. }
  103. });
  104. }
  105. return properties;
  106. }
  107. function getPropertyByName(root, propertyName) {
  108. var parts = parsePropertyName(propertyName);
  109. var obj = root;
  110. $.each(parts, function(index, property) {
  111. if (ko.isObservable(obj)) {
  112. obj = obj()[property];
  113. } else {
  114. obj = obj[property];
  115. }
  116. });
  117. return obj;
  118. }
  119. function setPropertyByName(root, propertyName, propertyValue) {
  120. var parts = parsePropertyName(propertyName);
  121. var obj = root;
  122. var parent = null;
  123. var propertyName = null;
  124. if (parts.length != 0) {
  125. $.each(parts, function(index, property) {
  126. propertyName = property;
  127. parent = obj;
  128. if (ko.isObservable(obj)) {
  129. obj = obj()[property];
  130. } else {
  131. obj = obj[property];
  132. }
  133. });
  134. if (ko.isObservable(parent)) {
  135. parent()[propertyName] = propertyValue;
  136. } else {
  137. parent[propertyName] = propertyValue;
  138. }
  139. }
  140. }
  141. var models = [];
  142. // Map to original model
  143. var root_model = ko.mapping.toJS(root);
  144. // ko.mapping.visitModel performs a breadth first search.
  145. ko.mapping.visitModel(root, function(value, parent_name) {
  146. if (value['fixModel']) {
  147. models.push(parent_name);
  148. }
  149. });
  150. // Need a depth first search result
  151. models = models.reverse();
  152. console.log(models);
  153. // Call fixModel on every node that has that method.
  154. $.each(models, function(index, parent_name) {
  155. var old_model = getPropertyByName(root_model, parent_name);
  156. var new_model = getPropertyByName(root, parent_name).fixModel(old_model);
  157. setPropertyByName(root_model, parent_name, new_model);
  158. });
  159. return root_model;
  160. }
  161. };
  162. //// JQuery Utils
  163. if (jQuery) {
  164. jQuery.extend({
  165. setdefault: function(obj, accessor, default_value) {
  166. if (!(accessor in obj)) {
  167. obj[accessor] = default_value;
  168. }
  169. return obj[accessor];
  170. }
  171. });
  172. }