diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js index 9d2f0d8bc13664e2f872dee0c2fb5d748192fcea..6ef62e274247af7cde970c708573b428ac271dbf 100644 --- a/js/cam/assemblers/Component.js +++ b/js/cam/assemblers/Component.js @@ -10,7 +10,7 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){ function Component(id, json){ this.object3D = new THREE.Object3D(); this.id = id || "id" + id++; - this.name = json.name || ""; + this.name = json.name || "Component" + id; this.parent = null; if (json.parent) this.parent = json.parent; this.parentObject = null; @@ -51,6 +51,12 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){ return false; }; + Component.prototype.getAncestry = function(ancestors){ + if (this.parent === null) return ancestors; + ancestors.push(this.parent); + return this.parentObject.getAncestry(ancestors); + }; + Component.prototype._removeChild = function(child){ if (this.children.indexOf(child) == -1){ console.warn("not a child"); diff --git a/js/menus/EditComponentMenuView.js b/js/menus/EditComponentMenuView.js index 5b1e1a0903fff2a77e4640376f68db6a3d8edd8d..2cfc5a1507101e6bbf7049e07f72d98a5b2f5154 100644 --- a/js/menus/EditComponentMenuView.js +++ b/js/menus/EditComponentMenuView.js @@ -10,9 +10,10 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'cam', 'text!editComponen events: { "click #finishComponent": "_save", - "click #cancelComponent": "_cancel", "click #deleteComponent": "_delete", - "click .removeChild": "_removeChild" + "click .removeChild": "_removeChild", + "click .addChild": "_addChild", + "click .changeParent": "_changeParent" }, _initialize: function(){ @@ -24,12 +25,6 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'cam', 'text!editComponen }, _save: function(e){ - e.preventDefault(); - console.log("save component"); - this._stopEditing(); - }, - - _cancel: function(e){ e.preventDefault(); this._stopEditing(); }, @@ -54,10 +49,41 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'cam', 'text!editComponen this.render(); }, + _addChild: function(e){ + e.preventDefault(); + var id = $(e.target).data("id"); + var assembler = cam.get("assembler"); + assembler.getComponent(cam.get("editingComponent")).addChild(assembler.getComponent(id)); + assembler.buildComponentTree(); + this.render(); + }, + + _changeParent: function(e){ + e.preventDefault(); + var id = $(e.target).data("id"); + console.log(id); + if (!id) id = null; + var assembler = cam.get("assembler"); + assembler.getComponent(id).addChild(assembler.getComponent(cam.get("editingComponent"))); + assembler.buildComponentTree(); + this.render(); + }, + _makeTemplateJSON: function(){ var assembler = cam.get("assembler"); - var component = assembler.getComponent(cam.get("editingComponent")); - return _.extend(this.model.toJSON(), cam.toJSON(), assembler.toJSON(), {thisComponent: component.toJSON()}); + var editingComponent = cam.get("editingComponent"); + var component = assembler.getComponent(editingComponent); + var allDescendants = []; + var allAncestors = component.getAncestry([]); + var correctBranch = false; + _.each(assembler.tree, function(level, id){ + if(correctBranch && level <= assembler.tree[editingComponent]) correctBranch = false; + if(id == editingComponent) correctBranch = true; + if (!correctBranch || assembler.tree[editingComponent] >= level) return; + allDescendants.push(id); + }); + return _.extend(this.model.toJSON(), cam.toJSON(), assembler.toJSON(), + {thisComponent: component.toJSON(), ancestors:allAncestors, descendants:allDescendants}); }, template: _.template(template) diff --git a/js/menus/MenuWrapperView.js b/js/menus/MenuWrapperView.js index 42a471971656d185605fa6f924fad434e0747247..0812f39fcc9c2b15982ae4359f1320651f107c64 100644 --- a/js/menus/MenuWrapperView.js +++ b/js/menus/MenuWrapperView.js @@ -202,6 +202,7 @@ define(['jquery', 'underscore', 'plist', 'backbone', 'lattice', 'appState', 'tex }, _setOwnerProperty: function(owner, property, value){ + console.log(value); if (owner instanceof Backbone.Model) owner.set(property, value); else { owner[property] = value; diff --git a/js/menus/templates/EditComponentMenuView.html b/js/menus/templates/EditComponentMenuView.html index 6c5bac1974551922e74d93daa3994f6edbfca952..4cdc5c3c2bd3ff9174eb10c7b751aacaafe817a0 100644 --- a/js/menus/templates/EditComponentMenuView.html +++ b/js/menus/templates/EditComponentMenuView.html @@ -5,9 +5,10 @@ Parent: <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><% if (thisComponent.parent){ %><%= components[thisComponent.parent].name %><% }else{ %>No Parent Selected<% } %><span class="caret"></span></button> <ul role="menu" class="dropdown-menu"> <% _.each(components, function(component){ %> - <% if (component.id == editingComponent) return; %> - <li><a class="component dropdownSelector" data-property="parent" data-value="<%= component.id %>" href="#"><%= component.name %></a></li> + <% if (component.id == editingComponent || descendants.indexOf(component.id)>=0) return; %> + <li><a class="changeParent dropdownSelector" data-id="<%= component.id %>" href="#"><%= component.name %></a></li> <% }); %> + <li><a class="changeParent dropdownSelector" href="#">None</a></li> </ul> </div><br/><br/> <% if(!isStock){ %> @@ -16,20 +17,18 @@ Parent: <button data-toggle="dropdown" class="btn dropdown-toggle" type="button">+ Add Child<span class="caret"></span></button> <ul role="menu" class="dropdown-menu"> <% _.each(components, function(component, id){ %> - <% if (component.id == editingComponent || component.id == thisComponent.parent) return; %> - <li><a class="component dropdownSelector" data-property="newChild" data-value="<%= id %>" href="#"><%= component.name %></a></li> + <% if (component.id == editingComponent || ancestors.indexOf(component.id)>=0 || thisComponent.children.indexOf(component.id)>=0) return; %> + <li><a class="addChild dropdownSelector" data-id="<%= id %>" href="#"><%= component.name %></a></li> <% }); %> <% _.each(stock, function(thisStock, id){ %> - <li><a class="component dropdownSelector" data-property="newChild" data-value="<%= id %>" href="#"><%= thisStock.name %></a></li> + <% if (thisStock.id == editingComponent || ancestors.indexOf(thisStock.id)>=0 || thisComponent.children.indexOf(thisStock.id)>=0) return; %> + <li><a class="addChild dropdownSelector" data-id="<%= id %>" href="#"><%= thisStock.name %></a></li> <% }); %> </ul> </div><br/><br/> <% var treeLevel = 0; %> - <% var correctBranch = false; %> <% _.each(tree, function(level, id){ %> - <% if(correctBranch && level <= tree[editingComponent]) correctBranch = false; %> - <% if(id == editingComponent) correctBranch = true; %> - <% if (!correctBranch || tree[editingComponent] >= level) return; %> + <% if (descendants.indexOf(id)<0) return; %> <% var component = components[id] || stock[id]; %> <% treeLevel++; %> <div class="assemblerTree" <% if (treeLevel%2 == 0){ %> style="background-color:#ddd"<% } %> ><label style="padding-left:<%= (level-tree[editingComponent]-1)*10 + 20 %>px"><%= component.name %> @@ -52,18 +51,17 @@ Parent: <!--Jog<br/>--> <% } %> <br/> - <!--STL: STL Name<br/><br/>--> - <!--<a id="uploadStl" href="#" class="btn btn-block btn-lg btn-default">Upload STL</a><br/>--> + <a id="uploadStl" href="#" class="btn btn-block btn-lg btn-default">Upload STL</a><br/> + STL: STL Name<br/><br/> <% } %> -<!--Offset (xyz): --> - <!--<input data-property="translation" data-key="x" value="<%= translation.x %>" placeholder="X" class="form-control floatInput stl" type="text"> --> - <!--<input data-property="translation" data-key="y" value="<%= translation.y %>" placeholder="Y" class="form-control floatInput stl" type="text"> --> - <!--<input data-property="translation" data-key="z" value="<%= translation.z %>" placeholder="Z" class="form-control floatInput stl" type="text"><br/><br/>--> -<!--Rotation (xyz): --> - <!--<input data-property="rotation" data-key="x" value="<%= rotation.x %>" placeholder="X" class="form-control floatInput stl" type="text"> --> - <!--<input data-property="rotation" data-key="y" value="<%= rotation.y %>" placeholder="Y" class="form-control floatInput stl" type="text"> --> - <!--<input data-property="rotation" data-key="z" value="<%= rotation.z %>" placeholder="Z" class="form-control floatInput stl" type="text"><br/><br/>--> -<!--<% if(!isStock){ %> Scale: <input data-property="scale" value="<%= scale %>" placeholder="Scale" class="form-control floatInput stl" type="text"><br/><br/><% } %>--> -<a id="finishComponent" href="#" class="btn btn-block btn-lg btn-success">Save <% if(isStock){ %>Stock<% }else{ %>Component<% } %></a><br/> +Offset (xyz): + <input data-property="translation" data-key="x" value="<%= translation.x %>" placeholder="X" class="form-control floatInput stl" type="text"> + <input data-property="translation" data-key="y" value="<%= translation.y %>" placeholder="Y" class="form-control floatInput stl" type="text"> + <input data-property="translation" data-key="z" value="<%= translation.z %>" placeholder="Z" class="form-control floatInput stl" type="text"><br/><br/> +Rotation (xyz): + <input data-property="rotation" data-key="x" value="<%= rotation.x %>" placeholder="X" class="form-control floatInput stl" type="text"> + <input data-property="rotation" data-key="y" value="<%= rotation.y %>" placeholder="Y" class="form-control floatInput stl" type="text"> + <input data-property="rotation" data-key="z" value="<%= rotation.z %>" placeholder="Z" class="form-control floatInput stl" type="text"><br/><br/> +<% if(!isStock){ %> Scale: <input data-property="scale" value="<%= scale %>" placeholder="Scale" class="form-control floatInput stl" type="text"><br/><br/><% } %> <a id="deleteComponent" href="#" class="btn btn-halfWidth btn-lg btn-default"><span class="fui-trash"></span> Delete</a> -<a id="cancelComponent" href="#" class="btn btn-halfWidth pull-right btn-lg btn-default">Cancel / Exit</a><br/><br/> +<a id="finishComponent" href="#" class="btn btn-halfWidth pull-right btn-lg btn-success">Save <% if(isStock){ %>Stock<% }else{ %>Component<% } %></a><br/><br/>