| 1 |
|
| 2 |
|
| 3 |
define([ |
| 4 |
"dojo/_base/array", |
| 5 |
"dojo/_base/connect", |
| 6 |
"dojo/_base/declare", |
| 7 |
"dojo/_base/event", |
| 8 |
"dojo/_base/window", |
| 9 |
"dojo/dom-class", |
| 10 |
"dojo/dom-geometry", |
| 11 |
"dojo/dom-style", |
| 12 |
"dojo/touch", |
| 13 |
"dojo/dom-attr", |
| 14 |
"dijit/registry", |
| 15 |
"./ListItem", |
| 16 |
"./common" |
| 17 |
], function(array, connect, declare, event, win, domClass, domGeometry, domStyle, touch, domAttr, registry, ListItem, common){ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
return declare("dojox.mobile._EditableListMixin", null, { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
rightIconForEdit: "mblDomButtonGrayKnob", |
| 30 |
deleteIconForEdit: "mblDomButtonRedCircleMinus", |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
isEditing: false, |
| 35 |
|
| 36 |
destroy: function(){ |
| 37 |
|
| 38 |
|
| 39 |
if(this._blankItem){ |
| 40 |
this._blankItem.destroy(); |
| 41 |
} |
| 42 |
this.inherited(arguments); |
| 43 |
}, |
| 44 |
|
| 45 |
_setupMoveItem: function(node){ |
| 46 |
|
| 47 |
|
| 48 |
domStyle.set(node, { |
| 49 |
width: domGeometry.getContentBox(node).w + "px", |
| 50 |
top: node.offsetTop + "px" |
| 51 |
}); |
| 52 |
domClass.add(node, "mblListItemFloat"); |
| 53 |
}, |
| 54 |
|
| 55 |
_resetMoveItem: function(node){ |
| 56 |
|
| 57 |
|
| 58 |
this.defer(function(){ |
| 59 |
domClass.remove(node, "mblListItemFloat"); |
| 60 |
domStyle.set(node, { |
| 61 |
width: "", |
| 62 |
top: "" |
| 63 |
}); |
| 64 |
}); |
| 65 |
}, |
| 66 |
|
| 67 |
_onClick: function(e){ |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if(e && e.type === "keydown" && e.keyCode !== 13){ return; } |
| 73 |
if(this.onClick(e) === false){ return; } |
| 74 |
var item = registry.getEnclosingWidget(e.target); |
| 75 |
for(var n = e.target; n !== item.domNode; n = n.parentNode){ |
| 76 |
if(n === item.deleteIconNode){ |
| 77 |
connect.publish("/dojox/mobile/deleteListItem", [item]); |
| 78 |
this.onDeleteItem(item); |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
}, |
| 83 |
|
| 84 |
onClick: function( ){ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
}, |
| 90 |
|
| 91 |
_onTouchStart: function(e){ |
| 92 |
|
| 93 |
|
| 94 |
if(this.getChildren().length <= 1){ return; } |
| 95 |
if(!this._blankItem){ |
| 96 |
this._blankItem = new ListItem(); |
| 97 |
} |
| 98 |
var item = this._movingItem = registry.getEnclosingWidget(e.target); |
| 99 |
this._startIndex = this.getIndexOfChild(item); |
| 100 |
var rightIconPressed = false; |
| 101 |
for(var n = e.target; n !== item.domNode; n = n.parentNode){ |
| 102 |
if(n === item.rightIconNode){ |
| 103 |
rightIconPressed = true; |
| 104 |
domAttr.set(item.rightIconNode, "aria-grabbed", "true"); |
| 105 |
domAttr.set(this.domNode, "aria-dropeffect", "move"); |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
if(!rightIconPressed){ return; } |
| 110 |
var ref = item.getNextSibling(); |
| 111 |
ref = ref ? ref.domNode : null; |
| 112 |
this.containerNode.insertBefore(this._blankItem.domNode, ref); |
| 113 |
this._setupMoveItem(item.domNode); |
| 114 |
this.containerNode.appendChild(item.domNode); |
| 115 |
|
| 116 |
if(!this._conn){ |
| 117 |
this._conn = [ |
| 118 |
this.connect(this.domNode, touch.move, "_onTouchMove"), |
| 119 |
this.connect(win.doc, touch.release, "_onTouchEnd") |
| 120 |
]; |
| 121 |
} |
| 122 |
this._pos = []; |
| 123 |
array.forEach(this.getChildren(), function(c, index){ |
| 124 |
this._pos.push(domGeometry.position(c.domNode, true).y); |
| 125 |
}, this); |
| 126 |
this.touchStartY = e.touches ? e.touches[0].pageY : e.pageY; |
| 127 |
this._startTop = domGeometry.getMarginBox(item.domNode).t; |
| 128 |
event.stop(e); |
| 129 |
}, |
| 130 |
|
| 131 |
_onTouchMove: function(e){ |
| 132 |
|
| 133 |
|
| 134 |
var y = e.touches ? e.touches[0].pageY : e.pageY; |
| 135 |
var index = this._pos.length - 1; |
| 136 |
for(var i = 1; i < this._pos.length; i++){ |
| 137 |
if(y < this._pos[i]){ |
| 138 |
index = i - 1; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
var item = this.getChildren()[index]; |
| 143 |
var blank = this._blankItem; |
| 144 |
if(item !== blank){ |
| 145 |
var p = item.domNode.parentNode; |
| 146 |
if(item.getIndexInParent() < blank.getIndexInParent()){ |
| 147 |
p.insertBefore(blank.domNode, item.domNode); |
| 148 |
}else{ |
| 149 |
p.insertBefore(item.domNode, blank.domNode); |
| 150 |
} |
| 151 |
} |
| 152 |
this._movingItem.domNode.style.top = this._startTop + (y - this.touchStartY) + "px"; |
| 153 |
}, |
| 154 |
|
| 155 |
_onTouchEnd: function(e){ |
| 156 |
|
| 157 |
|
| 158 |
var startIndex = this._startIndex; |
| 159 |
var endIndex = this.getIndexOfChild(this._blankItem); |
| 160 |
|
| 161 |
var ref = this._blankItem.getNextSibling(); |
| 162 |
ref = ref ? ref.domNode : null; |
| 163 |
if(ref === null){ |
| 164 |
|
| 165 |
endIndex--; |
| 166 |
} |
| 167 |
this.containerNode.insertBefore(this._movingItem.domNode, ref); |
| 168 |
this.containerNode.removeChild(this._blankItem.domNode); |
| 169 |
this._resetMoveItem(this._movingItem.domNode); |
| 170 |
|
| 171 |
array.forEach(this._conn, connect.disconnect); |
| 172 |
this._conn = null; |
| 173 |
|
| 174 |
this.onMoveItem(this._movingItem, startIndex, endIndex); |
| 175 |
|
| 176 |
domAttr.set(this._movingItem.rightIconNode, "aria-grabbed", "false"); |
| 177 |
domAttr.remove(this.domNode, "aria-dropeffect"); |
| 178 |
}, |
| 179 |
|
| 180 |
startEdit: function(){ |
| 181 |
|
| 182 |
|
| 183 |
this.isEditing = true; |
| 184 |
domClass.add(this.domNode, "mblEditableRoundRectList"); |
| 185 |
array.forEach(this.getChildren(), function(child){ |
| 186 |
if(!child.deleteIconNode){ |
| 187 |
child.set("rightIcon", this.rightIconForEdit); |
| 188 |
if(child.rightIconNode){ |
| 189 |
domAttr.set(child.rightIconNode, "role", "button"); |
| 190 |
domAttr.set(child.rightIconNode, "aria-grabbed", "false"); |
| 191 |
} |
| 192 |
child.set("deleteIcon", this.deleteIconForEdit); |
| 193 |
child.deleteIconNode.tabIndex = child.tabIndex; |
| 194 |
if(child.deleteIconNode){ |
| 195 |
domAttr.set(child.deleteIconNode, "role", "button"); |
| 196 |
} |
| 197 |
} |
| 198 |
child.rightIconNode.style.display = ""; |
| 199 |
child.deleteIconNode.style.display = ""; |
| 200 |
common._setTouchAction(child.rightIconNode, "none"); |
| 201 |
}, this); |
| 202 |
if(!this._handles){ |
| 203 |
this._handles = [ |
| 204 |
this.connect(this.domNode, touch.press, "_onTouchStart"), |
| 205 |
this.connect(this.domNode, "onclick", "_onClick"), |
| 206 |
this.connect(this.domNode, "onkeydown", "_onClick") |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
this.onStartEdit(); |
| 211 |
}, |
| 212 |
|
| 213 |
endEdit: function(){ |
| 214 |
|
| 215 |
|
| 216 |
domClass.remove(this.domNode, "mblEditableRoundRectList"); |
| 217 |
array.forEach(this.getChildren(), function(child){ |
| 218 |
child.rightIconNode.style.display = "none"; |
| 219 |
child.deleteIconNode.style.display = "none"; |
| 220 |
common._setTouchAction(child.rightIconNode, "auto"); |
| 221 |
}); |
| 222 |
if(this._handles){ |
| 223 |
array.forEach(this._handles, this.disconnect, this); |
| 224 |
this._handles = null; |
| 225 |
} |
| 226 |
this.isEditing = false; |
| 227 |
|
| 228 |
this.onEndEdit(); |
| 229 |
}, |
| 230 |
|
| 231 |
|
| 232 |
onDeleteItem: function(item){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
}, |
| 242 |
|
| 243 |
onMoveItem: function(item, from, to){ |
| 244 |
|
| 245 |
|
| 246 |
}, |
| 247 |
|
| 248 |
onStartEdit: function(){ |
| 249 |
|
| 250 |
|
| 251 |
}, |
| 252 |
|
| 253 |
onEndEdit: function(){ |
| 254 |
|
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
}); |
| 260 |
}); |