Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.
Объект DataTransfer используется для хранения данных, перетаскиваемых мышью во время операции drag and drop. Он может хранить от одного до нескольких элементов данных, вне зависимости от их типа. Для получения доп. информации об операции drag and drop, см. Drag and Drop
Объект может быть получен из свойства dataTransfer всех событий перетаскивания. Он не может быть отдельно создан.
Список свойств
Свойство | Тип |
dropEffect |
String |
effectAllowed |
String |
files |
FileList |
mozCursor |
String |
mozItemCount |
unsigned long |
mozSourceNode |
Node |
mozUserCancelled |
Boolean |
types |
DOMStringList |
Список методов
void addElement(in Element element) |
void clearData([in String type]) |
String getData(in String type) |
void setData(in String type, in String data) |
void setDragImage(in nsIDOMElement image, in long x, in long y) |
void mozClearDataAt([in String type, in unsigned long index]) |
nsIVariant mozGetDataAt(in String type, in unsigned long index) |
void mozSetDataAt(in String type, in nsIVariant data, in unsigned long index) |
StringList mozTypesAt([in unsigned long index]) |
Свойства
dropEffect
Реальное действие, которое будет выполнено, и единственное возможное значение свойства effectAllowed.
Для событий dragenter и dragover, dropEffect будет инициализировано значением, зависящим от того, какое действие запрашивает пользователь. То, как это определяется, зависит от платформы, но, как правило, пользователи могут зажать клавиши-модификаторы для выбора желаемого действия. В обработчике событий для dragenter и dragover, dropEffect следует модифицировать, если запрашиваемое пользователем действие не то, которое требовалось.
Для событий dragstart, drag, и dragleave, dropEffect будет инициализировано значением "none". Cвойству dropEffect можно присвоить любое значение, но оно не будет ни для чего использоваться.
Для событий drop и dragend, dropEffect будет инициализировано значением, которое было назначено свойству dropEffect после последнего события dragenter или dragover.
Возможные значения:
- copy: в новом расположении создается копия исходного элемента.
- move: элемент перемещается в новое расположение.
- link: в новом расположении создается ссылка на исходный элемент.
- none: элемент не может быть брошен.
Присвоение каких-либо других значений не возымеет эффекта и оставит старое значение.
effectAllowed
Specifies the effects that are allowed for this drag. You may set this in the dragstart
event to set the desired effects for the source, and within the dragenter
and dragover
events to set the desired effects for the target. The value is not used for other events.
Possible values:
- copy: A copy of the source item may be made at the new location.
- move: An item may be moved to a new location.
- link: A link may be established to the source at the new location.
- copyLink: A copy or link operation is permitted.
- copyMove: A copy or move operation is permitted.
- linkMove: A link or move operation is permitted.
- all: All operations are permitted.
- none: the item may not be dropped.
- uninitialized: the default value when the effect has not been set, equivalent to all.
Assigning any other value has no effect and retains the old value.
files
Contains a list of all the local files available on the data transfer. If the drag operation doesn't involve dragging files, this property is an empty list. An invalid index access on the FileList
specified by this property will return null
.
Example
This example dumps the list of files dragged into the browser window.
Unable to find file filednd.html
See this in effect at: https://jsfiddle.net/9C2EF/
types
Holds a list of the format types of the data that is stored for the first item, in the same order the data was added. An empty list will be returned if no data was added.
mozCursor
(Firefox 3.5 / Thunderbird 3.0 / SeaMonkey 2.0)The drag cursor's state. This is primarily used to control the cursor during tab drags.
Possible values
auto
- Uses the default system behavior.
default
- Uses the default Gecko behavior, which is to set the cursor to an arrow during the drag operation.
mozItemCount
The number of items being dragged.
mozSourceNode
The Node
over which the mouse cursor was located when the button was pressed to initiate the drag operation. This value is null
for external drags or if the caller can't access the node.
mozUserCancelled
This property applies only to the dragend
event, and is true
if the user canceled the drag operation by pressing escape. It will be false
in all other cases, including if the drag failed for any other reason, for instance due to a drop over an invalid location. This property is not currently implemented on Linux.
Methods
addElement()
Set the drag source. Usually you would not change this, but it will affect which node the drag and dragend events are fired at. The default target is the node that was dragged.
void addElement( in Element element );
Parameters
element
- The element to add.
clearData()
Remove the data associated with a given type. The type argument is optional. If the type is empty or not specified, the data associated with all types is removed. If data for the specified type does not exist, or the data transfer contains no data, this method will have no effect.
void clearData( [optional] in String type );
Parameters
type
- The type of data to remove.
getData()
Retrieves the data for a given type, or an empty string if data for that type does not exist or the data transfer contains no data.
A security error will occur if you attempt to retrieve data during a drag that was set from a different domain, or the caller would otherwise not have access to. This data will only be available once a drop occurs during the drop event.
String getData( in String type );
Parameters
type
- The type of data to retrieve.
setData()
Set the data for a given type. If data for the type does not exist, it is added at the end, such that the last item in the types list will be the new format. If data for the type already exists, the existing data is replaced in the same position. That is, the order of the types list is not changed when replacing data of the same type.
void setData( in String type, in String data );
Parameters
type
- The type of data to add.
data
- The data to add.
setDragImage()
Set the image to be used for dragging if a custom one is desired. Most of the time, this would not be set, as a default image is created from the node that was dragged.
If the node is an HTML img element, an HTML canvas element or a XUL image element, the image data is used. Otherwise, image should be a visible node and the drag image will be created from this. If image is null, any custom drag image is cleared and the default is used instead.
The coordinates specify the offset into the image where the mouse cursor should be. To center the image, for instance, use values that are half the width and height of the image.
void setDragImage( in Element image, in long x, in long y );
Parameters
image
- An element to use as the drag feedback image
x
- Horizontal offset within the image.
y
- Vertical offset within the image.
mozClearDataAt()
Removes the data associated with the given format for an item at the specified index. The index is in the range from zero to the number of items minus one.
If the last format for the item is removed, the entire item is removed, reducing mozItemCount
by one.
If the format
list is empty, then the data associated with all formats is removed. If the format is not found, then this method has no effect.
void mozClearDataAt( [optional] in String type, in unsigned long index );
Parameters
type
- The type of data to remove.
index
- The index of the data to remove.
mozGetDataAt()
Retrieves the data associated with the given format for an item at the specified index, or null if it does not exist. The index should be in the range from zero to the number of items minus one.
nsIVariant mozGetDataAt( [optional] in String type, in unsigned long index );
Parameters
type
- The type of data to retrieve.
index
- The index of the data to retrieve.
mozSetDataAt()
A data transfer may store multiple items, each at a given zero-based index. mozSetDataAt()
may only be called with an index argument less than mozItemCount
in which case an existing item is modified, or equal to mozItemCount
in which case a new item is added, and the mozItemCount
is incremented by one.
Data should be added in order of preference, with the most specific format added first and the least specific format added last. If data of the given format already exists, it is replaced in the same position as the old data.
The data should be either a string, a primitive boolean or number type (which will be converted into a string) or an nsISupports
.
void mozSetDataAt( [optional] in String type, in nsIVariant data, in unsigned long index );
Parameters
type
- The type of data to add.
data
- The data to add.
index
- The index of the data to add.
mozTypesAt()
Holds a list of the format types of the data that is stored for an item at the specified index. If the index is not in the range from 0 to the number of items minus one, an empty string list is returned.
nsIVariant mozTypesAt( in unsigned long index );
Parameters
index
- The index of the data for which to retrieve the types.