workflow.js 55 KB

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