Package pyjamas :: Package dnd :: Module DataTransfer
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.dnd.DataTransfer

  1  from pyjamas.Timer import Timer 
  2  from pyjamas.dnd.utils import cloneElement, DOMStringList 
  3   
  4  from pyjamas.dnd import READ_ONLY, READ_WRITE, PROTECTED, DISABLED 
  5   
6 -def identity(obj):
7 return obj
8
9 -class DataTransferItem(object):
10 """ 11 this is an in-browser string-kind-only implementation. 12 see: http://http://dev.w3.org/html5/spec/dnd.html#datatransferitem 13 """
14 - def __init__(self, type, data):
15 self._type = type 16 self._data = data
17
18 - def getType(self):
19 if self.mode == DISABLED: 20 return '' 21 return self._type
22 type = property(getType) 23
24 - def getKind(self):
25 if self.mode == DISABLED: 26 return '' 27 return 'string'
28 kind = property(getKind) 29
30 - def getMode(self):
31 return self._mode
32 # return DISABLED 33
34 - def setMode(self, mode):
35 self._mode = mode
36 mode = property(getMode, setMode) 37
38 - def getAsString(self, callback=None):
39 if callback and self.kind == 'string': 40 if self.mode in [READ_WRITE, READ_ONLY]: 41 callback(self._data)
42
43 - def __str__(self):
44 return self._data
45
46 -class DataTransferItems(list):
47
48 - def getMode(self):
49 return self._mode
50 - def setMode(self, mode):
51 self._mode = mode 52 for item in self: 53 item.mode = mode
54 mode = property(getMode, setMode) 55 56 # ack! length is a javascript keyword! Phoo! 57 # def getLength(self): 58 # if self.mode == DISABLED: 59 # return 0 60 # return self.__len__() 61 # length = property(getLength) 62
63 - def __delitem__(self, key):
64 if not self.mode == READ_WRITE: 65 raise Exception('Cannot delete item except in READ_WRITE mode.') 66 else: 67 list.__delitem__(self, key)
68
69 - def __len__(self):
70 if self.mode == DISABLED: 71 return 0 72 return list.__len__(self)
73
74 - def clear(self):
75 if self.mode == READ_WRITE: 76 while len(self): 77 del self[-1]
78
79 - def add(self, data, mime):
80 if self.mode == READ_WRITE: 81 item = DataTransferItem(mime.lower(), data) 82 self.append(item) 83 idx = self.index(item) 84 return self[idx]
85 86 87
88 -class DragDataStore(object):
89 _mode = PROTECTED
90 - def __init__(self):
91 self.items = DataTransferItems() 92 self.default_feedback = None 93 self.elements = [] 94 self.bitmap = None 95 self.hotspot_coordinate = None 96 self.allowed_effects_state = 'uninitialized'
97
98 - def setMode(self, mode):
99 if mode in [PROTECTED, READ_WRITE, READ_ONLY]: 100 self._mode = mode 101 self.items.mode = mode
102
103 - def getMode(self):
104 return self._mode
105 mode = property(getMode, setMode)
106 107
108 -class DataTransfer(object):
109 """ 110 DataTransfer implementation 111 http://dev.w3.org/html5/spec/dnd.html#datatransfer 112 """ 113
114 - def __init__(self, dataStore):
115 self.dataStore = dataStore 116 self.setEffectAllowed(dataStore.allowed_effects_state) 117 self._dropEffect = 'none' 118 self._value = None
119
120 - def acquireData(self, value):
121 self._value = value
122
123 - def setDropEffect(self, effect):
124 if effect in ('none', 'copy', 'link' ,'move'): 125 self._dropEffect = effect
126
127 - def getDropEffect(self):
128 return self._dropEffect
129 130 dropEffect = property(getDropEffect, setDropEffect) 131
132 - def setEffectAllowed(self, effect):
133 if effect in ('none', 'copy', 'copyLink', 'copyMove', 134 'link', 'linkMove', 'move', 'all', 'uninitialized'): 135 self._effectAllowed = effect
136
137 - def getEffectAllowed(self):
138 return self._effectAllowed
139 140 effectAllowed = property(getEffectAllowed, setEffectAllowed) 141
142 - def setData(self, format, data):
143 if self.dataStore.mode == READ_WRITE: 144 format = format.strip().lower() 145 if format == 'text': 146 format = 'text/plain' 147 elif format == 'url': 148 format = 'text/uri-list' 149 self.clearData(format) 150 items = self.dataStore.items 151 z = items.add(data, format) 152 z.mode = READ_WRITE
153
154 - def getTypes(self):
155 theList = [] 156 if self.dataStore is not None: 157 items = self.dataStore.items 158 for item in items: 159 theList.append(item.type) 160 return DOMStringList(theList)
161 162 types = property(getTypes) 163
164 - def getData(self, format):
165 format = format.strip().lower() 166 if format == 'text': 167 format = 'text/plain' 168 elif format == 'url': 169 format = 'text/uri-list' 170 theList = self.dataStore.items 171 for item in theList: 172 if item.type == format: 173 item.getAsString(self.acquireData) 174 return self._value 175 return ''
176
177 - def clearData(self, format=None):
178 thelist = self.dataStore.items 179 if self.dataStore is not None: 180 if format is None: 181 thelist.clear() 182 else: 183 format = format.lower() 184 to_del = [] 185 for idx, item in enumerate(thelist): 186 if item.type == format: 187 to_del.append(idx) 188 to_del.reverse() 189 for idx in to_del: 190 del thelist[idx]
191
192 - def addElement(self, element):
193 self.dataStore.elements.append(cloneElement(element))
194
195 - def setDragImage(self, element, x=0, y=0):
196 self.dataStore.bitmap = element 197 self.dataStore.hotspot_coordinate = (x, y, )
198