Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 4.47 KB  |  hits: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Dojo's dijit.calendar and isDisabledDate
  2. dojo.provide("custom.Calendar");
  3. dojo.declare("custom.Calendar", dijit.Calendar, {
  4.     _xhr: null,
  5.     // Month/Year navigation buttons clicked
  6.     _adjustDisplay: function(){
  7.         // Ensure all dates are initially enabled (prevents seepage)
  8.         this.isDisabledDate = function(date) {
  9.             return false;
  10.         };
  11.         this.inherited(arguments);
  12.         this._disableAndPopulate();
  13.     },
  14.     constructor: function(){
  15.         this.inherited(arguments);
  16.         this._disableAndPopulate();
  17.     },
  18.     // Month drop down box
  19.     _onMonthSelect: function(){
  20.         // Ensure all dates are initially enabled (prevents seepage)
  21.         this.isDisabledDate = function(date) {
  22.             return false;
  23.         };
  24.         this.inherited(arguments);
  25.         this._disableAndPopulate();
  26.     },
  27.     // Set disabled dates and re-render calendar
  28.     _disableAndPopulate: function(){
  29.         var currDate = this.currentFocus;
  30.  
  31.         // Get Lower bound date
  32.         var startDate = new Date();
  33.         startDate.setFullYear(currDate.getFullYear(), currDate.getMonth(), -5);
  34.         // Create ymd dates manually (10x faster than dojo.date.locale.format)
  35.         var startMonth = (startDate.getMonth()<9 ? '0' + (startDate.getMonth()+1) : startDate.getMonth()+1);
  36.         var startDay = (startDate.getDate()<10 ? '0' + startDate.getDate() : startDate.getDate());
  37.         var ymdStartDate = startDate.getFullYear() + '-' + startMonth + '-' + startDay;
  38.  
  39.         // Get Upper bound date
  40.         var endDate = new Date();
  41.         endDate.setFullYear(currDate.getFullYear(), currDate.getMonth() + 1, 14);
  42.         // Create ymd dates manually (10x faster than dojo.date.locale.format)
  43.         var endMonth = (endDate.getMonth()<9 ? '0' + (endDate.getMonth()+1) : endDate.getMonth()+1);
  44.         var endDay = (endDate.getDate()<10 ? '0' + endDate.getDate() : endDate.getDate());
  45.         var ymdEndDate = endDate.getFullYear() + '-' + endMonth + '-' + endDay;
  46.  
  47.         var calendar = this;
  48.         // Get IssueDates
  49.         var issueDates;
  50.         // If an existing xhr request is still running, cancel it before starting a new one
  51.         if (this._xhr) {
  52.             this._xhr.cancel();
  53.         }
  54.         this._xhr = dojo.xhrGet({
  55.             url: "http://.....", // url of server-side script
  56.             content: {
  57.                 startDate: ymdStartDate, // Earliest possible date displayed on current month
  58.                 endDate: ymdEndDate, // Last possible date displayed on current month
  59.                 filters: {} // Any additional criteria which your server-side script uses to determine which dates to return
  60.             },
  61.             failOk: true, // Prevent error being logged to console when previous XHR calls are cancelled
  62.             load: function(data){
  63.                 issueDates = dojo.fromJson(data);
  64.                 if (issueDates === undefined) {
  65.                     // Error with xhr
  66.                 } else {
  67.                     calendar.isDisabledDate = function(date) {
  68.                         var disable = true;
  69.                         // Create ymdDate manually (10x faster than dojo.date.locale.format)
  70.                         var month = (date.getMonth()<9 ? '0' + (date.getMonth()+1) : date.getMonth()+1);
  71.                         var day = (date.getDate()<10 ? '0' + date.getDate() : date.getDate());
  72.                         var ymdDate = date.getFullYear() + '-' + month + '-' + day;
  73.                         // Loop through array returned from XHR request, if it contains current date then
  74.                         // current date should not be disabled
  75.                         for (key in issueDates) {
  76.                             if (issueDates[key] == ymdDate) {
  77.                                 disable = false;
  78.                                 break;
  79.                             }
  80.                         }
  81.                         return disable;
  82.                     };
  83.                     calendar._populateGrid(); // Refresh calendar display
  84.                 }
  85.             },
  86.             // Log any errors to console (except when XHR request is cancelled)
  87.             error: function(args) {
  88.                 if (args.dojoType == 'cancel') {
  89.                     // Request cancelled
  90.                 } else {
  91.                     console.error(args);
  92.                 }
  93.             }
  94.         });
  95.     },
  96.     onClose: function() {
  97.         // If an existing xhr request is still running, cancel it before starting a new one
  98.         if (this._xhr) {
  99.             this._xhr.cancel();
  100.         }
  101.     }
  102. });