Calendar.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.widget.Calendar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.widget.Calendar"] = true;
  8. dojo.provide("dojox.widget.Calendar");
  9. dojo.experimental("dojox.widget.Calendar");
  10. dojo.require("dijit.Calendar");
  11. dojo.require("dijit._Container");
  12. dojo.declare("dojox.widget._CalendarBase", [dijit._Widget, dijit._Templated, dijit._Container], {
  13. // summary:
  14. // The Root class for all _Calendar extensions
  15. // templateString: String
  16. // The template to be used to construct the widget.
  17. templateString: dojo.cache("dojox.widget", "Calendar/Calendar.html", "<div class=\"dojoxCalendar\">\n <div tabindex=\"0\" class=\"dojoxCalendarContainer\" style=\"visibility: visible;\" dojoAttachPoint=\"container\">\n\t\t<div style=\"display:none\">\n\t\t\t<div dojoAttachPoint=\"previousYearLabelNode\"></div>\n\t\t\t<div dojoAttachPoint=\"nextYearLabelNode\"></div>\n\t\t\t<div dojoAttachPoint=\"monthLabelSpacer\"></div>\n\t\t</div>\n <div class=\"dojoxCalendarHeader\">\n <div>\n <div class=\"dojoxCalendarDecrease\" dojoAttachPoint=\"decrementMonth\"></div>\n </div>\n <div class=\"\">\n <div class=\"dojoxCalendarIncrease\" dojoAttachPoint=\"incrementMonth\"></div>\n </div>\n <div class=\"dojoxCalendarTitle\" dojoAttachPoint=\"header\" dojoAttachEvent=\"onclick: onHeaderClick\">\n </div>\n </div>\n <div class=\"dojoxCalendarBody\" dojoAttachPoint=\"containerNode\"></div>\n <div class=\"\">\n <div class=\"dojoxCalendarFooter\" dojoAttachPoint=\"footer\"> \n </div>\n </div>\n </div>\n</div>\n"),
  18. // _views: Array
  19. // The list of mixin views available on this calendar.
  20. _views: null,
  21. // useFx: Boolean
  22. // Specifies if visual effects should be applied to the widget.
  23. // The default behavior of the widget does not contain any effects.
  24. // The dojox.widget.CalendarFx package is needed for these.
  25. useFx: true,
  26. // widgetsInTemplate: Boolean
  27. // This widget is a container of other widgets, so this is true.
  28. widgetsInTemplate: true,
  29. // value: Date
  30. // The currently selected Date
  31. value: new Date(),
  32. constraints: null,
  33. // footerFormat: String
  34. // The date format of the date displayed in the footer. Can be
  35. // 'short', 'medium', and 'long'
  36. footerFormat: "medium",
  37. constructor: function(){
  38. this._views = [];
  39. this.value = new Date();
  40. },
  41. postMixInProperties: function(){
  42. var c = this.constraints;
  43. if(c){
  44. var fromISO = dojo.date.stamp.fromISOString;
  45. if(typeof c.min == "string"){
  46. c.min = fromISO(c.min);
  47. }
  48. if(typeof c.max == "string"){
  49. c.max = fromISO(c.max);
  50. }
  51. }
  52. this.value = this.parseInitialValue(this.value);
  53. },
  54. parseInitialValue: function(value){
  55. if (!value || value === -1){
  56. return new Date();
  57. }else if(value.getFullYear){
  58. return value;
  59. }else if (!isNaN(value)) {
  60. if (typeof this.value == "string") {
  61. value = parseInt(value);
  62. }
  63. value = this._makeDate(value);
  64. }
  65. return value;
  66. },
  67. _makeDate: function(value){
  68. return value;//new Date(value);
  69. },
  70. postCreate: function(){
  71. // summary:
  72. // Instantiates the mixin views
  73. this.displayMonth = new Date(this.get('value'));
  74. if(this._isInvalidDate(this.displayMonth)){
  75. this.displayMonth = new Date();
  76. }
  77. var mixin = {
  78. parent: this,
  79. _getValueAttr: dojo.hitch(this, function(){return new Date(this._internalValue || this.value);}),
  80. _getDisplayMonthAttr: dojo.hitch(this, function(){return new Date(this.displayMonth);}),
  81. _getConstraintsAttr: dojo.hitch(this, function(){return this.constraints;}),
  82. getLang: dojo.hitch(this, function(){return this.lang;}),
  83. isDisabledDate: dojo.hitch(this, this.isDisabledDate),
  84. getClassForDate: dojo.hitch(this, this.getClassForDate),
  85. addFx: this.useFx ? dojo.hitch(this, this.addFx) : function(){}
  86. };
  87. //Add the mixed in views.
  88. dojo.forEach(this._views, function(widgetType){
  89. var widget = new widgetType(mixin, dojo.create('div'));
  90. this.addChild(widget);
  91. var header = widget.getHeader();
  92. if(header){
  93. //place the views's header node in the header of the main widget
  94. this.header.appendChild(header);
  95. //hide the header node of the widget
  96. dojo.style(header, "display", "none");
  97. }
  98. //Hide all views
  99. dojo.style(widget.domNode, "visibility", "hidden");
  100. //Listen for the values in a view to be selected
  101. dojo.connect(widget, "onValueSelected", this, "_onDateSelected");
  102. widget.set("value", this.get('value'));
  103. }, this);
  104. if(this._views.length < 2){
  105. dojo.style(this.header, "cursor", "auto");
  106. }
  107. this.inherited(arguments);
  108. // Cache the list of children widgets.
  109. this._children = this.getChildren();
  110. this._currentChild = 0;
  111. //Populate the footer with today's date.
  112. var today = new Date();
  113. this.footer.innerHTML = "Today: "
  114. + dojo.date.locale.format(today, {
  115. formatLength:this.footerFormat,
  116. selector:'date',
  117. locale:this.lang});
  118. dojo.connect(this.footer, "onclick", this, "goToToday");
  119. var first = this._children[0];
  120. dojo.style(first.domNode, "top", "0px");
  121. dojo.style(first.domNode, "visibility", "visible");
  122. var header = first.getHeader();
  123. if(header){
  124. dojo.style(first.getHeader(), "display", "");
  125. }
  126. dojo[first.useHeader ? "removeClass" : "addClass"](this.container, "no-header");
  127. first.onDisplay();
  128. var _this = this;
  129. var typematic = function(nodeProp, dateProp, adj){
  130. dijit.typematic.addMouseListener(_this[nodeProp], _this, function(count){
  131. if(count >= 0){ _this._adjustDisplay(dateProp, adj);}
  132. }, 0.8, 500);
  133. };
  134. typematic("incrementMonth", "month", 1);
  135. typematic("decrementMonth", "month", -1);
  136. this._updateTitleStyle();
  137. },
  138. addFx: function(query, fromNode){
  139. // Stub function than can be overridden to add effects.
  140. },
  141. _isInvalidDate: function(/*Date*/ value){
  142. // summary:
  143. // Runs various tests on the value, checking for invalid conditions
  144. // tags:
  145. // private
  146. return !value || isNaN(value) || typeof value != "object" || value.toString() == this._invalidDate;
  147. },
  148. _setValueAttr: function(/*Date*/ value){
  149. // summary:
  150. // Set the current date and update the UI. If the date is disabled, the selection will
  151. // not change, but the display will change to the corresponding month.
  152. if(!value){
  153. value = new Date();
  154. }
  155. if(!value["getFullYear"]){
  156. value = dojo.date.stamp.fromISOString(value + "");
  157. }
  158. if(this._isInvalidDate(value)){
  159. return false;
  160. }
  161. if(!this.value || dojo.date.compare(value, this.value)){
  162. value = new Date(value);
  163. this.displayMonth = new Date(value);
  164. this._internalValue = value;
  165. if(!this.isDisabledDate(value, this.lang) && this._currentChild == 0){
  166. this.value = value;
  167. this.onChange(value);
  168. }
  169. if (this._children && this._children.length > 0) {
  170. this._children[this._currentChild].set("value", this.value);
  171. }
  172. return true;
  173. }
  174. return false;
  175. },
  176. isDisabledDate: function(/*Date*/date, /*String?*/locale){
  177. // summary:
  178. // May be overridden to disable certain dates in the calendar e.g. `isDisabledDate=dojo.date.locale.isWeekend`
  179. var c = this.constraints;
  180. var compare = dojo.date.compare;
  181. return c && (c.min && (compare(c.min, date, "date") > 0) ||
  182. (c.max && compare(c.max, date, "date") < 0));
  183. },
  184. onValueSelected: function(/*Date*/date){
  185. // summary:
  186. // A date cell was selected. It may be the same as the previous value.
  187. },
  188. _onDateSelected: function(date, formattedValue, force){
  189. this.displayMonth = date;
  190. this.set("value", date)
  191. //Only change the selected value if it was chosen from the
  192. //first child.
  193. if(!this._transitionVert(-1)){
  194. if(!formattedValue && formattedValue !== 0){
  195. formattedValue = this.get('value');
  196. }
  197. this.onValueSelected(formattedValue);
  198. }
  199. },
  200. onChange: function(/*Date*/date){
  201. // summary:
  202. // Called only when the selected date has changed
  203. },
  204. onHeaderClick: function(e){
  205. // summary:
  206. // Transitions to the next view.
  207. this._transitionVert(1);
  208. },
  209. goToToday: function(){
  210. this.set("value", new Date());
  211. this.onValueSelected(this.get('value'));
  212. },
  213. _transitionVert: function(/*Number*/direction){
  214. // summary:
  215. // Animates the views to show one and hide another, in a
  216. // vertical direction.
  217. // If 'direction' is 1, then the views slide upwards.
  218. // If 'direction' is -1, the views slide downwards.
  219. var curWidget = this._children[this._currentChild];
  220. var nextWidget = this._children[this._currentChild + direction];
  221. if(!nextWidget){return false;}
  222. dojo.style(nextWidget.domNode, "visibility", "visible");
  223. var height = dojo.style(this.containerNode, "height");
  224. nextWidget.set("value", this.displayMonth);
  225. if(curWidget.header){
  226. dojo.style(curWidget.header, "display", "none");
  227. }
  228. if(nextWidget.header){
  229. dojo.style(nextWidget.header, "display", "");
  230. }
  231. dojo.style(nextWidget.domNode, "top", (height * -1) + "px");
  232. dojo.style(nextWidget.domNode, "visibility", "visible");
  233. this._currentChild += direction;
  234. var height1 = height * direction;
  235. var height2 = 0;
  236. dojo.style(nextWidget.domNode, "top", (height1 * -1) + "px");
  237. // summary: Slides two nodes vertically.
  238. var anim1 = dojo.animateProperty({
  239. node: curWidget.domNode,
  240. properties: {top: height1},
  241. onEnd: function(){
  242. dojo.style(curWidget.domNode, "visibility", "hidden");
  243. }
  244. });
  245. var anim2 = dojo.animateProperty({
  246. node: nextWidget.domNode,
  247. properties: {top: height2},
  248. onEnd: function(){
  249. nextWidget.onDisplay();
  250. }
  251. });
  252. dojo[nextWidget.useHeader ? "removeClass" : "addClass"](this.container, "no-header");
  253. anim1.play();
  254. anim2.play();
  255. curWidget.onBeforeUnDisplay()
  256. nextWidget.onBeforeDisplay();
  257. this._updateTitleStyle();
  258. return true;
  259. },
  260. _updateTitleStyle: function(){
  261. dojo[this._currentChild < this._children.length -1 ? "addClass" : "removeClass"](this.header, "navToPanel");
  262. },
  263. _slideTable: function(/*String*/widget, /*Number*/direction, /*Function*/callback){
  264. // summary:
  265. // Animates the horizontal sliding of a table.
  266. var table = widget.domNode;
  267. //Clone the existing table
  268. var newTable = table.cloneNode(true);
  269. var left = dojo.style(table, "width");
  270. table.parentNode.appendChild(newTable);
  271. //Place the existing node either to the left or the right of the new node,
  272. //depending on which direction it is to slide.
  273. dojo.style(table, "left", (left * direction) + "px");
  274. //Call the function that generally populates the new cloned node with new data.
  275. //It may also attach event listeners.
  276. callback();
  277. //Animate the two nodes.
  278. var anim1 = dojo.animateProperty({node: newTable, properties:{left: left * direction * -1}, duration: 500, onEnd: function(){
  279. newTable.parentNode.removeChild(newTable);
  280. }});
  281. var anim2 = dojo.animateProperty({node: table, properties:{left: 0}, duration: 500});
  282. anim1.play();
  283. anim2.play();
  284. },
  285. _addView: function(view){
  286. //Insert the view at the start of the array.
  287. this._views.push(view);
  288. },
  289. getClassForDate: function(/*Date*/dateObject, /*String?*/locale){
  290. // summary:
  291. // May be overridden to return CSS classes to associate with the date entry for the given dateObject,
  292. // for example to indicate a holiday in specified locale.
  293. /*=====
  294. return ""; // String
  295. =====*/
  296. },
  297. _adjustDisplay: function(/*String*/part, /*int*/amount, noSlide){
  298. // summary:
  299. // This function overrides the base function defined in dijit.Calendar.
  300. // It changes the displayed years, months and days depending on the inputs.
  301. var child = this._children[this._currentChild];
  302. var month = this.displayMonth = child.adjustDate(this.displayMonth, amount);
  303. this._slideTable(child, amount, function(){
  304. child.set("value", month);
  305. });
  306. }
  307. });
  308. dojo.declare("dojox.widget._CalendarView", dijit._Widget, {
  309. // summary:
  310. // Base implementation for all view mixins.
  311. // All calendar views should extend this widget.
  312. headerClass: "",
  313. useHeader: true,
  314. cloneClass: function(clazz, n, before){
  315. // summary:
  316. // Clones all nodes with the class 'clazz' in a widget
  317. var template = dojo.query(clazz, this.domNode)[0];
  318. var i;
  319. if(!before){
  320. for(i = 0; i < n; i++){
  321. template.parentNode.appendChild(template.cloneNode(true));
  322. }
  323. }else{
  324. var bNode = dojo.query(clazz, this.domNode)[0];
  325. for(i = 0; i < n; i++){
  326. template.parentNode.insertBefore(template.cloneNode(true), bNode);
  327. }
  328. }
  329. },
  330. _setText: function(node, text){
  331. // summary:
  332. // Sets the text inside a node
  333. if(node.innerHTML != text){
  334. dojo.empty(node);
  335. node.appendChild(dojo.doc.createTextNode(text));
  336. }
  337. },
  338. getHeader: function(){
  339. // summary:
  340. // Returns the header node of a view. If none exists,
  341. // an empty DIV is created and returned.
  342. return this.header || (this.header = this.header = dojo.create("span", { "class":this.headerClass }));
  343. },
  344. onValueSelected: function(date){
  345. //Stub function called when a date is selected
  346. },
  347. adjustDate: function(date, amount){
  348. // summary:
  349. // Adds or subtracts values from a date.
  350. // The unit, e.g. "day", "month" or "year", is
  351. // specified in the "datePart" property of the
  352. // calendar view mixin.
  353. return dojo.date.add(date, this.datePart, amount);
  354. },
  355. onDisplay: function(){
  356. // summary:
  357. // Stub function that can be used to tell a view when it is shown.
  358. },
  359. onBeforeDisplay: function(){
  360. // summary:
  361. // Stub function that can be used to tell a view it is about to be shown.
  362. },
  363. onBeforeUnDisplay: function(){
  364. // summary:
  365. // Stub function that can be used to tell
  366. // a view when it is no longer shown.
  367. }
  368. });
  369. dojo.declare("dojox.widget._CalendarDay", null, {
  370. // summary:
  371. // Mixin for the dojox.widget.Calendar which provides
  372. // the standard day-view. A single month is shown at a time.
  373. parent: null,
  374. constructor: function(){
  375. this._addView(dojox.widget._CalendarDayView);
  376. }
  377. });
  378. dojo.declare("dojox.widget._CalendarDayView", [dojox.widget._CalendarView, dijit._Templated], {
  379. // summary: View class for the dojox.widget.Calendar.
  380. // Adds a view showing every day of a single month to the calendar.
  381. // This should not be mixed in directly with dojox.widget._CalendarBase.
  382. // Instead, use dojox.widget._CalendarDay
  383. // templateString: String
  384. // The template to be used to construct the widget.
  385. templateString: dojo.cache("dojox.widget", "Calendar/CalendarDay.html", "<div class=\"dijitCalendarDayLabels\" style=\"left: 0px;\" dojoAttachPoint=\"dayContainer\">\n\t<div dojoAttachPoint=\"header\">\n\t\t<div dojoAttachPoint=\"monthAndYearHeader\">\n\t\t\t<span dojoAttachPoint=\"monthLabelNode\" class=\"dojoxCalendarMonthLabelNode\"></span>\n\t\t\t<span dojoAttachPoint=\"headerComma\" class=\"dojoxCalendarComma\">,</span>\n\t\t\t<span dojoAttachPoint=\"yearLabelNode\" class=\"dojoxCalendarDayYearLabel\"></span>\n\t\t</div>\n\t</div>\n\t<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: auto;\">\n\t\t<thead>\n\t\t\t<tr>\n\t\t\t\t<td class=\"dijitCalendarDayLabelTemplate\"><div class=\"dijitCalendarDayLabel\"></div></td>\n\t\t\t</tr>\n\t\t</thead>\n\t\t<tbody dojoAttachEvent=\"onclick: _onDayClick\">\n\t\t\t<tr class=\"dijitCalendarWeekTemplate\">\n\t\t\t\t<td class=\"dojoxCalendarNextMonth dijitCalendarDateTemplate\">\n\t\t\t\t\t<div class=\"dijitCalendarDateLabel\"></div>\n\t\t\t\t</td>\n\t\t\t</tr>\n\t\t</tbody>\n\t</table>\n</div>\n"),
  386. // datePart: String
  387. // Specifies how much to increment the displayed date when the user
  388. // clicks the array button to increment of decrement the view.
  389. datePart: "month",
  390. // dayWidth: String
  391. // Specifies the type of day name to display. "narrow" causes just one letter to be shown.
  392. dayWidth: "narrow",
  393. postCreate: function(){
  394. // summary:
  395. // Constructs the calendar view.
  396. this.cloneClass(".dijitCalendarDayLabelTemplate", 6);
  397. this.cloneClass(".dijitCalendarDateTemplate", 6);
  398. // now make 6 week rows
  399. this.cloneClass(".dijitCalendarWeekTemplate", 5);
  400. // insert localized day names in the header
  401. var dayNames = dojo.date.locale.getNames('days', this.dayWidth, 'standAlone', this.getLang());
  402. var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.getLang());
  403. // Set the text of the day labels.
  404. dojo.query(".dijitCalendarDayLabel", this.domNode).forEach(function(label, i){
  405. this._setText(label, dayNames[(i + dayOffset) % 7]);
  406. }, this);
  407. },
  408. onDisplay: function(){
  409. if(!this._addedFx){
  410. // Add visual effects to the view, if any has been specified.
  411. this._addedFx = true;
  412. this.addFx(".dijitCalendarDateTemplate div", this.domNode);
  413. }
  414. },
  415. _onDayClick: function(e){
  416. // summary:
  417. // Executed when a day value is clicked.
  418. // If the user somehow clicked the TR, rather than a
  419. // cell, ignore it.
  420. if(typeof(e.target._date) == "undefined"){return;}
  421. var date = new Date(this.get("displayMonth"));
  422. var p = e.target.parentNode;
  423. var c = "dijitCalendar";
  424. var d = dojo.hasClass(p, c + "PreviousMonth") ? -1 :
  425. (dojo.hasClass(p, c + "NextMonth") ? 1 : 0);
  426. if(d){date = dojo.date.add(date, "month", d)}
  427. date.setDate(e.target._date);
  428. // If the day is disabled, ignore it
  429. if(this.isDisabledDate(date)){
  430. dojo.stopEvent(e);
  431. return;
  432. }
  433. this.parent._onDateSelected(date);
  434. },
  435. _setValueAttr: function(value){
  436. //Change the day values
  437. this._populateDays();
  438. },
  439. _populateDays: function(){
  440. // summary:
  441. // Fills the days of the current month.
  442. var currentDate = new Date(this.get("displayMonth"));
  443. currentDate.setDate(1);
  444. var firstDay = currentDate.getDay();
  445. var daysInMonth = dojo.date.getDaysInMonth(currentDate);
  446. var daysInPreviousMonth = dojo.date.getDaysInMonth(dojo.date.add(currentDate, "month", -1));
  447. var today = new Date();
  448. var selected = this.get('value');
  449. var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.getLang());
  450. if(dayOffset > firstDay){ dayOffset -= 7; }
  451. var compareDate = dojo.date.compare;
  452. var templateCls = ".dijitCalendarDateTemplate";
  453. var selectedCls = "dijitCalendarSelectedDate";
  454. var oldDate = this._lastDate;
  455. var redrawRequired = oldDate == null
  456. || oldDate.getMonth() != currentDate.getMonth()
  457. || oldDate.getFullYear() != currentDate.getFullYear();
  458. this._lastDate = currentDate;
  459. // If still showing the same month, it's much faster to not redraw,
  460. // and just change the selected date.
  461. if(!redrawRequired){
  462. dojo.query(templateCls, this.domNode)
  463. .removeClass(selectedCls)
  464. .filter(function(node){
  465. return node.className.indexOf("dijitCalendarCurrent") > -1
  466. && node._date == selected.getDate();
  467. })
  468. .addClass(selectedCls);
  469. return;
  470. }
  471. // Iterate through dates in the calendar and fill in date numbers and style info
  472. dojo.query(templateCls, this.domNode).forEach(function(template, i){
  473. i += dayOffset;
  474. var date = new Date(currentDate);
  475. var number, clazz = "dijitCalendar", adj = 0;
  476. if(i < firstDay){
  477. number = daysInPreviousMonth - firstDay + i + 1;
  478. adj = -1;
  479. clazz += "Previous";
  480. }else if(i >= (firstDay + daysInMonth)){
  481. number = i - firstDay - daysInMonth + 1;
  482. adj = 1;
  483. clazz += "Next";
  484. }else{
  485. number = i - firstDay + 1;
  486. clazz += "Current";
  487. }
  488. if(adj){
  489. date = dojo.date.add(date, "month", adj);
  490. }
  491. date.setDate(number);
  492. if(!compareDate(date, today, "date")){
  493. clazz = "dijitCalendarCurrentDate " + clazz;
  494. }
  495. if(!compareDate(date, selected, "date")
  496. && !compareDate(date, selected, "month")
  497. && !compareDate(date, selected, "year") ){
  498. clazz = selectedCls + " " + clazz;
  499. }
  500. if(this.isDisabledDate(date, this.getLang())){
  501. clazz = " dijitCalendarDisabledDate " + clazz;
  502. }
  503. var clazz2 = this.getClassForDate(date, this.getLang());
  504. if(clazz2){
  505. clazz = clazz2 + " " + clazz;
  506. }
  507. template.className = clazz + "Month dijitCalendarDateTemplate";
  508. template.dijitDateValue = date.valueOf();
  509. var label = dojo.query(".dijitCalendarDateLabel", template)[0];
  510. this._setText(label, date.getDate());
  511. label._date = label.parentNode._date = date.getDate();
  512. }, this);
  513. // Fill in localized month name
  514. var monthNames = dojo.date.locale.getNames('months', 'wide', 'standAlone', this.getLang());
  515. this._setText(this.monthLabelNode, monthNames[currentDate.getMonth()]);
  516. this._setText(this.yearLabelNode, currentDate.getFullYear());
  517. }
  518. });
  519. dojo.declare("dojox.widget._CalendarMonthYear", null, {
  520. // summary:
  521. // Mixin class for adding a view listing all 12
  522. // months of the year to the dojox.widget._CalendarBase
  523. constructor: function(){
  524. // summary:
  525. // Adds a dojox.widget._CalendarMonthView view to the calendar widget.
  526. this._addView(dojox.widget._CalendarMonthYearView);
  527. }
  528. });
  529. dojo.declare("dojox.widget._CalendarMonthYearView", [dojox.widget._CalendarView, dijit._Templated], {
  530. // summary:
  531. // A Calendar view listing the 12 months of the year
  532. // templateString: String
  533. // The template to be used to construct the widget.
  534. templateString: dojo.cache("dojox.widget", "Calendar/CalendarMonthYear.html", "<div class=\"dojoxCal-MY-labels\" style=\"left: 0px;\"\t\n\tdojoAttachPoint=\"myContainer\" dojoAttachEvent=\"onclick: onClick\">\n\t\t<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: auto;\">\n\t\t\t\t<tbody>\n\t\t\t\t\t\t<tr class=\"dojoxCal-MY-G-Template\">\n\t\t\t\t\t\t\t\t<td class=\"dojoxCal-MY-M-Template\">\n\t\t\t\t\t\t\t\t\t\t<div class=\"dojoxCalendarMonthLabel\"></div>\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t<td class=\"dojoxCal-MY-M-Template\">\n\t\t\t\t\t\t\t\t\t\t<div class=\"dojoxCalendarMonthLabel\"></div>\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t<td class=\"dojoxCal-MY-Y-Template\">\n\t\t\t\t\t\t\t\t\t\t<div class=\"dojoxCalendarYearLabel\"></div>\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t<td class=\"dojoxCal-MY-Y-Template\">\n\t\t\t\t\t\t\t\t\t\t<div class=\"dojoxCalendarYearLabel\"></div>\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t <tr class=\"dojoxCal-MY-btns\">\n\t\t\t\t\t\t \t <td class=\"dojoxCal-MY-btns\" colspan=\"4\">\n\t\t\t\t\t\t \t\t <span class=\"dijitReset dijitInline dijitButtonNode ok-btn\" dojoAttachEvent=\"onclick: onOk\" dojoAttachPoint=\"okBtn\">\n\t\t\t\t\t\t \t \t \t <button\tclass=\"dijitReset dijitStretch dijitButtonContents\">OK</button>\n\t\t\t\t\t\t\t\t </span>\n\t\t\t\t\t\t\t\t <span class=\"dijitReset dijitInline dijitButtonNode cancel-btn\" dojoAttachEvent=\"onclick: onCancel\" dojoAttachPoint=\"cancelBtn\">\n\t\t\t\t\t\t \t \t\t <button\tclass=\"dijitReset dijitStretch dijitButtonContents\">Cancel</button>\n\t\t\t\t\t\t\t\t </span>\n\t\t\t\t\t\t \t </td>\n\t\t\t\t\t\t </tr>\n\t\t\t\t</tbody>\n\t\t</table>\n</div>\n"),
  535. // datePart: String
  536. // Specifies how much to increment the displayed date when the user
  537. // clicks the array button to increment of decrement the view.
  538. datePart: "year",
  539. // displayedYears: Number
  540. // The number of years to display at once.
  541. displayedYears: 10,
  542. useHeader: false,
  543. postCreate: function(){
  544. this.cloneClass(".dojoxCal-MY-G-Template", 5, ".dojoxCal-MY-btns");
  545. this.monthContainer = this.yearContainer = this.myContainer;
  546. var yClass = "dojoxCalendarYearLabel";
  547. var dClass = "dojoxCalendarDecrease";
  548. var iClass = "dojoxCalendarIncrease";
  549. dojo.query("." + yClass, this.myContainer).forEach(function(node, idx){
  550. var clazz = iClass;
  551. switch(idx){
  552. case 0:
  553. clazz = dClass;
  554. case 1:
  555. dojo.removeClass(node, yClass);
  556. dojo.addClass(node, clazz);
  557. break;
  558. }
  559. });
  560. // Get the year increment and decrement buttons.
  561. this._decBtn = dojo.query('.' + dClass, this.myContainer)[0];
  562. this._incBtn = dojo.query('.' + iClass, this.myContainer)[0];
  563. dojo.query(".dojoxCal-MY-M-Template", this.domNode)
  564. .filter(function(item){
  565. return item.cellIndex == 1;
  566. })
  567. .addClass("dojoxCal-MY-M-last");
  568. dojo.connect(this, "onBeforeDisplay", dojo.hitch(this, function(){
  569. this._cachedDate = new Date(this.get("value").getTime());
  570. this._populateYears(this._cachedDate.getFullYear());
  571. this._populateMonths();
  572. this._updateSelectedMonth();
  573. this._updateSelectedYear();
  574. }));
  575. dojo.connect(this, "_populateYears", dojo.hitch(this, function(){
  576. this._updateSelectedYear();
  577. }));
  578. dojo.connect(this, "_populateMonths", dojo.hitch(this, function(){
  579. this._updateSelectedMonth();
  580. }));
  581. this._cachedDate = this.get("value");
  582. this._populateYears();
  583. this._populateMonths();
  584. // Add visual effects to the view, if any have been mixed in
  585. this.addFx(".dojoxCalendarMonthLabel,.dojoxCalendarYearLabel ", this.myContainer);
  586. },
  587. _setValueAttr: function(value){
  588. if (value && value.getFullYear()) {
  589. this._populateYears(value.getFullYear());
  590. }
  591. },
  592. getHeader: function(){
  593. return null;
  594. },
  595. _getMonthNames: function(format){
  596. // summary:
  597. // Returns localized month names
  598. this._monthNames = this._monthNames || dojo.date.locale.getNames('months', format, 'standAlone', this.getLang());
  599. return this._monthNames;
  600. },
  601. _populateMonths: function(){
  602. // summary:
  603. // Populate the month names using the localized values.
  604. var monthNames = this._getMonthNames('abbr');
  605. dojo.query(".dojoxCalendarMonthLabel", this.monthContainer).forEach(dojo.hitch(this, function(node, cnt){
  606. this._setText(node, monthNames[cnt]);
  607. }));
  608. var constraints = this.get('constraints');
  609. if(constraints){
  610. var date = new Date();
  611. date.setFullYear(this._year);
  612. var min = -1, max = 12;
  613. if(constraints.min){
  614. var minY = constraints.min.getFullYear();
  615. if(minY > this._year){
  616. min = 12;
  617. }else if(minY == this._year){
  618. min = constraints.min.getMonth();
  619. }
  620. }
  621. if(constraints.max){
  622. var maxY = constraints.max.getFullYear();
  623. if(maxY < this._year){
  624. max = -1;
  625. }else if(maxY == this._year){
  626. max = constraints.max.getMonth();
  627. }
  628. }
  629. dojo.query(".dojoxCalendarMonthLabel", this.monthContainer)
  630. .forEach(dojo.hitch(this, function(node, cnt){
  631. dojo[(cnt < min || cnt > max) ? "addClass" : "removeClass"]
  632. (node, 'dijitCalendarDisabledDate');
  633. }));
  634. }
  635. var h = this.getHeader();
  636. if(h){
  637. this._setText(this.getHeader(), this.get("value").getFullYear());
  638. }
  639. },
  640. _populateYears: function(year){
  641. // summary:
  642. // Fills the list of years with a range of 12 numbers, with the current year
  643. // being the 6th number.
  644. var constraints = this.get('constraints');
  645. var dispYear = year || this.get("value").getFullYear();
  646. var firstYear = dispYear - Math.floor(this.displayedYears/2);
  647. var min = constraints && constraints.min ? constraints.min.getFullYear() : firstYear -10000;
  648. firstYear = Math.max(min, firstYear);
  649. // summary: Writes the years to display to the view
  650. this._displayedYear = dispYear;
  651. var yearLabels = dojo.query(".dojoxCalendarYearLabel", this.yearContainer);
  652. var max = constraints && constraints.max ? constraints.max.getFullYear() - firstYear : yearLabels.length;
  653. var disabledClass = 'dijitCalendarDisabledDate';
  654. yearLabels.forEach(dojo.hitch(this, function(node, cnt){
  655. if(cnt <= max){
  656. this._setText(node, firstYear + cnt);
  657. dojo.removeClass(node, disabledClass);
  658. }else{
  659. dojo.addClass(node, disabledClass);
  660. }
  661. }));
  662. if(this._incBtn){
  663. dojo[max < yearLabels.length ? "addClass" : "removeClass"](this._incBtn, disabledClass);
  664. }
  665. if(this._decBtn){
  666. dojo[min >= firstYear ? "addClass" : "removeClass"](this._decBtn, disabledClass);
  667. }
  668. var h = this.getHeader();
  669. if(h){
  670. this._setText(this.getHeader(), firstYear + " - " + (firstYear + 11));
  671. }
  672. },
  673. _updateSelectedYear: function(){
  674. this._year = String((this._cachedDate || this.get("value")).getFullYear());
  675. this._updateSelectedNode(".dojoxCalendarYearLabel", dojo.hitch(this, function(node, idx){
  676. return this._year !== null && node.innerHTML == this._year;
  677. }));
  678. },
  679. _updateSelectedMonth: function(){
  680. var month = (this._cachedDate || this.get("value")).getMonth();
  681. this._month = month;
  682. this._updateSelectedNode(".dojoxCalendarMonthLabel", function(node, idx){
  683. return idx == month;
  684. });
  685. },
  686. _updateSelectedNode: function(query, filter){
  687. var sel = "dijitCalendarSelectedDate";
  688. dojo.query(query, this.domNode)
  689. .forEach(function(node, idx, array){
  690. dojo[filter(node, idx, array) ? "addClass" : "removeClass"](node.parentNode, sel);
  691. });
  692. var selMonth = dojo.query('.dojoxCal-MY-M-Template div', this.myContainer)
  693. .filter(function(node){
  694. return dojo.hasClass(node.parentNode, sel);
  695. })[0];
  696. if(!selMonth){return;}
  697. var disabled = dojo.hasClass(selMonth, 'dijitCalendarDisabledDate');
  698. dojo[disabled ? 'addClass' : 'removeClass'](this.okBtn, "dijitDisabled");
  699. },
  700. onClick: function(evt){
  701. // summary:
  702. // Handles clicks on month names
  703. var clazz;
  704. var _this = this;
  705. var sel = "dijitCalendarSelectedDate";
  706. function hc(c){
  707. return dojo.hasClass(evt.target, c);
  708. }
  709. if(hc('dijitCalendarDisabledDate')){
  710. dojo.stopEvent(evt);
  711. return false;
  712. }
  713. if(hc("dojoxCalendarMonthLabel")){
  714. clazz = "dojoxCal-MY-M-Template";
  715. this._month = evt.target.parentNode.cellIndex + (evt.target.parentNode.parentNode.rowIndex * 2);
  716. this._cachedDate.setMonth(this._month);
  717. this._updateSelectedMonth();
  718. }else if(hc( "dojoxCalendarYearLabel")){
  719. clazz = "dojoxCal-MY-Y-Template";
  720. this._year = Number(evt.target.innerHTML);
  721. this._cachedDate.setYear(this._year);
  722. this._populateMonths();
  723. this._updateSelectedYear();
  724. }else if(hc("dojoxCalendarDecrease")){
  725. this._populateYears(this._displayedYear - 10);
  726. return true;
  727. }else if(hc("dojoxCalendarIncrease")){
  728. this._populateYears(this._displayedYear + 10);
  729. return true;
  730. }else{
  731. return true;
  732. }
  733. dojo.stopEvent(evt);
  734. return false;
  735. },
  736. onOk: function(evt){
  737. dojo.stopEvent(evt);
  738. if(dojo.hasClass(this.okBtn, "dijitDisabled")){
  739. return false;
  740. }
  741. this.onValueSelected(this._cachedDate);
  742. return false;
  743. },
  744. onCancel: function(evt){
  745. dojo.stopEvent(evt);
  746. this.onValueSelected(this.get("value"));
  747. return false;
  748. }
  749. });
  750. dojo.declare("dojox.widget.Calendar2Pane",
  751. [dojox.widget._CalendarBase,
  752. dojox.widget._CalendarDay,
  753. dojox.widget._CalendarMonthYear], {
  754. // summary: A Calendar withtwo panes, the second one
  755. // containing both month and year
  756. }
  757. );
  758. dojo.declare("dojox.widget.Calendar",
  759. [dojox.widget._CalendarBase,
  760. dojox.widget._CalendarDay,
  761. dojox.widget._CalendarMonthYear], {
  762. // summary: The standard Calendar. It includes day and month/year views.
  763. // No visual effects are included.
  764. }
  765. );
  766. dojo.declare("dojox.widget.DailyCalendar",
  767. [dojox.widget._CalendarBase,
  768. dojox.widget._CalendarDay], {
  769. // summary: A calendar withonly a daily view.
  770. _makeDate: function(value){
  771. var now = new Date();
  772. now.setDate(value);
  773. return now;
  774. }
  775. }
  776. );
  777. dojo.declare("dojox.widget.MonthAndYearlyCalendar",
  778. [dojox.widget._CalendarBase,
  779. dojox.widget._CalendarMonthYear], {
  780. // summary: A calendar withonly a daily view.
  781. }
  782. );
  783. }