| 1 |
define([ |
| 2 |
"dijit/_WidgetBase", |
| 3 |
"dijit/_TemplatedMixin", |
| 4 |
"dijit/_Container", |
| 5 |
"dijit/_WidgetsInTemplateMixin", |
| 6 |
"dijit/typematic", |
| 7 |
"dojo/_base/declare", |
| 8 |
"dojo/date", |
| 9 |
"dojo/date/stamp", |
| 10 |
"dojo/date/locale", |
| 11 |
"dojo/dom-style", |
| 12 |
"dojo/dom-class", |
| 13 |
"dojo/_base/fx", |
| 14 |
"dojo/on", |
| 15 |
"dojo/_base/array", |
| 16 |
"dojo/_base/lang", |
| 17 |
"dojo/text!./Calendar/Calendar.html" |
| 18 |
], function(_WidgetBase, _TemplatedMixin, _Container, _WidgetsInTemplateMixin, dijitTypematic, |
| 19 |
declare, dojoDate, stamp, dojoDateLocale, domStyle, domClass, fx, on, array, lang, template){ |
| 20 |
return declare("dojox.widget._CalendarBase", [_WidgetBase, _TemplatedMixin, _Container, _WidgetsInTemplateMixin], { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
templateString: template, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
_views: null, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
useFx: true, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
value: new Date(), |
| 41 |
|
| 42 |
constraints: null, |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
footerFormat: "medium", |
| 48 |
|
| 49 |
constructor: function(){ |
| 50 |
this._views = []; |
| 51 |
this.value = new Date(); |
| 52 |
}, |
| 53 |
|
| 54 |
_setConstraintsAttr: function(constraints){ |
| 55 |
|
| 56 |
|
| 57 |
var c = this.constraints = constraints; |
| 58 |
if(c){ |
| 59 |
if(typeof c.min == "string"){ |
| 60 |
c.min = stamp.fromISOString(c.min); |
| 61 |
} |
| 62 |
if(typeof c.max == "string"){ |
| 63 |
c.max = stamp.fromISOString(c.max); |
| 64 |
} |
| 65 |
} |
| 66 |
}, |
| 67 |
|
| 68 |
postMixInProperties: function () { |
| 69 |
this.inherited(arguments); |
| 70 |
this.value = this.parseInitialValue(this.value); |
| 71 |
}, |
| 72 |
|
| 73 |
parseInitialValue: function(value){ |
| 74 |
if (!value || value === -1){ |
| 75 |
return new Date(); |
| 76 |
}else if(value.getFullYear){ |
| 77 |
return value; |
| 78 |
}else if (!isNaN(value)) { |
| 79 |
if (typeof this.value == "string") { |
| 80 |
value = parseInt(value); |
| 81 |
} |
| 82 |
value = this._makeDate(value); |
| 83 |
} |
| 84 |
return value; |
| 85 |
}, |
| 86 |
|
| 87 |
_makeDate: function(value){ |
| 88 |
return value; |
| 89 |
}, |
| 90 |
|
| 91 |
postCreate: function(){ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
this.displayMonth = new Date(this.get('value')); |
| 96 |
|
| 97 |
if(this._isInvalidDate(this.displayMonth)){ |
| 98 |
this.displayMonth = new Date(); |
| 99 |
} |
| 100 |
|
| 101 |
var mixin = { |
| 102 |
parent: this, |
| 103 |
_getValueAttr: lang.hitch(this, function(){return new Date(this._internalValue || this.value);}), |
| 104 |
_getDisplayMonthAttr: lang.hitch(this, function(){return new Date(this.displayMonth);}), |
| 105 |
_getConstraintsAttr: lang.hitch(this, function(){return this.constraints;}), |
| 106 |
getLang: lang.hitch(this, function(){return this.lang;}), |
| 107 |
isDisabledDate: lang.hitch(this, this.isDisabledDate), |
| 108 |
getClassForDate: lang.hitch(this, this.getClassForDate), |
| 109 |
addFx: this.useFx ? lang.hitch(this, this.addFx) : function(){} |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
array.forEach(this._views, function(widgetType){ |
| 114 |
var widget = new widgetType(mixin).placeAt(this); |
| 115 |
|
| 116 |
var header = widget.getHeader(); |
| 117 |
if(header){ |
| 118 |
|
| 119 |
this.header.appendChild(header); |
| 120 |
|
| 121 |
|
| 122 |
domStyle.set(header, "display", "none"); |
| 123 |
} |
| 124 |
|
| 125 |
domStyle.set(widget.domNode, "visibility", "hidden"); |
| 126 |
|
| 127 |
|
| 128 |
widget.on("valueSelected", lang.hitch(this, "_onDateSelected")); |
| 129 |
widget.set("value", this.get('value')); |
| 130 |
}, this); |
| 131 |
|
| 132 |
if(this._views.length < 2){ |
| 133 |
domStyle.set(this.header, "cursor", "auto"); |
| 134 |
} |
| 135 |
|
| 136 |
this.inherited(arguments); |
| 137 |
|
| 138 |
|
| 139 |
this._children = this.getChildren(); |
| 140 |
|
| 141 |
this._currentChild = 0; |
| 142 |
|
| 143 |
|
| 144 |
var today = new Date(); |
| 145 |
|
| 146 |
this.footer.innerHTML = "Today: " + |
| 147 |
dojoDateLocale.format(today, { |
| 148 |
formatLength:this.footerFormat, |
| 149 |
selector:'date', |
| 150 |
locale:this.lang}); |
| 151 |
|
| 152 |
on(this.footer, "click", lang.hitch(this, "goToToday")); |
| 153 |
|
| 154 |
var first = this._children[0]; |
| 155 |
|
| 156 |
domStyle.set(first.domNode, "top", "0px"); |
| 157 |
domStyle.set(first.domNode, "visibility", "visible"); |
| 158 |
|
| 159 |
var header = first.getHeader(); |
| 160 |
if(header){ |
| 161 |
domStyle.set(first.getHeader(), "display", ""); |
| 162 |
} |
| 163 |
|
| 164 |
domClass.toggle(this.container, "no-header", !first.useHeader); |
| 165 |
|
| 166 |
first.onDisplay(); |
| 167 |
|
| 168 |
var _this = this; |
| 169 |
|
| 170 |
var typematic = function(nodeProp, dateProp, adj){ |
| 171 |
dijitTypematic.addMouseListener(_this[nodeProp], _this, function(count){ |
| 172 |
if(count >= 0){ _this._adjustDisplay(dateProp, adj);} |
| 173 |
}, 0.8, 500); |
| 174 |
}; |
| 175 |
typematic("incrementMonth", "month", 1); |
| 176 |
typematic("decrementMonth", "month", -1); |
| 177 |
this._updateTitleStyle(); |
| 178 |
}, |
| 179 |
|
| 180 |
addFx: function(query, fromNode){ |
| 181 |
|
| 182 |
}, |
| 183 |
|
| 184 |
_isInvalidDate: function( value){ |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
return !value || isNaN(value) || typeof value != "object" || value.toString() == this._invalidDate; |
| 190 |
}, |
| 191 |
|
| 192 |
_setValueAttr: function( value){ |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if(!value){ |
| 197 |
value = new Date(); |
| 198 |
} |
| 199 |
if(!value["getFullYear"]){ |
| 200 |
value = stamp.fromISOString(value + ""); |
| 201 |
} |
| 202 |
if(this._isInvalidDate(value)){ |
| 203 |
return false; |
| 204 |
} |
| 205 |
if(!this.value || dojoDate.compare(value, this.value)){ |
| 206 |
value = new Date(value); |
| 207 |
this.displayMonth = new Date(value); |
| 208 |
this._internalValue = value; |
| 209 |
if(!this.isDisabledDate(value, this.lang) && this._currentChild === 0){ |
| 210 |
this.value = value; |
| 211 |
this.onChange(value); |
| 212 |
} |
| 213 |
if (this._children && this._children.length > 0) { |
| 214 |
this._children[this._currentChild].set("value", this.value); |
| 215 |
} |
| 216 |
return true; |
| 217 |
} |
| 218 |
this.onExecute(); |
| 219 |
return false; |
| 220 |
}, |
| 221 |
|
| 222 |
isDisabledDate: function(date, locale){ |
| 223 |
|
| 224 |
|
| 225 |
var c = this.constraints; |
| 226 |
var compare = dojoDate.compare; |
| 227 |
return c && (c.min && (compare(c.min, date, "date") > 0) || |
| 228 |
(c.max && compare(c.max, date, "date") < 0)); |
| 229 |
}, |
| 230 |
|
| 231 |
onValueSelected: function(date){ |
| 232 |
|
| 233 |
|
| 234 |
}, |
| 235 |
|
| 236 |
_onDateSelected: function(date, formattedValue, force){ |
| 237 |
this.displayMonth = date; |
| 238 |
|
| 239 |
this.set("value", date); |
| 240 |
|
| 241 |
|
| 242 |
if(!this._transitionVert(-1)){ |
| 243 |
if(!formattedValue && formattedValue !== 0){ |
| 244 |
formattedValue = this.get('value'); |
| 245 |
} |
| 246 |
this.onValueSelected(formattedValue); |
| 247 |
} |
| 248 |
|
| 249 |
}, |
| 250 |
|
| 251 |
onChange: function(date){ |
| 252 |
|
| 253 |
|
| 254 |
}, |
| 255 |
|
| 256 |
onHeaderClick: function(e){ |
| 257 |
|
| 258 |
|
| 259 |
this._transitionVert(1); |
| 260 |
}, |
| 261 |
|
| 262 |
goToToday: function(){ |
| 263 |
this.set("value", new Date()); |
| 264 |
this.onValueSelected(this.get('value')); |
| 265 |
}, |
| 266 |
|
| 267 |
_transitionVert: function(direction){ |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
var curWidget = this._children[this._currentChild]; |
| 274 |
var nextWidget = this._children[this._currentChild + direction]; |
| 275 |
if(!nextWidget){return false;} |
| 276 |
|
| 277 |
domStyle.set(nextWidget.domNode, "visibility", "visible"); |
| 278 |
|
| 279 |
var height = domStyle.get(this.containerNode, "height"); |
| 280 |
nextWidget.set("value", this.displayMonth); |
| 281 |
|
| 282 |
if(curWidget.header){ |
| 283 |
domStyle.set(curWidget.header, "display", "none"); |
| 284 |
} |
| 285 |
if(nextWidget.header){ |
| 286 |
domStyle.set(nextWidget.header, "display", ""); |
| 287 |
} |
| 288 |
domStyle.set(nextWidget.domNode, "top", (height * -1) + "px"); |
| 289 |
domStyle.set(nextWidget.domNode, "visibility", "visible"); |
| 290 |
|
| 291 |
this._currentChild += direction; |
| 292 |
|
| 293 |
var height1 = height * direction; |
| 294 |
var height2 = 0; |
| 295 |
domStyle.set(nextWidget.domNode, "top", (height1 * -1) + "px"); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
var anim1 = fx.animateProperty({ |
| 300 |
node: curWidget.domNode, |
| 301 |
properties: {top: height1}, |
| 302 |
onEnd: function(){ |
| 303 |
domStyle.set(curWidget.domNode, "visibility", "hidden"); |
| 304 |
} |
| 305 |
}); |
| 306 |
var anim2 = fx.animateProperty({ |
| 307 |
node: nextWidget.domNode, |
| 308 |
properties: {top: height2}, |
| 309 |
onEnd: function(){ |
| 310 |
nextWidget.onDisplay(); |
| 311 |
} |
| 312 |
}); |
| 313 |
|
| 314 |
domClass.toggle(this.container, "no-header", !nextWidget.useHeader); |
| 315 |
|
| 316 |
anim1.play(); |
| 317 |
anim2.play(); |
| 318 |
curWidget.onBeforeUnDisplay(); |
| 319 |
nextWidget.onBeforeDisplay(); |
| 320 |
|
| 321 |
this._updateTitleStyle(); |
| 322 |
return true; |
| 323 |
}, |
| 324 |
|
| 325 |
_updateTitleStyle: function(){ |
| 326 |
domClass.toggle(this.header, "navToPanel", this._currentChild < this._children.length -1); |
| 327 |
}, |
| 328 |
|
| 329 |
_slideTable: function(widget, direction, callback){ |
| 330 |
|
| 331 |
|
| 332 |
var table = widget.domNode; |
| 333 |
|
| 334 |
|
| 335 |
var newTable = table.cloneNode(true); |
| 336 |
var left = domStyle.get(table, "width"); |
| 337 |
|
| 338 |
table.parentNode.appendChild(newTable); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
domStyle.set(table, "left", (left * direction) + "px"); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
callback(); |
| 347 |
|
| 348 |
|
| 349 |
var anim1 = fx.animateProperty({node: newTable, properties:{left: left * direction * -1}, duration: 500, onEnd: function(){ |
| 350 |
newTable.parentNode.removeChild(newTable); |
| 351 |
}}); |
| 352 |
var anim2 = fx.animateProperty({node: table, properties:{left: 0}, duration: 500}); |
| 353 |
|
| 354 |
anim1.play(); |
| 355 |
anim2.play(); |
| 356 |
}, |
| 357 |
|
| 358 |
_addView: function(view){ |
| 359 |
|
| 360 |
this._views.push(view); |
| 361 |
}, |
| 362 |
|
| 363 |
getClassForDate: function(dateObject, locale){ |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
}, |
| 372 |
|
| 373 |
_adjustDisplay: function(part, amount, noSlide){ |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
var child = this._children[this._currentChild]; |
| 378 |
|
| 379 |
var month = this.displayMonth = child.adjustDate(this.displayMonth, amount); |
| 380 |
|
| 381 |
this._slideTable(child, amount, function(){ |
| 382 |
child.set("value", month); |
| 383 |
}); |
| 384 |
}, |
| 385 |
|
| 386 |
onExecute:function(){ |
| 387 |
|
| 388 |
|
| 389 |
} |
| 390 |
}); |
| 391 |
}); |