Debian Codegraph @

dojox/mobile/_EditableListMixin.js

raw symbols SCIP
1 // TODO: auto scroll?
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 // module:
20 // dojox/mobile/EditableRoundRectList
21
22 return declare("dojox.mobile._EditableListMixin", null, {
23 // summary:
24 // A rounded rectangle list.
25 // description:
26 // EditableRoundRectList is a rounded rectangle list, which can be used to
27 // display a group of items. Each item must be a dojox/mobile/ListItem.
28
29 rightIconForEdit: "mblDomButtonGrayKnob",
30 deleteIconForEdit: "mblDomButtonRedCircleMinus",
31
32 // isEditing: Boolean
33 // A read-only flag that indicates whether the widget is in the editing mode.
34 isEditing: false,
35
36 destroy: function(){
37 // summary:
38 // Destroys the widget.
39 if(this._blankItem){
40 this._blankItem.destroy();
41 }
42 this.inherited(arguments);
43 },
44
45 _setupMoveItem: function(/*DomNode*/node){
46 // tags:
47 // private
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(/*DomNode*/node){
56 // tags:
57 // private
58 this.defer(function(){ // iPhone needs setTimeout (via defer)
59 domClass.remove(node, "mblListItemFloat");
60 domStyle.set(node, {
61 width: "",
62 top: ""
63 });
64 });
65 },
66
67 _onClick: function(e){
68 // summary:
69 // Internal handler for click events.
70 // tags:
71 // private
72 if(e && e.type === "keydown" && e.keyCode !== 13){ return; }
73 if(this.onClick(e) === false){ return; } // user's click action
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); //callback
79 break;
80 }
81 }
82 },
83
84 onClick: function(/*Event*/ /*===== e =====*/){
85 // summary:
86 // User-defined function to handle clicks.
87 // tags:
88 // callback
89 },
90
91 _onTouchStart: function(e){
92 // tags:
93 // private
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 // tags:
133 // private
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 // tags:
157 // private
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 //If the item is droped at the end of the list the endIndex is wrong
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); //callback
175
176 domAttr.set(this._movingItem.rightIconNode, "aria-grabbed", "false");
177 domAttr.remove(this.domNode, "aria-dropeffect");
178 },
179
180 startEdit: function(){
181 // summary:
182 // Starts the editing.
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") // for desktop browsers
207 ];
208 }
209
210 this.onStartEdit(); // callback
211 },
212
213 endEdit: function(){
214 // summary:
215 // Ends the editing.
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(); // callback
229 },
230
231
232 onDeleteItem: function(/*Widget*/item){
233 // summary:
234 // Stub function to connect to from your application.
235 // description:
236 // This function is called when a user clicks the delete
237 // button.
238 // You have to provide that function or subscribe to /dojox/mobile/deleteListItem,
239 // otherwise the delete button will have no-effect.
240
241 },
242
243 onMoveItem: function(/*Widget*/item, /*int*/from, /*int*/to){
244 // summary:
245 // Stub function to connect to from your application.
246 },
247
248 onStartEdit: function(){
249 // summary:
250 // Stub function to connect to from your application.
251 },
252
253 onEndEdit: function(){
254 // summary:
255 // Stub function to connect to from your application.
256 }
257
258
259 });
260 });