Created
November 1, 2012 15:36
-
-
Save jfirebaugh/3994398 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (typeof iD === 'undefined') iD = {}; | |
| iD.SceneGraph = function () { | |
| this.ways = {}; | |
| this.nodes = {}; | |
| this.relations = {}; | |
| }; | |
| function mutable(entity) { | |
| if (entity.isFrozen()) { | |
| entity = Object.create(entity, {dirty: {value: true}}) | |
| } | |
| return entity; | |
| } | |
| function immutable(entity) { | |
| if (!entity.isFrozen()) { | |
| Object.freeze(entity); | |
| } | |
| return entity; | |
| } | |
| function mutate(entity, callback) { | |
| entity = mutable(entity); | |
| callback.call(entity, entity); | |
| return immutable(entity); | |
| } | |
| iD.SceneGraph.prototype.mutate = function () { | |
| mutate(this, function () { | |
| var callback = arguments.pop(); | |
| var entities = arguments.map(function (entity) { | |
| if (entity.entityType === 'node') { | |
| this.nodes = mutate(this.nodes); | |
| return this.nodes[entity.id] = mutate(entity); | |
| } else if (entity.entityType == 'way') { | |
| this.ways = mutate(this.ways); | |
| return this.ways[entity.id] = mutate(entity); | |
| } else if (entity.entityType == 'relation') { | |
| this.relations = mutate(this.relations); | |
| return this.relations[entity.id] = mutate(entity); | |
| } | |
| }); | |
| entities.shift(this); | |
| callback.apply(this, entities); | |
| entities.each(immutable); | |
| }); | |
| }; | |
| iD.SceneGraph.prototype.changes = function () { | |
| return { | |
| nodes: // filter by dirty property | |
| ways: // etc | |
| relations: // etc | |
| } | |
| }; | |
| iD.Actions = {}; | |
| iD.Actions.AddNodeToWay = function (scenegraph, way, node, index) { | |
| return scenegraph.mutate(way, function (scenegraph, way) { | |
| if (index === -1) { | |
| index = way.nodes.length; | |
| } | |
| nodes = way.mutableNodes(); | |
| nodes.splice(index, 0, node.id); | |
| }); | |
| }; | |
| iD.Actions.DeleteNode = function (scenegraph, node) { | |
| return scenegraph.mutate(function (scenegraph) { | |
| delete scenegraph.nodes[node.id]; | |
| // for ids in node.parent_way_ids | |
| // way = scenegraph.ways[id].mutable(scenegraph) | |
| // way.nodes.delete(node.id); // TODO: copy nodes first | |
| // for ids in node.parent_relation_ids | |
| // relation = scenegraph.relations[id].mutable(scenegraph) | |
| // relation.nodes.delete(node.id); // TODO: copy nodes first | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment