diff --git a/platform/commonUI/edit/README.md b/platform/commonUI/edit/README.md index 3691e8211a..a8c552fb3e 100644 --- a/platform/commonUI/edit/README.md +++ b/platform/commonUI/edit/README.md @@ -15,6 +15,8 @@ view's scope.) These additional properties are: then that function is assumed to be an accessor-mutator function (that is, it will be called with no arguments to get, and with an argument to set.) +* `method`: Name of a method to invoke upon a selected object when + a control is activated, e.g. on a button click. * `inclusive`: Optional; true if this control should be considered applicable whenever at least one element in the selection has the associated property. Otherwise, all members of the current diff --git a/platform/commonUI/edit/src/representers/EditToolbar.js b/platform/commonUI/edit/src/representers/EditToolbar.js index 591a312aa3..23bc7e8ed7 100644 --- a/platform/commonUI/edit/src/representers/EditToolbar.js +++ b/platform/commonUI/edit/src/representers/EditToolbar.js @@ -106,23 +106,41 @@ define( // to the current selection. function isApplicable(item) { var property = (item || {}).property, + method = (item || {}).method, exclusive = !(item || {}).inclusive; // Check if a selected item defines this property function hasProperty(selected) { - return selected[property] !== undefined; + return (property && (selected[property] !== undefined)) || + (method && (typeof selected[method] === 'function')); } - return property && selection.map(hasProperty).reduce( + return selection.map(hasProperty).reduce( exclusive ? and : or, exclusive ) && isConsistent(property); } + // Invoke all functions in selections with the given name + function invoke(method, value) { + if (method) { + selection.forEach(function (selected) { + if (typeof selected[method] === 'function') { + selected[method](value); + } + }); + } + } + // Prepare a toolbar item based on current selection function convertItem(item) { var converted = Object.create(item || {}); - converted.key = addKey(item.property); + if (item.property) { + converted.key = addKey(item.property); + } + if (item.method) { + converted.click = function (v) { invoke(item.method, v); }; + } return converted; }