sqoop.forms.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. function transform_keys(model, keys_dict) {
  17. $.each(keys_dict, function(key, new_key) {
  18. if (key in model) {
  19. model[new_key] = model[key];
  20. delete model[key];
  21. }
  22. });
  23. return model;
  24. }
  25. function transform_values(model, func_dict) {
  26. $.each(func_dict, function(key, f) {
  27. if (key in model) {
  28. model[key] = f(key, model[key]);
  29. }
  30. });
  31. return model;
  32. }
  33. function to_form(value) {
  34. return new forms.FormModel(value);
  35. }
  36. function to_forms(key, value) {
  37. $.each(value, function(index, form_dict) {
  38. value[index] = to_form(form_dict);
  39. });
  40. return value;
  41. }
  42. function to_input(value) {
  43. if (value.type.toLowerCase() == 'map') {
  44. return new forms.MapInputModel(value);
  45. } else {
  46. return new forms.InputModel(value);
  47. }
  48. }
  49. function to_inputs(key, value) {
  50. $.each(value, function(index, input_dict) {
  51. value[index] = to_input(input_dict);
  52. });
  53. return value;
  54. }
  55. var forms = (function($) {
  56. var map_form_properties = {
  57. 'create': function(options) {
  58. return new SqoopForm({modelDict: options.data});
  59. },
  60. 'update': function(options) {
  61. options.target.initialize({modelDict: options.data})
  62. return options.target;
  63. },
  64. };
  65. var map_input_properties = {
  66. 'create': function(options) {
  67. switch(options.data.type.toLowerCase()) {
  68. case 'map':
  69. return new SqoopMapInput({modelDict: options.data});
  70. default:
  71. return new SqoopInput({modelDict: options.data});
  72. }
  73. },
  74. 'update': function(options) {
  75. options.target.initialize({modelDict: options.data})
  76. return options.target;
  77. },
  78. };
  79. var map_properties = {
  80. 'connector': map_form_properties,
  81. 'framework': map_form_properties,
  82. 'con-forms': map_form_properties,
  83. 'job-forms': map_form_properties,
  84. 'inputs': map_input_properties
  85. };
  86. var FormModel = koify.Model.extend({
  87. 'id': -1,
  88. 'inputs': [],
  89. 'name': null,
  90. 'type': null,
  91. 'initialize': function(attrs) {
  92. var self = this;
  93. attrs = transform_values(attrs, {
  94. 'inputs': to_inputs
  95. });
  96. return attrs;
  97. }
  98. });
  99. var InputModel = koify.Model.extend({
  100. 'id': -1,
  101. 'name': null,
  102. 'type': null,
  103. 'size': -1,
  104. 'sensitive': false,
  105. 'values': null,
  106. 'value': "",
  107. 'initialize': function(attrs) {
  108. var self = this;
  109. var attrs = $.extend(true, {}, attrs);
  110. if ('values' in attrs && attrs['values']) {
  111. attrs['values'] = ($.isArray(attrs['values'])) ? attrs['values'] : attrs['values'].split(',');
  112. }
  113. if ('value' in attrs) {
  114. attrs.value = decodeURIComponent(attrs.value);
  115. }
  116. return attrs;
  117. }
  118. });
  119. var MapInputModel = InputModel.extend({
  120. 'value': {},
  121. 'initialize': function(attrs) {
  122. var self = this;
  123. if ('value' in attrs && attrs.value) {
  124. if (!$.isArray(attrs.value)) {
  125. var map = attrs['value'];
  126. attrs.value = [];
  127. $.each(map, function(key, value) {
  128. attrs.value.push({
  129. 'key': key,
  130. 'value': value
  131. });
  132. });
  133. }
  134. } else {
  135. attrs.value = [];
  136. }
  137. return attrs;
  138. }
  139. });
  140. // Form is reserved word
  141. var SqoopForm = koify.MinimalNode.extend({
  142. 'model_class': FormModel,
  143. 'map': function() {
  144. var self = this;
  145. var mapping_options = $.extend(true, {
  146. 'ignore': ['parent', 'initialize']
  147. }, map_properties);
  148. if ('__ko_mapping__' in self) {
  149. ko.mapping.fromJS(self.model, mapping_options, self);
  150. } else {
  151. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  152. $.extend(self, mapped);
  153. }
  154. }
  155. });
  156. var SqoopInput = koify.MinimalNode.extend({
  157. 'model_class': InputModel,
  158. 'map': function() {
  159. var self = this;
  160. var mapping_options = {
  161. 'ignore': ['parent', 'initialize']
  162. };
  163. if ('__ko_mapping__' in self) {
  164. ko.mapping.fromJS(self.model, mapping_options, self);
  165. } else {
  166. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  167. $.extend(self, mapped);
  168. }
  169. }
  170. });
  171. var SqoopMapInput = SqoopInput.extend({
  172. 'model_class': MapInputModel,
  173. 'initialize': function() {
  174. var self = this;
  175. self.parent.initialize.apply(self, arguments);
  176. self.addToMap = function() {
  177. var value = {
  178. 'key': ko.observable(""),
  179. 'value': ko.observable("")
  180. };
  181. value.key.subscribe(function() {
  182. self.value.valueHasMutated();
  183. });
  184. value.value.subscribe(function() {
  185. self.value.valueHasMutated();
  186. });
  187. self.value().push(value);
  188. self.value.valueHasMutated();
  189. };
  190. self.removeFromMap = function(index) {
  191. self.value().splice(index, 1);
  192. self.value.valueHasMutated();
  193. };
  194. },
  195. fixModel: function(model) {
  196. var map = {};
  197. $.each(model.value, function(index, obj) {
  198. if (obj.key) {
  199. map[obj.key] = obj.value;
  200. }
  201. });
  202. model.value = map;
  203. return model;
  204. }
  205. });
  206. return {
  207. 'FormModel': FormModel,
  208. 'InputModel': InputModel,
  209. 'MapInputModel': MapInputModel,
  210. 'Form': SqoopForm,
  211. 'Input': SqoopInput,
  212. 'MapInput': SqoopMapInput,
  213. 'MapProperties': map_properties
  214. };
  215. })(jQuery);