| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340 |
- // Licensed to Cloudera, Inc. under one
- // or more contributor license agreements. See the NOTICE file
- // distributed with this work for additional information
- // regarding copyright ownership. Cloudera, Inc. licenses this file
- // to you under the Apache License, Version 2.0 (the
- // "License"); you may not use this file except in compliance
- // with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // adding missing .filter for IE8
- if (!('filter' in Array.prototype)) {
- Array.prototype.filter= function(filter, that /*opt*/) {
- var other= [], v;
- for (var i=0, n= this.length; i<n; i++)
- if (i in this && filter.call(that, v= this[i], i, this))
- other.push(v);
- return other;
- };
- }
- // Since knockout maps arrays without calling "update" nor "create"
- // Provide a JSON string that will be parsed in custom 'create' and 'update' functions.
- var normalize_model_fields = (function() {
- var FIELDS = ['parameters', 'job_properties', 'files', 'archives', 'prepares', 'params',
- 'deletes', 'mkdirs', 'moves', 'chmods', 'touchzs'];
- function fn(node_model) {
- $.each(FIELDS, function(index, field) {
- if (field in node_model && $.isArray(node_model[field])) {
- node_model[field] = JSON.stringify(node_model[field]);
- }
- });
- }
- return fn;
- })();
- // Parse JSON if it is JSON and appropriately map.
- // Apply subscriber to each mapping.
- var map_params = function(options, subscribe) {
- options.data = ($.type(options.data) == "string") ? $.parseJSON(options.data) : options.data;
- if ($.isArray(options.data)) {
- var mapping = ko.mapping.fromJS(options.data);
- $.each(mapping(), function(index, value) {
- subscribe(value);
- });
- return mapping;
- } else {
- var mapping = ko.mapping.fromJS(options.data, {});
- subscribe(mapping);
- return mapping;
- }
- };
- /**
- * Registry of models
- * - Each model should have an ID attribute.
- */
- var RegistryModule = function($) {
- var module = function() {
- var self = this;
- self.nodes = {};
- module.prototype.initialize.apply(self, arguments);
- return self;
- };
- $.extend(module.prototype, {
- // Normal stuff
- initialize: function() {},
- toString: function() {
- var self = this;
- var s = $.map(self.nodes, function(node) {
- return node.id();
- }).join();
- return s;
- },
- add: function(id, node) {
- var self = this;
- $(self).trigger('registry:add');
- self.nodes[String(id)] = node;
- },
- remove: function(id) {
- var self = this;
- $(self).trigger('registry:remove');
- delete self.nodes[String(id)];
- },
- get: function(id) {
- var self = this;
- return self.nodes[id];
- },
- clear: function() {
- var self = this;
- delete self.nodes;
- self.nodes = {};
- }
- });
- return module;
- };
- var Registry = RegistryModule($);
- /**
- * Modal Control
- * Displays a single node in a modal window of your choosing.
- * Make sure to update the 'context' member and 'template' members.
- * IMPORTANT: Use 'setTemplate', then 'show'.
- */
- var ModalModule = function($, ko) {
- var module = function(modal, template) {
- var self = this;
- self.el = self.modal = $(modal);
- self.context = ko.observable();
- self.template = ko.observable(template || '');
- self.bound = false;
- };
- module.prototype.show = function(context) {
- var self = this;
- if (context) {
- self.context(context);
- }
- ko.applyBindings(self, self.modal[0]);
- self.modal.modal('show');
- };
- module.prototype.hide = function() {
- var self = this;
- self.el.modal('hide');
- };
- module.prototype.setTemplate = function(template) {
- var self = this;
- ko.cleanNode(self.modal[0]);
- self.template( template );
- };
- module.prototype.recenter = function(offset_x, offset_y) {
- var self = this;
- var top = ( ($(window).height() - self.modal.outerHeight(false)) / 2 );
- var left = ( ($(window).width() - self.modal.outerWidth(false)) / 2 );
- if (top < 0) {
- top = 0;
- }
- if (left < 0) {
- left = 0;
- }
- top += offset_y || 0;
- left += offset_x || 0;
- self.modal.css({top: top +'px', left: left+'px'});
- };
- module.prototype.addDecorations = function () {
- $(".popover").remove();
- $("input[name='job_xml']:not(.pathChooser)").addClass("pathChooser").after(getFileBrowseButton($("input[name='job_xml']:not(.pathChooser)")));
- $("input[name='jar_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='jar_path']")));
- $("input[name='script_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='script_path']")));
- $("input[name='command']").addClass("pathChooser").after(getFileBrowseButton($("input[name='command']")));
- $("*[rel=popover]").popover({
- placement:'left',
- trigger:'hover'
- });
- }
- return module;
- };
- var Modal = ModalModule($, ko);
- /**
- * ID Generator
- * Generate a new ID starting from 1.
- * - Accepts a prefix that will be prepended like so: <prefix>:<id number>
- */
- var IdGeneratorModule = function($) {
- return function(options) {
- var self = this;
- $.extend(self, options);
- self.counter = 1;
- self.nextId = function() {
- return ((self.prefix) ? self.prefix + ':' : '') + self.counter++;
- };
- };
- };
- var IdGenerator = IdGeneratorModule($);
- var ModelModule = function($) {
- var module = function(attrs) {
- var self = this;
- $.extend(self, attrs);
- module.prototype.initialize.apply(self, arguments);
- return self;
- };
- $.extend(module.prototype, {
- // Normal stuff
- initialize: function(){},
- toString: function() {
- var self = this;
- return JSON.stringify(self, null, '\t');
- }
- });
- return module;
- };
- var WorkflowModel = ModelModule($);
- $.extend(WorkflowModel.prototype, {
- id: 0,
- name: '',
- description: '',
- start: 0,
- end: 0,
- schema_version: 0.4,
- deployment_dir: '',
- is_shared: true,
- parameters: '[]',
- job_xml: ''
- });
- var NodeModel = ModelModule($);
- $.extend(NodeModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: '',
- workflow: 0,
- child_links: []
- });
- var ForkModel = ModelModule($);
- $.extend(ForkModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'fork',
- workflow: 0,
- child_links: []
- });
- var DecisionModel = ModelModule($);
- $.extend(DecisionModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'decision',
- workflow: 0,
- child_links: []
- });
- var DistCPModel = ModelModule($);
- $.extend(DistCPModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'distcp',
- workflow: 0,
- job_properties: '[]',
- prepares: '[]',
- job_xml: '',
- params: '[]',
- child_links: []
- });
- var MapReduceModel = ModelModule($);
- $.extend(MapReduceModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'mapreduce',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- jar_path: '',
- prepares: '[]',
- job_xml: '',
- child_links: []
- });
- var StreamingModel = ModelModule($);
- $.extend(StreamingModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'streaming',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- mapper: '',
- reducer: '',
- child_links: []
- });
- var JavaModel = ModelModule($);
- $.extend(JavaModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'java',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- jar_path: '',
- prepares: '[]',
- job_xml: '',
- main_class: '',
- args: '',
- java_opts: '',
- child_links: []
- });
- var PigModel = ModelModule($);
- $.extend(PigModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'pig',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- prepares: '[]',
- job_xml: '',
- params: '[]',
- script_path: '',
- child_links: []
- });
- var HiveModel = ModelModule($);
- $.extend(HiveModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'hive',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- prepares: '[]',
- job_xml: '',
- params: '[]',
- script_path: '',
- child_links: []
- });
- var SqoopModel = ModelModule($);
- $.extend(SqoopModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'sqoop',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- prepares: '[]',
- job_xml: '',
- params: '[]',
- script_path: '',
- child_links: []
- });
- var ShellModel = ModelModule($);
- $.extend(ShellModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'shell',
- workflow: 0,
- files: '[]',
- archives: '[]',
- job_properties: '[]',
- prepares: '[]',
- job_xml: '',
- params: '[]',
- command: '',
- capture_output: false,
- child_links: []
- });
- var SshModel = ModelModule($);
- $.extend(SshModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'ssh',
- workflow: 0,
- user: '',
- host: '',
- params: '[]',
- command: '',
- capture_output: false,
- child_links: []
- });
- var FsModel = ModelModule($);
- $.extend(FsModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'fs',
- workflow: 0,
- deletes: '[]',
- mkdirs: '[]',
- moves: '[]',
- chmods: '[]',
- touchzs: '[]',
- child_links: []
- });
- var EmailModel = ModelModule($);
- $.extend(EmailModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'email',
- workflow: 0,
- to: '',
- cc: '',
- subject: '',
- body: '',
- child_links: []
- });
- var SubWorkflowModel = ModelModule($);
- $.extend(SubWorkflowModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'subworkflow',
- workflow: 0,
- sub_workflow: 0,
- propagate_configuration: true,
- job_properties: '[]',
- child_links: []
- });
- var GenericModel = ModelModule($);
- $.extend(GenericModel.prototype, {
- id: 0,
- name: '',
- description: '',
- node_type: 'generic',
- workflow: 0,
- xml: '',
- child_links: []
- });
- function nodeModelChooser(node_type) {
- switch(node_type) {
- case 'mapreduce':
- return MapReduceModel;
- case 'streaming':
- return StreamingModel;
- case 'java':
- return JavaModel;
- case 'pig':
- return PigModel;
- case 'hive':
- return HiveModel;
- case 'sqoop':
- return SqoopModel;
- case 'shell':
- return ShellModel;
- case 'ssh':
- return SshModel;
- case 'distcp':
- return DistCPModel;
- case 'fs':
- return FsModel;
- case 'email':
- return EmailModel;
- case 'subworkflow':
- return SubWorkflowModel;
- case 'generic':
- return GenericModel;
- case 'fork':
- return ForkModel;
- case 'decision':
- return DecisionModel;
- default:
- return NodeModel;
- }
- }
- var IdGeneratorTable = {
- mapreduce: new IdGenerator({prefix: 'mapreduce'}),
- streaming: new IdGenerator({prefix: 'streaming'}),
- java: new IdGenerator({prefix: 'java'}),
- pig: new IdGenerator({prefix: 'pig'}),
- hive: new IdGenerator({prefix: 'hive'}),
- sqoop: new IdGenerator({prefix: 'sqoop'}),
- shell: new IdGenerator({prefix: 'shell'}),
- ssh: new IdGenerator({prefix: 'ssh'}),
- distcp: new IdGenerator({prefix: 'distcp'}),
- fs: new IdGenerator({prefix: 'fs'}),
- email: new IdGenerator({prefix: 'email'}),
- subworkflow: new IdGenerator({prefix: 'subworkflow'}),
- generic: new IdGenerator({prefix: 'generic'}),
- fork: new IdGenerator({prefix: 'fork'}),
- decision: new IdGenerator({prefix: 'decision'}),
- join: new IdGenerator({prefix: 'join'}),
- kill: new IdGenerator({prefix: 'kill'})
- };
- var NodeModule = function($, IdGeneratorTable) {
- var META_LINKS = ['related', 'default', 'error'];
- var linkTypeChooser = function(parent, child) {
- if (child.node_type() == 'kill') {
- return 'error';
- }
- switch(parent.node_type()) {
- case 'start':
- return (child.node_type() == 'end') ? 'related' : 'to';
- case 'fork':
- return (child.node_type() == 'join') ? 'related' : 'start';
- case 'decision':
- return 'start';
- case 'join':
- return 'to';
- default:
- return 'ok';
- };
- };
- var module = function(workflow, model, registry) {
- var self = this;
- self.map(model);
- self.links = ko.computed(function() {
- var links = self.child_links().filter(function(element, index, arr) {
- return $.inArray(element.name(), META_LINKS) == -1;
- });
- return links;
- });
- self.meta_links = ko.computed(function() {
- var links = self.child_links().filter(function(element, index, arr) {
- return $.inArray(element.name(), META_LINKS) != -1;
- });
- return links;
- });
- self._workflow = workflow;
- self.registry = registry;
- self.children = ko.observableArray([]);
- self.model = model;
- var errors = {};
- for(var key in model) {
- switch(key) {
- case 'child_links':
- case 'node_ptr':
- case 'initialize':
- case 'toString':
- break;
- default:
- errors[key] = [];
- break;
- }
- }
- self.errors = ko.mapping.fromJS(errors);
- self.edit_template = model.node_type + 'EditTemplate';
- switch(model.node_type) {
- case 'start':
- self.view_template = ko.observable('startTemplate');
- break;
- case 'kill':
- case 'end':
- self.view_template = ko.observable('emptyTemplate');
- break;
- case 'fork':
- self.view_template = ko.observable('forkTemplate');
- break;
- case 'decision':
- self.view_template = ko.observable('decisionTemplate');
- break;
- case 'join':
- self.view_template = ko.observable('joinTemplate');
- break;
- default:
- self.view_template = ko.observable('nodeTemplate');
- break;
- }
- // Data manipulation
- if ('job_properties' in model) {
- self.addProp = function() {
- var prop = { name: ko.observable(""), value: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.job_properties.valueHasMutated();
- });
- prop.value.subscribe(function(value) {
- self.job_properties.valueHasMutated();
- });
- self.job_properties.push(prop);
- // $(".propKey:last").each(addAutoComplete);
- };
- self.removeProp = function(val) {
- self.job_properties.remove(val);
- };
- }
- if ('files' in model) {
- self.addFile = function() {
- var prop = { name: ko.observable(""), dummy: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.files.valueHasMutated();
- });
- prop.dummy.subscribe(function(value) {
- self.files.valueHasMutated();
- });
- self.files.push(prop);
- };
- self.removeFile = function(val) {
- self.files.remove(val);
- };
- }
- if ('archives' in model) {
- self.addArchive = function() {
- var prop = { name: ko.observable(""), dummy: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.archives.valueHasMutated();
- });
- prop.dummy.subscribe(function(value) {
- self.archives.valueHasMutated();
- });
- self.archives.push(prop);
- };
- self.removeArchive = function(val) {
- self.archives.remove(val);
- };
- }
- if ('params' in model) {
- self.addParam = function() {
- var prop = { value: ko.observable(""), type: ko.observable("param") };
- prop.value.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- self.params.push(prop);
- };
- self.addArgument = function() {
- var prop = { value: ko.observable(""), type: ko.observable("argument") };
- prop.value.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- self.params.push(prop);
- };
- self.addArg = function() {
- var prop = { value: ko.observable(""), type: ko.observable("arg") };
- prop.value.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- self.params.push(prop);
- };
- self.addEnvVar = function() {
- var prop = { value: ko.observable(""), type: ko.observable("env-var") };
- prop.value.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.params.valueHasMutated();
- });
- self.params.push(prop);
- };
- self.removeParam = function(val) {
- self.params.remove(val);
- };
- }
- if ('prepares' in model) {
- self.addPrepareDelete = function() {
- var prop = { value: ko.observable(""), type: ko.observable("delete") };
- prop.value.subscribe(function(value) {
- self.prepares.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.prepares.valueHasMutated();
- });
- self.prepares.push(prop);
- };
- self.addPrepareMkdir = function() {
- var prop = { value: ko.observable(""), type: ko.observable("mkdir") };
- prop.value.subscribe(function(value) {
- self.prepares.valueHasMutated();
- });
- prop.type.subscribe(function(value) {
- self.prepares.valueHasMutated();
- });
- self.prepares.push(prop);
- };
- self.removePrepare = function(val) {
- self.prepares.remove(val);
- };
- }
- if ('deletes' in model) {
- self.addDelete = function() {
- var prop = { name: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.deletes.valueHasMutated();
- });
- self.deletes.push(prop);
- };
- self.removeDelete = function(val) {
- self.deletes.remove(val);
- };
- }
- if ('mkdirs' in model) {
- self.addMkdir = function() {
- var prop = { name: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.mkdirs.valueHasMutated();
- });
- self.mkdirs.push(prop);
- };
- self.removeMkdir = function(val) {
- self.mkdirs.remove(val);
- };
- }
- if ('moves' in model) {
- self.addMove = function() {
- var prop = { source: ko.observable(""), destination: ko.observable("") };
- prop.source.subscribe(function(value) {
- self.moves.valueHasMutated();
- });
- prop.destination.subscribe(function(value) {
- self.moves.valueHasMutated();
- });
- self.moves.push(prop);
- };
- self.removeMove = function(val) {
- self.moves.remove(val);
- };
- }
- if ('chmods' in model) {
- self.addChmod = function() {
- var prop = { path: ko.observable(""), permissions: ko.observable(""), recursive: ko.observable("") };
- prop.path.subscribe(function(value) {
- self.chmods.valueHasMutated();
- });
- prop.permissions.subscribe(function(value) {
- self.chmods.valueHasMutated();
- });
- prop.recursive.subscribe(function(value) {
- self.chmods.valueHasMutated();
- });
- self.chmods.push(prop);
- };
- self.removeChmod = function(val) {
- self.chmods.remove(val);
- };
- }
- if ('touchzs' in model) {
- self.addTouchz = function() {
- var prop = { name: ko.observable("") };
- prop.name.subscribe(function(value) {
- self.touchzs.valueHasMutated();
- });
- self.touchzs.push(prop);
- };
- self.removeTouchz = function(val) {
- self.touchzs.remove(val);
- };
- }
- self.initialize.apply(self, arguments);
- return self;
- };
- $.extend(module.prototype, {
- // Data.
- children: null,
- model: null,
- // Normal stuff
- /**
- * Called when creating a new node
- */
- initialize: function(workflow, model, registry) {},
- toString: function() {
- return '';
- },
- /**
- * Fetches registry
- */
- getRegistry: function() {
- return registry;
- },
- /**
- * Maps a model to self
- * Called when creating a new node before any thing else
- */
- map: function(model) {
- var self = this;
- // @see http://knockoutjs.com/documentation/plugins-mapping.html
- var mapping = ko.mapping.fromJS(model, {
- ignore: ['initialize', 'toString'],
- job_properties: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- files: {
- create: function(options) {
- return map_params(options, function() {});
- },
- update: function(options) {
- return map_params(options, function() {});
- }
- },
- archives: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.archives.valueHasMutated();
- });
- mapping.dummy.subscribe(function(value) {
- parent.archives.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.archives.valueHasMutated();
- });
- mapping.dummy.subscribe(function(value) {
- parent.archives.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- params: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.value.subscribe(function(value) {
- parent.params.valueHasMutated();
- });
- mapping.type.subscribe(function(value) {
- parent.params.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.value.subscribe(function(value) {
- parent.params.valueHasMutated();
- });
- mapping.type.subscribe(function(value) {
- parent.params.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- prepares: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.value.subscribe(function(value) {
- parent.prepares.valueHasMutated();
- });
- mapping.type.subscribe(function(value) {
- parent.prepares.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.value.subscribe(function(value) {
- parent.prepares.valueHasMutated();
- });
- mapping.type.subscribe(function(value) {
- parent.prepares.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- deletes: {
- create: function(options) {
- return map_params(options, function() {});
- },
- update: function(options) {
- return map_params(options, function() {});
- }
- },
- mkdirs: {
- create: function(options) {
- return map_params(options, function() {});
- },
- update: function(options) {
- return map_params(options, function() {});
- }
- },
- moves: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.source.subscribe(function(value) {
- parent.moves.valueHasMutated();
- });
- mapping.destination.subscribe(function(value) {
- parent.moves.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.source.subscribe(function(value) {
- parent.moves.valueHasMutated();
- });
- mapping.destination.subscribe(function(value) {
- parent.moves.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- chmods: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.path.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- mapping.permissions.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- mapping.recursive.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.path.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- mapping.permissions.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- mapping.recursive.subscribe(function(value) {
- parent.chmods.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- touchzs: {
- create: function(options) {
- return map_params(options, function() {});
- },
- update: function(options) {
- return map_params(options, function() {});
- }
- },
- });
- $.extend(self, mapping);
- $.each(mapping, function(key, value) {
- var key = key;
- if (ko.isObservable(self[key])) {
- self[key].subscribe(function(value) {
- model[key] = ko.mapping.toJS(value);
- });
- }
- });
- $.each(self.child_links(), function(index, link) {
- var $index = index;
- link.comment.subscribe(function(value) {
- self.model.child_links[$index].comment = value;
- });
- link.child.subscribe(function(value) {
- self.model.child_links[$index].child = value;
- });
- });
- },
- validate: function( ) {
- var self = this;
- var options = {};
- data = $.extend(true, {}, self.model);
- var success = false;
- var request = $.extend({
- url: '/oozie/workflows/' + self._workflow.id() + '/actions/' + self.node_type() + '/validate',
- type: 'POST',
- data: { node: JSON.stringify(data) },
- success: function(data) {
- ko.mapping.fromJS(data.data, self.errors);
- success = data.status == 0;
- },
- async: false,
- }, options);
- $.ajax(request);
- return success;
- },
- // Hierarchy manipulation.
- /**
- * Append node to self
- * Does not support multiple children.
- * Ensures single child.
- * Ensures no cycles.
- * 1. Finds all children and attaches them to node (cleans node first).
- * 2. Remove all children from self.
- * 3. Attach node to self.
- */
- append: function(node) {
- var self = this;
- // Not fork nor decision nor self
- if ($.inArray(self.node_type(), ['fork', 'decision']) == -1 && node.id() != self.id() && !self.isChild(node)) {
- node.removeAllChildren();
- $.each(self.links(), function(index, link) {
- node.addChild(self.registry.get(link.child()));
- });
- self.removeAllChildren();
- self.addChild(node);
- }
- },
- /**
- * Find all parents of current node
- */
- findParents: function() {
- var self = this;
- var parents = [];
- $.each(self.registry.nodes, function(id, node) {
- $.each(node.links(), function(index, link) {
- if (link.child() == self.id()) {
- parents.push(node);
- }
- });
- });
- return parents;
- },
- /**
- * Find all children of current node
- */
- findChildren: function() {
- var self = this;
- var children = [];
- $.each(self.links(), function(index, link) {
- children.push(self.registry.get(link.child()));
- });
- return children;
- },
- /**
- * Detach current node from the graph
- * 1. Takes children of self node, removes them from self node, and adds them to each parent of self node.
- * 2. The self node is then removed from every parent.
- * 3. Does not support multiple children since we do not automatically fork.
- */
- detach: function() {
- var self = this;
- $.each(self.findParents(), function(index, parent) {
- $.each(self.links(), function(index, link) {
- var node = self.registry.get(link.child());
- parent.replaceChild(self, node);
- });
- });
- $(self).trigger('detached');
- self.removeAllChildren();
- },
- /**
- * Add child
- * Update child links for this node.
- */
- addChild: function(node) {
- var self = this;
- var link = {
- parent: ko.observable(self.id()),
- child: ko.observable(node.id()),
- name: ko.observable(linkTypeChooser(self, node)),
- comment: ko.observable('')
- };
- self.child_links.unshift(link);
- },
- /**
- * Remove child node
- * 1. Find child node link
- * 2. Remove child node link
- */
- removeChild: function(node) {
- var self = this;
- var spliceIndex = -1;
- $.each(self.child_links(), function(index, link) {
- if (link.child() == node.id()) {
- spliceIndex = index;
- }
- });
- if (spliceIndex > -1) {
- self.child_links.splice(spliceIndex, 1);
- }
- return spliceIndex != -1;
- },
- /**
- * Remove all children
- * Removes all children except for related, default, and error links
- * Note: we hold on to related, default, and error links because
- * we have to.
- */
- removeAllChildren: function() {
- var self = this;
- var keep_links = [];
- $.each(self.child_links(), function(index, link) {
- if ($.inArray(link.name(), META_LINKS) > -1) {
- keep_links.push(link);
- }
- });
- self.child_links.removeAll();
- $.each(keep_links, function(index, link) {
- self.child_links.push(link);
- });
- },
- /**
- * Replace child node with another node in the following way:
- * 1. Find child index
- * 2. Remove child index
- * 3. Remove and remember every element after child
- * 4. Add replacement node
- * 5. Add every child that was remembered
- */
- replaceChild: function(child, replacement) {
- var self = this;
- var index = -1;
- $.each(self.child_links(), function(i, link) {
- if (link.child() == child.id()) {
- index = i;
- }
- });
- if (index > -1) {
- self.child_links.splice(index, 1);
- var links = self.child_links.splice(index);
- var link = {
- parent: ko.observable(self.id()),
- child: ko.observable(replacement.id()),
- name: ko.observable(linkTypeChooser(self, replacement)),
- comment: ko.observable(''),
- };
- self.child_links.push(link);
- $.each(links, function(index, link) {
- self.child_links.push(link);
- });
- }
- return index != -1;
- },
- isChild: function(node) {
- var self = this;
- var res = false;
- $.each(self.links(), function(index, link) {
- if (link.child() == node.id()) {
- res = true;
- }
- });
- return res;
- },
- erase: function() {
- var self = this;
- self.registry.remove(self.id());
- }
- });
- return module;
- };
- var Node = NodeModule($, IdGeneratorTable);
- var StartNode = NodeModule($, IdGeneratorTable);
- $.extend(StartNode.prototype, Node.prototype, {
- /**
- * Same as addChild for nodes, except if we reach the end,
- * we add the end node to our child_links in the form of a
- * 'related' link and 'start' link.
- * We should always have a start link in an empty workflow!
- */
- replaceChild: function(child, replacement) {
- var self = this;
- var index = -1;
- $.each(self.child_links(), function(i, link) {
- if (link.child() == child.id()) {
- index = i;
- }
- });
- if (index > -1) {
- self.child_links.splice(index, 1);
- var links = self.child_links.splice(index);
- var link = {
- parent: ko.observable(self.id()),
- child: ko.observable(replacement.id()),
- name: ko.observable('to'),
- comment: ko.observable(''),
- };
- self.child_links.push(link);
- $.each(links, function(index, link) {
- self.child_links.push(link);
- });
- }
- return index != -1;
- },
- });
- var ForkNode = NodeModule($, IdGeneratorTable);
- $.extend(ForkNode.prototype, Node.prototype, {
- // Join nodes are connected through 'related' links
- join: function() {
- var self = this;
- var join = null;
- $.each(self.child_links(), function(index, link) {
- if (link.name() == 'related') {
- join = self.registry.get(link.child());
- }
- });
- return join;
- },
- /**
- * Append a node to the current fork
- * Also adds join node to node.
- * When adding the join node, append will remove all the children from the join!
- * We need to make sure the join remembers its children.
- * NOTE: Cannot append a fork! Use addChild or replaceChild instead!
- */
- append: function(node) {
- var self = this;
- if (node.node_type() != 'fork') {
- var join = self.join();
- if (join.id() != node.id()) {
- var children = join.findChildren();
- self.addChild(node);
- node.append(join);
- // remember children
- $.each(children, function(index, child) {
- join.addChild(child);
- });
- }
- }
- },
- /**
- * Replace child node with another node
- * 1. Remove child if the replacement is related join
- * 2. Apply Node.replaceChild for all other causes
- * NOTE: can assume the only join this fork will ever see is the related join!
- * This is because the related will always be the closest join for children.
- * Also, the operations allowed that would make this all possible: detach, append;
- * are inherently going to remove any other joins if they exist.
- * This is also easier given nodes are contained within Forks!
- */
- replaceChild: function(child, replacement) {
- var self = this;
- var ret = true;
- if (self.join().id() == replacement.id()) {
- ret = self.removeChild(child);
- } else {
- ret = Node.prototype.replaceChild.apply(self, arguments);
- }
- var links = self.links().filter(function(element, index, arr) {
- return self.registry.get(element.child()).node_type() != 'join';
- });
- if (links.length < 2) {
- self.detach();
- self.join().detach();
- self.erase();
- self.join().erase();
- }
- return ret;
- },
- /**
- * Converts fork node into decision node in the following way:
- * 1. Copies contents of current fork node into a new decision node
- * 2. Detach fork node
- * 3. Erase fork node
- * 4. Append decision node to parent
- */
- convertToDecision: function() {
- var self = this;
- var join = self.join();
- var end = null;
- var child = join.findChildren()[0];
- if (child.findParents().length == 1) {
- end = child;
- }
- // Attaches child of join to parents of join
- join.detach();
- var decision_model = new DecisionModel({
- id: IdGeneratorTable['decision'].nextId(),
- name: self.name(),
- description: self.description(),
- node_type: 'decision',
- workflow: self.workflow(),
- child_links: self.model.child_links
- });
- var default_link = {
- parent: decision_model.id,
- child: self._workflow.end(),
- name: 'default',
- comment: ''
- };
- decision_model.child_links.push(default_link);
- $.each(decision_model.child_links, function(index, link) {
- link.parent = decision_model.id;
- });
- var decision_node = new DecisionNode(self._workflow, decision_model, self.registry);
- if (end) {
- decision_node.addEnd(end);
- }
- var parents = self.findParents();
- decision_node.removeChild(join);
- join.erase();
- self.erase();
- self.registry.add(decision_node.id(), decision_node);
- $.each(parents, function(index, parent) {
- parent.replaceChild(self, decision_node);
- });
- }
- });
- var DecisionNode = NodeModule($, IdGeneratorTable);
- $.extend(DecisionNode.prototype, ForkNode.prototype, {
- initialize: function(workflow, model, registry) {
- var self = this;
- var registry = registry;
- var end = null;
- $.each(self.child_links(), function(index, link) {
- if (link.name() == 'related') {
- $(registry).bind('registry:add', function(e) {
- var end = self.end();
- if (end) {
- self.removeEnd();
- self.addEnd(end);
- $(registry).unbind(e);
- }
- });
- }
- });
- },
- // Only a single end allowed for now!
- // Finds end for branch
- findEnd: function( ) {
- var self = this;
- var end = self.end();
- if (!end) {
- return self._findEnd( self, 0 );
- }
- return end;
- },
- _findEnd: function( node, count ) {
- var self = this;
- var end = null;
- if (node.findParents().length > 1 && --count == 0) {
- return node;
- }
- if (node.node_type() == 'decision' || node.node_type() == 'fork') {
- count++;
- }
- $.each(node.findChildren(), function(index, node) {
- if (end == null) {
- end = self._findEnd(node, count);
- }
- });
- return end;
- },
- end: function() {
- var self = this;
- var end = null;
- $.each(self.child_links(), function(index, link) {
- if (link.name() == 'related') {
- end = self.registry.get(link.child());
- }
- });
- return end;
- },
- addEnd: function(node) {
- var self = this;
- self.child_links.push({
- parent: ko.observable(self.id()),
- child: ko.observable(node.id()),
- name: ko.observable('related'),
- comment: ko.observable(''),
- });
- $(node).one('detached', function(e) {
- var end = self.end();
- if (end.links().length == 1) {
- self.removeEnd();
- var new_end = self.findEnd();
- self.addEnd(new_end);
- } else {
- // Should never hit this case.
- self.removeEnd();
- }
- });
- },
- removeEnd: function() {
- var self = this;
- var spliceIndex = -1;
- $.each(self.child_links(), function(index, link) {
- if (link.name() == 'related') {
- spliceIndex = index;
- }
- });
- if (spliceIndex > -1) {
- var end = registry.get(self.child_links()[spliceIndex].child());
- self.child_links.splice(spliceIndex, 1);
- return true;
- }
- return false;
- },
- isChild: function(node) {
- var self = this;
- return self._isChild(node, self, 0);
- },
- _isChild: function( test_child, node, count ) {
- var self = this;
- var result = node.id() == test_child.id();
- if (node.findParents().length > 1 && --count == 0) {
- return result;
- }
- if (node.node_type() == 'decision' || node.node_type() == 'fork') {
- count++;
- }
- $.each(node.findChildren(), function(index, node) {
- if (!result) {
- result = self._isChild(test_child, node, count);
- }
- });
- return result;
- },
- /**
- * Append a node to the current decision
- * Also appends end node to node.
- * NOTE: Cannot append a decision! Use addChild or replaceChild instead!
- */
- append: function(node) {
- var self = this;
- var end = self.findEnd();
- if (end.id() == node.id()) {
- return false;
- }
- self.addChild(node);
- node.append(end);
- return true;
- },
- /**
- * Replace child node with another node
- * 1. Remove child if the replacement is end
- * 2. Apply Node.replaceChild for all other causes
- */
- replaceChild: function(child, replacement) {
- var self = this;
- var ret = true;
- var end = self.findEnd();
- if (end && end.id() == replacement.id()) {
- ret = self.removeChild(child);
- } else {
- ret = Node.prototype.replaceChild.apply(self, arguments);
- }
- if (self.links().length < 2) {
- self.detach();
- self.erase();
- }
- return ret;
- }
- });
- /**
- * Workflow module
- */
- var WorkflowModule = function($, NodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable) {
- var module = function(options) {
- var self = this;
- // @see http://knockoutjs.com/documentation/plugins-mapping.html
- var mapping = ko.mapping.fromJS(options.model, {
- ignore: ['initialize', 'toString'],
- job_properties: {
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.job_properties.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- }
- },
- parameters: {
- // Will receive individual objects to subscribe.
- // Containing array is mapped automagically
- create: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.parameters.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.parameters.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- update: function(options) {
- var parent = options.parent;
- var subscribe = function(mapping) {
- mapping.name.subscribe(function(value) {
- parent.parameters.valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- parent.parameters.valueHasMutated();
- });
- };
- return map_params(options, subscribe);
- },
- }
- });
- $.extend(self, mapping);
- $.each(mapping['__ko_mapping__'].mappedProperties, function(key, value) {
- var key = key;
- self[key].subscribe(function(value) {
- self.is_dirty( true );
- self.model[key] = ko.mapping.toJS(value);
- });
- });
- self.model = options.model;
- self.registry = options.registry;
- self.options = options;
- self.el = $('#workflow');
- self.nodes = ko.observableArray([]);
- self.kill = null;
- self.is_dirty = ko.observable( false );
- self.url = ko.computed(function() {
- return '/oozie/workflows/' + self.id()
- });
- // Events
- self.el.on('workflow:rebuild', function() {
- self.rebuild();
- });
- self.el.on('workflow:events:load', function() {
- self.dragAndDropEvents();
- });
- self.el.on('workflow:droppables:load', function() {
- self.droppables();
- });
- self.el.on('workflow:draggables:load', function() {
- self.draggables();
- });
- self.dragAndDropEvents();
- self.el.trigger('workflow:events:loaded');
- module.prototype.initialize.apply(self, arguments);
- return self;
- };
- $.extend(module.prototype, {
- // Normal stuff
- initialize: function(options) {
- var self = this;
- if ('data' in options) {
- // Initialize nodes
- if (options.data.nodes) {
- self.registry.clear();
- $.each(options.data.nodes, function(index, node) {
- var NodeModel = NodeModelChooser(node.node_type);
- var model = new NodeModel(node);
- var temp = null;
- switch(node.node_type) {
- case 'start':
- temp = new StartNode(self, model, self.registry);
- break;
- case 'fork':
- temp = new ForkNode(self, model, self.registry);
- break;
- case 'decision':
- temp = new DecisionNode(self, model, self.registry);
- break;
- case 'kill':
- temp = self.kill = new Node(self, model, self.registry);
- break;
- default:
- temp = new Node(self, model, self.registry);
- break;
- }
- self.registry.add(temp.id(), temp);
- });
- }
- // Update data
- $.each(options.data, function (key, value) {
- if (key in self) {
- switch(key) {
- case 'job_properties':
- case 'parameters':
- // These may be serialized JSON data since that is how they are stored
- self[key].removeAll();
- var arr = $.parseJSON(value) || value;
- $.each(arr, function(index, obj) {
- var mapping = ko.mapping.fromJS(obj);
- mapping.name.subscribe(function(value) {
- self[key].valueHasMutated();
- });
- mapping.value.subscribe(function(value) {
- self[key].valueHasMutated();
- });
- self[key].push(mapping);
- });
- break;
- case 'nodes':
- break;
- default:
- self[key](value);
- break;
- }
- }
- });
- self.is_dirty( false );
- }
- if (!self.kill) {
- var kill_json = {
- "description": "",
- "workflow": self.id(),
- "child_links": [],
- "node_type": "kill",
- "message": "Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]",
- "name": "kill",
- "id": IdGeneratorTable['kill'].nextId()
- };
- var NodeModel = NodeModelChooser(kill_json.node_type);
- var model = new NodeModel(kill_json);
- self.kill = new Node(self, model, self.registry);
- self.registry.add(self.kill.id(), self.kill);
- }
- },
- toString: function() {
- return '';
- var s = '[';
- $.each(self.registry.nodes, function(key, node) {
- s += node.model.toString() + ",\n";
- });
- return s + ']';
- },
- // Data manipulation
- save: function( options ) {},
- load: function() {
- var self = this;
- $.getJSON(self.url(), function(data) {
- if (data.status == 0) {
- self.reload(data.data);
- } else {
- $.jHueNotify.error("${ _('Received invalid response from server: ') }" + JSON.stringify(data));
- }
- });
- },
- reload: function(data) {
- var self = this;
- // Clear all children
- $.each(self.registry.nodes, function(index, node) {
- node.children.removeAll();
- });
- self.nodes.removeAll();
- self.initialize({data: data});
- self.rebuild();
- self.el.trigger('workflow:loaded');
- },
- addParameter: function(data, event) {
- var self = this;
- var prop = { name: ko.observable(""), value: ko.observable("") };
- // force bubble up to containing observable array.
- prop.name.subscribe(function(){
- self.parameters.valueHasMutated();
- });
- prop.value.subscribe(function(){
- self.parameters.valueHasMutated();
- });
- self.parameters.push(prop);
- },
- removeParameter: function(data, event) {
- var self = this;
- self.parameters.remove(data);
- },
- addJobProperty: function(data, event) {
- var self = this;
- var prop = { name: ko.observable(""), value: ko.observable("") };
- // force bubble up to containing observable array.
- prop.name.subscribe(function(){
- self.parameters.valueHasMutated();
- });
- prop.value.subscribe(function(){
- self.parameters.valueHasMutated();
- });
- self.job_properties.push(prop);
- },
- removeJobProperty: function(data, event) {
- var self = this;
- self.job_properties.remove(data);
- },
- // Workflow UI
- // Function to build nodes... recursively.
- build: function() {
- var self = this;
- var maximum = 50;
- var count = 0;
- var methodChooser = function(node, collection, skip_parents_check) {
- if (count++ >= maximum) {
- if (window.error) {
- console.error('Hit maximum number of node recursion: ' + maximum);
- }
- return null;
- }
- if (!node) {
- return node;
- }
- var parents = node.findParents();
- // Found end of decision node or found join!
- if (parents.length > 1 && !skip_parents_check) {
- return node;
- }
- switch(node.node_type()) {
- case 'start':
- case 'end':
- case 'kill':
- case 'fork':
- case 'decision':
- case 'join':
- return control(node, collection);
- default:
- return normal(node, collection);
- }
- };
- var normal = function(node, collection) {
- collection.push(node);
- var retNode = null;
- $.each(node.links(), function(index, link) {
- var next_node = self.registry.get(link.child());
- retNode = methodChooser(next_node, collection, false, true);
- });
- return retNode;
- };
- var control = function(node, collection, skip_parents_check) {
- switch(node.node_type()) {
- case 'start':
- case 'end':
- case 'kill':
- case 'join':
- return normal(node, collection, false, true);
- case 'fork':
- collection.push(node);
- // Wait for join.
- // Iterate through all children and add them to child collection.
- var join = null;
- $.each(node.links(), function(index, link) {
- var next_node = self.registry.get(link.child());
- var collection = ko.observableArray([]);
- node.children.push(collection);
- join = methodChooser(next_node, collection, false, true);
- });
- // Add join to collection, then find its single child.
- return methodChooser(join, collection, true, true);
- case 'decision':
- collection.push(node);
- // Waits for end, then runs through children of end node
- var end = null;
- $.each(node.links(), function(index, link) {
- var next_node = self.registry.get(link.child());
- var collection = ko.observableArray([]);
- node.children.push(collection);
- end = methodChooser(next_node, collection, true, true);
- });
- // Add end
- if (node.end() && end.id() == node.end().id()) {
- return methodChooser(end, collection, true, true);
- } else {
- return end;
- }
- default:
- // Should never get here.
- return node;
- }
- };
- methodChooser(self.registry.get(self.start()), self.nodes, false, true);
- },
- rebuild: function() {
- var self = this;
- // Clear all children
- $.each(self.registry.nodes, function(index, node) {
- node.children.removeAll();
- });
- self.nodes.removeAll();
- // Rebuild
- self.build();
- self.draggables();
- self.droppables();
- self.el.trigger('workflow:rebuilt');
- },
- draggables: function() {
- var self = this;
- self.el.find('.node-action').each(function(index, el) {
- if (!$(el).hasClass('ui-draggable')) {
- $(el).draggable({
- containment: [ self.el.offset().left - 10, self.el.offset().top - 10,
- self.el.offset().left + self.el.outerWidth(), self.el.offset().top + self.el.outerHeight() ],
- handle: '.node',
- refreshPositions: true,
- revert: true
- });
- }
- });
- },
- droppables: function() {
- var self = this;
- self.el.find('.node-link').each(function(index, el) {
- $(el).droppable({
- 'hoverClass': 'node-link-hover',
- 'greedy': true,
- 'accept': '.node-action',
- 'tolerance': 'pointer'
- });
- });
- self.el.find('.node-decision-end').each(function(index, el) {
- $(el).droppable({
- 'hoverClass': 'node-link-hover',
- 'greedy': true,
- 'accept': '.node-action',
- 'tolerance': 'pointer'
- });
- });
- self.el.find('.node-fork .action').each(function(index, el) {
- $(el).droppable({
- 'hoverClass': 'node-fork-hover',
- 'greedy': true,
- 'accept': '.node-action',
- 'tolerance': 'pointer'
- });
- });
- self.el.find('.node-decision .action').each(function(index, el) {
- $(el).droppable({
- 'hoverClass': 'node-fork-hover',
- 'greedy': true,
- 'accept': '.node-action',
- 'tolerance': 'pointer'
- });
- });
- self.el.find('.node-action .action').each(function(index, el) {
- $(el).droppable({
- 'hoverClass': 'node-action-hover',
- 'greedy': true,
- 'accept': '.node-action',
- 'tolerance': 'pointer'
- });
- });
- },
- dragAndDropEvents: function() {
- var self = this;
- // Build event delegations.
- // Drop on node link
- self.el.on('drop', '.node-link', function(e, ui) {
- // draggable should be a node.
- // droppable should be a link.
- var draggable = ko.contextFor(ui.draggable[0]).$data;
- var droppable = ko.contextFor(this).$data;
- // If newParent is fork, prepend to child instead.
- // This will make it so that we can drop and drop to the top of a node list within a fork.
- var newParent = self.registry.get(droppable.parent());
- if (newParent.id() != draggable.id()) {
- if (newParent.isChild(draggable)) {
- if (draggable.findParents().length > 1) {
- // End of decision tree is being dragged to the bottom of a branch
- draggable.detach();
- newParent.append(draggable);
- workflow.is_dirty( true );
- self.rebuild();
- }
- } else {
- switch(newParent.node_type()) {
- case 'fork':
- case 'decision':
- draggable.detach();
- var child = self.registry.get(droppable.child());
- newParent.replaceChild(child, draggable);
- draggable.addChild(child);
- break;
- case 'join':
- // Join may disappear when we detach...
- // Remember its children and append to child.
- var parents = newParent.findParents();
- draggable.detach();
- if (newParent.findParents().length < 2) {
- $.each(parents, function(index, parent) {
- parent.append(draggable);
- });
- } else {
- newParent.append(draggable);
- }
- break;
- default:
- draggable.detach();
- newParent.append(draggable);
- break;
- }
- workflow.is_dirty( true );
- self.rebuild();
- }
- }
- // Prevent bubbling events
- return false;
- });
- // Drop on fork
- self.el.on('drop', '.node-fork', function(e, ui) {
- // draggable should be a node.
- // droppable should be a fork.
- var draggable = ko.contextFor(ui.draggable[0]).$data;
- var droppable = ko.contextFor(this).$data;
- if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
- draggable.detach();
- droppable.append(draggable);
- self.rebuild();
- }
- // Prevent bubbling events
- return false;
- });
- // Drop on decision
- self.el.on('drop', '.node-decision', function(e, ui) {
- // draggable should be a node.
- // droppable should be a fork.
- var draggable = ko.contextFor(ui.draggable[0]).$data;
- var droppable = ko.contextFor(this).$data;
- if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
- draggable.detach();
- droppable.append(draggable);
- self.rebuild();
- }
- // Prevent bubbling events
- return false;
- });
- // Drop on decision end link
- self.el.on('drop', '.node-decision-end', function(e, ui) {
- // draggable should be a node.
- // droppable should be a decision end link... which should give us the decision node.
- var draggable = ko.contextFor(ui.draggable[0]).$data;
- var droppable = ko.contextFor(this).$data;
- var end = droppable.findEnd(droppable);
- if (end.id() != draggable.id()) {
- draggable.detach();
- $.each(end.findParents(), function(index, parent) {
- if (droppable.isChild(parent)) {
- parent.append(draggable);
- }
- });
- droppable.removeEnd();
- droppable.addEnd(draggable);
- self.rebuild();
- }
- // Prevent bubbling events
- return false;
- });
- // Drop on action
- self.el.on('drop', '.node-action', function(e, ui) {
- // draggable should be a node.
- // droppable should be a node.
- var draggable = ko.contextFor(ui.draggable[0]).$data;
- var droppable = ko.contextFor(this).$data;
- // Create a fork and join programatically.
- var newParents = droppable.findParents();
- // skip forking beneathe a decision node
- if (droppable.id() != draggable.id() && newParents.length == 1 && draggable.findParents().length == 1) {
- var ForkModel = NodeModelChooser('fork');
- var JoinModel = NodeModelChooser('join');
- var fork = new ForkModel({
- id: IdGeneratorTable['fork'].nextId(),
- description: "",
- workflow: self.id,
- node_type: "fork",
- child_links: []
- });
- var forkNode = new ForkNode(self, fork, self.registry);
- var join = new JoinModel({
- id: IdGeneratorTable['join'].nextId(),
- description: "",
- workflow: self.id,
- node_type: "join",
- child_links: []
- });
- var joinNode = new Node(self, join, self.registry);
- self.registry.add(forkNode.id(), forkNode);
- self.registry.add(joinNode.id(), joinNode);
- forkNode.addChild(joinNode);
- // Handles fork creation.
- $.each(newParents, function(index, parent) {
- parent.replaceChild(droppable, forkNode);
- });
- draggable.detach();
- forkNode.append(draggable);
- forkNode.append(droppable);
- self.rebuild();
- }
- // Prevent bubbling events.
- return false;
- });
- }
- });
- return module;
- };
- var Workflow = WorkflowModule($, nodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable);
|