Fix matchesTags to populate tag editor for elements that have it

This commit is contained in:
Tom MacWright
2012-10-17 12:59:27 -04:00
parent 6673a5710e
commit 7e62af4344
2 changed files with 94 additions and 68 deletions
+40 -26
View File
@@ -1,50 +1,64 @@
// iD/tags/PresetList.js
// List of presets for a given type (e.g. nodes, ways)
define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/xhr'], function(declare,lang,xhr){
define(['dojo/_base/declare'], function(declare) {
declare("iD.tags.PresetList", null, {
entityType: null,
presets: null,
constructor:function(_type,_url) {
constructor:function(type, url) {
// summary: List of presets for a given type (e.g. nodes, ways)
this.entityType=_type;
this.entityType = type;
dojo.xhrGet({
url: _url,
handleAs: "json",
load: lang.hitch(this, this.loaded),
error: function(err) { console.log("Couldn't load presets"); }
console.log(url);
$.ajax({
url: url,
success: _.bind(this.loaded, this),
error: function(err) { console.log("Couldn't load presets for " + type); }
});
},
loaded:function(_obj) {
this.presets=_obj;
console.log("Loaded presets for "+this.entityType);
loaded:function(obj) {
this.presets = obj;
console.log("Loaded presets for " + this.entityType);
},
assembleEditorsForEntity:function(_entity) {
if (_entity.entityType!=this.entityType) return false;
var presetList={};
var editorList=[];
// This entity has all of the same tags as props, with all of the same
// values. It may have additional tags that are not in props.
matchesTags: function(entity, props) {
for (var k in props) {
if (!entity.tags[k] || entity.tags[k] !== props[k]) return false;
}
return true;
},
assembleEditorsForEntity:function(entity) {
if (entity.entityType != this.entityType) return false;
console.log(this.presets);
var presetList = {};
var editorList = [];
for (var group in this.presets) {
for (var preset in this.presets[group]) {
var props=this.presets[group][preset];
if (_entity.matchesTags(props.tags)) {
presetList[preset]=props;
for (var i in props.editors) {
var editor=props.editors[i];
if (editorList.indexOf(editor)==-1) { editorList.push(editor); }
var props = this.presets[group][preset];
if (this.matchesTags(entity, props.tags)) {
presetList[preset] = props;
for (var i in props.edtors) {
var editor = props.editors[i];
if (editorList.indexOf(editor) === -1) {
editorList.push(editor);
}
}
}
}
}
return { presets:presetList, editors: editorList };
return {
presets: presetList,
editors: editorList
};
}
});
// ----------------------------------------------------------------------
+54 -42
View File
@@ -11,86 +11,99 @@ declare("iD.tags.TagEditor", null, {
dialog: null,
editorContainers: null, // hash of DOM nodes to put editors in
constructor:function(_entity,_controller) {
constructor:function(entity, controller) {
// summary: Construct a tag editor dialog box.
console.log("TagEditor constructor");
this.entity=_entity;
this.controller=_controller;
this.editorContainers={};
this.entity = entity;
this.controller = controller;
this.editorContainers = {};
// Create the dialog, and the form to put the editors in
this.dialog = new dijit.Dialog({ title: "My Dialog", content: "Test content.", style: "width: 300px" });
var form = new dijit.form.Form({ encType: 'multipart/form-data', action: '', method: '',
this.dialog = new dijit.Dialog({
title: "Tags",
content: "",
style: "width: 300px" });
var form = new dijit.form.Form({
encType: 'multipart/form-data',
action: '',
method: '',
onSubmit: function(event) { console.log('submit'); }
}, dojo.doc.createElement('div'));
this.dialog.set('content',form);
this.dialog.set('content', form);
this.dialog.show();
// What editors are relevant?
var presetList=this.controller.presets[_entity.entityType];
var applicablePresets=presetList.assembleEditorsForEntity(_entity);
var presetList = this.controller.presets[entity.entityType];
var applicablePresets = presetList.assembleEditorsForEntity(entity);
var i;
// Add preset types
for (var i in applicablePresets.presets) {
this.appendPreset(i,applicablePresets.presets[i],form.domNode);
for (i in applicablePresets.presets) {
this.appendPreset(i, applicablePresets.presets[i], form.domNode);
}
// Add each editor
for (var i in applicablePresets.editors) {
this.appendEditor(applicablePresets.editors[i],form.domNode);
for (i in applicablePresets.editors) {
this.appendEditor(applicablePresets.editors[i], form.domNode);
}
},
// ------------
// Presets
appendPreset:function(_name,_preset,_destination) {
appendPreset:function(name, preset, destination) {
var element=domConstruct.create('h2');
element.appendChild(domConstruct.create('img', { src: 'presets/'+_preset.icon }));
element.appendChild(dojo.doc.createTextNode(_name));
_destination.appendChild(element);
element.appendChild(domConstruct.create('img', { src: 'presets/' + preset.icon }));
element.appendChild(dojo.doc.createTextNode(name));
destination.appendChild(element);
},
// ------------
// Editors
appendEditor:function(_editor,_destination) {
appendEditor:function(editor, destination) {
// summary: Request an editor (cached if available, XHR if not), and call renderEditor when it's available.
if (this.controller.editorCache[_editor]) {
this.renderEditor(_editor,_destination);
if (this.controller.editorCache[editor]) {
this.renderEditor(editor, destination);
} else {
dojo.xhrGet({
url: "presets/editors/"+_editor+".json",
handleAs: "json",
load: lang.hitch(this, this.loadedEditor, _editor, _destination),
// TODO: eliminate lang.hitch here
load: lang.hitch(this, this.loadedEditor, editor, destination),
error: function(err) { console.log("Couldn't load editor"); }
});
}
},
loadedEditor:function(_editor,_destination,_obj) {
// summary: Editor has loaded via XHR, so store it in the cache and render it.
this.controller.editorCache[_editor]=_obj;
this.renderEditor(_editor,_destination);
loadedEditor: function(editor, destination, obj) {
// summary: Editor has loaded via XHR, so store it in the cache and render it.
this.controller.editorCache[editor] = obj;
this.renderEditor(editor, destination);
},
renderEditor:function(_editor,_destination) {
// summary: Render an editor as a form.
renderEditor: function(_editor, destination) {
// summary: Render an editor as a form.
var editor=this.controller.editorCache[_editor];
// Add the subhead
var element=domConstruct.create('h3');
element.appendChild(dojo.doc.createTextNode(_editor));
_destination.appendChild(element);
destination.appendChild(element);
// Add each form element
for (var label in editor) {
var item=editor[label];
var value=this.getTagValue(item.key);
element=domConstruct.create('div');
var item = editor[label];
var value = this.getTagValue(item.key);
element = domConstruct.create('div');
switch (item.type) {
case 'text':
var textbox = new dijit.form.TextBox({ name: item.key, value: value, type: 'text' }, domConstruct.create('input'));
var textbox = new dijit.form.TextBox({
name: item.key,
value: value,
type: 'text'
}, domConstruct.create('input'));
element.appendChild(dojo.doc.createTextNode(label));
element.appendChild(textbox.domNode);
break;
@@ -98,19 +111,18 @@ declare("iD.tags.TagEditor", null, {
case 'relation':
case 'hidden':
}
_destination.appendChild(element);
destination.appendChild(element);
}
// var submitbtn = new dijit.form.Button({ name: 'submit', type: 'submit', value: 'Submit', label: "Submit" }, dojo.doc.createElement('button'));
// var resetbtn = new dijit.form.Button({ type: 'reset', label: 'Reset' }, dojo.doc.createElement('button'));
// _destination.appendChild(submitbtn.domNode);
// _destination.appendChild(resetbtn.domNode);
},
getTagValue:function(k) {
// summary: Get the value of a tag for the current entity, or empty string if unset.
return this.entity.tags[k] ? this.entity.tags[k] : '';
},
getTagValue:function(k) {
// summary: Get the value of a tag for the current entity, or empty string if unset.
return this.entity.tags[k] ? this.entity.tags[k] : '';
}
});
// ----------------------------------------------------------------------