michmech / xonomy

A schema-driven XML editor for the web.
MIT License
91 stars 32 forks source link

Allow unknowElement and unknownAttribute as a function #5

Closed filodej closed 7 years ago

filodej commented 7 years ago

Defining unknownElement or unknownAttribute as a function is much more powerful than a static definition.

Instead of the following distributed logic:

var docSpec={
  unknownElement: {
    menu: [{
      caption: function(jsElement) { 'Delete <'+jsElement.name+'>'; },
      action: Xonomy.deleteElement,
      hideIf: function(jsElement) { return jsElement.name === 'root'; }
    }];
  },
  unknownAttribute: {...}
};

... we can make the condition logic centralized as follows:

var docSpec={
  unknownElement: function(elName){
    return (elName !== 'root')
      ? { menu: [{ caption: 'Delete <'+elName+'>', action: Xonomy.deleteElement }]}
      : {};
  },
  unknownAttribute: function(elName, attrName) {
    ...
  },
};

... and for more advanced use-cases it may not be even possible to statically define specification up-front.

michmech commented 7 years ago

Very clever. Now people will want to put everything in these functions and .elements will be empty! :-)