[Edit] Add policy for the Edit action

Allow Edit mode only when editable views exist, WTD-1062.
This commit is contained in:
Victor Woeltjen 2015-04-01 15:45:43 -07:00
parent e3ec9c6130
commit ccf2ccc4c6
4 changed files with 70 additions and 2 deletions

View File

@ -108,7 +108,8 @@
"templateUrl": "templates/items/items.html",
"uses": [ "composition" ],
"gestures": [ "drop" ],
"type": "folder"
"type": "folder",
"editable": false
}
],
"components": [

View File

@ -68,6 +68,12 @@
"depends": [ "$location" ]
}
],
"policies": [
{
"category": "action",
"implementation": "policies/EditActionPolicy.js"
}
],
"templates": [
{
"key": "edit-library",

View File

@ -0,0 +1,61 @@
/*global define*/
define(
[],
function () {
"use strict";
/**
* Policy controlling when the `edit` and/or `properties` actions
* can appear as applicable actions of the `view-control` category
* (shown as buttons in the top-right of browse mode.)
* @constructor
*/
function EditActionPolicy() {
// Get a count of views which are not flagged as non-editable.
function countEditableViews(context) {
var domainObject = (context || {}).domainObject,
views = domainObject && domainObject.useCapability('view'),
count = 0;
// A view is editable unless explicitly flagged as not
(views || []).forEach(function (view) {
count += (view.editable !== false) ? 1 : 0;
});
return count;
}
return {
/**
* Check whether or not a given action is allowed by this
* policy.
* @param {Action} action the action
* @param context the context
* @returns {boolean} true if not disallowed
*/
allow: function (action, context) {
var key = action.getMetadata().key,
category = (context || {}).category;
// Only worry about actions in the view-control category
if (category === 'view-control') {
// Restrict 'edit' to cases where there are editable
// views (similarly, restrict 'properties' to when
// the converse is true)
if (key === 'edit') {
return countEditableViews(context) > 0;
} else if (key === 'properties') {
return countEditableViews(context) < 1;
}
}
// Like all policies, allow by default.
return true;
}
};
}
return EditActionPolicy;
}
);

View File

@ -89,7 +89,7 @@ define(
// Convert to an array if necessary
categories = Array.isArray(categories) ?
categories : [categories];
categories : [categories];
// Store action under all relevant categories
categories.forEach(function (category) {