From 44eb54884b431ca5909a05b05acf4687d88d4c30 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 7 Apr 2015 20:35:54 -0700 Subject: [PATCH] [Containment] Fire a link action on drop Fire a link action as the result of a drag-drop operation, WTD-962. --- .../commonUI/edit/src/actions/LinkAction.js | 6 +- platform/representation/bundle.json | 2 +- .../src/gestures/DropGesture.js | 64 +++++++++---------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/platform/commonUI/edit/src/actions/LinkAction.js b/platform/commonUI/edit/src/actions/LinkAction.js index 459fec0d5f..79d772738d 100644 --- a/platform/commonUI/edit/src/actions/LinkAction.js +++ b/platform/commonUI/edit/src/actions/LinkAction.js @@ -16,8 +16,10 @@ define( // Add this domain object's identifier function addId(model) { - model.composition = model.composition || []; - model.composition.push(selectedId); + if (Array.isArray(model.composition) && + model.composition.indexOf(selectedId) < 0) { + model.composition.push(selectedId); + } } // Persist changes to the domain object diff --git a/platform/representation/bundle.json b/platform/representation/bundle.json index 250fc2f730..84120541e0 100644 --- a/platform/representation/bundle.json +++ b/platform/representation/bundle.json @@ -21,7 +21,7 @@ { "key": "drop", "implementation": "gestures/DropGesture.js", - "depends": [ "$q" ] + "depends": [ "dndService", "$q" ] }, { "key": "menu", diff --git a/platform/representation/src/gestures/DropGesture.js b/platform/representation/src/gestures/DropGesture.js index 985fe102d3..1b6033a28c 100644 --- a/platform/representation/src/gestures/DropGesture.js +++ b/platform/representation/src/gestures/DropGesture.js @@ -19,7 +19,10 @@ define( * composition should be modified as a result of the drop. */ - function DropGesture($q, element, domainObject) { + function DropGesture(dndService, $q, element, domainObject) { + var actionCapability = domainObject.getCapability('action'), + action; // Action for the drop, when it occurs + function broadcastDrop(id, event) { // Find the relevant scope... var scope = element && element.scope && element.scope(), @@ -43,20 +46,27 @@ define( } } - function doPersist() { - var persistence = domainObject.getCapability("persistence"); - return $q.when(persistence && persistence.persist()); - } - function dragOver(e) { - var event = (e || {}).originalEvent || e; + var event = (e || {}).originalEvent || e, + selectedObject = dndService.getData( + GestureConstants.MCT_EXTENDED_DRAG_TYPE + ); - // TODO: Vary this based on modifier keys - event.dataTransfer.dropEffect = 'move'; + if (selectedObject) { + // TODO: Vary this based on modifier keys + action = actionCapability.getActions({ + key: 'link', + selectedObject: selectedObject + })[0]; - // Indicate that we will accept the drag - event.preventDefault(); // Required in Chrome? - return false; + if (action) { + event.dataTransfer.dropEffect = 'move'; + + // Indicate that we will accept the drag + event.preventDefault(); // Required in Chrome? + return false; + } + } } function drop(e) { @@ -67,38 +77,24 @@ define( // destination domain object's composition, and persist // the change. if (id) { - $q.when(domainObject.useCapability( - 'mutation', - function (model) { - var composition = model.composition; - // Don't store the same id more than once - if (composition && // not-contains - !(composition.map(function (i) { - return i === id; - }).reduce(function (a, b) { - return a || b; - }, false))) { - model.composition.push(id); - } - } - )).then(function (result) { + $q.when(action && action.perform()).then(function (result) { // Broadcast the drop event if it was successful if (result) { broadcastDrop(id, event); } - - // If mutation was successful, persist the change - return result && doPersist(); }); } } - // Listen for dragover, to indicate we'll accept a drag - element.on('dragover', dragOver); + // We can only handle drops if we have access to actions... + if (actionCapability) { + // Listen for dragover, to indicate we'll accept a drag + element.on('dragover', dragOver); - // Listen for the drop itself - element.on('drop', drop); + // Listen for the drop itself + element.on('drop', drop); + } return { /**