jobsub.ko.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /**
  17. * Design representation
  18. */
  19. var Design = (function($, ko, NodeFields) {
  20. var module = function(options) {
  21. var self = this;
  22. self.options = {};
  23. self.model = {};
  24. self.initialize(options);
  25. };
  26. // NodeFields is defined in oozie/static/js/workflow.node-fields.js.
  27. // It provides the more advanced field manipulation for the fields
  28. // that hava JSON representation.
  29. $.extend(module.prototype, NodeFields, {
  30. initialize: function(options) {
  31. var self = this;
  32. self.options = $.extend(self.options, options);
  33. self.model = options.model;
  34. self.model.errors = self.model.errors || {};
  35. for(var key in self.model) {
  36. switch(key) {
  37. case 'initialize':
  38. case 'toString':
  39. case 'errors':
  40. break;
  41. default:
  42. if (!(key in self.model.errors)) {
  43. self.model.errors[key] = [];
  44. }
  45. break;
  46. }
  47. }
  48. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  49. // MAPPING_OPTIONS comes from /oozie/static/js/workflow.models.js
  50. // We don't update the observed object using ko.mapping because
  51. // the plugin does not work with mixed objects.
  52. ko.mapping.fromJS(self.model, $.extend({
  53. is_shared: {
  54. create: function(options) {
  55. return ko.observable(($.type(options.data) == "string") ? value.toLowerCase() == "true" : new Boolean(options.data).valueOf());
  56. },
  57. update: function(options) {
  58. return ($.type(options.data) == "string") ? value.toLowerCase() == "true" : new Boolean(options.data);
  59. }
  60. },
  61. parameters: {
  62. // Will receive individual objects to subscribe.
  63. // Containing array is mapped automagically
  64. create: function(options) {
  65. var parent = options.parent;
  66. var subscribe = function(mapping) {
  67. mapping.name.subscribe(function(value) {
  68. parent.parameters.valueHasMutated();
  69. });
  70. mapping.value.subscribe(function(value) {
  71. parent.parameters.valueHasMutated();
  72. });
  73. };
  74. return map_params(options, subscribe);
  75. },
  76. update: function(options) {
  77. var parent = options.parent;
  78. var subscribe = function(mapping) {
  79. mapping.name.subscribe(function(value) {
  80. parent.parameters.valueHasMutated();
  81. });
  82. mapping.value.subscribe(function(value) {
  83. parent.parameters.valueHasMutated();
  84. });
  85. };
  86. return map_params(options, subscribe);
  87. }
  88. }
  89. }, MAPPING_OPTIONS), self);
  90. // hack on '<key>ErrorClass' and '<key>Condition'.
  91. $.each(self.__ko_mapping__, function(key, enabled) {
  92. if (ko.isObservable(self[key])) {
  93. self[key+'_condition'] = ko.computed(function() {
  94. return self[key]().length > 0;
  95. });
  96. self[key+'_error_class'] = ko.computed(function() {
  97. return ( self.errors[key]().length > 0 ) ? 'control-group error' : 'control-group';
  98. });
  99. }
  100. });
  101. if (!('is_dirty' in self)) {
  102. self.is_dirty = ko.observable(true);
  103. }
  104. if (!('new' in self)) {
  105. self.new = ko.computed(function() {
  106. return !self.id();
  107. });
  108. }
  109. if (!('editable' in self)) {
  110. self.editable = ko.observable(false);
  111. }
  112. $(document).trigger('initialize.design', [options, self]);
  113. },
  114. request: function(url, options) {
  115. var self = this;
  116. var request = $.extend({
  117. url: url,
  118. dataType: 'json',
  119. type: 'GET',
  120. success: $.noop,
  121. error: $.noop
  122. }, options || {});
  123. $.ajax(request);
  124. },
  125. load: function(options) {
  126. var self = this;
  127. var options = $.extend({
  128. success: function(data) {
  129. self.is_dirty(false);
  130. $(document).trigger('load.design', [options, data]);
  131. }
  132. }, options);
  133. this.request('/jobsub/designs/' + self.id(), options);
  134. },
  135. save: function(options) {
  136. // First try to save, then update error list if fail.
  137. // Response should be json object. IE: {data: {errors: {files: ['example', ...], ... }}}
  138. var self = this;
  139. var model_dict = {};
  140. $.each(ko.mapping.toJS(self), function(key, value) {
  141. if (key != 'errors') {
  142. model_dict[key] = ko.utils.unwrapObservable(value);
  143. }
  144. });
  145. var data = normalize_model_fields($.parseJSON(JSON.stringify(model_dict)));
  146. var options = $.extend({
  147. type: 'POST',
  148. data: data,
  149. error: function(xhr) {
  150. var response = $.parseJSON(xhr.responseText);
  151. if (response) {
  152. var model = ko.mapping.toJS(self);
  153. $.extend(model.errors, response.data.errors);
  154. ko.mapping.fromJS(model, self);
  155. $(document).trigger('error.design', [options, data]);
  156. }
  157. },
  158. success: function(data) {
  159. $(document).trigger('save.design', [options, data]);
  160. }
  161. }, options);
  162. self.request((self.new()) ? '/jobsub/designs/'+self.node_type()+'/new' : '/jobsub/designs/'+self.id()+'/save', options);
  163. },
  164. clone: function(options) {
  165. var self = this;
  166. var options = $.extend({
  167. type: 'POST',
  168. success: function(data) {
  169. $(document).trigger('clone.design', [options, data]);
  170. }
  171. }, options);
  172. this.request('/jobsub/designs/' + self.id() + '/clone', options);
  173. },
  174. delete: function(options) {
  175. var self = this;
  176. var options = $.extend({
  177. type: 'POST',
  178. success: function(data) {
  179. $(document).trigger('delete.design', [options, data]);
  180. }
  181. }, options);
  182. this.request('/jobsub/designs/' + self.id() + '/delete', options);
  183. },
  184. // More node field methods
  185. addParameter: function(data) {
  186. var self = this;
  187. var prop = { name: ko.observable(""), value: ko.observable("") };
  188. prop.name.subscribe(function(value) {
  189. self.parameters.valueHasMutated();
  190. });
  191. prop.value.subscribe(function(value) {
  192. self.parameters.valueHasMutated();
  193. });
  194. self.parameters.push(prop);
  195. $(document).trigger('add.parameter.workflow', [data]);
  196. },
  197. removeParameter: function(data) {
  198. var self = this;
  199. self.parameters.remove(data);
  200. $(document).trigger('remove.parameter.workflow', [data]);
  201. }
  202. });
  203. return module;
  204. })($, ko, NodeFields);
  205. /**
  206. * List of designs
  207. */
  208. var Designs = (function($, ko, NodeModelChooser) {
  209. var module = function(options) {
  210. var self = this;
  211. self.options = options || {
  212. models: []
  213. };
  214. self.temporary = ko.observable();
  215. self.designs = ko.observableArray([]);
  216. self.selectedDesignObjects = ko.computed(function() {
  217. var selected = [];
  218. $.each(self.designs(), function(index, designObject) {
  219. if (designObject.selected()) {
  220. selected.push(designObject);
  221. }
  222. });
  223. return selected;
  224. });
  225. self.selectedDesignObject = ko.computed(function () {
  226. return self.selectedDesignObjects()[0];
  227. });
  228. self.selectedDesign = ko.computed(function() {
  229. if (self.selectedDesignObject()) {
  230. return self.selectedDesignObject().design();
  231. } else {
  232. return null;
  233. }
  234. });
  235. self.selectedIndex = ko.computed(function() {
  236. var selected = -1;
  237. $.each(self.designs(), function(index, designObject) {
  238. if (selected == -1 && designObject.selected()) {
  239. selected = index;
  240. }
  241. });
  242. return selected;
  243. });
  244. self.allSelected = ko.computed(function() {
  245. return self.selectedDesignObjects().length == self.designs().length;
  246. });
  247. self.initialize(options);
  248. };
  249. $.extend(module.prototype, {
  250. initialize: function(options) {
  251. var self = this;
  252. self.options = $.extend(self.options, options);
  253. self.designs.removeAll();
  254. self.createDesigns(self.options.models);
  255. self.temporary({
  256. design: ko.observable(null),
  257. selected: ko.observable(false),
  258. template: ko.observable(null)
  259. })
  260. self.deselectAll();
  261. $(document).trigger('initialize.designs', [options, self]);
  262. },
  263. load: function(options) {
  264. // Fetch designs from backend.
  265. var self = this;
  266. var request = $.extend({
  267. url: '/jobsub/designs',
  268. dataType: 'json',
  269. type: 'GET',
  270. success: function(data) {
  271. $(document).trigger('load.designs', [options, data]);
  272. self.initialize({models: data});
  273. },
  274. error: $.noop
  275. }, options || {});
  276. $.ajax(request);
  277. },
  278. ensureListFields: function(model) {
  279. if (!('name' in model)) {
  280. model.name = '';
  281. }
  282. if (!('description' in model)) {
  283. model.description = '';
  284. }
  285. if (!('owner' in model)) {
  286. model.owner = '';
  287. }
  288. if (!('last_modified' in model)) {
  289. model.last_modified = 0;
  290. }
  291. return model;
  292. },
  293. createDesign: function(model) {
  294. var self = this;
  295. var NodeModel = NodeModelChooser(model.node_type);
  296. var node_model = new NodeModel(self.ensureListFields(model));
  297. var design = new Design({model: node_model});
  298. return design;
  299. },
  300. createDesigns: function(models) {
  301. var self = this;
  302. $.each(models, function(index, model) {
  303. self.designs.push({
  304. design: ko.observable(self.createDesign(model)),
  305. selected: ko.observable(false),
  306. template: ko.observable(model.node_type)
  307. });
  308. });
  309. },
  310. toggleSelect: function(index) {
  311. var self = this;
  312. self.designs()[index].selected(!self.designs()[index].selected());
  313. },
  314. select: function(index) {
  315. var self = this;
  316. self.designs()[index].selected(true);
  317. },
  318. toggleSelectAll: function() {
  319. var self = this;
  320. if (self.allSelected()) {
  321. self.deselectAll();
  322. } else {
  323. self.selectAll();
  324. }
  325. },
  326. selectAll: function() {
  327. var self = this;
  328. $.each(self.designs(), function(index, value) {
  329. value.selected(true);
  330. });
  331. },
  332. deselectAll: function() {
  333. var self = this;
  334. $.each(self.designs(), function(index, value) {
  335. value.selected(false);
  336. });
  337. },
  338. //// Design delegation
  339. newDesign: function(node_type) {
  340. var self = this;
  341. var design = self.createDesign({
  342. id: null,
  343. node_type: node_type,
  344. is_shared: true,
  345. parameters: '[{"name":"oozie.use.system.libpath","value":"true"}]'
  346. });
  347. // Reversing the order of the next two statements may cause KO to break.
  348. self.temporary().template(node_type);
  349. self.temporary().design(design);
  350. // Do not do any thing with any other design.
  351. self.deselectAll();
  352. $(document).trigger('new.design', [design]);
  353. },
  354. saveDesign: function(data, event) {
  355. var self = this;
  356. self.temporary().design().save();
  357. },
  358. cloneDesigns: function() {
  359. var self = this;
  360. $.each(self.selectedDesignObjects(), function(index, designObject) {
  361. designObject.design().clone();
  362. });
  363. },
  364. deleteDesigns: function() {
  365. var self = this;
  366. $.each(self.selectedDesignObjects(), function(index, designObject) {
  367. designObject.design().delete();
  368. });
  369. },
  370. editDesign: function(index) {
  371. var self = this;
  372. if (self.selectedDesignObject()) {
  373. var design = self.selectedDesignObject().design();
  374. if (design.is_dirty()) {
  375. $(document).one('load.design', function(e, options, data) {
  376. design.initialize({model: data});
  377. self.temporary().design(design);
  378. self.temporary().template(self.selectedDesignObject().template());
  379. $(document).trigger('edit.design', [design]);
  380. });
  381. design.load();
  382. } else {
  383. self.temporary().design(design);
  384. self.temporary().template(self.selectedDesignObject().template());
  385. $(document).trigger('edit.design', [design]);
  386. }
  387. }
  388. },
  389. closeDesign: function() {
  390. var self = this;
  391. self.temporary().design(null);
  392. self.temporary().template(null);
  393. }
  394. });
  395. return module;
  396. })($, ko, nodeModelChooser);
  397. var designs = new Designs({models: []});