Debian Codegraph @

dojox/mobile/Carousel.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/connect",
4 "dojo/_base/declare",
5 "dojo/_base/event",
6 "dojo/_base/lang",
7 "dojo/sniff",
8 "dojo/dom-class",
9 "dojo/dom-construct",
10 "dojo/dom-style",
11 "dijit/registry",
12 "dijit/_Contained",
13 "dijit/_Container",
14 "dijit/_WidgetBase",
15 "./lazyLoadUtils",
16 "./CarouselItem",
17 "./PageIndicator",
18 "./SwapView",
19 "require",
20 "dojo/has!dojo-bidi?dojox/mobile/bidi/Carousel",
21 "dojo/i18n!dojox/mobile/nls/messages"
22 ], function(array, connect, declare, event, lang, has, domClass, domConstruct, domStyle, registry, Contained, Container, WidgetBase, lazyLoadUtils, CarouselItem, PageIndicator, SwapView, require, BidiCarousel, messages){
23
24 // module:
25 // dojox/mobile/Carousel
26
27 var Carousel = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiCarousel" : "dojox.mobile.Carousel", [WidgetBase, Container, Contained], {
28 // summary:
29 // A carousel widget that manages a list of images.
30 // description:
31 // The carousel widget manages a list of images that can be
32 // displayed horizontally, and allows the user to scroll through
33 // the list and select a single item.
34 //
35 // This widget itself has no data store support, but there are two
36 // subclasses, dojox/mobile/DataCarousel and dojox/mobile/StoreCarousel,
37 // available for generating the contents from a data store.
38 // To feed data into a Carousel through a dojo/data, use DataCarousel.
39 // To feed data into a Carousel through a dojo/store, use StoreCarousel.
40 //
41 // The Carousel widget loads and instantiates its item contents in
42 // a lazy manner. For example, if the number of visible items
43 // (see the property numVisible) is 2, the widget creates 4 items, 2 for the
44 // initial pane and 2 for the next page, at startup time. If you
45 // swipe the page to open the second page, the widget creates 2 more
46 // items for the third page. If the item to create is a dojo widget,
47 // its module is dynamically loaded automatically before instantiation.
48
49 // numVisible: Number
50 // The number of visible items.
51 numVisible: 2,
52
53 // itemWidth: Number
54 // The number of visible items (=numVisible) is determined by
55 // (carousel_width / itemWidth).
56 // If itemWidth is specified, numVisible is automatically calculated.
57 // If resize() is called, numVisible is recalculated and the layout
58 // is changed accordingly.
59 itemWidth: 0,
60
61 // title: String
62 // A title of the carousel to be displayed on the title bar.
63 title: "",
64
65 // pageIndicator: [const] Boolean
66 // If true, a page indicator, a series of small dots that indicate
67 // the current page, is displayed on the title bar.
68 // Note that changing the value of the property after the widget
69 // creation has no effect.
70 pageIndicator: true,
71
72 // navButton: [const] Boolean
73 // If true, navigation buttons are displayed on the title bar.
74 // Note that changing the value of the property after the widget
75 // creation has no effect.
76 navButton: false,
77
78 // height: [const] String
79 // Explicitly specified height of the widget (ex. "300px"). If
80 // "inherit" is specified, the height is inherited from its offset
81 // parent.
82 // Note that changing the value of the property after the widget
83 // creation has no effect.
84 height: "",
85
86 // selectable: Boolean
87 // If true, an item can be selected by clicking it.
88 selectable: true,
89
90 /* internal properties */
91
92 // baseClass: String
93 // The name of the CSS class of this widget.
94 baseClass: "mblCarousel",
95
96 buildRendering: function(){
97 this.containerNode = domConstruct.create("div", {className: "mblCarouselPages", id: this.id + "_pages"});
98 this.inherited(arguments);
99 var i, len;
100 if(this.srcNodeRef){
101 // reparent
102 for(i = 0, len = this.srcNodeRef.childNodes.length; i < len; i++){
103 this.containerNode.appendChild(this.srcNodeRef.firstChild);
104 }
105 }
106
107 this.headerNode = domConstruct.create("div", {className: "mblCarouselHeaderBar"}, this.domNode);
108
109 if(this.navButton){
110 this.btnContainerNode = domConstruct.create("div", {
111 className: "mblCarouselBtnContainer"
112 }, this.headerNode);
113 domStyle.set(this.btnContainerNode, "float", "right"); // workaround for webkit rendering problem
114 this.prevBtnNode = domConstruct.create("button", {
115 className: "mblCarouselBtn",
116 title: messages["CarouselPrevious"],
117 innerHTML: "&lt;",
118 "aria-controls": this.containerNode.id
119 }, this.btnContainerNode);
120 this.nextBtnNode = domConstruct.create("button", {
121 className: "mblCarouselBtn",
122 title: messages["CarouselNext"],
123 innerHTML: "&gt;",
124 "aria-controls": this.containerNode.id
125 }, this.btnContainerNode);
126 this._prevHandle = this.connect(this.prevBtnNode, "onclick", "onPrevBtnClick");
127 this._nextHandle = this.connect(this.nextBtnNode, "onclick", "onNextBtnClick");
128 }
129
130 if(this.pageIndicator){
131 if(!this.title){
132 this.title = "&nbsp;";
133 }
134 this.piw = new PageIndicator();
135 this.headerNode.appendChild(this.piw.domNode);
136 }
137
138 this.titleNode = domConstruct.create("div", {
139 className: "mblCarouselTitle"
140 }, this.headerNode);
141
142 this.domNode.appendChild(this.containerNode);
143 this.subscribe("/dojox/mobile/viewChanged", "handleViewChanged");
144 this.connect(this.domNode, "onclick", "_onClick");
145 this.connect(this.domNode, "onkeydown", "_onClick");
146 this._dragstartHandle = this.connect(this.domNode, "ondragstart", event.stop);
147 this.selectedItemIndex = -1;
148 this.items = [];
149 },
150
151 startup: function(){
152 if(this._started){ return; }
153
154 var h;
155 if(this.height === "inherit"){
156 if(this.domNode.offsetParent){
157 h = this.domNode.offsetParent.offsetHeight + "px";
158 }
159 }else if(this.height){
160 h = this.height;
161 }
162 if(h){
163 this.domNode.style.height = h;
164 }
165
166 if(this.store){
167 if(!this.setStore){
168 throw new Error("Use StoreCarousel or DataCarousel instead of Carousel.");
169 }
170 var store = this.store;
171 this.store = null;
172 this.setStore(store, this.query, this.queryOptions);
173 }else{
174 this.resizeItems();
175 }
176 this.inherited(arguments);
177
178 this.currentView = array.filter(this.getChildren(), function(view){
179 return view.isVisible();
180 })[0];
181 },
182
183 resizeItems: function(){
184 // summary:
185 // Resizes the child items of the carousel.
186 var idx = 0, i, len;
187 var h = this.domNode.offsetHeight - (this.headerNode ? this.headerNode.offsetHeight : 0);
188 var m = (has("ie") < 10) ? 5 / this.numVisible - 1 : 5 / this.numVisible;
189 var node, item;
190 array.forEach(this.getChildren(), function(view){
191 if(!(view instanceof SwapView)){ return; }
192 if(!(view.lazy)){
193 view._instantiated = true;
194 }
195 var ch = view.containerNode.childNodes;
196 for(i = 0, len = ch.length; i < len; i++){
197 node = ch[i];
198 if(node.nodeType !== 1){ continue; }
199 item = this.items[idx] || {};
200 domStyle.set(node, {
201 width: item.width || (90 / this.numVisible + "%"),
202 height: item.height || h + "px",
203 margin: "0 " + (item.margin || m + "%")
204 });
205 domClass.add(node, "mblCarouselSlot");
206 idx++;
207 }
208 }, this);
209
210 if(this.piw){
211 this.piw.refId = this.containerNode.firstChild;
212 this.piw.reset();
213 }
214 },
215
216 resize: function(){
217 if(!this.itemWidth){ return; }
218 var num = Math.floor(this.domNode.offsetWidth / this.itemWidth);
219 if(num === this.numVisible){ return; }
220 this.selectedItemIndex = this.getIndexByItemWidget(this.selectedItem);
221 this.numVisible = num;
222 if(this.items.length > 0){
223 this.onComplete(this.items);
224 this.select(this.selectedItemIndex);
225 }
226 },
227
228 fillPages: function(){
229 array.forEach(this.getChildren(), function(child, i){
230 var s = "";
231 var j;
232 for(j = 0; j < this.numVisible; j++){
233 var type, props = "", mixins;
234 var idx = i * this.numVisible + j;
235 var item = {};
236 if(idx < this.items.length){
237 item = this.items[idx];
238 type = this.store.getValue(item, "type");
239 if(type){
240 props = this.store.getValue(item, "props");
241 mixins = this.store.getValue(item, "mixins");
242 }else{
243 type = "dojox.mobile.CarouselItem";
244 array.forEach(["alt", "src", "headerText", "footerText"], function(p){
245 var v = this.store.getValue(item, p);
246 if(v !== undefined){
247 if(props){ props += ','; }
248 props += p + ':"' + v + '"';
249 }
250 }, this);
251 }
252 }else{
253 type = "dojox.mobile.CarouselItem";
254 props = 'src:"' + require.toUrl("dojo/resources/blank.gif") + '"' +
255 ', className:"mblCarouselItemBlank"';
256 }
257
258 s += '<div data-dojo-type="' + type + '"';
259 if(props){
260 s += ' data-dojo-props=\'' + props + '\'';
261 }
262 if(mixins){
263 s += ' data-dojo-mixins=\'' + mixins + '\'';
264 }
265 s += '></div>';
266 }
267 child.containerNode.innerHTML = s;
268 }, this);
269 },
270
271 onComplete: function(/*Array*/items){
272 // summary:
273 // A handler that is called after the fetch completes.
274 array.forEach(this.getChildren(), function(child){
275 if(child instanceof SwapView){
276 child.destroyRecursive();
277 }
278 });
279 this.selectedItem = null;
280 this.items = items;
281 var nPages = Math.ceil(items.length / this.numVisible),
282 i, h = this.domNode.offsetHeight - this.headerNode.offsetHeight,
283 idx = this.selectedItemIndex === -1 ? 0 : this.selectedItemIndex,
284 pg = Math.floor(idx / this.numVisible); // current page
285 for(i = 0; i < nPages; i++){
286 var w = new SwapView({height: h + "px", lazy:true});
287 this.addChild(w);
288 if(i === pg){
289 w.show();
290 this.currentView = w;
291 }else{
292 w.hide();
293 }
294 }
295 this.fillPages();
296 this.resizeItems();
297 var children = this.getChildren();
298 var from = pg - 1 < 0 ? 0 : pg - 1;
299 var to = pg + 1 > nPages - 1 ? nPages - 1 : pg + 1;
300 for(i = from; i <= to; i++){
301 this.instantiateView(children[i]);
302 }
303 },
304
305 onError: function(/*String*/ /*===== errText =====*/){
306 // summary:
307 // An error handler.
308 },
309
310 onUpdate: function(/*Object*/ /*===== item, =====*/ /*Number*/ /*===== insertedInto =====*/){
311 // summary:
312 // Adds a new item or updates an existing item.
313 },
314
315 onDelete: function(/*Object*/ /*===== item, =====*/ /*Number*/ /*===== removedFrom =====*/){
316 // summary:
317 // Deletes an existing item.
318 },
319
320 onSet: function(item, attribute, oldValue, newValue){
321 },
322
323 onNew: function(newItem, parentInfo){
324 },
325
326 onStoreClose: function(request){
327 // summary:
328 // Called when the store is closed.
329 },
330
331 getParentView: function(/*DomNode*/node){
332 // summary:
333 // Returns the parent view of the given DOM node.
334 var w;
335 for(w = registry.getEnclosingWidget(node); w; w = w.getParent()){
336 if(w.getParent() instanceof SwapView){ return w; }
337 }
338 return null;
339 },
340
341 getIndexByItemWidget: function(/*Widget*/w){
342 // summary:
343 // Returns the index of a given item widget.
344 if(!w){ return -1; }
345 var view = w.getParent();
346 return array.indexOf(this.getChildren(), view) * this.numVisible +
347 array.indexOf(view.getChildren(), w);
348 },
349
350 getItemWidgetByIndex: function(/*Number*/index){
351 // summary:
352 // Returns the index of an item widget at a given index.
353 if(index === -1){ return null; }
354 var view = this.getChildren()[Math.floor(index / this.numVisible)];
355 return view.getChildren()[index % this.numVisible];
356 },
357
358 onPrevBtnClick: function(/*Event*/ /*===== e =====*/){
359 // summary:
360 // Called when the "previous" button is clicked.
361 if(this.currentView){
362 this.currentView.goTo(-1);
363 }
364 },
365
366 onNextBtnClick: function(/*Event*/ /*===== e =====*/){
367 // summary:
368 // Called when the "next" button is clicked.
369 if(this.currentView){
370 this.currentView.goTo(1);
371 }
372 },
373
374 _onClick: function(e){
375 // summary:
376 // Internal handler for click events.
377 // tags:
378 // private
379 if(this.onClick(e) === false){ return; } // user's click action
380 if(e && e.type === "keydown"){ // keyboard navigation for accessibility
381 if(e.keyCode === 39){ // right arrow
382 this.onNextBtnClick();
383 }else if(e.keyCode === 37){ // left arrow
384 this.onPrevBtnClick();
385 }else if(e.keyCode !== 13){ // !Enter
386 return;
387 }
388 }
389
390 var w;
391 for(w = registry.getEnclosingWidget(e.target); ; w = w.getParent()){
392 if(!w){ return; }
393 if(w.getParent() instanceof SwapView){ break; }
394 }
395 this.select(w);
396 var idx = this.getIndexByItemWidget(w);
397 connect.publish("/dojox/mobile/carouselSelect", [this, w, this.items[idx], idx]);
398 },
399
400 select: function(/*Widget|Number*/itemWidget){
401 // summary:
402 // Selects the given widget.
403 if(typeof(itemWidget) === "number"){
404 itemWidget = this.getItemWidgetByIndex(itemWidget);
405 }
406 if(this.selectable){
407 if(this.selectedItem){
408 this.selectedItem.set("selected", false);
409 domClass.remove(this.selectedItem.domNode, "mblCarouselSlotSelected");
410 }
411 if(itemWidget){
412 itemWidget.set("selected", true);
413 domClass.add(itemWidget.domNode, "mblCarouselSlotSelected");
414 }
415 this.selectedItem = itemWidget;
416 }
417 },
418
419 onClick: function(/*Event*/ /*===== e =====*/){
420 // summary:
421 // User-defined function to handle clicks.
422 // tags:
423 // callback
424 },
425
426 instantiateView: function(view){
427 // summary:
428 // Instantiates the given view.
429 if(view && !view._instantiated){
430 var isHidden = (domStyle.get(view.domNode, "display") === "none");
431 if(isHidden){
432 domStyle.set(view.domNode, {visibility:"hidden", display:""});
433 }
434 lazyLoadUtils.instantiateLazyWidgets(view.containerNode, null, function(root){
435 if(isHidden){
436 domStyle.set(view.domNode, {visibility:"visible", display:"none"});
437 }
438 });
439 view._instantiated = true;
440 }
441 },
442
443 handleViewChanged: function(view){
444 // summary:
445 // Listens to "/dojox/mobile/viewChanged" events.
446 if(view.getParent() !== this){ return; }
447 if(this.currentView.nextView(this.currentView.domNode) === view){
448 this.instantiateView(view.nextView(view.domNode));
449 }else{
450 this.instantiateView(view.previousView(view.domNode));
451 }
452 this.currentView = view;
453 },
454
455 _setTitleAttr: function(/*String*/title){
456 // tags:
457 // private
458 this.titleNode.innerHTML = this._cv ? this._cv(title) : title;
459 this._set("title", title);
460 }
461 });
462
463 Carousel.ChildSwapViewProperties = {
464 // summary:
465 // This property can be specified for the SwapView children of a dojox/mobile/Carousel.
466
467 // lazy: Boolean
468 // Specifies that the Carousel child must be lazily loaded.
469 lazy: false
470 };
471
472 // Since any widget can be specified as an Accordion child, mix ChildWidgetProperties
473 // into the base widget class. (This is a hack, but it's effective.)
474 // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
475 lang.extend(SwapView, /*===== {} || =====*/ Carousel.ChildSwapViewProperties);
476
477 return has("dojo-bidi") ? declare("dojox.mobile.Carousel", [Carousel, BidiCarousel]) : Carousel;
478 });