Debian Codegraph @

dojox/mobile/_ItemBase.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/_base/window",
6 "dojo/dom-class",
7 "dojo/touch",
8 "dijit/registry",
9 "dijit/_Contained",
10 "dijit/_Container",
11 "dijit/_WidgetBase",
12 "./TransitionEvent",
13 "./iconUtils",
14 "./sniff",
15 "./viewRegistry",
16 "dojo/has!dojo-bidi?dojox/mobile/bidi/_ItemBase"
17 ], function(array, declare, lang, win, domClass, touch, registry, Contained, Container,
18 WidgetBase, TransitionEvent, iconUtils, has, viewRegistry, BidiItemBase){
19
20 // module:
21 // dojox/mobile/_ItemBase
22
23 var _ItemBase = declare(has("dojo-bidi") ? "dojox.mobile._NonBidiItemBase" : "dojox.mobile._ItemBase", [WidgetBase, Container, Contained], {
24 // summary:
25 // A base class for item classes (e.g. ListItem, IconItem, etc.).
26 // description:
27 // _ItemBase is a base class for widgets that have capability to
28 // make a view transition when clicked.
29
30 // icon: String
31 // An icon image to display. The value can be either a path for an
32 // image file or a class name of a DOM button. If icon is not
33 // specified, the iconBase parameter of the parent widget is used.
34 icon: "",
35
36 // iconPos: String
37 // The position of an aggregated icon. IconPos is comma separated
38 // values like top,left,width,height (ex. "0,0,29,29"). If iconPos
39 // is not specified, the iconPos parameter of the parent widget is
40 // used.
41 iconPos: "", // top,left,width,height (ex. "0,0,29,29")
42
43 // alt: String
44 // An alternate text for the icon image.
45 alt: "",
46
47 // href: String
48 // A URL of another web page to go to.
49 href: "",
50
51 // hrefTarget: String
52 // A target that specifies where to open a page specified by
53 // href. The value will be passed to the 2nd argument of
54 // window.open().
55 hrefTarget: "",
56
57 // moveTo: String
58 // The id of the transition destination view which resides in the
59 // current page.
60 //
61 // If the value has a hash sign ('#') before the id (e.g. #view1)
62 // and the dojo/hash module is loaded by the user application, the
63 // view transition updates the hash in the browser URL so that the
64 // user can bookmark the destination view. In this case, the user
65 // can also use the browser's back/forward button to navigate
66 // through the views in the browser history.
67 //
68 // If null, transitions to a blank view.
69 // If '#', returns immediately without transition.
70 moveTo: "",
71
72 // scene: String
73 // The name of a scene. Used from dojox/mobile/app.
74 scene: "",
75
76 // clickable: Boolean
77 // If true, this item becomes clickable even if a transition
78 // destination (moveTo, etc.) is not specified.
79 clickable: false,
80
81 // url: String
82 // A URL of an html fragment page or JSON data that represents a
83 // new view content. The view content is loaded with XHR and
84 // inserted in the current page. Then a view transition occurs to
85 // the newly created view. The view is cached so that subsequent
86 // requests would not load the content again.
87 url: "",
88
89 // urlTarget: String
90 // Node id under which a new view will be created according to the
91 // url parameter. If not specified, The new view will be created as
92 // a sibling of the current view.
93 urlTarget: "",
94
95 // back: Boolean
96 // If true, history.back() is called when clicked.
97 back: false,
98
99 // transition: String
100 // A type of animated transition effect. You can choose from the
101 // standard transition types, "slide", "fade", "flip", or from the
102 // extended transition types, "cover", "coverv", "dissolve",
103 // "reveal", "revealv", "scaleIn", "scaleOut", "slidev",
104 // "swirl", "zoomIn", "zoomOut", "cube", and "swap". If "none" is
105 // specified, transition occurs immediately without animation.
106 transition: "",
107
108 // transitionDir: Number
109 // The transition direction. If 1, transition forward. If -1,
110 // transition backward. For example, the slide transition slides
111 // the view from right to left when dir == 1, and from left to
112 // right when dir == -1.
113 transitionDir: 1,
114
115 // transitionOptions: Object
116 // A hash object that holds transition options.
117 transitionOptions: null,
118
119 // callback: Function|String
120 // A callback function that is called when the transition has been
121 // finished. A function reference, or name of a function in
122 // context.
123 callback: null,
124
125 // label: String
126 // A label of the item. If the label is not specified, innerHTML is
127 // used as a label.
128 label: "",
129
130 // toggle: Boolean
131 // If true, the item acts like a toggle button.
132 toggle: false,
133
134 // selected: Boolean
135 // If true, the item is highlighted to indicate it is selected.
136 selected: false,
137
138 // tabIndex: String
139 // Tabindex setting for the item so users can hit the tab key to
140 // focus on it.
141 tabIndex: "0",
142
143 // _setTabIndexAttr: [private] String
144 // Sets tabIndex to domNode.
145 _setTabIndexAttr: "",
146
147 /* internal properties */
148
149 // paramsToInherit: String
150 // Comma separated parameters to inherit from the parent.
151 paramsToInherit: "transition,icon",
152
153 // _selStartMethod: String
154 // Specifies how the item enters the selected state.
155 //
156 // - "touch": Use touch events to enter the selected state.
157 // - "none": Do not change the selected state.
158 _selStartMethod: "none", // touch or none
159
160 // _selEndMethod: String
161 // Specifies how the item leaves the selected state.
162 //
163 // - "touch": Use touch events to leave the selected state.
164 // - "timer": Use setTimeout to leave the selected state.
165 // - "none": Do not change the selected state.
166 _selEndMethod: "none", // touch, timer, or none
167
168 // _delayedSelection: Boolean
169 // If true, selection is delayed 100ms and canceled if dragged in
170 // order to avoid selection when flick operation is performed.
171 _delayedSelection: false,
172
173 // _duration: Number
174 // Duration of selection, milliseconds.
175 _duration: 800,
176
177 // _handleClick: Boolean
178 // If true, this widget listens to touch events.
179 _handleClick: true,
180
181 buildRendering: function(){
182 this.inherited(arguments);
183 this._isOnLine = this.inheritParams();
184 },
185
186 startup: function(){
187 if(this._started){ return; }
188 if(!this._isOnLine){
189 this.inheritParams();
190 }
191 this._updateHandles();
192 this.inherited(arguments);
193 },
194
195 inheritParams: function(){
196 // summary:
197 // Copies from the parent the values of parameters specified
198 // by the property paramsToInherit.
199 var parent = this.getParent();
200 if(parent){
201 array.forEach(this.paramsToInherit.split(/,/), function(p){
202 if(p.match(/icon/i)){
203 var base = p + "Base", pos = p + "Pos";
204 if(this[p] && parent[base] &&
205 parent[base].charAt(parent[base].length - 1) === '/'){
206 this[p] = parent[base] + this[p];
207 }
208 if(!this[p]){ this[p] = parent[base]; }
209 if(!this[pos]){ this[pos] = parent[pos]; }
210 }
211 if(!this[p]){ this[p] = parent[p]; }
212 }, this);
213 }
214 return !!parent;
215 },
216
217 _updateHandles: function(){
218 // tags:
219 // private
220 if(this._handleClick && this._selStartMethod === "touch"){
221 if(!this._onTouchStartHandle){
222 this._onTouchStartHandle = this.connect(this.domNode, touch.press, "_onTouchStart");
223 }
224 }else{
225 if(this._onTouchStartHandle){
226 this.disconnect(this._onTouchStartHandle);
227 this._onTouchStartHandle = null;
228 }
229 }
230 },
231
232 getTransOpts: function(){
233 // summary:
234 // Copies from the parent and returns the values of parameters
235 // specified by the property paramsToInherit.
236 var opts = this.transitionOptions || {};
237 array.forEach(["moveTo", "href", "hrefTarget", "url", "target",
238 "urlTarget", "scene", "transition", "transitionDir"], function(p){
239 opts[p] = opts[p] || this[p];
240 }, this);
241 return opts; // Object
242 },
243
244 userClickAction: function(/*Event*/ /*===== e =====*/){
245 // summary:
246 // User-defined click action.
247 },
248
249 defaultClickAction: function(/*Event*/e){
250 // summary:
251 // The default action of this item.
252 this.handleSelection(e);
253 if(this.userClickAction(e) === false){ return; } // user's click action
254 this.makeTransition(e);
255 },
256
257 handleSelection: function(/*Event*/e){
258 // summary:
259 // Handles this items selection state.
260
261 // Before transitioning, we want the visual effect of selecting the item.
262 // To ensure this effect happens even if _delayedSelection is true:
263 if(this._delayedSelection){
264 this.set("selected", true);
265 } // the item will be deselected after transition.
266
267 if(this._onTouchEndHandle){
268 this.disconnect(this._onTouchEndHandle);
269 this._onTouchEndHandle = null;
270 }
271
272 var p = this.getParent();
273 if(this.toggle){
274 this.set("selected", !this._currentSel);
275 }else if(p && p.selectOne){
276 this.set("selected", true);
277 }else{
278 if(this._selEndMethod === "touch"){
279 this.set("selected", false);
280 }else if(this._selEndMethod === "timer"){
281 this.defer(function(){
282 this.set("selected", false);
283 }, this._duration);
284 }
285 }
286 },
287
288 makeTransition: function(/*Event*/e){
289 // summary:
290 // Makes a transition.
291 if(this.back && history){
292 history.back();
293 return;
294 }
295 if (this.href && this.hrefTarget && this.hrefTarget != "_self") {
296 win.global.open(this.href, this.hrefTarget || "_blank");
297 this._onNewWindowOpened(e);
298 return;
299 }
300 var opts = this.getTransOpts();
301 var doTransition =
302 !!(opts.moveTo || opts.href || opts.url || opts.target || opts.scene);
303 if(this._prepareForTransition(e, doTransition ? opts : null) === false){ return; }
304 if(doTransition){
305 this.setTransitionPos(e);
306 new TransitionEvent(this.domNode, opts, e).dispatch();
307 }
308 },
309
310 _onNewWindowOpened: function(/*Event*/ /*===== e =====*/){
311 // summary:
312 // Subclasses may want to implement it.
313 },
314
315 _prepareForTransition: function(/*Event*/e, /*Object*/transOpts){
316 // summary:
317 // Subclasses may want to implement it.
318 },
319
320 _onTouchStart: function(e){
321 // tags:
322 // private
323 if(this.getParent().isEditing || this.onTouchStart(e) === false){ return; } // user's touchStart action
324 var enclosingScrollable = viewRegistry.getEnclosingScrollable(this.domNode);
325 if(enclosingScrollable &&
326 domClass.contains(enclosingScrollable.containerNode, "mblScrollableScrollTo2")){
327 // #17165: do not select the item during scroll animation
328 return;
329 }
330 if(!this._onTouchEndHandle && this._selStartMethod === "touch"){
331 // Connect to the entire window. Otherwise, fail to receive
332 // events if operation is performed outside this widget.
333 // Expose both connect handlers in case the user has interest.
334 this._onTouchMoveHandle = this.connect(win.body(), touch.move, "_onTouchMove");
335 this._onTouchEndHandle = this.connect(win.body(), touch.release, "_onTouchEnd");
336 }
337 this.touchStartX = e.touches ? e.touches[0].pageX : e.clientX;
338 this.touchStartY = e.touches ? e.touches[0].pageY : e.clientY;
339 this._currentSel = this.selected;
340
341 if(this._delayedSelection){
342 // so as not to make selection when the user flicks on ScrollableView
343 this._selTimer = this.defer(function(){
344 this.set("selected", true);
345 }, 100);
346 }else{
347 this.set("selected", true);
348 }
349 },
350
351 onTouchStart: function(/*Event*/ /*===== e =====*/){
352 // summary:
353 // User-defined function to handle touchStart events.
354 // tags:
355 // callback
356 },
357
358 _onTouchMove: function(e){
359 // tags:
360 // private
361 var x = e.touches ? e.touches[0].pageX : e.clientX;
362 var y = e.touches ? e.touches[0].pageY : e.clientY;
363 if(Math.abs(x - this.touchStartX) >= 4 ||
364 Math.abs(y - this.touchStartY) >= 4){ // dojox/mobile/scrollable.threshold
365 this.cancel();
366 var p = this.getParent();
367 if(p && p.selectOne){
368 this._prevSel && this._prevSel.set("selected", true);
369 }else{
370 this.set("selected", false);
371 }
372 }
373 },
374
375 _disconnect: function(){
376 // tags:
377 // private
378 this.disconnect(this._onTouchMoveHandle);
379 this.disconnect(this._onTouchEndHandle);
380 this._onTouchMoveHandle = this._onTouchEndHandle = null;
381 },
382
383 cancel: function(){
384 // summary:
385 // Cancels an ongoing selection (if any).
386 if(this._selTimer){
387 this._selTimer.remove();
388 this._selTimer = null;
389 }
390 this._disconnect();
391 },
392
393 _onTouchEnd: function(e){
394 // tags:
395 // private
396 if(!this._selTimer && this._delayedSelection){ return; }
397 this.cancel();
398 this._onClick(e);
399 },
400
401 setTransitionPos: function(e){
402 // summary:
403 // Stores the clicked position for later use.
404 // description:
405 // Some of the transition animations (e.g. ScaleIn) need the
406 // clicked position.
407 var w = this;
408 while(true){
409 w = w.getParent();
410 if(!w || domClass.contains(w.domNode, "mblView")){ break; }
411 }
412 if(w){
413 w.clickedPosX = e.clientX;
414 w.clickedPosY = e.clientY;
415 }
416 },
417
418 transitionTo: function(/*String|Object*/moveTo, /*String*/href, /*String*/url, /*String*/scene){
419 // summary:
420 // Performs a view transition.
421 // description:
422 // Given a transition destination, this method performs a view
423 // transition. This method is typically called when this item
424 // is clicked.
425 var opts = (moveTo && typeof(moveTo) === "object") ? moveTo :
426 {moveTo: moveTo, href: href, url: url, scene: scene,
427 transition: this.transition, transitionDir: this.transitionDir};
428 new TransitionEvent(this.domNode, opts).dispatch();
429 },
430
431 _setIconAttr: function(icon){
432 // tags:
433 // private
434 if(!this._isOnLine){
435 // record the value to be able to reapply it (see the code in the startup method)
436 this._pendingIcon = icon;
437 return;
438 } // icon may be invalid because inheritParams is not called yet
439 this._set("icon", icon);
440 this.iconNode = iconUtils.setIcon(icon, this.iconPos, this.iconNode, this.alt, this.iconParentNode, this.refNode, this.position);
441 },
442
443 _setLabelAttr: function(/*String*/text){
444 // tags:
445 // private
446 this._set("label", text);
447 this.labelNode.innerHTML = this._cv ? this._cv(text) : text;
448 },
449
450 _setSelectedAttr: function(/*Boolean*/selected){
451 // summary:
452 // Makes this widget in the selected or unselected state.
453 // description:
454 // Subclass should override.
455 // tags:
456 // private
457 if(selected){
458 var p = this.getParent();
459 if(p && p.selectOne){
460 // deselect the currently selected item
461 var arr = array.filter(p.getChildren(), function(w){
462 return w.selected;
463 });
464 array.forEach(arr, function(c){
465 this._prevSel = c;
466 c.set("selected", false);
467 }, this);
468 }
469 }
470 this._set("selected", selected);
471 }
472 });
473 return has("dojo-bidi") ? declare("dojox.mobile._ItemBase", [_ItemBase, BidiItemBase]) : _ItemBase;
474 });