Debian Codegraph @

dojox/mobile/ListItem.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/dom-class",
6 "dojo/dom-construct",
7 "dojo/dom-style",
8 "dojo/dom-attr",
9 "dijit/registry",
10 "dijit/_WidgetBase",
11 "./iconUtils",
12 "./_ItemBase",
13 "./ProgressIndicator",
14 "dojo/has",
15 "dojo/has!dojo-bidi?dojox/mobile/bidi/ListItem"
16 ], function(array, declare, lang, domClass, domConstruct, domStyle, domAttr, registry, WidgetBase, iconUtils, ItemBase, ProgressIndicator, has, BidiListItem){
17
18 // module:
19 // dojox/mobile/ListItem
20
21 var ListItem = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiListItem" : "dojox.mobile.ListItem", ItemBase, {
22 // summary:
23 // An item of either RoundRectList or EdgeToEdgeList.
24 // description:
25 // ListItem represents an item of either RoundRectList or
26 // EdgeToEdgeList. There are three ways to move to a different view:
27 // moveTo, href, and url. You can choose only one of them.
28 //
29 // A child DOM node (or widget) can have the layout attribute,
30 // whose value is "left", "right", or "center". Such nodes will be
31 // aligned as specified.
32 // example:
33 // | <li data-dojo-type="dojox.mobile.ListItem">
34 // | <div layout="left">Left Node</div>
35 // | <div layout="right">Right Node</div>
36 // | <div layout="center">Center Node</div>
37 // | </li>
38 //
39 // Similarly, a child widget can have the preventTouch
40 // attribute, whose value is a boolean (or data-mobile-prevent-touch
41 // for children which are not widgets), such that touching such
42 // child doesn't trigger the item action.
43 //
44 // Note that even if you specify variableHeight="true" for the list
45 // and place a tall object inside the layout node as in the example
46 // below, the layout node does not expand as you may expect,
47 // because layout node is aligned using float:left, float:right, or
48 // position:absolute.
49 // example:
50 // | <li data-dojo-type="dojox.mobile.ListItem" variableHeight="true">
51 // | <div layout="left"><img src="large-picture.jpg"></div>
52 // | </li>
53
54 // rightText: String
55 // A right-aligned text to display on the item.
56 rightText: "",
57
58 // rightIcon: String
59 // An icon to display at the right hand side of the item. The value
60 // can be either a path for an image file or a class name of a DOM
61 // button.
62 rightIcon: "",
63
64 // rightIcon2: String
65 // An icon to display at the left of the rightIcon. The value can
66 // be either a path for an image file or a class name of a DOM
67 // button.
68 rightIcon2: "",
69
70 // deleteIcon: String
71 // A delete icon to display at the left of the item. The value can
72 // be either a path for an image file or a class name of a DOM
73 // button.
74 deleteIcon: "",
75
76 // anchorLabel: Boolean
77 // If true, the label text becomes a clickable anchor text. When
78 // the user clicks on the text, the onAnchorLabelClicked handler is
79 // called. You can override or connect to the handler and implement
80 // any action. The handler has no default action.
81 anchorLabel: false,
82
83 // noArrow: Boolean
84 // If true, the right hand side arrow is not displayed.
85 noArrow: false,
86
87 // checked: Boolean
88 // If true, a check mark is displayed at the right of the item.
89 checked: false,
90
91 // arrowClass: String
92 // An icon to display as an arrow. The value can be either a path
93 // for an image file or a class name of a DOM button.
94 arrowClass: "",
95
96 // checkClass: String
97 // An icon to display as a check mark. The value can be either a
98 // path for an image file or a class name of a DOM button.
99 checkClass: "",
100
101 // uncheckClass: String
102 // An icon to display as an uncheck mark. The value can be either a
103 // path for an image file or a class name of a DOM button.
104 uncheckClass: "",
105
106 // variableHeight: Boolean
107 // If true, the height of the item varies according to its content.
108 variableHeight: false,
109
110 // rightIconTitle: String
111 // An alt text for the right icon.
112 rightIconTitle: "",
113
114 // rightIcon2Title: String
115 // An alt text for the right icon2.
116 rightIcon2Title: "",
117
118 // header: Boolean
119 // If true, this item is rendered as a category header.
120 header: false,
121
122 // tag: String
123 // A name of html tag to create as domNode.
124 tag: "li",
125
126 // busy: Boolean
127 // If true, a progress indicator spins.
128 busy: false,
129
130 // progStyle: String
131 // A css class name to add to the progress indicator.
132 progStyle: "",
133
134 // layoutOnResize: Boolean
135 // If true, a call to resize() will force computation of variable height items layout. You should not need this as in most
136 // cases ListItem height doesn't change on container resize. Depending on number and complexity
137 // of items in a view, setting to true may have a high impact on performance.
138 layoutOnResize: false,
139
140 /* internal properties */
141 // The following properties are overrides of those in _ItemBase.
142 paramsToInherit: "variableHeight,transition,deleteIcon,icon,rightIcon,rightIcon2,uncheckIcon,arrowClass,checkClass,uncheckClass,deleteIconTitle,deleteIconRole",
143 baseClass: "mblListItem",
144
145 _selStartMethod: "touch",
146 _selEndMethod: "timer",
147 _delayedSelection: true,
148
149 _selClass: "mblListItemSelected",
150
151 buildRendering: function(){
152 this._templated = !!this.templateString; // true if this widget is templated
153 if(!this._templated){
154 // Create root node if it wasn't created by _TemplatedMixin
155 this.domNode = this.containerNode = this.srcNodeRef || domConstruct.create(this.tag);
156 }
157 this.inherited(arguments);
158
159 if(this.selected){
160 domClass.add(this.domNode, this._selClass);
161 }
162 if(this.header){
163 domClass.replace(this.domNode, "mblEdgeToEdgeCategory", this.baseClass);
164 }
165
166 if(!this._templated){
167 this.labelNode =
168 domConstruct.create("div", {className:"mblListItemLabel"});
169 var ref = this.srcNodeRef;
170 if(ref && ref.childNodes.length === 1 && ref.firstChild.nodeType === 3){
171 // if ref has only one text node, regard it as a label
172 this.labelNode.appendChild(ref.firstChild);
173 }
174 this.domNode.appendChild(this.labelNode);
175 }
176 this._layoutChildren = [];
177 },
178
179 startup: function(){
180 if(this._started){ return; }
181 var parent = this.getParent();
182 var opts = this.getTransOpts();
183 // When using a template, labelNode may be created via an attach point.
184 // The attach points are not yet set when ListItem.buildRendering()
185 // executes, hence the need to use them in startup().
186 if((!this._templated || this.labelNode) && this.anchorLabel){
187 this.labelNode.style.display = "inline"; // to narrow the text region
188 this.labelNode.style.cursor = "pointer";
189 this.connect(this.labelNode, "onclick", "_onClick");
190 this.onTouchStart = function(e){
191 return (e.target !== this.labelNode);
192 };
193 }
194
195 this.inherited(arguments);
196
197 if(domClass.contains(this.domNode, "mblVariableHeight")){
198 this.variableHeight = true;
199 }
200 if(this.variableHeight){
201 domClass.add(this.domNode, "mblVariableHeight");
202 this.defer("layoutVariableHeight");
203 }
204
205 if(!this._isOnLine){
206 this._isOnLine = true;
207 this.set({
208 // retry applying the attributes for which the custom setter delays the actual
209 // work until _isOnLine is true
210 icon: this._pending_icon !== undefined ? this._pending_icon : this.icon,
211 deleteIcon: this._pending_deleteIcon !== undefined ? this._pending_deleteIcon : this.deleteIcon,
212 rightIcon: this._pending_rightIcon !== undefined ? this._pending_rightIcon : this.rightIcon,
213 rightIcon2: this._pending_rightIcon2 !== undefined ? this._pending_rightIcon2 : this.rightIcon2,
214 uncheckIcon: this._pending_uncheckIcon !== undefined ? this._pending_uncheckIcon : this.uncheckIcon
215 });
216 // Not needed anymore (this code executes only once per life cycle):
217 delete this._pending_icon;
218 delete this._pending_deleteIcon;
219 delete this._pending_rightIcon;
220 delete this._pending_rightIcon2;
221 delete this._pending_uncheckIcon;
222 }
223 if(parent && parent.select){
224 // retry applying the attributes for which the custom setter delays the actual
225 // work until _isOnLine is true.
226 this.set("checked", this._pendingChecked !== undefined ? this._pendingChecked : this.checked);
227 domAttr.set(this.domNode, "role", "option");
228 if(this._pendingChecked || this.checked){
229 domAttr.set(this.domNode, "aria-selected", "true");
230 }
231 // Not needed anymore (this code executes only once per life cycle):
232 delete this._pendingChecked;
233 }
234 this.setArrow();
235 this.layoutChildren();
236 },
237
238 _updateHandles: function(){
239 // tags:
240 // private
241 var parent = this.getParent();
242 var opts = this.getTransOpts();
243 if(opts.moveTo || opts.href || opts.url || this.clickable || (parent && parent.select)){
244 if(!this._keydownHandle){
245 this._keydownHandle = this.connect(this.domNode, "onkeydown", "_onClick"); // for desktop browsers
246 }
247 this._handleClick = true;
248 }else{
249 if(this._keydownHandle){
250 this.disconnect(this._keydownHandle);
251 this._keydownHandle = null;
252 }
253 this._handleClick = false;
254 }
255 this.inherited(arguments);
256 },
257
258 layoutChildren: function(){
259 var centerNode;
260 array.forEach(this.domNode.childNodes, function(n){
261 if(n.nodeType !== 1){ return; }
262 var layout = n.getAttribute("layout") || // TODO: Remove the non-HTML5-compliant attribute in 2.0
263 n.getAttribute("data-mobile-layout") ||
264 (registry.byNode(n) || {}).layout;
265 if(layout){
266 domClass.add(n, "mblListItemLayout" +
267 layout.charAt(0).toUpperCase() + layout.substring(1));
268 this._layoutChildren.push(n);
269 if(layout === "center"){ centerNode = n; }
270 }
271 }, this);
272 if(centerNode){
273 this.domNode.insertBefore(centerNode, this.domNode.firstChild);
274 }
275 },
276
277 resize: function(){
278 if(this.layoutOnResize && this.variableHeight){
279 this.layoutVariableHeight();
280 }
281 // labelNode may not exist only when using a template (if not created by an attach point)
282 if(!this._templated || this.labelNode){
283 // If labelNode is empty, shrink it so as not to prevent user clicks.
284 this.labelNode.style.display = this.labelNode.firstChild ? "block" : "inline";
285 }
286 },
287
288 _onTouchStart: function(e){
289 // tags:
290 // private
291 if(e.target.getAttribute("preventTouch") || // TODO: Remove the non-HTML5-compliant attribute in 2.0
292 e.target.getAttribute("data-mobile-prevent-touch") ||
293 (registry.getEnclosingWidget(e.target) || {}).preventTouch){
294 return;
295 }
296 this.inherited(arguments);
297 },
298
299 _onClick: function(e){
300 // summary:
301 // Internal handler for click events.
302 // tags:
303 // private
304 if(this.getParent().isEditing || e && e.type === "keydown" && e.keyCode !== 13){ return; }
305 if(this.onClick(e) === false){ return; } // user's click action
306 var n = this.labelNode;
307 // labelNode may not exist only when using a template
308 if((this._templated || n) && this.anchorLabel && e.currentTarget === n){
309 domClass.add(n, "mblListItemLabelSelected");
310 this.defer(function(){
311 domClass.remove(n, "mblListItemLabelSelected");
312 }, this._duration);
313 this.onAnchorLabelClicked(e);
314 return;
315 }
316 var parent = this.getParent();
317 if(parent.select){
318 if(parent.select === "single"){
319 if(!this.checked){
320 this.set("checked", true);
321 }
322 }else if(parent.select === "multiple"){
323 this.set("checked", !this.checked);
324 }
325 }
326 this.defaultClickAction(e);
327 },
328
329 onClick: function(/*Event*/ /*===== e =====*/){
330 // summary:
331 // User-defined function to handle clicks.
332 // tags:
333 // callback
334 },
335
336 onAnchorLabelClicked: function(e){
337 // summary:
338 // Stub function to connect to from your application.
339 },
340
341 layoutVariableHeight: function(){
342 // summary:
343 // Lays out the current item with variable height.
344 var h = this.domNode.offsetHeight;
345 if(h === this.domNodeHeight){ return; }
346 this.domNodeHeight = h;
347 array.forEach(this._layoutChildren.concat([
348 this.rightTextNode,
349 this.rightIcon2Node,
350 this.rightIconNode,
351 this.uncheckIconNode,
352 this.iconNode,
353 this.deleteIconNode,
354 this.knobIconNode
355 ]), function(n){
356 if(n){
357 var domNode = this.domNode;
358 var f = function(){
359 var t = Math.round((domNode.offsetHeight - n.offsetHeight) / 2) -
360 domStyle.get(domNode, "paddingTop");
361 n.style.marginTop = t + "px";
362 };
363 if(n.offsetHeight === 0 && n.tagName === "IMG"){
364 n.onload = f;
365 }else{
366 f();
367 }
368 }
369 }, this);
370 },
371
372 setArrow: function(){
373 // summary:
374 // Sets the arrow icon if necessary.
375 if(this.checked){ return; }
376 var c = "";
377 var parent = this.getParent();
378 var opts = this.getTransOpts();
379 if(opts.moveTo || opts.href || opts.url || this.clickable){
380 if(!this.noArrow && !(parent && parent.selectOne)){
381 c = this.arrowClass || "mblDomButtonArrow";
382 domAttr.set(this.domNode, "role", "button");
383 }
384 }
385 if(c){
386 this._setRightIconAttr(c);
387 }
388 },
389
390 _findRef: function(/*String*/type){
391 // summary:
392 // Find an appropriate position to insert a new child node.
393 // tags:
394 // private
395 var i, node, list = ["deleteIcon", "icon", "rightIcon", "uncheckIcon", "rightIcon2", "rightText"];
396 for(i = array.indexOf(list, type) + 1; i < list.length; i++){
397 node = this[list[i] + "Node"];
398 if(node){ return node; }
399 }
400 for(i = list.length - 1; i >= 0; i--){
401 node = this[list[i] + "Node"];
402 if(node){ return node.nextSibling; }
403 }
404 return this.domNode.firstChild;
405 },
406
407 _setIcon: function(/*String*/icon, /*String*/type){
408 // tags:
409 // private
410 if(!this._isOnLine){
411 // record the value to be able to reapply it (see the code in the startup method)
412 this["_pending_" + type] = icon;
413 return;
414 } // icon may be invalid because inheritParams is not called yet
415 this._set(type, icon);
416 this[type + "Node"] = iconUtils.setIcon(icon, this[type + "Pos"],
417 this[type + "Node"], this[type + "Title"] || this.alt, this.domNode, this._findRef(type), "before");
418 if(this[type + "Node"]){
419 var cap = type.charAt(0).toUpperCase() + type.substring(1);
420 domClass.add(this[type + "Node"], "mblListItem" + cap);
421 }
422 var role = this[type + "Role"];
423 if(role){
424 this[type + "Node"].setAttribute("role", role);
425 }
426 },
427
428 _setDeleteIconAttr: function(/*String*/icon){
429 // tags:
430 // private
431 this._setIcon(icon, "deleteIcon");
432 },
433
434 _setIconAttr: function(icon){
435 // tags:
436 // private
437 this._setIcon(icon, "icon");
438 },
439
440 _setRightTextAttr: function(/*String*/text){
441 // tags:
442 // private
443 if(!this._templated && !this.rightTextNode){
444 // When using a template, let the template create the element.
445 this.rightTextNode = domConstruct.create("div", {className:"mblListItemRightText"}, this.labelNode, "before");
446 }
447 this.rightText = text;
448 this.rightTextNode.innerHTML = this._cv ? this._cv(text) : text;
449 },
450
451 _setRightIconAttr: function(/*String*/icon){
452 // tags:
453 // private
454 this._setIcon(icon, "rightIcon");
455 },
456
457 _setUncheckIconAttr: function(/*String*/icon){
458 // tags:
459 // private
460 this._setIcon(icon, "uncheckIcon");
461 },
462
463 _setRightIcon2Attr: function(/*String*/icon){
464 // tags:
465 // private
466 this._setIcon(icon, "rightIcon2");
467 },
468
469 _setCheckedAttr: function(/*Boolean*/checked){
470 // tags:
471 // private
472 if(!this._isOnLine){
473 // record the value to be able to reapply it (see the code in the startup method)
474 this._pendingChecked = checked;
475 return;
476 } // icon may be invalid because inheritParams is not called yet
477 var parent = this.getParent();
478 if(parent && parent.select === "single" && checked){
479 array.forEach(parent.getChildren(), function(child){
480 child !== this && child.checked && child.set("checked", false) && domAttr.set(child.domNode, "aria-selected", "false");
481 }, this);
482 }
483 this._setRightIconAttr(this.checkClass || "mblDomButtonCheck");
484 this._setUncheckIconAttr(this.uncheckClass);
485
486 domClass.toggle(this.domNode, "mblListItemChecked", checked);
487 domClass.toggle(this.domNode, "mblListItemUnchecked", !checked);
488 domClass.toggle(this.domNode, "mblListItemHasUncheck", !!this.uncheckIconNode);
489 this.rightIconNode.style.position = (this.uncheckIconNode && !checked) ? "absolute" : "";
490
491 if(parent && this.checked !== checked){
492 parent.onCheckStateChanged(this, checked);
493 }
494 this._set("checked", checked);
495 domAttr.set(this.domNode, "aria-selected", checked ? "true" : "false");
496 },
497
498 _setBusyAttr: function(/*Boolean*/busy){
499 // tags:
500 // private
501 var prog = this._prog;
502 if(busy){
503 if(!this._progNode){
504 this._progNode = domConstruct.create("div", {className:"mblListItemIcon"});
505 prog = this._prog = new ProgressIndicator({size:25, center:false, removeOnStop:false});
506 domClass.add(prog.domNode, this.progStyle);
507 this._progNode.appendChild(prog.domNode);
508 }
509 if(this.iconNode){
510 this.domNode.replaceChild(this._progNode, this.iconNode);
511 }else{
512 domConstruct.place(this._progNode, this._findRef("icon"), "before");
513 }
514 prog.start();
515 }else if(this._progNode){
516 if(this.iconNode){
517 this.domNode.replaceChild(this.iconNode, this._progNode);
518 }else{
519 this.domNode.removeChild(this._progNode);
520 }
521 prog.stop();
522 }
523 this._set("busy", busy);
524 },
525
526 _setSelectedAttr: function(/*Boolean*/selected){
527 // summary:
528 // Makes this widget in the selected or unselected state.
529 // tags:
530 // private
531 this.inherited(arguments);
532 domClass.toggle(this.domNode, this._selClass, selected);
533 },
534
535 _setClickableAttr: function(/*Boolean*/clickable){
536 // tags:
537 // private
538 this._set("clickable", clickable);
539 this._updateHandles();
540 },
541
542 _setMoveToAttr: function(/*String*/moveTo){
543 // tags:
544 // private
545 this._set("moveTo", moveTo);
546 this._updateHandles();
547 },
548
549 _setHrefAttr: function(/*String*/href){
550 // tags:
551 // private
552 this._set("href", href);
553 this._updateHandles();
554 },
555
556 _setUrlAttr: function(/*String*/url){
557 // tags:
558 // private
559 this._set("url", url);
560 this._updateHandles();
561 }
562 });
563
564 ListItem.ChildWidgetProperties = {
565 // summary:
566 // These properties can be specified for the children of a dojox/mobile/ListItem.
567
568 // layout: String
569 // Specifies the position of the ListItem child ("left", "center" or "right").
570 layout: "",
571
572 // preventTouch: Boolean
573 // Disables touch events on the ListItem child.
574 preventTouch: false
575 };
576
577 // Since any widget can be specified as a ListItem child, mix ChildWidgetProperties
578 // into the base widget class. (This is a hack, but it's effective.)
579 // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
580 lang.extend(WidgetBase, /*===== {} || =====*/ ListItem.ChildWidgetProperties);
581
582 return has("dojo-bidi") ? declare("dojox.mobile.ListItem", [ListItem, BidiListItem]) : ListItem;
583 });