workflow.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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. };
  396. var NodeModule = function($, IdGeneratorTable) {
  397. var META_LINKS = ['related', 'default', 'error'];
  398. var linkTypeChooser = function(parent, child) {
  399. if (child.node_type() == 'kill') {
  400. return 'error';
  401. }
  402. switch(parent.node_type()) {
  403. case 'start':
  404. return (child.node_type() == 'end') ? 'related' : 'to';
  405. case 'fork':
  406. return (child.node_type() == 'join') ? 'related' : 'start';
  407. case 'decision':
  408. return 'start';
  409. case 'join':
  410. return 'to';
  411. default:
  412. return 'ok';
  413. };
  414. };
  415. var module = function(workflow, model, registry) {
  416. var self = this;
  417. self.map(model);
  418. self.links = ko.computed(function() {
  419. var links = self.child_links().filter(function(element, index, arr) {
  420. return $.inArray(element.name(), META_LINKS) == -1;
  421. });
  422. return links;
  423. });
  424. self.meta_links = ko.computed(function() {
  425. var links = self.child_links().filter(function(element, index, arr) {
  426. return $.inArray(element.name(), META_LINKS) != -1;
  427. });
  428. return links;
  429. });
  430. self._workflow = workflow;
  431. self.registry = registry;
  432. self.children = ko.observableArray([]);
  433. self.model = model;
  434. var errors = {};
  435. for(var key in model) {
  436. switch(key) {
  437. case 'child_links':
  438. case 'node_ptr':
  439. case 'initialize':
  440. case 'toString':
  441. break;
  442. default:
  443. errors[key] = [];
  444. break;
  445. }
  446. }
  447. self.errors = ko.mapping.fromJS(errors);
  448. self.edit_template = model.node_type + 'EditTemplate';
  449. switch(model.node_type) {
  450. case 'start':
  451. self.view_template = ko.observable('startTemplate');
  452. break;
  453. case 'kill':
  454. case 'end':
  455. self.view_template = ko.observable('emptyTemplate');
  456. break;
  457. case 'fork':
  458. self.view_template = ko.observable('forkTemplate');
  459. break;
  460. case 'decision':
  461. self.view_template = ko.observable('decisionTemplate');
  462. break;
  463. case 'join':
  464. self.view_template = ko.observable('joinTemplate');
  465. break;
  466. default:
  467. self.view_template = ko.observable('nodeTemplate');
  468. break;
  469. }
  470. // Data manipulation
  471. if ('job_properties' in model) {
  472. self.addProp = function() {
  473. var prop = { name: ko.observable(""), value: ko.observable("") };
  474. prop.name.subscribe(function(value) {
  475. self.job_properties.valueHasMutated();
  476. });
  477. prop.value.subscribe(function(value) {
  478. self.job_properties.valueHasMutated();
  479. });
  480. self.job_properties.push(prop);
  481. // $(".propKey:last").each(addAutoComplete);
  482. };
  483. self.removeProp = function(val) {
  484. self.job_properties.remove(val);
  485. };
  486. }
  487. if ('files' in model) {
  488. self.addFile = function() {
  489. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  490. prop.name.subscribe(function(value) {
  491. self.files.valueHasMutated();
  492. });
  493. prop.dummy.subscribe(function(value) {
  494. self.files.valueHasMutated();
  495. });
  496. self.files.push(prop);
  497. };
  498. self.removeFile = function(val) {
  499. self.files.remove(val);
  500. };
  501. }
  502. if ('archives' in model) {
  503. self.addArchive = function() {
  504. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  505. prop.name.subscribe(function(value) {
  506. self.archives.valueHasMutated();
  507. });
  508. prop.dummy.subscribe(function(value) {
  509. self.archives.valueHasMutated();
  510. });
  511. self.archives.push(prop);
  512. };
  513. self.removeArchive = function(val) {
  514. self.archives.remove(val);
  515. };
  516. }
  517. if ('params' in model) {
  518. self.addParam = function() {
  519. var prop = { value: ko.observable(""), type: ko.observable("param") };
  520. prop.value.subscribe(function(value) {
  521. self.params.valueHasMutated();
  522. });
  523. prop.type.subscribe(function(value) {
  524. self.params.valueHasMutated();
  525. });
  526. self.params.push(prop);
  527. };
  528. self.addArgument = function() {
  529. var prop = { value: ko.observable(""), type: ko.observable("argument") };
  530. prop.value.subscribe(function(value) {
  531. self.params.valueHasMutated();
  532. });
  533. prop.type.subscribe(function(value) {
  534. self.params.valueHasMutated();
  535. });
  536. self.params.push(prop);
  537. };
  538. self.addArg = function() {
  539. var prop = { value: ko.observable(""), type: ko.observable("arg") };
  540. prop.value.subscribe(function(value) {
  541. self.params.valueHasMutated();
  542. });
  543. prop.type.subscribe(function(value) {
  544. self.params.valueHasMutated();
  545. });
  546. self.params.push(prop);
  547. };
  548. self.addEnvVar = function() {
  549. var prop = { value: ko.observable(""), type: ko.observable("env-var") };
  550. prop.value.subscribe(function(value) {
  551. self.params.valueHasMutated();
  552. });
  553. prop.type.subscribe(function(value) {
  554. self.params.valueHasMutated();
  555. });
  556. self.params.push(prop);
  557. };
  558. self.removeParam = function(val) {
  559. self.params.remove(val);
  560. };
  561. }
  562. if ('prepares' in model) {
  563. self.addPrepareDelete = function() {
  564. var prop = { value: ko.observable(""), type: ko.observable("delete") };
  565. prop.value.subscribe(function(value) {
  566. self.prepares.valueHasMutated();
  567. });
  568. prop.type.subscribe(function(value) {
  569. self.prepares.valueHasMutated();
  570. });
  571. self.prepares.push(prop);
  572. };
  573. self.addPrepareMkdir = function() {
  574. var prop = { value: ko.observable(""), type: ko.observable("mkdir") };
  575. prop.value.subscribe(function(value) {
  576. self.prepares.valueHasMutated();
  577. });
  578. prop.type.subscribe(function(value) {
  579. self.prepares.valueHasMutated();
  580. });
  581. self.prepares.push(prop);
  582. };
  583. self.removePrepare = function(val) {
  584. self.prepares.remove(val);
  585. };
  586. }
  587. self.initialize.apply(self, arguments);
  588. return self;
  589. };
  590. $.extend(module.prototype, {
  591. // Data.
  592. children: null,
  593. model: null,
  594. // Normal stuff
  595. /**
  596. * Called when creating a new node
  597. */
  598. initialize: function(workflow, model, registry) {},
  599. toString: function() {
  600. return '';
  601. },
  602. /**
  603. * Fetches registry
  604. */
  605. getRegistry: function() {
  606. return registry;
  607. },
  608. /**
  609. * Maps a model to self
  610. * Called when creating a new node before any thing else
  611. */
  612. map: function(model) {
  613. var self = this;
  614. var map_params = function(options, subscribe) {
  615. options.data = ($.type(options.data) == "string") ? $.parseJSON(options.data) : options.data;
  616. if ($.isArray(options.data)) {
  617. var mapping = ko.mapping.fromJS(options.data);
  618. $.each(mapping(), function(index, value) {
  619. subscribe(value);
  620. });
  621. return mapping;
  622. } else {
  623. var mapping = ko.mapping.fromJS(options.data, {});
  624. subscribe(mapping);
  625. return mapping;
  626. }
  627. };
  628. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  629. var mapping = ko.mapping.fromJS(model, {
  630. ignore: ['initialize','toString'],
  631. job_properties: {
  632. create: function(options) {
  633. var parent = options.parent;
  634. var subscribe = function(mapping) {
  635. mapping.name.subscribe(function(value) {
  636. parent.job_properties.valueHasMutated();
  637. });
  638. mapping.value.subscribe(function(value) {
  639. parent.job_properties.valueHasMutated();
  640. });
  641. };
  642. return map_params(options, subscribe);
  643. },
  644. update: function(options) {
  645. var parent = options.parent;
  646. var subscribe = function(mapping) {
  647. mapping.name.subscribe(function(value) {
  648. parent.job_properties.valueHasMutated();
  649. });
  650. mapping.value.subscribe(function(value) {
  651. parent.job_properties.valueHasMutated();
  652. });
  653. };
  654. return map_params(options, subscribe);
  655. }
  656. },
  657. files: {
  658. create: function(options) {
  659. return map_params(options, function() {});
  660. },
  661. update: function(options) {
  662. return map_params(options, function() {});
  663. }
  664. },
  665. archives: {
  666. create: function(options) {
  667. var parent = options.parent;
  668. var subscribe = function(mapping) {
  669. mapping.name.subscribe(function(value) {
  670. parent.archives.valueHasMutated();
  671. });
  672. mapping.dummy.subscribe(function(value) {
  673. parent.archives.valueHasMutated();
  674. });
  675. };
  676. return map_params(options, subscribe);
  677. },
  678. update: function(options) {
  679. var parent = options.parent;
  680. var subscribe = function(mapping) {
  681. mapping.name.subscribe(function(value) {
  682. parent.archives.valueHasMutated();
  683. });
  684. mapping.dummy.subscribe(function(value) {
  685. parent.archives.valueHasMutated();
  686. });
  687. };
  688. return map_params(options, subscribe);
  689. }
  690. },
  691. params: {
  692. create: function(options) {
  693. var parent = options.parent;
  694. var subscribe = function(mapping) {
  695. mapping.value.subscribe(function(value) {
  696. parent.params.valueHasMutated();
  697. });
  698. mapping.type.subscribe(function(value) {
  699. parent.params.valueHasMutated();
  700. });
  701. };
  702. return map_params(options, subscribe);
  703. },
  704. update: function(options) {
  705. var parent = options.parent;
  706. var subscribe = function(mapping) {
  707. mapping.value.subscribe(function(value) {
  708. parent.params.valueHasMutated();
  709. });
  710. mapping.type.subscribe(function(value) {
  711. parent.params.valueHasMutated();
  712. });
  713. };
  714. return map_params(options, subscribe);
  715. }
  716. },
  717. prepares: {
  718. create: function(options) {
  719. var parent = options.parent;
  720. var subscribe = function(mapping) {
  721. mapping.value.subscribe(function(value) {
  722. parent.prepares.valueHasMutated();
  723. });
  724. mapping.type.subscribe(function(value) {
  725. parent.prepares.valueHasMutated();
  726. });
  727. };
  728. return map_params(options, subscribe);
  729. },
  730. update: function(options) {
  731. var parent = options.parent;
  732. var subscribe = function(mapping) {
  733. mapping.value.subscribe(function(value) {
  734. parent.prepares.valueHasMutated();
  735. });
  736. mapping.type.subscribe(function(value) {
  737. parent.prepares.valueHasMutated();
  738. });
  739. };
  740. return map_params(options, subscribe);
  741. }
  742. }
  743. });
  744. $.extend(self, mapping);
  745. $.each(mapping, function(key, value) {
  746. var key = key;
  747. if (ko.isObservable(self[key])) {
  748. self[key].subscribe(function(value) {
  749. model[key] = ko.mapping.toJS(value);
  750. });
  751. }
  752. });
  753. $.each(self.child_links(), function(index, link) {
  754. var $index = index;
  755. link.comment.subscribe(function(value) {
  756. self.model.child_links[$index].comment = value;
  757. });
  758. link.child.subscribe(function(value) {
  759. self.model.child_links[$index].child = value;
  760. });
  761. });
  762. },
  763. validate: function( ) {
  764. var self = this;
  765. var options = {};
  766. data = $.extend(true, {}, self.model);
  767. var success = false;
  768. var request = $.extend({
  769. url: '/oozie/workflows/' + self._workflow.id() + '/actions/' + self.node_type() + '/validate',
  770. type: 'POST',
  771. data: { node: JSON.stringify(data) },
  772. success: function(data) {
  773. ko.mapping.fromJS(data.data, self.errors);
  774. success = data.status == 0;
  775. },
  776. async: false,
  777. }, options);
  778. $.ajax(request);
  779. return success;
  780. },
  781. // Hierarchy manipulation.
  782. /**
  783. * Append node to self
  784. * Does not support multiple children.
  785. * Ensures single child.
  786. * Ensures no cycles.
  787. * 1. Finds all children and attaches them to node (cleans node first).
  788. * 2. Remove all children from self.
  789. * 3. Attach node to self.
  790. */
  791. append: function(node) {
  792. var self = this;
  793. // Not fork nor decision nor self
  794. if ($.inArray(self.node_type(), ['fork', 'decision']) == -1 && node.id() != self.id()) {
  795. node.removeAllChildren();
  796. $.each(self.links(), function(index, link) {
  797. node.addChild(registry.get(link.child()));
  798. });
  799. self.removeAllChildren();
  800. self.addChild(node);
  801. }
  802. },
  803. /**
  804. * Find all parents of current node
  805. */
  806. findParents: function() {
  807. var self = this;
  808. var parents = [];
  809. $.each(registry.nodes, function(id, node) {
  810. $.each(node.links(), function(index, link) {
  811. if (link.child() == self.id()) {
  812. parents.push(node);
  813. }
  814. });
  815. });
  816. return parents;
  817. },
  818. /**
  819. * Find all children of current node
  820. */
  821. findChildren: function() {
  822. var self = this;
  823. var children = [];
  824. $.each(self.links(), function(index, link) {
  825. children.push(registry.get(link.child()));
  826. });
  827. return children;
  828. },
  829. /**
  830. * Detach current node from the graph
  831. * 1. Takes children of self node, removes them from self node, and adds them to each parent of self node.
  832. * 2. The self node is then removed from every parent.
  833. * 3. Does not support multiple children since we do not automatically fork.
  834. */
  835. detach: function() {
  836. var self = this;
  837. $.each(self.findParents(), function(index, parent) {
  838. $.each(self.links(), function(index, link) {
  839. var node = registry.get(link.child());
  840. parent.replaceChild(self, node);
  841. });
  842. });
  843. $(self).trigger('detached');
  844. self.removeAllChildren();
  845. },
  846. /**
  847. * Add child
  848. * Update child links for this node.
  849. */
  850. addChild: function(node) {
  851. var self = this;
  852. var link = {
  853. parent: ko.observable(self.id()),
  854. child: ko.observable(node.id()),
  855. name: ko.observable(linkTypeChooser(self, node)),
  856. comment: ko.observable('')
  857. };
  858. self.child_links.unshift(link);
  859. },
  860. /**
  861. * Remove child node
  862. * 1. Find child node link
  863. * 2. Remove child node link
  864. */
  865. removeChild: function(node) {
  866. var self = this;
  867. var spliceIndex = -1;
  868. $.each(self.child_links(), function(index, link) {
  869. if (link.child() == node.id()) {
  870. spliceIndex = index;
  871. }
  872. });
  873. if (spliceIndex > -1) {
  874. self.child_links.splice(spliceIndex, 1);
  875. }
  876. return spliceIndex != -1;
  877. },
  878. /**
  879. * Remove all children
  880. * Removes all children except for related, default, and error links
  881. * Note: we hold on to related, default, and error links because
  882. * we have to.
  883. */
  884. removeAllChildren: function() {
  885. var self = this;
  886. var keep_links = [];
  887. $.each(self.child_links(), function(index, link) {
  888. if ($.inArray(link.name(), META_LINKS) > -1) {
  889. keep_links.push(link);
  890. }
  891. });
  892. self.child_links.removeAll();
  893. $.each(keep_links, function(index, link) {
  894. self.child_links.push(link);
  895. });
  896. },
  897. /**
  898. * Replace child node with another node in the following way:
  899. * 1. Find child index
  900. * 2. Remove child index
  901. * 3. Remove and remember every element after child
  902. * 4. Add replacement node
  903. * 5. Add every child that was remembered
  904. */
  905. replaceChild: function(child, replacement) {
  906. var self = this;
  907. var index = -1;
  908. $.each(self.child_links(), function(i, link) {
  909. if (link.child() == child.id()) {
  910. index = i;
  911. }
  912. });
  913. if (index > -1) {
  914. self.child_links.splice(index, 1);
  915. var links = self.child_links.splice(index);
  916. var link = {
  917. parent: ko.observable(self.id()),
  918. child: ko.observable(replacement.id()),
  919. name: ko.observable(linkTypeChooser(self, replacement)),
  920. comment: ko.observable(''),
  921. };
  922. self.child_links.push(link);
  923. $.each(links, function(index, link) {
  924. self.child_links.push(link);
  925. });
  926. }
  927. return index != -1;
  928. },
  929. isChild: function(node) {
  930. var self = this;
  931. var res = false;
  932. $.each(self.links(), function(index, link) {
  933. if (link.child() == node.id()) {
  934. res = true;
  935. }
  936. });
  937. return res;
  938. },
  939. erase: function() {
  940. var self = this;
  941. self.registry.remove(self.id());
  942. }
  943. });
  944. return module;
  945. };
  946. var Node = NodeModule($, IdGeneratorTable);
  947. var ForkNode = NodeModule($, IdGeneratorTable);
  948. $.extend(ForkNode.prototype, Node.prototype, {
  949. // Join nodes are connected through 'related' links
  950. join: function() {
  951. var self = this;
  952. var join = null;
  953. $.each(self.child_links(), function(index, link) {
  954. if (link.name() == 'related') {
  955. join = self.registry.get(link.child());
  956. }
  957. });
  958. return join;
  959. },
  960. /**
  961. * Append a node to the current fork
  962. * Also adds join node to node.
  963. * When adding the join node, append will remove all the children from the join!
  964. * We need to make sure the join remembers its children.
  965. * NOTE: Cannot append a fork! Use addChild or replaceChild instead!
  966. */
  967. append: function(node) {
  968. var self = this;
  969. if (node.node_type() != 'fork') {
  970. var join = self.join();
  971. if (join.id() != node.id()) {
  972. var children = join.findChildren();
  973. self.addChild(node);
  974. node.append(join);
  975. // remember children
  976. $.each(children, function(index, child) {
  977. join.addChild(child);
  978. });
  979. }
  980. }
  981. },
  982. /**
  983. * Replace child node with another node
  984. * 1. Remove child if the replacement is related join
  985. * 2. Apply Node.replaceChild for all other causes
  986. * NOTE: can assume the only join this fork will ever see is the related join!
  987. * This is because the related will always be the closest join for children.
  988. * Also, the operations allowed that would make this all possible: detach, append;
  989. * are inherently going to remove any other joins if they exist.
  990. * This is also easier given nodes are contained within Forks!
  991. */
  992. replaceChild: function(child, replacement) {
  993. var self = this;
  994. var ret = true;
  995. if (self.join().id() == replacement.id()) {
  996. ret = self.removeChild(child);
  997. } else {
  998. ret = Node.prototype.replaceChild.apply(self, arguments);
  999. }
  1000. var links = self.links().filter(function(element, index, arr) {
  1001. return self.registry.get(element.child()).node_type() != 'join';
  1002. });
  1003. if (links.length < 2) {
  1004. self.detach();
  1005. self.join().detach();
  1006. self.erase();
  1007. self.join().erase();
  1008. }
  1009. return ret;
  1010. },
  1011. /**
  1012. * Converts fork node into decision node in the following way:
  1013. * 1. Copies contents of current fork node into a new decision node
  1014. * 2. Detach fork node
  1015. * 3. Erase fork node
  1016. * 4. Append decision node to parent
  1017. */
  1018. convertToDecision: function() {
  1019. var self = this;
  1020. var join = self.join();
  1021. var end = null;
  1022. var child = join.findChildren()[0];
  1023. if (child.findParents().length == 1) {
  1024. end = child;
  1025. }
  1026. // Attaches child of join to parents of join
  1027. join.detach();
  1028. var decision_model = new DecisionModel({
  1029. id: IdGeneratorTable['decision'].nextId(),
  1030. name: self.name(),
  1031. description: self.description(),
  1032. node_type: 'decision',
  1033. workflow: self.workflow(),
  1034. child_links: self.model.child_links
  1035. });
  1036. var default_link = {
  1037. parent: decision_model.id,
  1038. child: self._workflow.end(),
  1039. name: 'default',
  1040. comment: ''
  1041. };
  1042. decision_model.child_links.push(default_link);
  1043. $.each(decision_model.child_links, function(index, link) {
  1044. link.parent = decision_model.id;
  1045. });
  1046. var decision_node = new DecisionNode(self._workflow, decision_model, self.registry);
  1047. if (end) {
  1048. decision_node.addEnd(end);
  1049. }
  1050. var parents = self.findParents();
  1051. decision_node.removeChild(join);
  1052. join.erase();
  1053. self.erase();
  1054. self.registry.add(decision_node.id(), decision_node);
  1055. $.each(parents, function(index, parent) {
  1056. parent.replaceChild(self, decision_node);
  1057. });
  1058. }
  1059. });
  1060. var DecisionNode = NodeModule($, IdGeneratorTable);
  1061. $.extend(DecisionNode.prototype, ForkNode.prototype, {
  1062. initialize: function(workflow, model, registry) {
  1063. var self = this;
  1064. var registry = registry;
  1065. var end = null;
  1066. $.each(self.child_links(), function(index, link) {
  1067. if (link.name() == 'related') {
  1068. $(registry).bind('registry:add', function(e) {
  1069. var end = self.end();
  1070. if (end) {
  1071. self.removeEnd();
  1072. self.addEnd(end);
  1073. $(registry).unbind(e);
  1074. }
  1075. });
  1076. }
  1077. });
  1078. },
  1079. // Only a single end allowed for now!
  1080. // Finds end for branch
  1081. findEnd: function( ) {
  1082. var self = this;
  1083. var end = self.end();
  1084. if (!end) {
  1085. return self._findEnd( self, 0 );
  1086. }
  1087. return end;
  1088. },
  1089. _findEnd: function( node, count ) {
  1090. var self = this;
  1091. var end = null;
  1092. if (node.findParents().length > 1 && --count == 0) {
  1093. return node;
  1094. }
  1095. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1096. count++;
  1097. }
  1098. $.each(node.findChildren(), function(index, node) {
  1099. if (end == null) {
  1100. end = self._findEnd(node, count);
  1101. }
  1102. });
  1103. return end;
  1104. },
  1105. end: function() {
  1106. var self = this;
  1107. var end = null;
  1108. $.each(self.child_links(), function(index, link) {
  1109. if (link.name() == 'related') {
  1110. end = self.registry.get(link.child());
  1111. }
  1112. });
  1113. return end;
  1114. },
  1115. addEnd: function(node) {
  1116. var self = this;
  1117. self.child_links.push({
  1118. parent: ko.observable(self.id()),
  1119. child: ko.observable(node.id()),
  1120. name: ko.observable('related'),
  1121. comment: ko.observable(''),
  1122. });
  1123. $(node).one('detached', function(e) {
  1124. var end = self.end();
  1125. if (end.links().length == 1) {
  1126. self.removeEnd();
  1127. var new_end = self.findEnd();
  1128. self.addEnd(new_end);
  1129. } else {
  1130. // Should never hit this case.
  1131. self.removeEnd();
  1132. }
  1133. });
  1134. },
  1135. removeEnd: function() {
  1136. var self = this;
  1137. var spliceIndex = -1;
  1138. $.each(self.child_links(), function(index, link) {
  1139. if (link.name() == 'related') {
  1140. spliceIndex = index;
  1141. }
  1142. });
  1143. if (spliceIndex > -1) {
  1144. var end = registry.get(self.child_links()[spliceIndex].child());
  1145. self.child_links.splice(spliceIndex, 1);
  1146. return true;
  1147. }
  1148. return false;
  1149. },
  1150. isChild: function(node) {
  1151. var self = this;
  1152. return self._isChild(node, self, 0);
  1153. },
  1154. _isChild: function( test_child, node, count ) {
  1155. var self = this;
  1156. var result = node.id() == test_child.id();
  1157. if (node.findParents().length > 1 && --count == 0) {
  1158. return result;
  1159. }
  1160. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1161. count++;
  1162. }
  1163. $.each(node.findChildren(), function(index, node) {
  1164. if (!result) {
  1165. result = self._isChild(test_child, node, count);
  1166. }
  1167. });
  1168. return result;
  1169. },
  1170. /**
  1171. * Append a node to the current decision
  1172. * Also appends end node to node.
  1173. * NOTE: Cannot append a decision! Use addChild or replaceChild instead!
  1174. */
  1175. append: function(node) {
  1176. var self = this;
  1177. var end = self.findEnd();
  1178. if (end.id() == node.id()) {
  1179. return false;
  1180. }
  1181. self.addChild(node);
  1182. node.append(end);
  1183. return true;
  1184. },
  1185. /**
  1186. * Replace child node with another node
  1187. * 1. Remove child if the replacement is end
  1188. * 2. Apply Node.replaceChild for all other causes
  1189. */
  1190. replaceChild: function(child, replacement) {
  1191. var self = this;
  1192. var ret = true;
  1193. var end = self.findEnd();
  1194. if (end && end.id() == replacement.id()) {
  1195. ret = self.removeChild(child);
  1196. } else {
  1197. ret = Node.prototype.replaceChild.apply(self, arguments);
  1198. }
  1199. if (self.links().length < 2) {
  1200. self.detach();
  1201. self.erase();
  1202. }
  1203. return ret;
  1204. }
  1205. });
  1206. /**
  1207. * Workflow module
  1208. */
  1209. var WorkflowModule = function($, NodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable) {
  1210. var module = function(options) {
  1211. var self = this;
  1212. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  1213. var mapping = ko.mapping.fromJS(options.model, {
  1214. ignore: ['initialize','toString'],
  1215. job_properties: {
  1216. create: function(options) {
  1217. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1218. var parent = options.parent;
  1219. mapping.name.subscribe(function(value) {
  1220. parent.job_properties.valueHasMutated();
  1221. });
  1222. mapping.value.subscribe(function(value) {
  1223. parent.job_properties.valueHasMutated();
  1224. });
  1225. return mapping;
  1226. },
  1227. update: function(options) {
  1228. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1229. var parent = options.parent;
  1230. mapping.name.subscribe(function(value) {
  1231. parent.job_properties.valueHasMutated();
  1232. });
  1233. mapping.value.subscribe(function(value) {
  1234. parent.job_properties.valueHasMutated();
  1235. });
  1236. return mapping;
  1237. }
  1238. },
  1239. parameters: {
  1240. // Will receive individual objects to subscribe.
  1241. // Containing array is mapped automagically
  1242. create: function(options) {
  1243. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1244. var parent = options.parent;
  1245. mapping.name.subscribe(function(value) {
  1246. parent.parameters.valueHasMutated();
  1247. });
  1248. mapping.value.subscribe(function(value) {
  1249. parent.parameters.valueHasMutated();
  1250. });
  1251. return mapping;
  1252. },
  1253. update: function(options) {
  1254. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1255. var parent = options.parent;
  1256. mapping.name.subscribe(function(value) {
  1257. parent.parameters.valueHasMutated();
  1258. });
  1259. mapping.value.subscribe(function(value) {
  1260. parent.parameters.valueHasMutated();
  1261. });
  1262. return mapping;
  1263. },
  1264. }
  1265. });
  1266. $.extend(self, mapping);
  1267. $.each(mapping['__ko_mapping__'].mappedProperties, function(key, value) {
  1268. var key = key;
  1269. self[key].subscribe(function(value) {
  1270. workflow.model.is_dirty = true;
  1271. self.model[key] = ko.mapping.toJS(value);
  1272. });
  1273. });
  1274. self.model = options.model;
  1275. self.registry = options.registry;
  1276. self.options = options;
  1277. self.el = $('#workflow');
  1278. self.nodes = ko.observableArray([]);
  1279. self.kill = null;
  1280. self.url = ko.computed(function() {
  1281. return '/oozie/workflows/' + self.id()
  1282. });
  1283. // Events
  1284. self.el.on('workflow:rebuild', function() {
  1285. self.rebuild();
  1286. });
  1287. self.el.on('workflow:events:load', function() {
  1288. self.dragAndDropEvents();
  1289. });
  1290. self.el.on('workflow:droppables:load', function() {
  1291. self.droppables();
  1292. });
  1293. self.el.on('workflow:draggables:load', function() {
  1294. self.draggables();
  1295. });
  1296. self.dragAndDropEvents();
  1297. self.el.trigger('workflow:events:loaded');
  1298. module.prototype.initialize.apply(self, arguments);
  1299. return self;
  1300. };
  1301. $.extend(module.prototype, {
  1302. // Normal stuff
  1303. initialize: function(options) {
  1304. var self = this;
  1305. if ('data' in options) {
  1306. // Initialize nodes
  1307. if (options.data.nodes) {
  1308. self.registry.clear();
  1309. $.each(options.data.nodes, function(index, node) {
  1310. var NodeModel = NodeModelChooser(node.node_type);
  1311. var model = new NodeModel(node);
  1312. var temp = null;
  1313. switch(node.node_type) {
  1314. case 'fork':
  1315. temp = new ForkNode(self, model, self.registry);
  1316. break;
  1317. case 'decision':
  1318. temp = new DecisionNode(self, model, self.registry);
  1319. break;
  1320. case 'kill':
  1321. temp = self.kill = new Node(self, model, self.registry);
  1322. break;
  1323. default:
  1324. temp = new Node(self, model, self.registry);
  1325. break;
  1326. }
  1327. self.registry.add(temp.id(), temp);
  1328. });
  1329. }
  1330. // Update data
  1331. $.each(options.data, function (key, value) {
  1332. if (key in self) {
  1333. switch(key) {
  1334. case 'job_properties':
  1335. case 'parameters':
  1336. // These may be serialized JSON data since that is how they are stored
  1337. self[key].removeAll();
  1338. var arr = $.parseJSON(value) || value;
  1339. $.each(arr, function(index, obj) {
  1340. var mapping = ko.mapping.fromJS(obj);
  1341. mapping.name.subscribe(function(value) {
  1342. self[key].valueHasMutated();
  1343. });
  1344. mapping.value.subscribe(function(value) {
  1345. self[key].valueHasMutated();
  1346. });
  1347. self[key].push(mapping);
  1348. });
  1349. break;
  1350. case 'nodes':
  1351. break;
  1352. default:
  1353. self[key](value);
  1354. break;
  1355. }
  1356. }
  1357. });
  1358. workflow.model.is_dirty = false;
  1359. }
  1360. },
  1361. toString: function() {
  1362. return '';
  1363. var s = '[';
  1364. $.each(self.registry.nodes, function(key, node) {
  1365. s += node.model.toString() + ",\n";
  1366. });
  1367. return s + ']';
  1368. },
  1369. // Data manipulation
  1370. cloneNode: function(node, event) {
  1371. var self = this;
  1372. var model_copy = $.extend(true, {}, node.model);
  1373. var template = model_copy.node_type + 'EditTemplate';
  1374. var NodeModel = NodeModelChooser(node.node_type());
  1375. model_copy.id = IdGeneratorTable[model_copy.node_type].nextId();
  1376. model_copy.name += '-copy';
  1377. model_copy.child_links = [];
  1378. var model = new NodeModel(model_copy);
  1379. var new_node = new Node(self, model, self.registry);
  1380. self.registry.add(new_node.id(), new_node);
  1381. new_node.addChild(self.kill);
  1382. node.append(new_node);
  1383. self.rebuild();
  1384. modal.setTemplate(template);
  1385. modal.show(new_node);
  1386. modal.recenter(280, 250);
  1387. modal.addDecorations();
  1388. // $(".propKey").each(addAutoComplete);
  1389. workflow.model.is_dirty = true;
  1390. },
  1391. removeNode: function(node, event) {
  1392. var self = this;
  1393. node.detach();
  1394. node.erase();
  1395. self.rebuild();
  1396. workflow.model.is_dirty = true;
  1397. },
  1398. save: function( options ) {},
  1399. load: function() {
  1400. var self = this;
  1401. $.getJSON(self.url(), function(data) {
  1402. if (data.status == 0) {
  1403. self.reload(data.data);
  1404. } else {
  1405. $.jHueNotify.error("${ _('Received invalid response from server: ') }" + JSON.stringify(data));
  1406. }
  1407. });
  1408. },
  1409. reload: function(data) {
  1410. var self = this;
  1411. // Clear all children
  1412. $.each(self.registry.nodes, function(index, node) {
  1413. node.children.removeAll();
  1414. });
  1415. self.nodes.removeAll();
  1416. self.initialize({data: data});
  1417. self.rebuild();
  1418. self.el.trigger('workflow:loaded');
  1419. },
  1420. addParameter: function(data, event) {
  1421. var self = this;
  1422. var prop = { name: ko.observable(""), value: ko.observable("") };
  1423. // force bubble up to containing observable array.
  1424. prop.name.subscribe(function(){
  1425. self.parameters.valueHasMutated();
  1426. });
  1427. prop.value.subscribe(function(){
  1428. self.parameters.valueHasMutated();
  1429. });
  1430. self.parameters.push(prop);
  1431. },
  1432. removeParameter: function(data, event) {
  1433. var self = this;
  1434. self.parameters.remove(data);
  1435. },
  1436. addJobProperty: function(data, event) {
  1437. var self = this;
  1438. var prop = { name: ko.observable(""), value: ko.observable("") };
  1439. // force bubble up to containing observable array.
  1440. prop.name.subscribe(function(){
  1441. self.parameters.valueHasMutated();
  1442. });
  1443. prop.value.subscribe(function(){
  1444. self.parameters.valueHasMutated();
  1445. });
  1446. self.job_properties.push(prop);
  1447. },
  1448. removeJobProperty: function(data, event) {
  1449. var self = this;
  1450. self.job_properties.remove(data);
  1451. },
  1452. // Workflow UI
  1453. // Function to build nodes... recursively.
  1454. build: function() {
  1455. var self = this;
  1456. var maximum = 50;
  1457. var count = 0;
  1458. var methodChooser = function(node, collection, skip_parents_check) {
  1459. if (count++ >= maximum) {
  1460. if (window.error) {
  1461. console.error('Hit maximum number of node recursion: ' + maximum);
  1462. }
  1463. return null;
  1464. }
  1465. if (!node) {
  1466. return node;
  1467. }
  1468. var parents = node.findParents();
  1469. // Found end of decision node or found join!
  1470. if (parents.length > 1 && !skip_parents_check) {
  1471. return node;
  1472. }
  1473. switch(node.node_type()) {
  1474. case 'start':
  1475. case 'end':
  1476. case 'kill':
  1477. case 'fork':
  1478. case 'decision':
  1479. case 'join':
  1480. return control(node, collection);
  1481. default:
  1482. return normal(node, collection);
  1483. }
  1484. };
  1485. var normal = function(node, collection) {
  1486. collection.push(node);
  1487. var retNode = null;
  1488. $.each(node.links(), function(index, link) {
  1489. var next_node = self.registry.get(link.child());
  1490. retNode = methodChooser(next_node, collection, false, true);
  1491. });
  1492. return retNode;
  1493. };
  1494. var control = function(node, collection, skip_parents_check) {
  1495. switch(node.node_type()) {
  1496. case 'start':
  1497. case 'end':
  1498. case 'kill':
  1499. case 'join':
  1500. return normal(node, collection, false, true);
  1501. case 'fork':
  1502. collection.push(node);
  1503. // Wait for join.
  1504. // Iterate through all children and add them to child collection.
  1505. var join = null;
  1506. $.each(node.links(), function(index, link) {
  1507. var next_node = self.registry.get(link.child());
  1508. var collection = ko.observableArray([]);
  1509. node.children.push(collection);
  1510. join = methodChooser(next_node, collection, false, true);
  1511. });
  1512. // Add join to collection, then find its single child.
  1513. return methodChooser(join, collection, true, true);
  1514. case 'decision':
  1515. collection.push(node);
  1516. // Waits for end, then runs through children of end node
  1517. var end = null;
  1518. $.each(node.links(), function(index, link) {
  1519. var next_node = self.registry.get(link.child());
  1520. var collection = ko.observableArray([]);
  1521. node.children.push(collection);
  1522. end = methodChooser(next_node, collection, true, true);
  1523. });
  1524. // Add end
  1525. if (node.end() && end.id() == node.end().id()) {
  1526. return methodChooser(end, collection, true, true);
  1527. } else {
  1528. return end;
  1529. }
  1530. default:
  1531. // Should never get here.
  1532. return node;
  1533. }
  1534. };
  1535. methodChooser(self.registry.get(self.start()), self.nodes, false, true);
  1536. },
  1537. rebuild: function() {
  1538. var self = this;
  1539. // Clear all children
  1540. $.each(self.registry.nodes, function(index, node) {
  1541. node.children.removeAll();
  1542. });
  1543. self.nodes.removeAll();
  1544. // Rebuild
  1545. self.build();
  1546. self.draggables();
  1547. self.droppables();
  1548. self.el.trigger('workflow:rebuilt');
  1549. },
  1550. draggables: function() {
  1551. var self = this;
  1552. self.el.find('.node-action').each(function(index, el) {
  1553. if (!$(el).hasClass('ui-draggable')) {
  1554. $(el).draggable({
  1555. containment: [ self.el.offset().left - 10, self.el.offset().top - 10,
  1556. self.el.offset().left + self.el.outerWidth(), self.el.offset().top + self.el.outerHeight() ],
  1557. handle: '.node',
  1558. refreshPositions: true,
  1559. revert: true
  1560. });
  1561. }
  1562. });
  1563. },
  1564. droppables: function() {
  1565. var self = this;
  1566. self.el.find('.node-link').each(function(index, el) {
  1567. $(el).droppable({
  1568. 'hoverClass': 'node-link-hover',
  1569. 'greedy': true,
  1570. 'accept': '.node-action',
  1571. 'tolerance': 'pointer'
  1572. });
  1573. });
  1574. self.el.find('.node-decision-end').each(function(index, el) {
  1575. $(el).droppable({
  1576. 'hoverClass': 'node-link-hover',
  1577. 'greedy': true,
  1578. 'accept': '.node-action',
  1579. 'tolerance': 'pointer'
  1580. });
  1581. });
  1582. self.el.find('.node-fork .action').each(function(index, el) {
  1583. $(el).droppable({
  1584. 'hoverClass': 'node-fork-hover',
  1585. 'greedy': true,
  1586. 'accept': '.node-action',
  1587. 'tolerance': 'pointer'
  1588. });
  1589. });
  1590. self.el.find('.node-decision .action').each(function(index, el) {
  1591. $(el).droppable({
  1592. 'hoverClass': 'node-fork-hover',
  1593. 'greedy': true,
  1594. 'accept': '.node-action',
  1595. 'tolerance': 'pointer'
  1596. });
  1597. });
  1598. self.el.find('.node-action .action').each(function(index, el) {
  1599. $(el).droppable({
  1600. 'hoverClass': 'node-action-hover',
  1601. 'greedy': true,
  1602. 'accept': '.node-action',
  1603. 'tolerance': 'pointer'
  1604. });
  1605. });
  1606. },
  1607. dragAndDropEvents: function() {
  1608. var self = this;
  1609. // Build event delegations.
  1610. // Drop on node link
  1611. self.el.on('drop', '.node-link', function(e, ui) {
  1612. // draggable should be a node.
  1613. // droppable should be a link.
  1614. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1615. var droppable = ko.contextFor(this).$data;
  1616. // If newParent is fork, prepend to child instead.
  1617. // This will make it so that we can drop and drop to the top of a node list within a fork.
  1618. var newParent = self.registry.get(droppable.parent());
  1619. if (newParent.id() != draggable.id()) {
  1620. if (newParent.isChild(draggable)) {
  1621. if (draggable.findParents().length > 1) {
  1622. // End of decision tree is being dragged to the bottom of a branch
  1623. draggable.detach();
  1624. newParent.append(draggable);
  1625. workflow.model.is_dirty = true;
  1626. self.rebuild();
  1627. }
  1628. } else {
  1629. switch(newParent.node_type()) {
  1630. case 'fork':
  1631. case 'decision':
  1632. draggable.detach();
  1633. var child = self.registry.get(droppable.child());
  1634. newParent.replaceChild(child, draggable);
  1635. draggable.addChild(child);
  1636. break;
  1637. case 'join':
  1638. // Join may disappear when we detach...
  1639. // Remember its children and append to child.
  1640. var parents = newParent.findParents();
  1641. draggable.detach();
  1642. if (newParent.findParents().length < 2) {
  1643. $.each(parents, function(index, parent) {
  1644. parent.append(draggable);
  1645. });
  1646. } else {
  1647. newParent.append(draggable);
  1648. }
  1649. break;
  1650. default:
  1651. draggable.detach();
  1652. newParent.append(draggable);
  1653. break;
  1654. }
  1655. workflow.model.is_dirty = true;
  1656. self.rebuild();
  1657. }
  1658. }
  1659. // Prevent bubbling events
  1660. return false;
  1661. });
  1662. // Drop on fork
  1663. self.el.on('drop', '.node-fork', function(e, ui) {
  1664. // draggable should be a node.
  1665. // droppable should be a fork.
  1666. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1667. var droppable = ko.contextFor(this).$data;
  1668. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1669. draggable.detach();
  1670. droppable.append(draggable);
  1671. self.rebuild();
  1672. }
  1673. // Prevent bubbling events
  1674. return false;
  1675. });
  1676. // Drop on decision
  1677. self.el.on('drop', '.node-decision', function(e, ui) {
  1678. // draggable should be a node.
  1679. // droppable should be a fork.
  1680. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1681. var droppable = ko.contextFor(this).$data;
  1682. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1683. draggable.detach();
  1684. droppable.append(draggable);
  1685. self.rebuild();
  1686. }
  1687. // Prevent bubbling events
  1688. return false;
  1689. });
  1690. // Drop on decision end link
  1691. self.el.on('drop', '.node-decision-end', function(e, ui) {
  1692. // draggable should be a node.
  1693. // droppable should be a decision end link... which should give us the decision node.
  1694. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1695. var droppable = ko.contextFor(this).$data;
  1696. var end = droppable.findEnd(droppable);
  1697. if (end.id() != draggable.id()) {
  1698. draggable.detach();
  1699. $.each(end.findParents(), function(index, parent) {
  1700. if (droppable.isChild(parent)) {
  1701. parent.append(draggable);
  1702. }
  1703. });
  1704. droppable.removeEnd();
  1705. droppable.addEnd(draggable);
  1706. self.rebuild();
  1707. }
  1708. // Prevent bubbling events
  1709. return false;
  1710. });
  1711. // Drop on action
  1712. self.el.on('drop', '.node-action', function(e, ui) {
  1713. // draggable should be a node.
  1714. // droppable should be a node.
  1715. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1716. var droppable = ko.contextFor(this).$data;
  1717. // Create a fork and join programatically.
  1718. var newParents = droppable.findParents();
  1719. // skip forking beneathe a decision node
  1720. if (droppable.id() != draggable.id() && newParents.length == 1 && draggable.findParents().length == 1) {
  1721. var ForkModel = NodeModelChooser('fork');
  1722. var JoinModel = NodeModelChooser('join');
  1723. var fork = new ForkModel({
  1724. id: IdGeneratorTable['fork'].nextId(),
  1725. description: "",
  1726. workflow: self.id,
  1727. node_type: "fork",
  1728. child_links: []
  1729. });
  1730. var forkNode = new ForkNode(self, fork, self.registry);
  1731. var join = new JoinModel({
  1732. id: IdGeneratorTable['join'].nextId(),
  1733. description: "",
  1734. workflow: self.id,
  1735. node_type: "join",
  1736. child_links: []
  1737. });
  1738. var joinNode = new Node(self, join, self.registry);
  1739. self.registry.add(forkNode.id(), forkNode);
  1740. self.registry.add(joinNode.id(), joinNode);
  1741. forkNode.addChild(joinNode);
  1742. // Handles fork creation.
  1743. $.each(newParents, function(index, parent) {
  1744. parent.replaceChild(droppable, forkNode);
  1745. });
  1746. draggable.detach();
  1747. forkNode.append(draggable);
  1748. forkNode.append(droppable);
  1749. self.rebuild();
  1750. }
  1751. // Prevent bubbling events.
  1752. return false;
  1753. });
  1754. }
  1755. });
  1756. return module;
  1757. };
  1758. var Workflow = WorkflowModule($, nodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable);