workflow.js 63 KB

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