GithubHelp home page GithubHelp logo

Comments (3)

 avatar commented on August 23, 2024

If found some workaround to achieve what i wanted.
The general idea is to highlight the allowed ranges with a 'dateClassMap'. And then to listen to the 'date-clicked' event to check if the date falls in an open period, if it doesn't false is returned so the date cannot be selected.

It would be nice though if there was a function that could be called without me having to code this ;-)
(my code could probably be cleaner, but I'm not that used to writing javascript :$)

I'm leaving this here, it might interest someone:

var open_periods = [];

open_periods[0] = { 
    start: '2011-01-01', 
    end: '2011-12-31',
    start_stamp: Date.parse('2011-01-01'), 
    end_stamp: Date.parse('2011-12-31'),
    is_current: false,
};
open_periods[1] = { 
    start: '2012-01-01', 
    end: '2012-12-31',
    start_stamp: Date.parse('2012-01-01'), 
    end_stamp: Date.parse('2012-12-31'),
    is_current: true,
};

// create open periods map
  var classMap = [];
  for(i = 0; i < open_periods.length; i++) {
      var current = Kalendae.moment(open_periods[i].start);
      var end = Kalendae.moment(open_periods[i].end);
      // walk all days in the range
      for(current; current.valueOf() <= end.valueOf(); current.add({d:1})) {
          classMap[ current.format('YYYY-MM-DD') ] = 'btn-warning';
      }
  }

  var kalFinancialDate = new Kalendae.Input('invoice_financial_date', {format: "YYYY-MM-DD", dateClassMap: classMap});
  kalFinancialDate.subscribe('date-clicked', function (moment_object) {
     var selectedDate = moment_object.format('YYYY-MM-DD');
     var selectedDateStamp = moment_object.valueOf();
     return inv.fallsInOpenPeriod(selectedDateStamp, selectedDate);
  });
/**
     * checks if the timestamp falls in an open periods
     * the variable "open_periods" must be set first.
     */
    this.fallsInOpenPeriod = function(selectedDateStamp, selectedDate) {
        var ok = false;
        for(i = 0; i < open_periods.length; i++) {
            // When the date falls inside an open period, select the same date as financial date 
            if(open_periods[i].start_stamp <= selectedDateStamp && open_periods[i].end_stamp >= selectedDateStamp) {
                $('#invoice_financial_date').val(selectedDate);
                ok = true;
                break;
            }
        }
        return ok;
    }

from kalendae.

Twipped avatar Twipped commented on August 23, 2024

You would still use blackout to acheive this (assuming I understand what you want), you just do the inverse and only allow dates that fall in the range.

var open_periods = [];
open_periods[0] = { start: '2010-01-01', end: '2010-12-31' };
open_periods[1] = { start: '2012-10-01', end: '2013-09-31' };

new Kalendae(document.body, {
    months:2,
    blackout:function (date) {
        var mo = Kalendae.moment,
            i=open_periods.length,
            yd = date.yearDay(),
            start,
            stop;

        while (i--) {
            start = mo(open_periods[i].start, 'YYYY-MM-DD');
            stop = mo(open_periods[i].end, 'YYYY-MM-DD');
            if (yd >= start.yearDay() && yd <= stop.yearDay()) {
                return false; //date is within the allowed period.
            }
        }
        return true;
    }
});

from kalendae.

 avatar commented on August 23, 2024

Indeed that does what I wanted to achieve :-)
Thanks!

from kalendae.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.