| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
var Carousel = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiCarousel" : "dojox.mobile.Carousel", [WidgetBase, Container, Contained], { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
numVisible: 2, |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
itemWidth: 0, |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
title: "", |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pageIndicator: true, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
navButton: false, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
height: "", |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
selectable: true, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 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"); |
| 114 |
this.prevBtnNode = domConstruct.create("button", { |
| 115 |
className: "mblCarouselBtn", |
| 116 |
title: messages["CarouselPrevious"], |
| 117 |
innerHTML: "<", |
| 118 |
"aria-controls": this.containerNode.id |
| 119 |
}, this.btnContainerNode); |
| 120 |
this.nextBtnNode = domConstruct.create("button", { |
| 121 |
className: "mblCarouselBtn", |
| 122 |
title: messages["CarouselNext"], |
| 123 |
innerHTML: ">", |
| 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 = " "; |
| 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 |
|
| 185 |
|
| 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(items){ |
| 272 |
|
| 273 |
|
| 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); |
| 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( ){ |
| 306 |
|
| 307 |
|
| 308 |
}, |
| 309 |
|
| 310 |
onUpdate: function( ){ |
| 311 |
|
| 312 |
|
| 313 |
}, |
| 314 |
|
| 315 |
onDelete: function( ){ |
| 316 |
|
| 317 |
|
| 318 |
}, |
| 319 |
|
| 320 |
onSet: function(item, attribute, oldValue, newValue){ |
| 321 |
}, |
| 322 |
|
| 323 |
onNew: function(newItem, parentInfo){ |
| 324 |
}, |
| 325 |
|
| 326 |
onStoreClose: function(request){ |
| 327 |
|
| 328 |
|
| 329 |
}, |
| 330 |
|
| 331 |
getParentView: function(node){ |
| 332 |
|
| 333 |
|
| 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(w){ |
| 342 |
|
| 343 |
|
| 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(index){ |
| 351 |
|
| 352 |
|
| 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( ){ |
| 359 |
|
| 360 |
|
| 361 |
if(this.currentView){ |
| 362 |
this.currentView.goTo(-1); |
| 363 |
} |
| 364 |
}, |
| 365 |
|
| 366 |
onNextBtnClick: function( ){ |
| 367 |
|
| 368 |
|
| 369 |
if(this.currentView){ |
| 370 |
this.currentView.goTo(1); |
| 371 |
} |
| 372 |
}, |
| 373 |
|
| 374 |
_onClick: function(e){ |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if(this.onClick(e) === false){ return; } |
| 380 |
if(e && e.type === "keydown"){ |
| 381 |
if(e.keyCode === 39){ |
| 382 |
this.onNextBtnClick(); |
| 383 |
}else if(e.keyCode === 37){ |
| 384 |
this.onPrevBtnClick(); |
| 385 |
}else if(e.keyCode !== 13){ |
| 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(itemWidget){ |
| 401 |
|
| 402 |
|
| 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( ){ |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
}, |
| 425 |
|
| 426 |
instantiateView: function(view){ |
| 427 |
|
| 428 |
|
| 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 |
|
| 445 |
|
| 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(title){ |
| 456 |
|
| 457 |
|
| 458 |
this.titleNode.innerHTML = this._cv ? this._cv(title) : title; |
| 459 |
this._set("title", title); |
| 460 |
} |
| 461 |
}); |
| 462 |
|
| 463 |
Carousel.ChildSwapViewProperties = { |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
lazy: false |
| 470 |
}; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
lang.extend(SwapView, Carousel.ChildSwapViewProperties); |
| 476 |
|
| 477 |
return has("dojo-bidi") ? declare("dojox.mobile.Carousel", [Carousel, BidiCarousel]) : Carousel; |
| 478 |
}); |