diff --git a/arches/app/media/js/models/card.js b/arches/app/media/js/models/card.js index 1056c132fcc..06e02a7c23e 100644 --- a/arches/app/media/js/models/card.js +++ b/arches/app/media/js/models/card.js @@ -47,7 +47,6 @@ define([ this.disabled = ko.observable(); this.component_id = ko.observable(); this.constraints = ko.observableArray(); - this.uniqueConstraints = ko.observableArray(); this.set('cards', this.cards); this.set('nodes', this.nodes); @@ -114,13 +113,31 @@ define([ this._card = ko.observable('{}'); this.dirty = ko.computed(function() { - return JSON.stringify(_.extend(JSON.parse(self._card()), self.toJSON())) !== self._card(); + return JSON.stringify( + _.extend( + JSON.parse(self._card()), + self.toJSON()) + ) !== self._card(); }); this.isContainer = ko.computed(function() { return !!self.get('cards')().length; }); + this.setConstraints = function(arr) { + var self = this; + arr.forEach(function(constraint){ + var constraintViewModel = new CardConstraintsViewModel({ + constraint: koMapping.fromJS(constraint), + widgets: self.widgets() + }); + constraintViewModel.constraint.nodes.subscribe(function(){ + self.toJSON(); + }, self); + self.constraints.push(constraintViewModel); + }); + }; + this.sourceData = attributes; this.parse(attributes); this.parseNodes.call(this, attributes); @@ -145,37 +162,6 @@ define([ this.parseNodes(attributes); }, this); - this.updateConstraints = function(){ - var self = this; - return function(){ - var updatedConstraints = self.uniqueConstraints().map(function(c){ - return { - nodeIds: ko.unwrap(c.nodeIds), - uniquetoallinstances: ko.unwrap(c.uniqueToAllInstances), - constraintid: c.constraintid, - cardid: c.cardid - }; - }); - self.constraints(updatedConstraints); - self.set('constraints', updatedConstraints); - self.constraints.valueHasMutated(); - }; - }; - - this.setConstraints = function(arr) { - var self = this; - self.uniqueConstraints.removeAll(); - arr.forEach(function(constraint){ - constraint.widgets = self.widgets(); - var constraintViewModel = new CardConstraintsViewModel(constraint); - constraintViewModel.nodeIds.subscribe(self.updateConstraints()); - constraintViewModel.uniqueToAllInstances.subscribe(self.updateConstraints()); - self.uniqueConstraints.push(constraintViewModel); - }); - }; - - this.setConstraints(this.constraints()); - this.disposables.push(componentIdSubscription); this.disposables.push(cardSubscription); this.disposables.push(widgetSubscription); @@ -198,6 +184,8 @@ define([ */ parse: function(attributes) { var self = this; + // console.log(attributes.data.constraints[0].nodes) + // console.log(attributes.data.constraints[0].nodes) this._attributes = attributes; _.each(attributes.data, function(value, key) { @@ -238,7 +226,13 @@ define([ this.get(key)(value); break; case 'constraints': - this.setConstraints ? this.setConstraints(value) : this.get(key)(value); + if (this.constraints().length === 0) { + this.setConstraints(value); + } else { + this.constraints().forEach(function(constraint, i){ + constraint.update(value[i]); + }); + } break; case 'name': case 'nodegroup_id': @@ -320,7 +314,6 @@ define([ widget.label(originalWidgetData.label); widget.widget_id(originalWidgetData.widget_id); } - // this.setConstraints(this._attributes.data.constraints); }, this); this.parse(this._attributes); }, @@ -333,7 +326,12 @@ define([ key !== 'widgets' && key !== 'datatypes' && key !== 'data' && key !== 'helpactive' && key !== 'config') { if (ko.isObservable(this.attributes[key])) { - if (key === 'cards') { + if (key === 'constraints') { + ret[key] = []; + this.attributes[key]().forEach(function(constraint) { + ret[key].push(constraint.toJSON()); + }, this); + } else if (key === 'cards') { ret[key] = []; this.attributes[key]().forEach(function(card) { ret[key].push(card.toJSON()); diff --git a/arches/app/media/js/viewmodels/card-constraints.js b/arches/app/media/js/viewmodels/card-constraints.js index 36286dd9cbf..d6cbbfa55a6 100644 --- a/arches/app/media/js/viewmodels/card-constraints.js +++ b/arches/app/media/js/viewmodels/card-constraints.js @@ -1,26 +1,31 @@ define([ 'knockout', -], function(ko) { + 'knockout-mapping' +], function(ko, koMapping) { var ConstraintViewModel = function(params) { - - this.uniqueToAllInstances = ko.observable(params.uniquetoallinstances || false); this.widgets = params.widgets || []; - this.nodeIds = ko.observableArray(params.nodes); - this.cardid = params.card_id; - this.constraintid = params.constraintid; + this.constraint = params.constraint; this.getSelect2ConstraintConfig = function(placeholder){ var nodeOptions = this.widgets.map(function(c){return {text: c.label(), id: c.node.nodeid};}); return { clickBubble: true, disabled: false, data: {results: nodeOptions}, - value: this.nodeIds, + value: this.constraint.nodes, multiple: params.multiple || true, closeOnSelect: false, placeholder: placeholder || "Select Widgets", allowClear: true }; }; + this.update = function(val){ + this.constraint.nodes(val.nodes); + this.constraint.constraintid(val.constraintid); + this.constraint.uniquetoallinstances(val.uniquetoallinstances); + }; + this.toJSON = function(){ + return koMapping.toJS(this.constraint); + }; }; diff --git a/arches/app/media/js/views/graph/graph-designer/card-tree.js b/arches/app/media/js/views/graph/graph-designer/card-tree.js index 4e1c417548e..bcf8577ce19 100644 --- a/arches/app/media/js/views/graph/graph-designer/card-tree.js +++ b/arches/app/media/js/views/graph/graph-designer/card-tree.js @@ -36,7 +36,7 @@ define([ if (cardConstraints.length === 0) { cardConstraints.push({ uniquetoallinstances: false, - nodeIds:[], + nodes:[], cardid: undefined, constraintid: undefined }); diff --git a/arches/app/models/card.py b/arches/app/models/card.py index 90572ccb330..374468f2104 100644 --- a/arches/app/models/card.py +++ b/arches/app/models/card.py @@ -45,7 +45,7 @@ def add_nodeconstraints(nodeids, constraint_model): for constraint in constraints: constraintid = constraint.get('constraintid', None) unique_to_all = constraint.get('uniquetoallinstances', False) - nodeids = constraint.get('nodeIds', []) + nodeids = constraint.get('nodes', []) if constraintid is None: constraint_model = models.ConstraintModel() constraint_model.card = self diff --git a/arches/app/templates/views/graph/graph-designer/card-configuration.htm b/arches/app/templates/views/graph/graph-designer/card-configuration.htm index d8a21b35ce9..cfa20f65f03 100644 --- a/arches/app/templates/views/graph/graph-designer/card-configuration.htm +++ b/arches/app/templates/views/graph/graph-designer/card-configuration.htm @@ -117,15 +117,15 @@