Skip to content

Commit

Permalink
Initial checkin iD.restrictions
Browse files Browse the repository at this point in the history
* extensible rule based editing restrictions
* can prevent changes to geometry and/or tagging
* methods in context for testing restriction rulesets
  • Loading branch information
bhousel committed Jun 26, 2015
1 parent 728036b commit 8467433
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ dist/iD.js: \
js/id/ui/intro/*.js \
js/id/presets.js \
js/id/presets/*.js \
js/id/restrictions.js \
js/id/restrictions/*.js \
js/id/validations.js \
js/id/validations/*.js \
js/id/end.js \
Expand Down
6 changes: 6 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ en:
description: Description
on_wiki: "{tag} on wiki.osm.org"
used_with: "used with {type}"
restrictions:
restrict_edit: You may not edit because
restrict_tag: You may not change tags because
this_is: this feature is
these_are: these features are
connected_to_hidden: connected to other hidden features.
validations:
untagged_point: Untagged point
untagged_line: Untagged line
Expand Down
7 changes: 7 additions & 0 deletions dist/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@
"on_wiki": "{tag} on wiki.osm.org",
"used_with": "used with {type}"
},
"restrictions": {
"restrict_edit": "You may not edit because",
"restrict_tag": "You may not change tags because",
"this_is": "this feature is",
"these_are": "these features are",
"connected_to_hidden": "connected to other hidden features."
},
"validations": {
"untagged_point": "Untagged point",
"untagged_line": "Untagged line",
Expand Down
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@
<script src='js/id/presets/collection.js'></script>
<script src='js/id/presets/field.js'></script>

<script src='js/id/restrictions.js'></script>
<script src='js/id/restrictions/connected_to_hidden.js'></script>

<script src='js/id/validations.js'></script>
<script src='js/id/validations/deprecated_tag.js'></script>
<script src='js/id/validations/many_deletions.js'></script>
Expand Down
36 changes: 33 additions & 3 deletions js/id/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,39 @@ window.iD = function () {
var features = iD.Features(context);
context.features = function() { return features; };
context.hasHiddenConnections = function(id) {
var graph = history.graph(),
entity = graph.entity(id);
return features.hasHiddenConnections(entity, graph);
// var graph = history.graph(),
// entity = graph.entity(id);
// return features.hasHiddenConnections(entity, graph);
return context.geometryLocked([id]);
};

/* Editing Restrictions */
var restrict = {};
context.restrictions = {
all: function() { return restrict.all; },
geometry: function() { return restrict.geometry; },
tags: function() { return restrict.tags; },
reload: function() {
restrict.all = _.map(iD.restrictions, function(fn) { return fn(context); });
restrict.geometry = _.filter(restrict.all, 'lockGeometry');
restrict.tags = _.filter(restrict.all, 'lockTags');
},
test: function(ruleset, ids) {
return _.reduce(ruleset, function(result, restriction) {
return result || restriction.test(ids);
}, false);
}
};
context.restrictions.reload();

context.editingLocked = function(ids) {
return context.restrictions.test(restrict.all, ids);
};
context.geometryLocked = function(ids) {
return context.restrictions.test(restrict.geometry, ids);
};
context.tagsLocked = function(ids) {
return context.restrictions.test(restrict.tags, ids);
};

/* Map */
Expand Down
1 change: 1 addition & 0 deletions js/id/restrictions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iD.restrictions = {};
23 changes: 23 additions & 0 deletions js/id/restrictions/connected_to_hidden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
iD.restrictions.ConnectedToHidden = function(context) {
var restriction = {
id: 'connected_to_hidden',
lockGeometry: true,
lockTags: false
};

restriction.test = function(ids) {
var graph = context.graph(),
entities = _.map(ids, context.entity),
hasHiddenConnections = function(entity) {
return context.features().hasHiddenConnections(entity, graph);
};

if (_.any(entities, hasHiddenConnections)) {
return (ids.length === 1 ? t('restrictions.this_is') : t('restrictions.these_are')) +
' ' + t('restrictions.connected_to_hidden');
}
return false;
};

return restriction;
};
3 changes: 3 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@
<script src='../js/id/util/session_mutex.js'></script>
<script src='../js/id/util/suggest_names.js'></script>

<script src='../js/id/restrictions.js'></script>
<script src='../js/id/restrictions/connected_to_hidden.js'></script>

<script src='../js/id/validations.js'></script>
<script src='../js/id/validations/deprecated_tag.js'></script>
<script src='../js/id/validations/many_deletions.js'></script>
Expand Down

0 comments on commit 8467433

Please sign in to comment.