workflow.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  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. // adding missing .filter for IE8
  17. if (!('filter' in Array.prototype)) {
  18. Array.prototype.filter= function(filter, that /*opt*/) {
  19. var other= [], v;
  20. for (var i=0, n= this.length; i<n; i++)
  21. if (i in this && filter.call(that, v= this[i], i, this))
  22. other.push(v);
  23. return other;
  24. };
  25. }
  26. // Since knockout maps arrays without calling "update" nor "create"
  27. // Provide a JSON string that will be parsed in custom 'create' and 'update' functions.
  28. var normalize_model_fields = (function() {
  29. var FIELDS = ['parameters', 'job_properties', 'files', 'archives', 'prepares', 'params',
  30. 'deletes', 'mkdirs', 'moves', 'chmods', 'touchzs'];
  31. function fn(node_model) {
  32. $.each(FIELDS, function(index, field) {
  33. if (field in node_model && $.isArray(node_model[field])) {
  34. node_model[field] = JSON.stringify(node_model[field]);
  35. }
  36. });
  37. }
  38. return fn;
  39. })();
  40. /**
  41. * Registry of models
  42. * - Each model should have an ID attribute.
  43. */
  44. var RegistryModule = function($) {
  45. var module = function() {
  46. var self = this;
  47. self.nodes = {};
  48. module.prototype.initialize.apply(self, arguments);
  49. return self;
  50. };
  51. $.extend(module.prototype, {
  52. // Normal stuff
  53. initialize: function() {},
  54. toString: function() {
  55. var self = this;
  56. var s = $.map(self.nodes, function(node) {
  57. return node.id();
  58. }).join();
  59. return s;
  60. },
  61. add: function(id, node) {
  62. var self = this;
  63. $(self).trigger('registry:add');
  64. self.nodes[String(id)] = node;
  65. },
  66. remove: function(id) {
  67. var self = this;
  68. $(self).trigger('registry:remove');
  69. delete self.nodes[String(id)];
  70. },
  71. get: function(id) {
  72. var self = this;
  73. return self.nodes[id];
  74. },
  75. clear: function() {
  76. var self = this;
  77. delete self.nodes;
  78. self.nodes = {};
  79. }
  80. });
  81. return module;
  82. };
  83. var Registry = RegistryModule($);
  84. /**
  85. * Modal Control
  86. * Displays a single node in a modal window of your choosing.
  87. * Make sure to update the 'context' member and 'template' members.
  88. * IMPORTANT: Use 'setTemplate', then 'show'.
  89. */
  90. var ModalModule = function($, ko) {
  91. var module = function(modal, template) {
  92. var self = this;
  93. self.el = self.modal = $(modal);
  94. self.context = ko.observable();
  95. self.template = ko.observable(template || '');
  96. self.bound = false;
  97. };
  98. module.prototype.show = function(context) {
  99. var self = this;
  100. if (context) {
  101. self.context(context);
  102. }
  103. ko.applyBindings(self, self.modal[0]);
  104. self.modal.modal('show');
  105. };
  106. module.prototype.hide = function(context) {
  107. var self = this;
  108. self.el.modal('hide');
  109. };
  110. module.prototype.setTemplate = function(template) {
  111. var self = this;
  112. ko.cleanNode(self.modal[0]);
  113. self.template( template );
  114. };
  115. module.prototype.recenter = function(offset_x, offset_y) {
  116. var self = this;
  117. var top = ( ($(window).height() - self.modal.outerHeight(false)) / 2 );
  118. var left = ( ($(window).width() - self.modal.outerWidth(false)) / 2 );
  119. if (top < 0) {
  120. top = 0;
  121. }
  122. if (left < 0) {
  123. left = 0;
  124. }
  125. top += offset_y || 0;
  126. left += offset_x || 0;
  127. self.modal.css({top: top +'px', left: left+'px'});
  128. };
  129. module.prototype.addDecorations = function () {
  130. $(".popover").remove();
  131. $("input[name='job_xml']:not(.pathChooser)").addClass("pathChooser").after(getFileBrowseButton($("input[name='job_xml']:not(.pathChooser)")));
  132. $("input[name='jar_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='jar_path']")));
  133. $("input[name='script_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='script_path']")));
  134. $("input[name='command']").addClass("pathChooser").after(getFileBrowseButton($("input[name='command']")));
  135. $("*[rel=popover]").popover({
  136. placement:'left',
  137. trigger:'hover'
  138. });
  139. }
  140. return module;
  141. };
  142. var Modal = ModalModule($, ko);
  143. /**
  144. * ID Generator
  145. * Generate a new ID starting from 1.
  146. * - Accepts a prefix that will be prepended like so: <prefix>:<id number>
  147. */
  148. var IdGeneratorModule = function($) {
  149. return function(options) {
  150. var self = this;
  151. $.extend(self, options);
  152. self.counter = 1;
  153. self.nextId = function() {
  154. return ((self.prefix) ? self.prefix + ':' : '') + self.counter++;
  155. };
  156. };
  157. };
  158. var IdGenerator = IdGeneratorModule($);
  159. var ModelModule = function($) {
  160. var module = function(attrs) {
  161. var self = this;
  162. $.extend(self, attrs);
  163. module.prototype.initialize.apply(self, arguments);
  164. return self;
  165. };
  166. $.extend(module.prototype, {
  167. // Normal stuff
  168. initialize: function(){},
  169. toString: function() {
  170. var self = this;
  171. return JSON.stringify(self, null, '\t');
  172. }
  173. });
  174. return module;
  175. };
  176. var WorkflowModel = ModelModule($);
  177. $.extend(WorkflowModel.prototype, {
  178. id: 0,
  179. name: '',
  180. description: '',
  181. start: 0,
  182. end: 0,
  183. schema_version: 0.4,
  184. deployment_dir: '',
  185. is_shared: true,
  186. parameters: '[]',
  187. job_xml: ''
  188. });
  189. var NodeModel = ModelModule($);
  190. $.extend(NodeModel.prototype, {
  191. id: 0,
  192. name: '',
  193. description: '',
  194. node_type: '',
  195. workflow: 0,
  196. child_links: []
  197. });
  198. var ForkModel = ModelModule($);
  199. $.extend(ForkModel.prototype, {
  200. id: 0,
  201. name: '',
  202. description: '',
  203. node_type: 'fork',
  204. workflow: 0,
  205. child_links: []
  206. });
  207. var DecisionModel = ModelModule($);
  208. $.extend(DecisionModel.prototype, {
  209. id: 0,
  210. name: '',
  211. description: '',
  212. node_type: 'decision',
  213. workflow: 0,
  214. child_links: []
  215. });
  216. var DistCPModel = ModelModule($);
  217. $.extend(DistCPModel.prototype, {
  218. id: 0,
  219. name: '',
  220. description: '',
  221. node_type: 'distcp',
  222. workflow: 0,
  223. job_properties: '[]',
  224. prepares: '[]',
  225. job_xml: '',
  226. params: '[]',
  227. child_links: []
  228. });
  229. var MapReduceModel = ModelModule($);
  230. $.extend(MapReduceModel.prototype, {
  231. id: 0,
  232. name: '',
  233. description: '',
  234. node_type: 'mapreduce',
  235. workflow: 0,
  236. files: '[]',
  237. archives: '[]',
  238. job_properties: '[]',
  239. jar_path: '',
  240. prepares: '[]',
  241. job_xml: '',
  242. child_links: []
  243. });
  244. var StreamingModel = ModelModule($);
  245. $.extend(StreamingModel.prototype, {
  246. id: 0,
  247. name: '',
  248. description: '',
  249. node_type: 'streaming',
  250. workflow: 0,
  251. files: '[]',
  252. archives: '[]',
  253. job_properties: '[]',
  254. mapper: '',
  255. reducer: '',
  256. child_links: []
  257. });
  258. var JavaModel = ModelModule($);
  259. $.extend(JavaModel.prototype, {
  260. id: 0,
  261. name: '',
  262. description: '',
  263. node_type: 'java',
  264. workflow: 0,
  265. files: '[]',
  266. archives: '[]',
  267. job_properties: '[]',
  268. jar_path: '',
  269. prepares: '[]',
  270. job_xml: '',
  271. main_class: '',
  272. args: '',
  273. java_opts: '',
  274. child_links: []
  275. });
  276. var PigModel = ModelModule($);
  277. $.extend(PigModel.prototype, {
  278. id: 0,
  279. name: '',
  280. description: '',
  281. node_type: 'pig',
  282. workflow: 0,
  283. files: '[]',
  284. archives: '[]',
  285. job_properties: '[]',
  286. prepares: '[]',
  287. job_xml: '',
  288. params: '[]',
  289. script_path: '',
  290. child_links: []
  291. });
  292. var HiveModel = ModelModule($);
  293. $.extend(HiveModel.prototype, {
  294. id: 0,
  295. name: '',
  296. description: '',
  297. node_type: 'hive',
  298. workflow: 0,
  299. files: '[]',
  300. archives: '[]',
  301. job_properties: '[]',
  302. prepares: '[]',
  303. job_xml: '',
  304. params: '[]',
  305. script_path: '',
  306. child_links: []
  307. });
  308. var SqoopModel = ModelModule($);
  309. $.extend(SqoopModel.prototype, {
  310. id: 0,
  311. name: '',
  312. description: '',
  313. node_type: 'sqoop',
  314. workflow: 0,
  315. files: '[]',
  316. archives: '[]',
  317. job_properties: '[]',
  318. prepares: '[]',
  319. job_xml: '',
  320. params: '[]',
  321. script_path: '',
  322. child_links: []
  323. });
  324. var ShellModel = ModelModule($);
  325. $.extend(ShellModel.prototype, {
  326. id: 0,
  327. name: '',
  328. description: '',
  329. node_type: 'shell',
  330. workflow: 0,
  331. files: '[]',
  332. archives: '[]',
  333. job_properties: '[]',
  334. prepares: '[]',
  335. job_xml: '',
  336. params: '[]',
  337. command: '',
  338. capture_output: false,
  339. child_links: []
  340. });
  341. var SshModel = ModelModule($);
  342. $.extend(SshModel.prototype, {
  343. id: 0,
  344. name: '',
  345. description: '',
  346. node_type: 'ssh',
  347. workflow: 0,
  348. user: '',
  349. host: '',
  350. params: '[]',
  351. command: '',
  352. capture_output: false,
  353. child_links: []
  354. });
  355. var FsModel = ModelModule($);
  356. $.extend(FsModel.prototype, {
  357. id: 0,
  358. name: '',
  359. description: '',
  360. node_type: 'fs',
  361. workflow: 0,
  362. deletes: '[]',
  363. mkdirs: '[]',
  364. moves: '[]',
  365. chmods: '[]',
  366. touchzs: '[]',
  367. child_links: []
  368. });
  369. var EmailModel = ModelModule($);
  370. $.extend(EmailModel.prototype, {
  371. id: 0,
  372. name: '',
  373. description: '',
  374. node_type: 'email',
  375. workflow: 0,
  376. to: '',
  377. cc: '',
  378. subject: '',
  379. body: '',
  380. child_links: []
  381. });
  382. var SubWorkflowModel = ModelModule($);
  383. $.extend(SubWorkflowModel.prototype, {
  384. id: 0,
  385. name: '',
  386. description: '',
  387. node_type: 'subworkflow',
  388. workflow: 0,
  389. sub_workflow: 0,
  390. propagate_configuration: true,
  391. job_properties: '[]',
  392. child_links: []
  393. });
  394. var GenericModel = ModelModule($);
  395. $.extend(GenericModel.prototype, {
  396. id: 0,
  397. name: '',
  398. description: '',
  399. node_type: 'generic',
  400. workflow: 0,
  401. xml: '',
  402. child_links: []
  403. });
  404. function nodeModelChooser(node_type) {
  405. switch(node_type) {
  406. case 'mapreduce':
  407. return MapReduceModel;
  408. case 'streaming':
  409. return StreamingModel;
  410. case 'java':
  411. return JavaModel;
  412. case 'pig':
  413. return PigModel;
  414. case 'hive':
  415. return HiveModel;
  416. case 'sqoop':
  417. return SqoopModel;
  418. case 'shell':
  419. return ShellModel;
  420. case 'ssh':
  421. return SshModel;
  422. case 'distcp':
  423. return DistCPModel;
  424. case 'fs':
  425. return FsModel;
  426. case 'email':
  427. return EmailModel;
  428. case 'subworkflow':
  429. return SubWorkflowModel;
  430. case 'generic':
  431. return GenericModel;
  432. case 'fork':
  433. return ForkModel;
  434. case 'decision':
  435. return DecisionModel;
  436. default:
  437. return NodeModel;
  438. }
  439. }
  440. var IdGeneratorTable = {
  441. mapreduce: new IdGenerator({prefix: 'mapreduce'}),
  442. streaming: new IdGenerator({prefix: 'streaming'}),
  443. java: new IdGenerator({prefix: 'java'}),
  444. pig: new IdGenerator({prefix: 'pig'}),
  445. hive: new IdGenerator({prefix: 'hive'}),
  446. sqoop: new IdGenerator({prefix: 'sqoop'}),
  447. shell: new IdGenerator({prefix: 'shell'}),
  448. ssh: new IdGenerator({prefix: 'ssh'}),
  449. distcp: new IdGenerator({prefix: 'distcp'}),
  450. fs: new IdGenerator({prefix: 'fs'}),
  451. email: new IdGenerator({prefix: 'email'}),
  452. subworkflow: new IdGenerator({prefix: 'subworkflow'}),
  453. generic: new IdGenerator({prefix: 'generic'}),
  454. fork: new IdGenerator({prefix: 'fork'}),
  455. decision: new IdGenerator({prefix: 'decision'}),
  456. join: new IdGenerator({prefix: 'join'}),
  457. kill: new IdGenerator({prefix: 'kill'})
  458. };
  459. var NodeModule = function($, IdGeneratorTable) {
  460. var META_LINKS = ['related', 'default', 'error'];
  461. var linkTypeChooser = function(parent, child) {
  462. if (child.node_type() == 'kill') {
  463. return 'error';
  464. }
  465. switch(parent.node_type()) {
  466. case 'start':
  467. return (child.node_type() == 'end') ? 'related' : 'to';
  468. case 'fork':
  469. return (child.node_type() == 'join') ? 'related' : 'start';
  470. case 'decision':
  471. return 'start';
  472. case 'join':
  473. return 'to';
  474. default:
  475. return 'ok';
  476. };
  477. };
  478. var module = function(workflow, model, registry) {
  479. var self = this;
  480. self.map(model);
  481. self.links = ko.computed(function() {
  482. var links = self.child_links().filter(function(element, index, arr) {
  483. return $.inArray(element.name(), META_LINKS) == -1;
  484. });
  485. return links;
  486. });
  487. self.meta_links = ko.computed(function() {
  488. var links = self.child_links().filter(function(element, index, arr) {
  489. return $.inArray(element.name(), META_LINKS) != -1;
  490. });
  491. return links;
  492. });
  493. self._workflow = workflow;
  494. self.registry = registry;
  495. self.children = ko.observableArray([]);
  496. self.model = model;
  497. var errors = {};
  498. for(var key in model) {
  499. switch(key) {
  500. case 'child_links':
  501. case 'node_ptr':
  502. case 'initialize':
  503. case 'toString':
  504. break;
  505. default:
  506. errors[key] = [];
  507. break;
  508. }
  509. }
  510. self.errors = ko.mapping.fromJS(errors);
  511. self.edit_template = model.node_type + 'EditTemplate';
  512. switch(model.node_type) {
  513. case 'start':
  514. self.view_template = ko.observable('startTemplate');
  515. break;
  516. case 'kill':
  517. case 'end':
  518. self.view_template = ko.observable('emptyTemplate');
  519. break;
  520. case 'fork':
  521. self.view_template = ko.observable('forkTemplate');
  522. break;
  523. case 'decision':
  524. self.view_template = ko.observable('decisionTemplate');
  525. break;
  526. case 'join':
  527. self.view_template = ko.observable('joinTemplate');
  528. break;
  529. default:
  530. self.view_template = ko.observable('nodeTemplate');
  531. break;
  532. }
  533. // Data manipulation
  534. if ('job_properties' in model) {
  535. self.addProp = function() {
  536. var prop = { name: ko.observable(""), value: ko.observable("") };
  537. prop.name.subscribe(function(value) {
  538. self.job_properties.valueHasMutated();
  539. });
  540. prop.value.subscribe(function(value) {
  541. self.job_properties.valueHasMutated();
  542. });
  543. self.job_properties.push(prop);
  544. // $(".propKey:last").each(addAutoComplete);
  545. };
  546. self.removeProp = function(val) {
  547. self.job_properties.remove(val);
  548. };
  549. }
  550. if ('files' in model) {
  551. self.addFile = function() {
  552. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  553. prop.name.subscribe(function(value) {
  554. self.files.valueHasMutated();
  555. });
  556. prop.dummy.subscribe(function(value) {
  557. self.files.valueHasMutated();
  558. });
  559. self.files.push(prop);
  560. };
  561. self.removeFile = function(val) {
  562. self.files.remove(val);
  563. };
  564. }
  565. if ('archives' in model) {
  566. self.addArchive = function() {
  567. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  568. prop.name.subscribe(function(value) {
  569. self.archives.valueHasMutated();
  570. });
  571. prop.dummy.subscribe(function(value) {
  572. self.archives.valueHasMutated();
  573. });
  574. self.archives.push(prop);
  575. };
  576. self.removeArchive = function(val) {
  577. self.archives.remove(val);
  578. };
  579. }
  580. if ('params' in model) {
  581. self.addParam = function() {
  582. var prop = { value: ko.observable(""), type: ko.observable("param") };
  583. prop.value.subscribe(function(value) {
  584. self.params.valueHasMutated();
  585. });
  586. prop.type.subscribe(function(value) {
  587. self.params.valueHasMutated();
  588. });
  589. self.params.push(prop);
  590. };
  591. self.addArgument = function() {
  592. var prop = { value: ko.observable(""), type: ko.observable("argument") };
  593. prop.value.subscribe(function(value) {
  594. self.params.valueHasMutated();
  595. });
  596. prop.type.subscribe(function(value) {
  597. self.params.valueHasMutated();
  598. });
  599. self.params.push(prop);
  600. };
  601. self.addArg = function() {
  602. var prop = { value: ko.observable(""), type: ko.observable("arg") };
  603. prop.value.subscribe(function(value) {
  604. self.params.valueHasMutated();
  605. });
  606. prop.type.subscribe(function(value) {
  607. self.params.valueHasMutated();
  608. });
  609. self.params.push(prop);
  610. };
  611. self.addEnvVar = function() {
  612. var prop = { value: ko.observable(""), type: ko.observable("env-var") };
  613. prop.value.subscribe(function(value) {
  614. self.params.valueHasMutated();
  615. });
  616. prop.type.subscribe(function(value) {
  617. self.params.valueHasMutated();
  618. });
  619. self.params.push(prop);
  620. };
  621. self.removeParam = function(val) {
  622. self.params.remove(val);
  623. };
  624. }
  625. if ('prepares' in model) {
  626. self.addPrepareDelete = function() {
  627. var prop = { value: ko.observable(""), type: ko.observable("delete") };
  628. prop.value.subscribe(function(value) {
  629. self.prepares.valueHasMutated();
  630. });
  631. prop.type.subscribe(function(value) {
  632. self.prepares.valueHasMutated();
  633. });
  634. self.prepares.push(prop);
  635. };
  636. self.addPrepareMkdir = function() {
  637. var prop = { value: ko.observable(""), type: ko.observable("mkdir") };
  638. prop.value.subscribe(function(value) {
  639. self.prepares.valueHasMutated();
  640. });
  641. prop.type.subscribe(function(value) {
  642. self.prepares.valueHasMutated();
  643. });
  644. self.prepares.push(prop);
  645. };
  646. self.removePrepare = function(val) {
  647. self.prepares.remove(val);
  648. };
  649. }
  650. if ('deletes' in model) {
  651. self.addDelete = function() {
  652. var prop = { name: ko.observable("") };
  653. prop.name.subscribe(function(value) {
  654. self.deletes.valueHasMutated();
  655. });
  656. self.deletes.push(prop);
  657. };
  658. self.removeDelete = function(val) {
  659. self.deletes.remove(val);
  660. };
  661. }
  662. if ('mkdirs' in model) {
  663. self.addMkdir = function() {
  664. var prop = { name: ko.observable("") };
  665. prop.name.subscribe(function(value) {
  666. self.mkdirs.valueHasMutated();
  667. });
  668. self.mkdirs.push(prop);
  669. };
  670. self.removeMkdir = function(val) {
  671. self.mkdirs.remove(val);
  672. };
  673. }
  674. if ('moves' in model) {
  675. self.addMove = function() {
  676. var prop = { source: ko.observable(""), destination: ko.observable("") };
  677. prop.source.subscribe(function(value) {
  678. self.moves.valueHasMutated();
  679. });
  680. prop.destination.subscribe(function(value) {
  681. self.moves.valueHasMutated();
  682. });
  683. self.moves.push(prop);
  684. };
  685. self.removeMove = function(val) {
  686. self.moves.remove(val);
  687. };
  688. }
  689. if ('chmods' in model) {
  690. self.addChmod = function() {
  691. var prop = { path: ko.observable(""), permissions: ko.observable(""), recursive: ko.observable("") };
  692. prop.path.subscribe(function(value) {
  693. self.chmods.valueHasMutated();
  694. });
  695. prop.permissions.subscribe(function(value) {
  696. self.chmods.valueHasMutated();
  697. });
  698. prop.recursive.subscribe(function(value) {
  699. self.chmods.valueHasMutated();
  700. });
  701. self.chmods.push(prop);
  702. };
  703. self.removeChmod = function(val) {
  704. self.chmods.remove(val);
  705. };
  706. }
  707. if ('touchzs' in model) {
  708. self.addTouchz = function() {
  709. var prop = { name: ko.observable("") };
  710. prop.name.subscribe(function(value) {
  711. self.touchzs.valueHasMutated();
  712. });
  713. self.touchzs.push(prop);
  714. };
  715. self.removeTouchz = function(val) {
  716. self.touchzs.remove(val);
  717. };
  718. }
  719. self.initialize.apply(self, arguments);
  720. return self;
  721. };
  722. $.extend(module.prototype, {
  723. // Data.
  724. children: null,
  725. model: null,
  726. // Normal stuff
  727. /**
  728. * Called when creating a new node
  729. */
  730. initialize: function(workflow, model, registry) {},
  731. toString: function() {
  732. return '';
  733. },
  734. /**
  735. * Fetches registry
  736. */
  737. getRegistry: function() {
  738. return registry;
  739. },
  740. /**
  741. * Maps a model to self
  742. * Called when creating a new node before any thing else
  743. */
  744. map: function(model) {
  745. var self = this;
  746. var map_params = function(options, subscribe) {
  747. options.data = ($.type(options.data) == "string") ? $.parseJSON(options.data) : options.data;
  748. if ($.isArray(options.data)) {
  749. var mapping = ko.mapping.fromJS(options.data);
  750. $.each(mapping(), function(index, value) {
  751. subscribe(value);
  752. });
  753. return mapping;
  754. } else {
  755. var mapping = ko.mapping.fromJS(options.data, {});
  756. subscribe(mapping);
  757. return mapping;
  758. }
  759. };
  760. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  761. var mapping = ko.mapping.fromJS(model, {
  762. ignore: ['initialize', 'toString'],
  763. job_properties: {
  764. create: function(options) {
  765. var parent = options.parent;
  766. var subscribe = function(mapping) {
  767. mapping.name.subscribe(function(value) {
  768. parent.job_properties.valueHasMutated();
  769. });
  770. mapping.value.subscribe(function(value) {
  771. parent.job_properties.valueHasMutated();
  772. });
  773. };
  774. return map_params(options, subscribe);
  775. },
  776. update: function(options) {
  777. var parent = options.parent;
  778. var subscribe = function(mapping) {
  779. mapping.name.subscribe(function(value) {
  780. parent.job_properties.valueHasMutated();
  781. });
  782. mapping.value.subscribe(function(value) {
  783. parent.job_properties.valueHasMutated();
  784. });
  785. };
  786. return map_params(options, subscribe);
  787. }
  788. },
  789. files: {
  790. create: function(options) {
  791. return map_params(options, function() {});
  792. },
  793. update: function(options) {
  794. return map_params(options, function() {});
  795. }
  796. },
  797. archives: {
  798. create: function(options) {
  799. var parent = options.parent;
  800. var subscribe = function(mapping) {
  801. mapping.name.subscribe(function(value) {
  802. parent.archives.valueHasMutated();
  803. });
  804. mapping.dummy.subscribe(function(value) {
  805. parent.archives.valueHasMutated();
  806. });
  807. };
  808. return map_params(options, subscribe);
  809. },
  810. update: function(options) {
  811. var parent = options.parent;
  812. var subscribe = function(mapping) {
  813. mapping.name.subscribe(function(value) {
  814. parent.archives.valueHasMutated();
  815. });
  816. mapping.dummy.subscribe(function(value) {
  817. parent.archives.valueHasMutated();
  818. });
  819. };
  820. return map_params(options, subscribe);
  821. }
  822. },
  823. params: {
  824. create: function(options) {
  825. var parent = options.parent;
  826. var subscribe = function(mapping) {
  827. mapping.value.subscribe(function(value) {
  828. parent.params.valueHasMutated();
  829. });
  830. mapping.type.subscribe(function(value) {
  831. parent.params.valueHasMutated();
  832. });
  833. };
  834. return map_params(options, subscribe);
  835. },
  836. update: function(options) {
  837. var parent = options.parent;
  838. var subscribe = function(mapping) {
  839. mapping.value.subscribe(function(value) {
  840. parent.params.valueHasMutated();
  841. });
  842. mapping.type.subscribe(function(value) {
  843. parent.params.valueHasMutated();
  844. });
  845. };
  846. return map_params(options, subscribe);
  847. }
  848. },
  849. prepares: {
  850. create: function(options) {
  851. var parent = options.parent;
  852. var subscribe = function(mapping) {
  853. mapping.value.subscribe(function(value) {
  854. parent.prepares.valueHasMutated();
  855. });
  856. mapping.type.subscribe(function(value) {
  857. parent.prepares.valueHasMutated();
  858. });
  859. };
  860. return map_params(options, subscribe);
  861. },
  862. update: function(options) {
  863. var parent = options.parent;
  864. var subscribe = function(mapping) {
  865. mapping.value.subscribe(function(value) {
  866. parent.prepares.valueHasMutated();
  867. });
  868. mapping.type.subscribe(function(value) {
  869. parent.prepares.valueHasMutated();
  870. });
  871. };
  872. return map_params(options, subscribe);
  873. }
  874. },
  875. deletes: {
  876. create: function(options) {
  877. return map_params(options, function() {});
  878. },
  879. update: function(options) {
  880. return map_params(options, function() {});
  881. }
  882. },
  883. mkdirs: {
  884. create: function(options) {
  885. return map_params(options, function() {});
  886. },
  887. update: function(options) {
  888. return map_params(options, function() {});
  889. }
  890. },
  891. moves: {
  892. create: function(options) {
  893. var parent = options.parent;
  894. var subscribe = function(mapping) {
  895. mapping.source.subscribe(function(value) {
  896. parent.moves.valueHasMutated();
  897. });
  898. mapping.destination.subscribe(function(value) {
  899. parent.moves.valueHasMutated();
  900. });
  901. };
  902. return map_params(options, subscribe);
  903. },
  904. update: function(options) {
  905. var parent = options.parent;
  906. var subscribe = function(mapping) {
  907. mapping.source.subscribe(function(value) {
  908. parent.moves.valueHasMutated();
  909. });
  910. mapping.destination.subscribe(function(value) {
  911. parent.moves.valueHasMutated();
  912. });
  913. };
  914. return map_params(options, subscribe);
  915. }
  916. },
  917. chmods: {
  918. create: function(options) {
  919. var parent = options.parent;
  920. var subscribe = function(mapping) {
  921. mapping.path.subscribe(function(value) {
  922. parent.chmods.valueHasMutated();
  923. });
  924. mapping.permissions.subscribe(function(value) {
  925. parent.chmods.valueHasMutated();
  926. });
  927. mapping.recursive.subscribe(function(value) {
  928. parent.chmods.valueHasMutated();
  929. });
  930. };
  931. return map_params(options, subscribe);
  932. },
  933. update: function(options) {
  934. var parent = options.parent;
  935. var subscribe = function(mapping) {
  936. mapping.path.subscribe(function(value) {
  937. parent.chmods.valueHasMutated();
  938. });
  939. mapping.permissions.subscribe(function(value) {
  940. parent.chmods.valueHasMutated();
  941. });
  942. mapping.recursive.subscribe(function(value) {
  943. parent.chmods.valueHasMutated();
  944. });
  945. };
  946. return map_params(options, subscribe);
  947. }
  948. },
  949. touchzs: {
  950. create: function(options) {
  951. return map_params(options, function() {});
  952. },
  953. update: function(options) {
  954. return map_params(options, function() {});
  955. }
  956. },
  957. });
  958. $.extend(self, mapping);
  959. $.each(mapping, function(key, value) {
  960. var key = key;
  961. if (ko.isObservable(self[key])) {
  962. self[key].subscribe(function(value) {
  963. model[key] = ko.mapping.toJS(value);
  964. });
  965. }
  966. });
  967. $.each(self.child_links(), function(index, link) {
  968. var $index = index;
  969. link.comment.subscribe(function(value) {
  970. self.model.child_links[$index].comment = value;
  971. });
  972. link.child.subscribe(function(value) {
  973. self.model.child_links[$index].child = value;
  974. });
  975. });
  976. },
  977. validate: function( ) {
  978. var self = this;
  979. var options = {};
  980. data = $.extend(true, {}, self.model);
  981. var success = false;
  982. var request = $.extend({
  983. url: '/oozie/workflows/' + self._workflow.id() + '/actions/' + self.node_type() + '/validate',
  984. type: 'POST',
  985. data: { node: JSON.stringify(data) },
  986. success: function(data) {
  987. ko.mapping.fromJS(data.data, self.errors);
  988. success = data.status == 0;
  989. },
  990. async: false,
  991. }, options);
  992. $.ajax(request);
  993. return success;
  994. },
  995. // Hierarchy manipulation.
  996. /**
  997. * Append node to self
  998. * Does not support multiple children.
  999. * Ensures single child.
  1000. * Ensures no cycles.
  1001. * 1. Finds all children and attaches them to node (cleans node first).
  1002. * 2. Remove all children from self.
  1003. * 3. Attach node to self.
  1004. */
  1005. append: function(node) {
  1006. var self = this;
  1007. // Not fork nor decision nor self
  1008. if ($.inArray(self.node_type(), ['fork', 'decision']) == -1 && node.id() != self.id()) {
  1009. node.removeAllChildren();
  1010. $.each(self.links(), function(index, link) {
  1011. node.addChild(registry.get(link.child()));
  1012. });
  1013. self.removeAllChildren();
  1014. self.addChild(node);
  1015. }
  1016. },
  1017. /**
  1018. * Find all parents of current node
  1019. */
  1020. findParents: function() {
  1021. var self = this;
  1022. var parents = [];
  1023. $.each(registry.nodes, function(id, node) {
  1024. $.each(node.links(), function(index, link) {
  1025. if (link.child() == self.id()) {
  1026. parents.push(node);
  1027. }
  1028. });
  1029. });
  1030. return parents;
  1031. },
  1032. /**
  1033. * Find all children of current node
  1034. */
  1035. findChildren: function() {
  1036. var self = this;
  1037. var children = [];
  1038. $.each(self.links(), function(index, link) {
  1039. children.push(registry.get(link.child()));
  1040. });
  1041. return children;
  1042. },
  1043. /**
  1044. * Detach current node from the graph
  1045. * 1. Takes children of self node, removes them from self node, and adds them to each parent of self node.
  1046. * 2. The self node is then removed from every parent.
  1047. * 3. Does not support multiple children since we do not automatically fork.
  1048. */
  1049. detach: function() {
  1050. var self = this;
  1051. $.each(self.findParents(), function(index, parent) {
  1052. $.each(self.links(), function(index, link) {
  1053. var node = registry.get(link.child());
  1054. parent.replaceChild(self, node);
  1055. });
  1056. });
  1057. $(self).trigger('detached');
  1058. self.removeAllChildren();
  1059. },
  1060. /**
  1061. * Add child
  1062. * Update child links for this node.
  1063. */
  1064. addChild: function(node) {
  1065. var self = this;
  1066. var link = {
  1067. parent: ko.observable(self.id()),
  1068. child: ko.observable(node.id()),
  1069. name: ko.observable(linkTypeChooser(self, node)),
  1070. comment: ko.observable('')
  1071. };
  1072. self.child_links.unshift(link);
  1073. },
  1074. /**
  1075. * Remove child node
  1076. * 1. Find child node link
  1077. * 2. Remove child node link
  1078. */
  1079. removeChild: function(node) {
  1080. var self = this;
  1081. var spliceIndex = -1;
  1082. $.each(self.child_links(), function(index, link) {
  1083. if (link.child() == node.id()) {
  1084. spliceIndex = index;
  1085. }
  1086. });
  1087. if (spliceIndex > -1) {
  1088. self.child_links.splice(spliceIndex, 1);
  1089. }
  1090. return spliceIndex != -1;
  1091. },
  1092. /**
  1093. * Remove all children
  1094. * Removes all children except for related, default, and error links
  1095. * Note: we hold on to related, default, and error links because
  1096. * we have to.
  1097. */
  1098. removeAllChildren: function() {
  1099. var self = this;
  1100. var keep_links = [];
  1101. $.each(self.child_links(), function(index, link) {
  1102. if ($.inArray(link.name(), META_LINKS) > -1) {
  1103. keep_links.push(link);
  1104. }
  1105. });
  1106. self.child_links.removeAll();
  1107. $.each(keep_links, function(index, link) {
  1108. self.child_links.push(link);
  1109. });
  1110. },
  1111. /**
  1112. * Replace child node with another node in the following way:
  1113. * 1. Find child index
  1114. * 2. Remove child index
  1115. * 3. Remove and remember every element after child
  1116. * 4. Add replacement node
  1117. * 5. Add every child that was remembered
  1118. */
  1119. replaceChild: function(child, replacement) {
  1120. var self = this;
  1121. var index = -1;
  1122. $.each(self.child_links(), function(i, link) {
  1123. if (link.child() == child.id()) {
  1124. index = i;
  1125. }
  1126. });
  1127. if (index > -1) {
  1128. self.child_links.splice(index, 1);
  1129. var links = self.child_links.splice(index);
  1130. var link = {
  1131. parent: ko.observable(self.id()),
  1132. child: ko.observable(replacement.id()),
  1133. name: ko.observable(linkTypeChooser(self, replacement)),
  1134. comment: ko.observable(''),
  1135. };
  1136. self.child_links.push(link);
  1137. $.each(links, function(index, link) {
  1138. self.child_links.push(link);
  1139. });
  1140. }
  1141. return index != -1;
  1142. },
  1143. isChild: function(node) {
  1144. var self = this;
  1145. var res = false;
  1146. $.each(self.links(), function(index, link) {
  1147. if (link.child() == node.id()) {
  1148. res = true;
  1149. }
  1150. });
  1151. return res;
  1152. },
  1153. erase: function() {
  1154. var self = this;
  1155. self.registry.remove(self.id());
  1156. }
  1157. });
  1158. return module;
  1159. };
  1160. var Node = NodeModule($, IdGeneratorTable);
  1161. var StartNode = NodeModule($, IdGeneratorTable);
  1162. $.extend(StartNode.prototype, Node.prototype, {
  1163. /**
  1164. * Same as addChild for nodes, except if we reach the end,
  1165. * we add the end node to our child_links in the form of a
  1166. * 'related' link and 'start' link.
  1167. * We should always have a start link in an empty workflow!
  1168. */
  1169. replaceChild: function(child, replacement) {
  1170. var self = this;
  1171. var index = -1;
  1172. $.each(self.child_links(), function(i, link) {
  1173. if (link.child() == child.id()) {
  1174. index = i;
  1175. }
  1176. });
  1177. if (index > -1) {
  1178. self.child_links.splice(index, 1);
  1179. var links = self.child_links.splice(index);
  1180. var link = {
  1181. parent: ko.observable(self.id()),
  1182. child: ko.observable(replacement.id()),
  1183. name: ko.observable('to'),
  1184. comment: ko.observable(''),
  1185. };
  1186. self.child_links.push(link);
  1187. $.each(links, function(index, link) {
  1188. self.child_links.push(link);
  1189. });
  1190. }
  1191. return index != -1;
  1192. },
  1193. });
  1194. var ForkNode = NodeModule($, IdGeneratorTable);
  1195. $.extend(ForkNode.prototype, Node.prototype, {
  1196. // Join nodes are connected through 'related' links
  1197. join: function() {
  1198. var self = this;
  1199. var join = null;
  1200. $.each(self.child_links(), function(index, link) {
  1201. if (link.name() == 'related') {
  1202. join = self.registry.get(link.child());
  1203. }
  1204. });
  1205. return join;
  1206. },
  1207. /**
  1208. * Append a node to the current fork
  1209. * Also adds join node to node.
  1210. * When adding the join node, append will remove all the children from the join!
  1211. * We need to make sure the join remembers its children.
  1212. * NOTE: Cannot append a fork! Use addChild or replaceChild instead!
  1213. */
  1214. append: function(node) {
  1215. var self = this;
  1216. if (node.node_type() != 'fork') {
  1217. var join = self.join();
  1218. if (join.id() != node.id()) {
  1219. var children = join.findChildren();
  1220. self.addChild(node);
  1221. node.append(join);
  1222. // remember children
  1223. $.each(children, function(index, child) {
  1224. join.addChild(child);
  1225. });
  1226. }
  1227. }
  1228. },
  1229. /**
  1230. * Replace child node with another node
  1231. * 1. Remove child if the replacement is related join
  1232. * 2. Apply Node.replaceChild for all other causes
  1233. * NOTE: can assume the only join this fork will ever see is the related join!
  1234. * This is because the related will always be the closest join for children.
  1235. * Also, the operations allowed that would make this all possible: detach, append;
  1236. * are inherently going to remove any other joins if they exist.
  1237. * This is also easier given nodes are contained within Forks!
  1238. */
  1239. replaceChild: function(child, replacement) {
  1240. var self = this;
  1241. var ret = true;
  1242. if (self.join().id() == replacement.id()) {
  1243. ret = self.removeChild(child);
  1244. } else {
  1245. ret = Node.prototype.replaceChild.apply(self, arguments);
  1246. }
  1247. var links = self.links().filter(function(element, index, arr) {
  1248. return self.registry.get(element.child()).node_type() != 'join';
  1249. });
  1250. if (links.length < 2) {
  1251. self.detach();
  1252. self.join().detach();
  1253. self.erase();
  1254. self.join().erase();
  1255. }
  1256. return ret;
  1257. },
  1258. /**
  1259. * Converts fork node into decision node in the following way:
  1260. * 1. Copies contents of current fork node into a new decision node
  1261. * 2. Detach fork node
  1262. * 3. Erase fork node
  1263. * 4. Append decision node to parent
  1264. */
  1265. convertToDecision: function() {
  1266. var self = this;
  1267. var join = self.join();
  1268. var end = null;
  1269. var child = join.findChildren()[0];
  1270. if (child.findParents().length == 1) {
  1271. end = child;
  1272. }
  1273. // Attaches child of join to parents of join
  1274. join.detach();
  1275. var decision_model = new DecisionModel({
  1276. id: IdGeneratorTable['decision'].nextId(),
  1277. name: self.name(),
  1278. description: self.description(),
  1279. node_type: 'decision',
  1280. workflow: self.workflow(),
  1281. child_links: self.model.child_links
  1282. });
  1283. var default_link = {
  1284. parent: decision_model.id,
  1285. child: self._workflow.end(),
  1286. name: 'default',
  1287. comment: ''
  1288. };
  1289. decision_model.child_links.push(default_link);
  1290. $.each(decision_model.child_links, function(index, link) {
  1291. link.parent = decision_model.id;
  1292. });
  1293. var decision_node = new DecisionNode(self._workflow, decision_model, self.registry);
  1294. if (end) {
  1295. decision_node.addEnd(end);
  1296. }
  1297. var parents = self.findParents();
  1298. decision_node.removeChild(join);
  1299. join.erase();
  1300. self.erase();
  1301. self.registry.add(decision_node.id(), decision_node);
  1302. $.each(parents, function(index, parent) {
  1303. parent.replaceChild(self, decision_node);
  1304. });
  1305. }
  1306. });
  1307. var DecisionNode = NodeModule($, IdGeneratorTable);
  1308. $.extend(DecisionNode.prototype, ForkNode.prototype, {
  1309. initialize: function(workflow, model, registry) {
  1310. var self = this;
  1311. var registry = registry;
  1312. var end = null;
  1313. $.each(self.child_links(), function(index, link) {
  1314. if (link.name() == 'related') {
  1315. $(registry).bind('registry:add', function(e) {
  1316. var end = self.end();
  1317. if (end) {
  1318. self.removeEnd();
  1319. self.addEnd(end);
  1320. $(registry).unbind(e);
  1321. }
  1322. });
  1323. }
  1324. });
  1325. },
  1326. // Only a single end allowed for now!
  1327. // Finds end for branch
  1328. findEnd: function( ) {
  1329. var self = this;
  1330. var end = self.end();
  1331. if (!end) {
  1332. return self._findEnd( self, 0 );
  1333. }
  1334. return end;
  1335. },
  1336. _findEnd: function( node, count ) {
  1337. var self = this;
  1338. var end = null;
  1339. if (node.findParents().length > 1 && --count == 0) {
  1340. return node;
  1341. }
  1342. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1343. count++;
  1344. }
  1345. $.each(node.findChildren(), function(index, node) {
  1346. if (end == null) {
  1347. end = self._findEnd(node, count);
  1348. }
  1349. });
  1350. return end;
  1351. },
  1352. end: function() {
  1353. var self = this;
  1354. var end = null;
  1355. $.each(self.child_links(), function(index, link) {
  1356. if (link.name() == 'related') {
  1357. end = self.registry.get(link.child());
  1358. }
  1359. });
  1360. return end;
  1361. },
  1362. addEnd: function(node) {
  1363. var self = this;
  1364. self.child_links.push({
  1365. parent: ko.observable(self.id()),
  1366. child: ko.observable(node.id()),
  1367. name: ko.observable('related'),
  1368. comment: ko.observable(''),
  1369. });
  1370. $(node).one('detached', function(e) {
  1371. var end = self.end();
  1372. if (end.links().length == 1) {
  1373. self.removeEnd();
  1374. var new_end = self.findEnd();
  1375. self.addEnd(new_end);
  1376. } else {
  1377. // Should never hit this case.
  1378. self.removeEnd();
  1379. }
  1380. });
  1381. },
  1382. removeEnd: function() {
  1383. var self = this;
  1384. var spliceIndex = -1;
  1385. $.each(self.child_links(), function(index, link) {
  1386. if (link.name() == 'related') {
  1387. spliceIndex = index;
  1388. }
  1389. });
  1390. if (spliceIndex > -1) {
  1391. var end = registry.get(self.child_links()[spliceIndex].child());
  1392. self.child_links.splice(spliceIndex, 1);
  1393. return true;
  1394. }
  1395. return false;
  1396. },
  1397. isChild: function(node) {
  1398. var self = this;
  1399. return self._isChild(node, self, 0);
  1400. },
  1401. _isChild: function( test_child, node, count ) {
  1402. var self = this;
  1403. var result = node.id() == test_child.id();
  1404. if (node.findParents().length > 1 && --count == 0) {
  1405. return result;
  1406. }
  1407. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1408. count++;
  1409. }
  1410. $.each(node.findChildren(), function(index, node) {
  1411. if (!result) {
  1412. result = self._isChild(test_child, node, count);
  1413. }
  1414. });
  1415. return result;
  1416. },
  1417. /**
  1418. * Append a node to the current decision
  1419. * Also appends end node to node.
  1420. * NOTE: Cannot append a decision! Use addChild or replaceChild instead!
  1421. */
  1422. append: function(node) {
  1423. var self = this;
  1424. var end = self.findEnd();
  1425. if (end.id() == node.id()) {
  1426. return false;
  1427. }
  1428. self.addChild(node);
  1429. node.append(end);
  1430. return true;
  1431. },
  1432. /**
  1433. * Replace child node with another node
  1434. * 1. Remove child if the replacement is end
  1435. * 2. Apply Node.replaceChild for all other causes
  1436. */
  1437. replaceChild: function(child, replacement) {
  1438. var self = this;
  1439. var ret = true;
  1440. var end = self.findEnd();
  1441. if (end && end.id() == replacement.id()) {
  1442. ret = self.removeChild(child);
  1443. } else {
  1444. ret = Node.prototype.replaceChild.apply(self, arguments);
  1445. }
  1446. if (self.links().length < 2) {
  1447. self.detach();
  1448. self.erase();
  1449. }
  1450. return ret;
  1451. }
  1452. });
  1453. /**
  1454. * Workflow module
  1455. */
  1456. var WorkflowModule = function($, NodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable) {
  1457. var module = function(options) {
  1458. var self = this;
  1459. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  1460. var mapping = ko.mapping.fromJS(options.model, {
  1461. ignore: ['initialize', 'toString'],
  1462. job_properties: {
  1463. create: function(options) {
  1464. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1465. var parent = options.parent;
  1466. mapping.name.subscribe(function(value) {
  1467. parent.job_properties.valueHasMutated();
  1468. });
  1469. mapping.value.subscribe(function(value) {
  1470. parent.job_properties.valueHasMutated();
  1471. });
  1472. return mapping;
  1473. },
  1474. update: function(options) {
  1475. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1476. var parent = options.parent;
  1477. mapping.name.subscribe(function(value) {
  1478. parent.job_properties.valueHasMutated();
  1479. });
  1480. mapping.value.subscribe(function(value) {
  1481. parent.job_properties.valueHasMutated();
  1482. });
  1483. return mapping;
  1484. }
  1485. },
  1486. parameters: {
  1487. // Will receive individual objects to subscribe.
  1488. // Containing array is mapped automagically
  1489. create: function(options) {
  1490. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1491. var parent = options.parent;
  1492. mapping.name.subscribe(function(value) {
  1493. parent.parameters.valueHasMutated();
  1494. });
  1495. mapping.value.subscribe(function(value) {
  1496. parent.parameters.valueHasMutated();
  1497. });
  1498. return mapping;
  1499. },
  1500. update: function(options) {
  1501. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1502. var parent = options.parent;
  1503. mapping.name.subscribe(function(value) {
  1504. parent.parameters.valueHasMutated();
  1505. });
  1506. mapping.value.subscribe(function(value) {
  1507. parent.parameters.valueHasMutated();
  1508. });
  1509. return mapping;
  1510. },
  1511. }
  1512. });
  1513. $.extend(self, mapping);
  1514. $.each(mapping['__ko_mapping__'].mappedProperties, function(key, value) {
  1515. var key = key;
  1516. self[key].subscribe(function(value) {
  1517. self.is_dirty( true );
  1518. self.model[key] = ko.mapping.toJS(value);
  1519. });
  1520. });
  1521. self.model = options.model;
  1522. self.registry = options.registry;
  1523. self.options = options;
  1524. self.el = $('#workflow');
  1525. self.nodes = ko.observableArray([]);
  1526. self.kill = null;
  1527. self.is_dirty = ko.observable( false );
  1528. self.url = ko.computed(function() {
  1529. return '/oozie/workflows/' + self.id()
  1530. });
  1531. // Events
  1532. self.el.on('workflow:rebuild', function() {
  1533. self.rebuild();
  1534. });
  1535. self.el.on('workflow:events:load', function() {
  1536. self.dragAndDropEvents();
  1537. });
  1538. self.el.on('workflow:droppables:load', function() {
  1539. self.droppables();
  1540. });
  1541. self.el.on('workflow:draggables:load', function() {
  1542. self.draggables();
  1543. });
  1544. self.dragAndDropEvents();
  1545. self.el.trigger('workflow:events:loaded');
  1546. module.prototype.initialize.apply(self, arguments);
  1547. return self;
  1548. };
  1549. $.extend(module.prototype, {
  1550. // Normal stuff
  1551. initialize: function(options) {
  1552. var self = this;
  1553. if ('data' in options) {
  1554. // Initialize nodes
  1555. if (options.data.nodes) {
  1556. self.registry.clear();
  1557. $.each(options.data.nodes, function(index, node) {
  1558. var NodeModel = NodeModelChooser(node.node_type);
  1559. var model = new NodeModel(node);
  1560. var temp = null;
  1561. switch(node.node_type) {
  1562. case 'start':
  1563. temp = new StartNode(self, model, self.registry);
  1564. break;
  1565. case 'fork':
  1566. temp = new ForkNode(self, model, self.registry);
  1567. break;
  1568. case 'decision':
  1569. temp = new DecisionNode(self, model, self.registry);
  1570. break;
  1571. case 'kill':
  1572. temp = self.kill = new Node(self, model, self.registry);
  1573. break;
  1574. default:
  1575. temp = new Node(self, model, self.registry);
  1576. break;
  1577. }
  1578. self.registry.add(temp.id(), temp);
  1579. });
  1580. }
  1581. // Update data
  1582. $.each(options.data, function (key, value) {
  1583. if (key in self) {
  1584. switch(key) {
  1585. case 'job_properties':
  1586. case 'parameters':
  1587. // These may be serialized JSON data since that is how they are stored
  1588. self[key].removeAll();
  1589. var arr = $.parseJSON(value) || value;
  1590. $.each(arr, function(index, obj) {
  1591. var mapping = ko.mapping.fromJS(obj);
  1592. mapping.name.subscribe(function(value) {
  1593. self[key].valueHasMutated();
  1594. });
  1595. mapping.value.subscribe(function(value) {
  1596. self[key].valueHasMutated();
  1597. });
  1598. self[key].push(mapping);
  1599. });
  1600. break;
  1601. case 'nodes':
  1602. break;
  1603. default:
  1604. self[key](value);
  1605. break;
  1606. }
  1607. }
  1608. });
  1609. workflow.is_dirty( false );
  1610. }
  1611. if (!self.kill) {
  1612. var kill_json = {
  1613. "description": "",
  1614. "workflow": self.id(),
  1615. "child_links": [],
  1616. "node_type": "kill",
  1617. "message": "Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]",
  1618. "name": "kill",
  1619. "id": IdGeneratorTable['kill'].nextId()
  1620. };
  1621. var NodeModel = NodeModelChooser(kill_json.node_type);
  1622. var model = new NodeModel(kill_json);
  1623. self.kill = new Node(self, model, self.registry);
  1624. self.registry.add(self.kill.id(), self.kill);
  1625. }
  1626. },
  1627. toString: function() {
  1628. return '';
  1629. var s = '[';
  1630. $.each(self.registry.nodes, function(key, node) {
  1631. s += node.model.toString() + ",\n";
  1632. });
  1633. return s + ']';
  1634. },
  1635. // Data manipulation
  1636. cloneNode: function(node, event) {
  1637. var self = this;
  1638. var model_copy = $.extend(true, {}, node.model);
  1639. var template = model_copy.node_type + 'EditTemplate';
  1640. var NodeModel = NodeModelChooser(node.node_type());
  1641. model_copy.id = IdGeneratorTable[model_copy.node_type].nextId();
  1642. model_copy.name += '-copy';
  1643. model_copy.child_links = [];
  1644. var model = new NodeModel(model_copy);
  1645. var new_node = new Node(self, model, self.registry);
  1646. self.registry.add(new_node.id(), new_node);
  1647. new_node.addChild(self.kill);
  1648. node.append(new_node);
  1649. self.rebuild();
  1650. modal.setTemplate(template);
  1651. modal.show(new_node);
  1652. modal.recenter(280, 250);
  1653. modal.addDecorations();
  1654. // $(".propKey").each(addAutoComplete);
  1655. workflow.is_dirty( true );
  1656. },
  1657. removeNode: function(node, event) {
  1658. var self = this;
  1659. node.detach();
  1660. node.erase();
  1661. self.rebuild();
  1662. workflow.is_dirty( true );
  1663. },
  1664. save: function( options ) {},
  1665. load: function() {
  1666. var self = this;
  1667. $.getJSON(self.url(), function(data) {
  1668. if (data.status == 0) {
  1669. self.reload(data.data);
  1670. } else {
  1671. $.jHueNotify.error("${ _('Received invalid response from server: ') }" + JSON.stringify(data));
  1672. }
  1673. });
  1674. },
  1675. reload: function(data) {
  1676. var self = this;
  1677. // Clear all children
  1678. $.each(self.registry.nodes, function(index, node) {
  1679. node.children.removeAll();
  1680. });
  1681. self.nodes.removeAll();
  1682. self.initialize({data: data});
  1683. self.rebuild();
  1684. self.el.trigger('workflow:loaded');
  1685. },
  1686. addParameter: function(data, event) {
  1687. var self = this;
  1688. var prop = { name: ko.observable(""), value: ko.observable("") };
  1689. // force bubble up to containing observable array.
  1690. prop.name.subscribe(function(){
  1691. self.parameters.valueHasMutated();
  1692. });
  1693. prop.value.subscribe(function(){
  1694. self.parameters.valueHasMutated();
  1695. });
  1696. self.parameters.push(prop);
  1697. },
  1698. removeParameter: function(data, event) {
  1699. var self = this;
  1700. self.parameters.remove(data);
  1701. },
  1702. addJobProperty: function(data, event) {
  1703. var self = this;
  1704. var prop = { name: ko.observable(""), value: ko.observable("") };
  1705. // force bubble up to containing observable array.
  1706. prop.name.subscribe(function(){
  1707. self.parameters.valueHasMutated();
  1708. });
  1709. prop.value.subscribe(function(){
  1710. self.parameters.valueHasMutated();
  1711. });
  1712. self.job_properties.push(prop);
  1713. },
  1714. removeJobProperty: function(data, event) {
  1715. var self = this;
  1716. self.job_properties.remove(data);
  1717. },
  1718. // Workflow UI
  1719. // Function to build nodes... recursively.
  1720. build: function() {
  1721. var self = this;
  1722. var maximum = 50;
  1723. var count = 0;
  1724. var methodChooser = function(node, collection, skip_parents_check) {
  1725. if (count++ >= maximum) {
  1726. if (window.error) {
  1727. console.error('Hit maximum number of node recursion: ' + maximum);
  1728. }
  1729. return null;
  1730. }
  1731. if (!node) {
  1732. return node;
  1733. }
  1734. var parents = node.findParents();
  1735. // Found end of decision node or found join!
  1736. if (parents.length > 1 && !skip_parents_check) {
  1737. return node;
  1738. }
  1739. switch(node.node_type()) {
  1740. case 'start':
  1741. case 'end':
  1742. case 'kill':
  1743. case 'fork':
  1744. case 'decision':
  1745. case 'join':
  1746. return control(node, collection);
  1747. default:
  1748. return normal(node, collection);
  1749. }
  1750. };
  1751. var normal = function(node, collection) {
  1752. collection.push(node);
  1753. var retNode = null;
  1754. $.each(node.links(), function(index, link) {
  1755. var next_node = self.registry.get(link.child());
  1756. retNode = methodChooser(next_node, collection, false, true);
  1757. });
  1758. return retNode;
  1759. };
  1760. var control = function(node, collection, skip_parents_check) {
  1761. switch(node.node_type()) {
  1762. case 'start':
  1763. case 'end':
  1764. case 'kill':
  1765. case 'join':
  1766. return normal(node, collection, false, true);
  1767. case 'fork':
  1768. collection.push(node);
  1769. // Wait for join.
  1770. // Iterate through all children and add them to child collection.
  1771. var join = null;
  1772. $.each(node.links(), function(index, link) {
  1773. var next_node = self.registry.get(link.child());
  1774. var collection = ko.observableArray([]);
  1775. node.children.push(collection);
  1776. join = methodChooser(next_node, collection, false, true);
  1777. });
  1778. // Add join to collection, then find its single child.
  1779. return methodChooser(join, collection, true, true);
  1780. case 'decision':
  1781. collection.push(node);
  1782. // Waits for end, then runs through children of end node
  1783. var end = null;
  1784. $.each(node.links(), function(index, link) {
  1785. var next_node = self.registry.get(link.child());
  1786. var collection = ko.observableArray([]);
  1787. node.children.push(collection);
  1788. end = methodChooser(next_node, collection, true, true);
  1789. });
  1790. // Add end
  1791. if (node.end() && end.id() == node.end().id()) {
  1792. return methodChooser(end, collection, true, true);
  1793. } else {
  1794. return end;
  1795. }
  1796. default:
  1797. // Should never get here.
  1798. return node;
  1799. }
  1800. };
  1801. methodChooser(self.registry.get(self.start()), self.nodes, false, true);
  1802. },
  1803. rebuild: function() {
  1804. var self = this;
  1805. // Clear all children
  1806. $.each(self.registry.nodes, function(index, node) {
  1807. node.children.removeAll();
  1808. });
  1809. self.nodes.removeAll();
  1810. // Rebuild
  1811. self.build();
  1812. self.draggables();
  1813. self.droppables();
  1814. self.el.trigger('workflow:rebuilt');
  1815. },
  1816. draggables: function() {
  1817. var self = this;
  1818. self.el.find('.node-action').each(function(index, el) {
  1819. if (!$(el).hasClass('ui-draggable')) {
  1820. $(el).draggable({
  1821. containment: [ self.el.offset().left - 10, self.el.offset().top - 10,
  1822. self.el.offset().left + self.el.outerWidth(), self.el.offset().top + self.el.outerHeight() ],
  1823. handle: '.node',
  1824. refreshPositions: true,
  1825. revert: true
  1826. });
  1827. }
  1828. });
  1829. },
  1830. droppables: function() {
  1831. var self = this;
  1832. self.el.find('.node-link').each(function(index, el) {
  1833. $(el).droppable({
  1834. 'hoverClass': 'node-link-hover',
  1835. 'greedy': true,
  1836. 'accept': '.node-action',
  1837. 'tolerance': 'pointer'
  1838. });
  1839. });
  1840. self.el.find('.node-decision-end').each(function(index, el) {
  1841. $(el).droppable({
  1842. 'hoverClass': 'node-link-hover',
  1843. 'greedy': true,
  1844. 'accept': '.node-action',
  1845. 'tolerance': 'pointer'
  1846. });
  1847. });
  1848. self.el.find('.node-fork .action').each(function(index, el) {
  1849. $(el).droppable({
  1850. 'hoverClass': 'node-fork-hover',
  1851. 'greedy': true,
  1852. 'accept': '.node-action',
  1853. 'tolerance': 'pointer'
  1854. });
  1855. });
  1856. self.el.find('.node-decision .action').each(function(index, el) {
  1857. $(el).droppable({
  1858. 'hoverClass': 'node-fork-hover',
  1859. 'greedy': true,
  1860. 'accept': '.node-action',
  1861. 'tolerance': 'pointer'
  1862. });
  1863. });
  1864. self.el.find('.node-action .action').each(function(index, el) {
  1865. $(el).droppable({
  1866. 'hoverClass': 'node-action-hover',
  1867. 'greedy': true,
  1868. 'accept': '.node-action',
  1869. 'tolerance': 'pointer'
  1870. });
  1871. });
  1872. },
  1873. dragAndDropEvents: function() {
  1874. var self = this;
  1875. // Build event delegations.
  1876. // Drop on node link
  1877. self.el.on('drop', '.node-link', function(e, ui) {
  1878. // draggable should be a node.
  1879. // droppable should be a link.
  1880. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1881. var droppable = ko.contextFor(this).$data;
  1882. // If newParent is fork, prepend to child instead.
  1883. // This will make it so that we can drop and drop to the top of a node list within a fork.
  1884. var newParent = self.registry.get(droppable.parent());
  1885. if (newParent.id() != draggable.id()) {
  1886. if (newParent.isChild(draggable)) {
  1887. if (draggable.findParents().length > 1) {
  1888. // End of decision tree is being dragged to the bottom of a branch
  1889. draggable.detach();
  1890. newParent.append(draggable);
  1891. workflow.is_dirty( true );
  1892. self.rebuild();
  1893. }
  1894. } else {
  1895. switch(newParent.node_type()) {
  1896. case 'fork':
  1897. case 'decision':
  1898. draggable.detach();
  1899. var child = self.registry.get(droppable.child());
  1900. newParent.replaceChild(child, draggable);
  1901. draggable.addChild(child);
  1902. break;
  1903. case 'join':
  1904. // Join may disappear when we detach...
  1905. // Remember its children and append to child.
  1906. var parents = newParent.findParents();
  1907. draggable.detach();
  1908. if (newParent.findParents().length < 2) {
  1909. $.each(parents, function(index, parent) {
  1910. parent.append(draggable);
  1911. });
  1912. } else {
  1913. newParent.append(draggable);
  1914. }
  1915. break;
  1916. default:
  1917. draggable.detach();
  1918. newParent.append(draggable);
  1919. break;
  1920. }
  1921. workflow.is_dirty( true );
  1922. self.rebuild();
  1923. }
  1924. }
  1925. // Prevent bubbling events
  1926. return false;
  1927. });
  1928. // Drop on fork
  1929. self.el.on('drop', '.node-fork', function(e, ui) {
  1930. // draggable should be a node.
  1931. // droppable should be a fork.
  1932. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1933. var droppable = ko.contextFor(this).$data;
  1934. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1935. draggable.detach();
  1936. droppable.append(draggable);
  1937. self.rebuild();
  1938. }
  1939. // Prevent bubbling events
  1940. return false;
  1941. });
  1942. // Drop on decision
  1943. self.el.on('drop', '.node-decision', function(e, ui) {
  1944. // draggable should be a node.
  1945. // droppable should be a fork.
  1946. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1947. var droppable = ko.contextFor(this).$data;
  1948. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1949. draggable.detach();
  1950. droppable.append(draggable);
  1951. self.rebuild();
  1952. }
  1953. // Prevent bubbling events
  1954. return false;
  1955. });
  1956. // Drop on decision end link
  1957. self.el.on('drop', '.node-decision-end', function(e, ui) {
  1958. // draggable should be a node.
  1959. // droppable should be a decision end link... which should give us the decision node.
  1960. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1961. var droppable = ko.contextFor(this).$data;
  1962. var end = droppable.findEnd(droppable);
  1963. if (end.id() != draggable.id()) {
  1964. draggable.detach();
  1965. $.each(end.findParents(), function(index, parent) {
  1966. if (droppable.isChild(parent)) {
  1967. parent.append(draggable);
  1968. }
  1969. });
  1970. droppable.removeEnd();
  1971. droppable.addEnd(draggable);
  1972. self.rebuild();
  1973. }
  1974. // Prevent bubbling events
  1975. return false;
  1976. });
  1977. // Drop on action
  1978. self.el.on('drop', '.node-action', function(e, ui) {
  1979. // draggable should be a node.
  1980. // droppable should be a node.
  1981. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1982. var droppable = ko.contextFor(this).$data;
  1983. // Create a fork and join programatically.
  1984. var newParents = droppable.findParents();
  1985. // skip forking beneathe a decision node
  1986. if (droppable.id() != draggable.id() && newParents.length == 1 && draggable.findParents().length == 1) {
  1987. var ForkModel = NodeModelChooser('fork');
  1988. var JoinModel = NodeModelChooser('join');
  1989. var fork = new ForkModel({
  1990. id: IdGeneratorTable['fork'].nextId(),
  1991. description: "",
  1992. workflow: self.id,
  1993. node_type: "fork",
  1994. child_links: []
  1995. });
  1996. var forkNode = new ForkNode(self, fork, self.registry);
  1997. var join = new JoinModel({
  1998. id: IdGeneratorTable['join'].nextId(),
  1999. description: "",
  2000. workflow: self.id,
  2001. node_type: "join",
  2002. child_links: []
  2003. });
  2004. var joinNode = new Node(self, join, self.registry);
  2005. self.registry.add(forkNode.id(), forkNode);
  2006. self.registry.add(joinNode.id(), joinNode);
  2007. forkNode.addChild(joinNode);
  2008. // Handles fork creation.
  2009. $.each(newParents, function(index, parent) {
  2010. parent.replaceChild(droppable, forkNode);
  2011. });
  2012. draggable.detach();
  2013. forkNode.append(draggable);
  2014. forkNode.append(droppable);
  2015. self.rebuild();
  2016. }
  2017. // Prevent bubbling events.
  2018. return false;
  2019. });
  2020. }
  2021. });
  2022. return module;
  2023. };
  2024. var Workflow = WorkflowModule($, nodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable);