diff --git a/docs/coding_standards.txt b/docs/coding_standards.txt index 0394e7206..432125e3c 100755 --- a/docs/coding_standards.txt +++ b/docs/coding_standards.txt @@ -1,22 +1,12 @@ Coding standards and advice for iD ================================== -Classes +Objects ------- -All constructors must initialise objects and arrays. It is not enough to say - array:[], - constructor:function() { - }, -but rather, you should do - array: null, // effectively a placeholder - constructor:function() { - this.array=[], - }, - -or bad things will happen. You should still declare the object outside the constructor, but for clarity rather than functionality. - -(This doesn't apply to simple types - numbers, strings, booleans - which you can declare as normal.) +Most of iD is written with [a js module pattern](http://macwright.org/2012/06/04/the-module-pattern.html): +that is, they do not use the `new` operator, and they use scope instead of +`this` to reference variables and functions. Function names -------------- @@ -30,14 +20,10 @@ and commented as such. Underscores are also used to prefix private methods. File naming ----------- -The filename should be the name of the base class. You can add subclasses within that file for clarity. Don't add extra classes that aren't subclasses, unless they're not referenced from elsewhere. +The filename should be the name of the base class. You can add subclasses within +that file for clarity. Don't add extra classes that aren't subclasses, +unless they're not referenced from elsewhere. Layout ------ -* Hard tabs, indent of 4. -* Do not indent the root level of the module. Add an 'End of module' comment instead. - -Useful stuff to know about Dojo -------------------------------- -* The array and lang modules are full of useful add-ons to basic JavaScript functionality. lang/hitch will save your life with scopes. - +* Soft tabs diff --git a/index.html b/index.html index bd7c660d9..5b7d3250c 100755 --- a/index.html +++ b/index.html @@ -34,12 +34,13 @@ +
@@ -73,7 +74,7 @@ // ---------------------------------------------------- // Data is loaded and app ready to go // Set initial controllerState - map.controller.setState(new iD.controller.edit.NoSelection()); + // map.controller.setState(iD.DrawWay()); // ---------------------------------------------------- // Mode button handlers @@ -82,7 +83,9 @@ }); d3.select('#add-road').on('click', function() { - map.controller.setState(new iD.controller.shape.NoSelection('way')); + // map.controller.setState(new iD.controller.shape.DrawWay('way')); + console.log(iD.DrawWay.enter()); + console.log(iD.DrawWay.enter(map)); }); d3.select('#add-area').on('click', function() { @@ -106,6 +109,7 @@ }); }); + /* $('#undo').click(function() { map.controller.undoStack.undo(); map.updateUIs(true, true); @@ -126,6 +130,7 @@ $('#zoomOut').click(function() { map.zoomOut(); }); + */ diff --git a/js/iD/Connection.js b/js/iD/Connection.js index f3df2f47f..ab71070e0 100755 --- a/js/iD/Connection.js +++ b/js/iD/Connection.js @@ -76,12 +76,10 @@ iD.Connection = function(apiURL) { }); } - // ---------- - // OSM parser function loadFromAPI(box, callback) { // summary: Request data within the bbox from an external OSM server. Currently hardcoded // to use Overpass API (which has the relevant CORS headers). - loadFromURL("http://www.overpass-api.de/api/xapi?map?bbox=" + + loadFromURL('http://www.overpass-api.de/api/xapi?map?bbox=' + [box[0][0], box[1][1], box[1][0], box[0][1]], callback); } diff --git a/js/iD/Entity.js b/js/iD/Entity.js index d8eefa263..6e3478b73 100755 --- a/js/iD/Entity.js +++ b/js/iD/Entity.js @@ -1,69 +1,53 @@ if (typeof iD === 'undefined') iD = {}; iD.Entity = function() { - this.parents = {}; - // The ID locally - this._id = iD.Util.id(); - this.connection = null; + var entity = {}; + // The ID in OSM terms - this.id = NaN; - this.loaded = false; - this.entityType = ''; - this.modified = false; - this.deleted = false; -}; - -iD.Entity.prototype = { - toString:function() { - return this.entityType + " . " + this.id; - }, - - // Provoke redraw and other changes - refresh:function() { - // summary: Ask the connection to provoke redraw and other changes. - this.connection.refreshEntity(this); - }, - - // Bounding box check (to be overridden) - within:function(left, right, top, bottom) { - // summary: Is the entity within the specified bbox? - return !this.deleted; // Boolean - }, + entity.parents = {}; + entity.connection = null; + entity._id = iD.Util.id(); + entity.id = NaN; + entity.loaded = false; + entity.entityType = ''; + entity.modified = false; + entity.deleted = false; // Parent-handling - addParent: function(entity) { + entity.addParent = function(x) { // summary: Record a parent (a relation or way which contains this entity). - this.parents[entity._id] = entity; - }, - removeParent: function(entity) { + entity.parents[x._id] = x; + }; + entity.removeParent = function(x) { // summary: Remove a parent (e.g. when node removed from a way). - delete this.parents[entity._id]; - }, - hasParent: function(entity) { + delete entity.parents[x._id]; + }; + entity.hasParent = function(x) { // summary: Does this entity have the specified parent (e.g. is it in a certain relation)? - return !!this.parents[entity._id]; - }, - parentObjects: function() { + return !!entity.parents[x._id]; + }; + entity.parentObjects = function() { // summary: List of all parents of this entity. - return _.values(this.parents); - }, - hasParentWays: function() { + return _.values(entity.parents); + }; + entity.hasParentWays = function() { // summary: Does this entity have any parents which are ways? - return !!_.find(this.parentObjects(), function(p) { + return !!_.find(entity.parentObjects(), function(p) { return p.entityType === 'way'; }); - }, - parentWays: function() { - // summary: Return an array of all ways that this entity is a member of. - return this._parentObjectsOfClass('way'); // Array - }, - parentRelations: function() { - // summary: Return an array of all relations that this entity is a member of. - return this._parentObjectsOfClass('relation'); // Array - }, - _parentObjectsOfClass: function(_class) { - return _.filter(this.parentObjects(), function(p) { + }; + entity.parentWays = function() { + return entity._parentObjectsOfClass('way'); + }; + entity.parentRelations = function() { + return entity._parentObjectsOfClass('relation'); + }; + function _parentObjectsOfClass(_class) { + return _.filter(entity.parentObjects(), function(p) { return p.entityType === _class; }); } + + return entity; }; + diff --git a/js/iD/Node.js b/js/iD/Node.js index ceae6d4fa..6bab8abd7 100644 --- a/js/iD/Node.js +++ b/js/iD/Node.js @@ -6,7 +6,7 @@ iD.Node = function(connection, id, lat, lon, tags, loaded) { this.connection = connection; this.id = id; this._id = iD.Util.id(); - this.entity = new iD.Entity(); + this.entity = iD.Entity(); this.lat = lat; this.lon = lon; // TODO: keep or trash this custom diff --git a/js/iD/Way.js b/js/iD/Way.js index 2800a791e..54230e8f1 100644 --- a/js/iD/Way.js +++ b/js/iD/Way.js @@ -7,7 +7,7 @@ iD.Way = function(connection, id, nodes, tags, loaded) { this.id = id; this._id = iD.Util.id(); this.deleted = false; - this.entity = new iD.Entity(); + this.entity = iD.Entity(); this.tags = tags || {}; this.loaded = (loaded === undefined) ? true : loaded; this.modified = this.id < 0; diff --git a/test/spec/Entity.js b/test/spec/Entity.js index 3541f633d..492d514e6 100644 --- a/test/spec/Entity.js +++ b/test/spec/Entity.js @@ -2,7 +2,7 @@ describe('Entity', function() { var entity; beforeEach(function() { - entity = new iD.Entity(); + entity = iD.Entity(); }); it('has no entity type', function() {