| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dom-class", |
| 5 |
"dojo/dom-construct", |
| 6 |
"dojo/keys", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/on", |
| 9 |
"dojo/topic", |
| 10 |
"../focus", |
| 11 |
"../registry", |
| 12 |
"../_Widget", |
| 13 |
"../_TemplatedMixin", |
| 14 |
"../_Container", |
| 15 |
"../form/ToggleButton", |
| 16 |
"dojo/touch" |
| 17 |
], function(array, declare, domClass, domConstruct, keys, lang, on, topic, |
| 18 |
focus, registry, _Widget, _TemplatedMixin,_Container, ToggleButton){ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
var StackButton = declare("dijit.layout._StackButton", ToggleButton, { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
tabIndex: "-1", |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
closeButton: false, |
| 39 |
|
| 40 |
_aria_attr: "aria-selected", |
| 41 |
|
| 42 |
buildRendering: function( evt){ |
| 43 |
this.inherited(arguments); |
| 44 |
(this.focusNode || this.domNode).setAttribute("role", "tab"); |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
|
| 49 |
var StackController = declare("dijit.layout.StackController", [_Widget, _TemplatedMixin, _Container], { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
baseClass: "dijitStackController", |
| 57 |
|
| 58 |
templateString: "<span role='tablist' data-dojo-attach-event='onkeydown'></span>", |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
containerId: "", |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
buttonWidget: StackButton, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
buttonWidgetCloseClass: "dijitStackCloseButton", |
| 71 |
|
| 72 |
pane2button: function( id){ |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
return registry.byId(this.id + "_" + id); |
| 78 |
}, |
| 79 |
|
| 80 |
postCreate: function(){ |
| 81 |
this.inherited(arguments); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
this.own( |
| 88 |
topic.subscribe(this.containerId + "-startup", lang.hitch(this, "onStartup")), |
| 89 |
topic.subscribe(this.containerId + "-addChild", lang.hitch(this, "onAddChild")), |
| 90 |
topic.subscribe(this.containerId + "-removeChild", lang.hitch(this, "onRemoveChild")), |
| 91 |
topic.subscribe(this.containerId + "-selectChild", lang.hitch(this, "onSelectChild")), |
| 92 |
topic.subscribe(this.containerId + "-containerKeyDown", lang.hitch(this, "onContainerKeyDown")) |
| 93 |
); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
this.containerNode.dojoClick = true; |
| 100 |
this.own(on(this.containerNode, 'click', lang.hitch(this, function(evt){ |
| 101 |
var button = registry.getEnclosingWidget(evt.target); |
| 102 |
if(button != this.containerNode && !button.disabled && button.page){ |
| 103 |
for(var target = evt.target; target !== this.containerNode; target = target.parentNode){ |
| 104 |
if(domClass.contains(target, this.buttonWidgetCloseClass)){ |
| 105 |
this.onCloseButtonClick(button.page); |
| 106 |
break; |
| 107 |
}else if(target == button.domNode){ |
| 108 |
this.onButtonClick(button.page); |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
}))); |
| 114 |
}, |
| 115 |
|
| 116 |
onStartup: function( info){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
this.textDir = info.textDir; |
| 122 |
array.forEach(info.children, this.onAddChild, this); |
| 123 |
if(info.selected){ |
| 124 |
|
| 125 |
|
| 126 |
this.onSelectChild(info.selected); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
var containerNode = registry.byId(this.containerId).containerNode, |
| 131 |
pane2button = lang.hitch(this, "pane2button"), |
| 132 |
paneToButtonAttr = { |
| 133 |
"title": "label", |
| 134 |
"showtitle": "showLabel", |
| 135 |
"iconclass": "iconClass", |
| 136 |
"closable": "closeButton", |
| 137 |
"tooltip": "title", |
| 138 |
"disabled": "disabled", |
| 139 |
"textdir": "textdir" |
| 140 |
}, |
| 141 |
connectFunc = function(attr, buttonAttr){ |
| 142 |
return on(containerNode, "attrmodified-" + attr, function(evt){ |
| 143 |
var button = pane2button(evt.detail && evt.detail.widget && evt.detail.widget.id); |
| 144 |
if(button){ |
| 145 |
button.set(buttonAttr, evt.detail.newValue); |
| 146 |
} |
| 147 |
}); |
| 148 |
}; |
| 149 |
for(var attr in paneToButtonAttr){ |
| 150 |
this.own(connectFunc(attr, paneToButtonAttr[attr])); |
| 151 |
} |
| 152 |
}, |
| 153 |
|
| 154 |
destroy: function(preserveDom){ |
| 155 |
|
| 156 |
|
| 157 |
this.destroyDescendants(preserveDom); |
| 158 |
this.inherited(arguments); |
| 159 |
}, |
| 160 |
|
| 161 |
onAddChild: function( page, insertIndex){ |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
var Cls = lang.isString(this.buttonWidget) ? lang.getObject(this.buttonWidget) : this.buttonWidget; |
| 171 |
var button = new Cls({ |
| 172 |
id: this.id + "_" + page.id, |
| 173 |
name: this.id + "_" + page.id, |
| 174 |
label: page.title, |
| 175 |
disabled: page.disabled, |
| 176 |
ownerDocument: this.ownerDocument, |
| 177 |
dir: page.dir, |
| 178 |
lang: page.lang, |
| 179 |
textDir: page.textDir || this.textDir, |
| 180 |
showLabel: page.showTitle, |
| 181 |
iconClass: page.iconClass, |
| 182 |
closeButton: page.closable, |
| 183 |
title: page.tooltip, |
| 184 |
page: page |
| 185 |
}); |
| 186 |
|
| 187 |
this.addChild(button, insertIndex); |
| 188 |
page.controlButton = button; |
| 189 |
if(!this._currentChild){ |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
this.onSelectChild(page); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
var labelledby = page._wrapper.getAttribute("aria-labelledby") ? |
| 200 |
page._wrapper.getAttribute("aria-labelledby") + " " + button.id : button.id; |
| 201 |
page._wrapper.removeAttribute("aria-label"); |
| 202 |
page._wrapper.setAttribute("aria-labelledby", labelledby); |
| 203 |
}, |
| 204 |
|
| 205 |
onRemoveChild: function( page){ |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if(this._currentChild === page){ |
| 213 |
this._currentChild = null; |
| 214 |
} |
| 215 |
|
| 216 |
var button = this.pane2button(page.id); |
| 217 |
if(button){ |
| 218 |
this.removeChild(button); |
| 219 |
button.destroy(); |
| 220 |
} |
| 221 |
delete page.controlButton; |
| 222 |
}, |
| 223 |
|
| 224 |
onSelectChild: function( page){ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if(!page){ |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
if(this._currentChild){ |
| 235 |
var oldButton = this.pane2button(this._currentChild.id); |
| 236 |
oldButton.set('checked', false); |
| 237 |
oldButton.focusNode.setAttribute("tabIndex", "-1"); |
| 238 |
} |
| 239 |
|
| 240 |
var newButton = this.pane2button(page.id); |
| 241 |
newButton.set('checked', true); |
| 242 |
this._currentChild = page; |
| 243 |
newButton.focusNode.setAttribute("tabIndex", "0"); |
| 244 |
var container = registry.byId(this.containerId); |
| 245 |
}, |
| 246 |
|
| 247 |
onButtonClick: function( page){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
var button = this.pane2button(page.id); |
| 254 |
|
| 255 |
|
| 256 |
focus.focus(button.focusNode); |
| 257 |
|
| 258 |
if(this._currentChild && this._currentChild.id === page.id){ |
| 259 |
|
| 260 |
button.set('checked', true); |
| 261 |
} |
| 262 |
var container = registry.byId(this.containerId); |
| 263 |
container.selectChild(page); |
| 264 |
}, |
| 265 |
|
| 266 |
onCloseButtonClick: function( page){ |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
var container = registry.byId(this.containerId); |
| 273 |
container.closeChild(page); |
| 274 |
if(this._currentChild){ |
| 275 |
var b = this.pane2button(this._currentChild.id); |
| 276 |
if(b){ |
| 277 |
focus.focus(b.focusNode || b.domNode); |
| 278 |
} |
| 279 |
} |
| 280 |
}, |
| 281 |
|
| 282 |
|
| 283 |
adjacent: function( forward){ |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if(!this.isLeftToRight() && (!this.tabPosition || /top|bottom/.test(this.tabPosition))){ |
| 290 |
forward = !forward; |
| 291 |
} |
| 292 |
|
| 293 |
var children = this.getChildren(); |
| 294 |
var idx = array.indexOf(children, this.pane2button(this._currentChild.id)), |
| 295 |
current = children[idx]; |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
var child; |
| 300 |
do{ |
| 301 |
idx = (idx + (forward ? 1 : children.length - 1)) % children.length; |
| 302 |
child = children[idx]; |
| 303 |
}while(child.disabled && child != current); |
| 304 |
|
| 305 |
return child; |
| 306 |
}, |
| 307 |
|
| 308 |
onkeydown: function( e, fromContainer){ |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if(this.disabled || e.altKey){ |
| 316 |
return; |
| 317 |
} |
| 318 |
var forward = null; |
| 319 |
if(e.ctrlKey || !e._djpage){ |
| 320 |
switch(e.keyCode){ |
| 321 |
case keys.LEFT_ARROW: |
| 322 |
case keys.UP_ARROW: |
| 323 |
if(!e._djpage){ |
| 324 |
forward = false; |
| 325 |
} |
| 326 |
break; |
| 327 |
case keys.PAGE_UP: |
| 328 |
if(e.ctrlKey){ |
| 329 |
forward = false; |
| 330 |
} |
| 331 |
break; |
| 332 |
case keys.RIGHT_ARROW: |
| 333 |
case keys.DOWN_ARROW: |
| 334 |
if(!e._djpage){ |
| 335 |
forward = true; |
| 336 |
} |
| 337 |
break; |
| 338 |
case keys.PAGE_DOWN: |
| 339 |
if(e.ctrlKey){ |
| 340 |
forward = true; |
| 341 |
} |
| 342 |
break; |
| 343 |
case keys.HOME: |
| 344 |
|
| 345 |
var children = this.getChildren(); |
| 346 |
for(var idx = 0; idx < children.length; idx++){ |
| 347 |
var child = children[idx]; |
| 348 |
if(!child.disabled){ |
| 349 |
this.onButtonClick(child.page); |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
e.stopPropagation(); |
| 354 |
e.preventDefault(); |
| 355 |
break; |
| 356 |
case keys.END: |
| 357 |
|
| 358 |
var children = this.getChildren(); |
| 359 |
for(var idx = children.length - 1; idx >= 0; idx--){ |
| 360 |
var child = children[idx]; |
| 361 |
if(!child.disabled){ |
| 362 |
this.onButtonClick(child.page); |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
e.stopPropagation(); |
| 367 |
e.preventDefault(); |
| 368 |
break; |
| 369 |
case keys.DELETE: |
| 370 |
case "W".charCodeAt(0): |
| 371 |
if(this._currentChild.closable && |
| 372 |
(e.keyCode == keys.DELETE || e.ctrlKey)){ |
| 373 |
this.onCloseButtonClick(this._currentChild); |
| 374 |
|
| 375 |
|
| 376 |
e.stopPropagation(); |
| 377 |
e.preventDefault(); |
| 378 |
} |
| 379 |
break; |
| 380 |
case keys.TAB: |
| 381 |
if(e.ctrlKey){ |
| 382 |
this.onButtonClick(this.adjacent(!e.shiftKey).page); |
| 383 |
e.stopPropagation(); |
| 384 |
e.preventDefault(); |
| 385 |
} |
| 386 |
break; |
| 387 |
} |
| 388 |
|
| 389 |
if(forward !== null){ |
| 390 |
this.onButtonClick(this.adjacent(forward).page); |
| 391 |
e.stopPropagation(); |
| 392 |
e.preventDefault(); |
| 393 |
} |
| 394 |
} |
| 395 |
}, |
| 396 |
|
| 397 |
onContainerKeyDown: function( info){ |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
info.e._djpage = info.page; |
| 403 |
this.onkeydown(info.e); |
| 404 |
} |
| 405 |
}); |
| 406 |
|
| 407 |
StackController.StackButton = StackButton; |
| 408 |
|
| 409 |
return StackController; |
| 410 |
}); |