| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/date", |
| 4 |
"dojo/date/locale", |
| 5 |
"dojo/date/stamp", |
| 6 |
"dojo/_base/declare", |
| 7 |
"dojo/dom-class", |
| 8 |
"dojo/dom-construct", |
| 9 |
"dojo/_base/kernel", |
| 10 |
"dojo/keys", |
| 11 |
"dojo/_base/lang", |
| 12 |
"dojo/sniff", |
| 13 |
"dojo/query", |
| 14 |
"dojo/mouse", |
| 15 |
"dojo/on", |
| 16 |
"./_WidgetBase", |
| 17 |
"./form/_ListMouseMixin" |
| 18 |
], function(array, ddate, locale, stamp, declare, domClass, domConstruct, kernel, keys, lang, has, query, mouse, on, |
| 19 |
_WidgetBase, _ListMouseMixin){ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
var TimePicker = declare("dijit._TimePicker", [_WidgetBase, _ListMouseMixin], { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
baseClass: "dijitTimePicker", |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
pickerMin: "T00:00:00", |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pickerMax: "T23:59:59", |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
clickableIncrement: "T00:15:00", |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
visibleIncrement: "T01:00:00", |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
value: new Date(), |
| 75 |
|
| 76 |
_visibleIncrement: 2, |
| 77 |
_clickableIncrement: 1, |
| 78 |
_totalIncrements: 10, |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
constraints: {}, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
serialize: stamp.toISOString, |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
buildRendering: function(){ |
| 105 |
this.inherited(arguments); |
| 106 |
this.containerNode = this.domNode; |
| 107 |
this.timeMenu = this.domNode; |
| 108 |
}, |
| 109 |
|
| 110 |
setValue: function( value){ |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
kernel.deprecated("dijit._TimePicker:setValue() is deprecated. Use set('value', ...) instead.", "", "2.0"); |
| 116 |
this.set('value', value); |
| 117 |
}, |
| 118 |
|
| 119 |
_setValueAttr: function( date){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
this._set("value", date); |
| 128 |
this._showText(); |
| 129 |
}, |
| 130 |
|
| 131 |
_setFilterStringAttr: function(val){ |
| 132 |
|
| 133 |
|
| 134 |
this._set("filterString", val); |
| 135 |
this._showText(); |
| 136 |
}, |
| 137 |
|
| 138 |
isDisabledDate: function(){ |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
return false; |
| 146 |
}, |
| 147 |
|
| 148 |
_getFilteredNodes: function( start, maxNum, before, lastNode){ |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
var nodes = this.ownerDocument.createDocumentFragment(); |
| 158 |
|
| 159 |
for(var i = 0 ; i < this._maxIncrement; i++){ |
| 160 |
var n = this._createOption(i); |
| 161 |
if(n){ |
| 162 |
nodes.appendChild(n); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return nodes; |
| 167 |
}, |
| 168 |
|
| 169 |
_showText: function(){ |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
var fromIso = stamp.fromISOString; |
| 175 |
this.domNode.innerHTML = ""; |
| 176 |
this._clickableIncrementDate = fromIso(this.clickableIncrement); |
| 177 |
this._visibleIncrementDate = fromIso(this.visibleIncrement); |
| 178 |
|
| 179 |
var |
| 180 |
sinceMidnight = function( date){ |
| 181 |
return date.getHours() * 60 * 60 + date.getMinutes() * 60 + date.getSeconds(); |
| 182 |
}, |
| 183 |
clickableIncrementSeconds = sinceMidnight(this._clickableIncrementDate), |
| 184 |
visibleIncrementSeconds = sinceMidnight(this._visibleIncrementDate), |
| 185 |
|
| 186 |
time = (this.value || this.currentFocus).getTime(); |
| 187 |
|
| 188 |
this._refDate = fromIso(this.pickerMin); |
| 189 |
this._refDate.setFullYear(1970, 0, 1); |
| 190 |
|
| 191 |
|
| 192 |
this._clickableIncrement = 1; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
this._visibleIncrement = visibleIncrementSeconds / clickableIncrementSeconds; |
| 198 |
|
| 199 |
|
| 200 |
var endDate = fromIso(this.pickerMax); |
| 201 |
endDate.setFullYear(1970, 0, 1); |
| 202 |
var visibleRange = (endDate.getTime() - this._refDate.getTime()) * 0.001; |
| 203 |
this._maxIncrement = Math.ceil((visibleRange + 1) / clickableIncrementSeconds); |
| 204 |
|
| 205 |
var nodes = this._getFilteredNodes(); |
| 206 |
|
| 207 |
|
| 208 |
if(!nodes.firstChild && this.filterString){ |
| 209 |
this.filterString = ''; |
| 210 |
this._showText(); |
| 211 |
}else{ |
| 212 |
this.domNode.appendChild(nodes); |
| 213 |
} |
| 214 |
}, |
| 215 |
|
| 216 |
constructor: function(){ |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
this.constraints = {}; |
| 227 |
}, |
| 228 |
|
| 229 |
postMixInProperties: function(){ |
| 230 |
this.inherited(arguments); |
| 231 |
this._setConstraintsAttr(this.constraints); |
| 232 |
}, |
| 233 |
|
| 234 |
|
| 235 |
_setConstraintsAttr: function( constraints){ |
| 236 |
|
| 237 |
for (var key in { clickableIncrement: 1, visibleIncrement: 1, pickerMin: 1, pickerMax: 1 }) { |
| 238 |
if (key in constraints) { |
| 239 |
this[key] = constraints[key]; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if(!constraints.locale){ |
| 245 |
constraints.locale = this.lang; |
| 246 |
} |
| 247 |
}, |
| 248 |
|
| 249 |
_createOption: function( index){ |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
var date = new Date(this._refDate); |
| 255 |
var incrementDate = this._clickableIncrementDate; |
| 256 |
date.setHours(date.getHours() + incrementDate.getHours() * index, |
| 257 |
date.getMinutes() + incrementDate.getMinutes() * index, |
| 258 |
date.getSeconds() + incrementDate.getSeconds() * index); |
| 259 |
if(this.constraints.selector == "time"){ |
| 260 |
date.setFullYear(1970, 0, 1); |
| 261 |
} |
| 262 |
var dateString = locale.format(date, this.constraints); |
| 263 |
if(this.filterString && dateString.toLowerCase().indexOf(this.filterString) !== 0){ |
| 264 |
|
| 265 |
return null; |
| 266 |
} |
| 267 |
|
| 268 |
var div = this.ownerDocument.createElement("div"); |
| 269 |
div.className = this.baseClass + "Item"; |
| 270 |
div.date = date; |
| 271 |
div.idx = index; |
| 272 |
domConstruct.create('div', { |
| 273 |
"class": this.baseClass + "ItemInner", |
| 274 |
innerHTML: dateString |
| 275 |
}, div); |
| 276 |
|
| 277 |
var marker = index % this._visibleIncrement < 1 && index % this._visibleIncrement > -1, |
| 278 |
tick = !marker && !(index % this._clickableIncrement); |
| 279 |
if(marker){ |
| 280 |
div.className += " " + this.baseClass + "Marker"; |
| 281 |
}else if(tick){ |
| 282 |
div.className += " " + this.baseClass + "Tick"; |
| 283 |
} |
| 284 |
|
| 285 |
if(this.isDisabledDate(date)){ |
| 286 |
|
| 287 |
div.className += " " + this.baseClass + "ItemDisabled"; |
| 288 |
} |
| 289 |
if(this.value && !ddate.compare(this.value, date, this.constraints.selector)){ |
| 290 |
div.selected = true; |
| 291 |
div.className += " " + this.baseClass + "ItemSelected"; |
| 292 |
this._selectedDiv = div; |
| 293 |
if(marker){ |
| 294 |
div.className += " " + this.baseClass + "MarkerSelected"; |
| 295 |
}else if(tick){ |
| 296 |
div.className += " " + this.baseClass + "TickSelected"; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
this._highlightOption(div, true); |
| 302 |
} |
| 303 |
return div; |
| 304 |
}, |
| 305 |
|
| 306 |
onOpen: function(){ |
| 307 |
this.inherited(arguments); |
| 308 |
|
| 309 |
|
| 310 |
this.set("selected", this._selectedDiv); |
| 311 |
}, |
| 312 |
|
| 313 |
_onOptionSelected: function( tgt, change){ |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
var tdate = tgt.target.date || tgt.target.parentNode.date; |
| 323 |
if(!tdate || this.isDisabledDate(tdate)){ |
| 324 |
return; |
| 325 |
} |
| 326 |
this._set('value', tdate); |
| 327 |
this.emit("input"); |
| 328 |
if(change) { |
| 329 |
this._highlighted_option = null; |
| 330 |
this.set('value', tdate); |
| 331 |
this.onChange(tdate); |
| 332 |
} |
| 333 |
}, |
| 334 |
|
| 335 |
onChange: function( ){ |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
}, |
| 341 |
|
| 342 |
_highlightOption: function( node, highlight){ |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
if(!node){ |
| 348 |
return; |
| 349 |
} |
| 350 |
if(highlight){ |
| 351 |
if(this._highlighted_option){ |
| 352 |
this._highlightOption(this._highlighted_option, false); |
| 353 |
} |
| 354 |
this._highlighted_option = node; |
| 355 |
}else if(this._highlighted_option !== node){ |
| 356 |
return; |
| 357 |
}else{ |
| 358 |
this._highlighted_option = null; |
| 359 |
} |
| 360 |
domClass.toggle(node, this.baseClass + "ItemHover", highlight); |
| 361 |
if(domClass.contains(node, this.baseClass + "Marker")){ |
| 362 |
domClass.toggle(node, this.baseClass + "MarkerHover", highlight); |
| 363 |
}else{ |
| 364 |
domClass.toggle(node, this.baseClass + "TickHover", highlight); |
| 365 |
} |
| 366 |
}, |
| 367 |
|
| 368 |
handleKey: function( e){ |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
if(e.keyCode == keys.DOWN_ARROW){ |
| 375 |
this.selectNextNode(); |
| 376 |
this._onOptionSelected({target: this._highlighted_option}, false); |
| 377 |
e.stopPropagation(); |
| 378 |
e.preventDefault(); |
| 379 |
return false; |
| 380 |
}else if(e.keyCode == keys.UP_ARROW){ |
| 381 |
this.selectPreviousNode(); |
| 382 |
this._onOptionSelected({target: this._highlighted_option}, false); |
| 383 |
e.stopPropagation(); |
| 384 |
e.preventDefault(); |
| 385 |
return false; |
| 386 |
}else if(e.keyCode == keys.ENTER || e.keyCode === keys.TAB){ |
| 387 |
|
| 388 |
if(!this._keyboardSelected && e.keyCode === keys.TAB){ |
| 389 |
return true; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
if(this._highlighted_option){ |
| 394 |
this._onOptionSelected({target: this._highlighted_option}, true); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
return e.keyCode === keys.TAB; |
| 400 |
} |
| 401 |
return undefined; |
| 402 |
}, |
| 403 |
|
| 404 |
|
| 405 |
onHover: function( node){ |
| 406 |
this._highlightOption(node, true); |
| 407 |
}, |
| 408 |
|
| 409 |
onUnhover: function( node){ |
| 410 |
this._highlightOption(node, false); |
| 411 |
}, |
| 412 |
|
| 413 |
onSelect: function( node){ |
| 414 |
this._highlightOption(node, true); |
| 415 |
}, |
| 416 |
|
| 417 |
onDeselect: function( node){ |
| 418 |
this._highlightOption(node, false); |
| 419 |
}, |
| 420 |
|
| 421 |
onClick: function( node){ |
| 422 |
this._onOptionSelected({target: node}, true); |
| 423 |
} |
| 424 |
}); |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
return TimePicker; |
| 435 |
}); |