sqoop.utils.js 5.4 KB

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