- Dojo's dijit.calendar and isDisabledDate
- dojo.provide("custom.Calendar");
- dojo.declare("custom.Calendar", dijit.Calendar, {
- _xhr: null,
- // Month/Year navigation buttons clicked
- _adjustDisplay: function(){
- // Ensure all dates are initially enabled (prevents seepage)
- this.isDisabledDate = function(date) {
- return false;
- };
- this.inherited(arguments);
- this._disableAndPopulate();
- },
- constructor: function(){
- this.inherited(arguments);
- this._disableAndPopulate();
- },
- // Month drop down box
- _onMonthSelect: function(){
- // Ensure all dates are initially enabled (prevents seepage)
- this.isDisabledDate = function(date) {
- return false;
- };
- this.inherited(arguments);
- this._disableAndPopulate();
- },
- // Set disabled dates and re-render calendar
- _disableAndPopulate: function(){
- var currDate = this.currentFocus;
- // Get Lower bound date
- var startDate = new Date();
- startDate.setFullYear(currDate.getFullYear(), currDate.getMonth(), -5);
- // Create ymd dates manually (10x faster than dojo.date.locale.format)
- var startMonth = (startDate.getMonth()<9 ? '0' + (startDate.getMonth()+1) : startDate.getMonth()+1);
- var startDay = (startDate.getDate()<10 ? '0' + startDate.getDate() : startDate.getDate());
- var ymdStartDate = startDate.getFullYear() + '-' + startMonth + '-' + startDay;
- // Get Upper bound date
- var endDate = new Date();
- endDate.setFullYear(currDate.getFullYear(), currDate.getMonth() + 1, 14);
- // Create ymd dates manually (10x faster than dojo.date.locale.format)
- var endMonth = (endDate.getMonth()<9 ? '0' + (endDate.getMonth()+1) : endDate.getMonth()+1);
- var endDay = (endDate.getDate()<10 ? '0' + endDate.getDate() : endDate.getDate());
- var ymdEndDate = endDate.getFullYear() + '-' + endMonth + '-' + endDay;
- var calendar = this;
- // Get IssueDates
- var issueDates;
- // If an existing xhr request is still running, cancel it before starting a new one
- if (this._xhr) {
- this._xhr.cancel();
- }
- this._xhr = dojo.xhrGet({
- url: "http://.....", // url of server-side script
- content: {
- startDate: ymdStartDate, // Earliest possible date displayed on current month
- endDate: ymdEndDate, // Last possible date displayed on current month
- filters: {} // Any additional criteria which your server-side script uses to determine which dates to return
- },
- failOk: true, // Prevent error being logged to console when previous XHR calls are cancelled
- load: function(data){
- issueDates = dojo.fromJson(data);
- if (issueDates === undefined) {
- // Error with xhr
- } else {
- calendar.isDisabledDate = function(date) {
- var disable = true;
- // Create ymdDate manually (10x faster than dojo.date.locale.format)
- var month = (date.getMonth()<9 ? '0' + (date.getMonth()+1) : date.getMonth()+1);
- var day = (date.getDate()<10 ? '0' + date.getDate() : date.getDate());
- var ymdDate = date.getFullYear() + '-' + month + '-' + day;
- // Loop through array returned from XHR request, if it contains current date then
- // current date should not be disabled
- for (key in issueDates) {
- if (issueDates[key] == ymdDate) {
- disable = false;
- break;
- }
- }
- return disable;
- };
- calendar._populateGrid(); // Refresh calendar display
- }
- },
- // Log any errors to console (except when XHR request is cancelled)
- error: function(args) {
- if (args.dojoType == 'cancel') {
- // Request cancelled
- } else {
- console.error(args);
- }
- }
- });
- },
- onClose: function() {
- // If an existing xhr request is still running, cancel it before starting a new one
- if (this._xhr) {
- this._xhr.cancel();
- }
- }
- });