calendar

class webix.ui.calendar(data)
Arguments:
  • data (object) – A configuration object

Calendar view.

External references

Official documentation page.

Code

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
webix.protoUI({
    name:"calendar",

    defaults:{
        date: new Date(), //selected date, not selected by default
        select: false,
        navigation: true,
        monthSelect: true,
        weekHeader: true,
        weekNumber: false,
        skipEmptyWeeks: false,

        calendarHeader: "%F %Y",
        calendarWeekHeader: "W#",
        //calendarTime: "%H:%i",
        events:webix.Date.isHoliday,
        minuteStep: 5,
        icons: false,
        timepickerHeight: 30,
        headerHeight: 70,
        dayTemplate: function(d){
            return d.getDate();
        },
        width: 260,
        height: 250
    },

    dayTemplate_setter: webix.template,
    calendarHeader_setter:webix.Date.dateToStr,
    calendarWeekHeader_setter:webix.Date.dateToStr,
    calendarTime_setter:function(format){
        this._calendarTime = format;
        return webix.Date.dateToStr(format);
    },
    date_setter:function(date){
        return this._string_to_date(date);
    },
    maxDate_setter:function(date){
        return this._string_to_date(date);
    },
    minDate_setter:function(date){
        return this._string_to_date(date);
    },
    minTime_setter:function(time){
        if(typeof(time) == "string"){
            time = webix.i18n.parseTimeFormatDate(time);
            time = [time.getHours(),time.getMinutes()];

        }

        return time;
    },
    maxTime_setter:function(time){
        if(typeof(time) == "string"){
            time = webix.i18n.parseTimeFormatDate(time);
            time = [time.getHours(),time.getMinutes()];
        }
        return time;
    },
    _ariaFocus:function(){
        var ev = "focus"+(webix.env.isIE?"in":"");

        webix._event(this.$view, ev, webix.bind(function(e){
            var t = e.target.className;
            var css = t.indexOf("webix_cal_day")!==-1 ? "webix_cal_day" : (t.indexOf("webix_cal_block")!==-1?"webix_cal_block":"");

            if(new Date() - webix.UIManager._tab_time > 300 && css){
                var prev = e.relatedTarget;
                if(prev && !webix.isUndefined(prev.className)){
                    var date = (css=="webix_cal_day")?
                        this._locate_day(e.target):
                        this._locate_date(e.target);
                    this._moveSelection(date, false);
                }
            }
        }, this), {capture:!webix.env.isIE});
    },
    $init: function() {
        this._viewobj.className += " webix_calendar";
        this._viewobj.setAttribute("role", "region");
        this._viewobj.setAttribute("aria-label", webix.i18n.aria.calendar);

        //special dates
        this._special_dates = {};
        this._selected_date_part = this._selected_date = null;
        this._zoom_level = 0;

        //navigation and aria
        this._ariaFocus();
        this.attachEvent("onKeyPress", this._onKeyPress);
        this.attachEvent("onAfterZoom", function(zoom){
            if(zoom >= 0) this.$view.querySelector(".webix_cal_month_name").blur();
        });
    },
    type_setter: function(value){
        if(value == "time"){
            this._zoom_in = true;
            this._zoom_level = -1;
        }
        else if(value == "year"){
            this._fixed = true;
        }
        return value;
    },
    $setSize:function(x,y){

        if(webix.ui.view.prototype.$setSize.call(this,x,y)){
            //repaint calendar when size changed
            this.render();
        }
    },
    $getSize:function(dx, dy){
        if (this._settings.cellHeight){
            var state = this._getDateBoundaries(this._settings.date);
            this._settings.height = this._settings.cellHeight * state._rows + (webix.skin.$active.calendarHeight||70);
        }
        return webix.ui.view.prototype.$getSize.call(this, dx,dy);
    },
    moveSelection:function(mode){
        if(this.config.master) return; //in daterange

        var date = webix.Date.copy(this.getSelectedDate() || this.getVisibleDate());
        this._moveSelection(date, mode);

    },
    _moveSelection:function(date, mode){
        var css = this._zoom_logic[this._zoom_level]._keyshift(date, mode, this);

        var sel = this._viewobj.querySelector("."+css+"[tabindex='0']");
        if(sel) sel.focus();
    },
    _getDateBoundaries: function(date, reset) {
        // addition information about rendering event:
        // how many days from the previous month,
        // next,
        // number of weeks to display and so on

        if (!this._set_date_bounds || reset){
            var month = date.getMonth();
            var year = date.getFullYear();

            var next = new Date(year, month+1, 1);
            var start = webix.Date.weekStart(new Date(year, month, 1));

            var days = Math.round((next.valueOf() - start.valueOf())/(60*1000*60*24));
            var rows = this._settings.skipEmptyWeeks?Math.ceil(days/7):6;

            this._set_date_bounds = { _month: month, _start:start, _next:next, _rows: rows};
        }

        return this._set_date_bounds;
    },
    $skin:function(){
        if(webix.skin.$active.calendar){
            if( webix.skin.$active.calendar.width)
                this.defaults.width = webix.skin.$active.calendar.width;
            if( webix.skin.$active.calendar.height)
                this.defaults.height = webix.skin.$active.calendar.height;
            if( webix.skin.$active.calendar.headerHeight)
                this.defaults.headerHeight = webix.skin.$active.calendar.headerHeight;
            if( webix.skin.$active.calendar.timepickerHeight)
                this.defaults.timepickerHeight = webix.skin.$active.calendar.timepickerHeight;
        }

    },
    _getColumnConfigSizes: function(date){
        var bounds = this._getDateBoundaries(date);

        var s = this._settings;
        var _columnsHeight = [];
        var _columnsWidth = [];

        var containerWidth = this._content_width - 36;

        var containerHeight = this._content_height - this._settings.headerHeight - 10 - (this._settings.timepicker||this._icons?this._settings.timepickerHeight:0);

        var columnsNumber = (s.weekNumber)?8:7;
        for(var i=0; i<columnsNumber; i++) {
            _columnsWidth[i] = Math.ceil(containerWidth/(columnsNumber-i));
            containerWidth -= _columnsWidth[i];
        }

        var rowsNumber = bounds._rows;
        for (var k = 0; k < rowsNumber; k++) {
            _columnsHeight[k] = Math.ceil(containerHeight/(rowsNumber-k) );
            containerHeight -= _columnsHeight[k];
        }
        return [_columnsWidth, _columnsHeight];
    },
    icons_setter: function(value){
        if(!value)
            this._icons = null;
        else if(typeof value == "object")
            this._icons = value;
        else
            this._icons = this._icons2;
    },
    _icons: [],
    _icons2: [

        {
            template: function(){
                return "<span role='button' tabindex='0' class='webix_cal_icon_today webix_cal_icon'>"+webix.i18n.calendar.today+"</span>";
            },
            on_click:{
                "webix_cal_icon_today": function(){
                    this.setValue(new Date());
                    this.callEvent("onTodaySet",[this.getSelectedDate()]);
                }
            }
        },
        {
            template: function(){
                return "<span role='button' tabindex='0' class='webix_cal_icon_clear webix_cal_icon'>"+webix.i18n.calendar.clear+"</span>";
            },
            on_click:{
                "webix_cal_icon_clear": function(){
                    this.setValue("");
                    this.callEvent("onDateClear",[this.getSelectedDate()]);
                }
            }
        }
    ],
    refresh:function(){ this.render(); },
    render: function() {
        //reset zoom level
        this._zoom_level = 0;
        this._zoom_size = false;

        var s = this._settings;

        if (!this.isVisible(s.id)) return;
        this._current_time = webix.Date.datePart(new Date());

        if (webix.debug_render)
            webix.log("Render: "+this.name+"@"+s.id);
        this.callEvent("onBeforeRender",[]);

        var date = this._settings.date;

        var bounds = this._getDateBoundaries(date, true);
        var sizes = this._getColumnConfigSizes(date);
        var width = sizes[0];
        var height = sizes[1];

        var html = "<div class='webix_cal_month'><span role='button' tabindex='0' aria-live='assertive' aria-atomic='true' class='webix_cal_month_name"+(!this._settings.monthSelect?" webix_readonly":"")+"'>"+s.calendarHeader(date)+'</span>';
        if (s.navigation)
            html += "<div role='button' tabindex='0' aria-label='"+webix.i18n.aria.navMonth[0]+"' class='webix_cal_prev_button'></div><div role='button' tabindex='0' aria-label='"+webix.i18n.aria.navMonth[1]+"' class='webix_cal_next_button'></div>";
        html += "</div>";

        if(s.weekHeader)
            html += "<div class='webix_cal_header' aria-hidden='true'>"+this._week_template(width)+"</div>";
        html += "<div class='webix_cal_body'>"+this._body_template(width, height, bounds)+"</div>";

        if (this._settings.timepicker || this._icons){
            html += "<div class='webix_cal_footer'>";
            if(this._settings.timepicker)
                html += this._timepicker_template(date);

            if(this._icons)
                html += this._icons_template();
            html += "</div>";
        }

        this._contentobj.innerHTML = html;

        if(this._settings.type == "time"){
            var time = this._settings.date;
            if(time){
                if(typeof(time) == "string"){
                    date = webix.i18n.parseTimeFormatDate(time);
                }
                else if(webix.isArray(time)){
                    date.setHours(time[0]);
                    date.setMinutes(time[1]);
                }
            }
            this._changeZoomLevel(-1,date);
        }
        else if(this._settings.type == "month"){
            this._changeZoomLevel(1,date);
        }
        else if(this._settings.type == "year"){
            this._changeZoomLevel(2,date);
        }

        this.callEvent("onAfterRender",[]);
    },
    _icons_template: function(date){
        var html =    "<div class='webix_cal_icons'>";
        var icons = this._icons;

        for(var i=0; i < icons.length; i++){
            if(icons[i].template){
                var template = (typeof(icons[i].template) == "function"?icons[i].template: webix.template(icons[i].template));
                html += template.call(this,date);
            }
            if(icons[i].on_click){
                webix.extend(this.on_click,icons[i].on_click);
            }
        }
        html += "</div>";
        return html;
    },
    _timepicker_template:function(date){
        var timeFormat = this._settings.calendarTime||webix.i18n.timeFormatStr;
        var tpl = "";

        if(!this._settings.master)
            tpl = "<div role='button' tabindex='0' class='webix_cal_time"+(this._icons?" webix_cal_time_icons":"")+"'><span class='webix_icon fa-clock-o'></span> "+timeFormat(date)+"</div>";
        else{
            //daterange needs two clocks
            var range_date = webix.copy(webix.$$(this._settings.master)._settings.value);
            if(webix.Date.equal(range_date.end, date))
                range_date.start = range_date.end;

            for(var i in range_date){
                tpl += "<div role='button' tabindex='0' class='webix_range_time_"+i+" webix_cal_time'><span class='webix_icon fa-clock-o'></span> "+timeFormat(range_date[i])+"</div>";
            }
        }
        return tpl;
    },
    _week_template: function(widths){
        var s = this._settings;
        var week_template = '';
        var correction = 0;

        if(s.weekNumber) {
            correction = 1;
            week_template += "<div class='webix_cal_week_header' style='width: "+widths[0]+"px;' >"+s.calendarWeekHeader()+"</div>";
        }

        var k = (webix.Date.startOnMonday)?1:0;
        for (var i=0; i<7; i++){ // 7 days total
            var day_index = (k + i) % 7; // 0 - Sun, 6 - Sat as in Locale.date.day_short
            var day = webix.i18n.calendar.dayShort[day_index]; // 01, 02 .. 31
            week_template += "<div day='"+day_index+"' style='width: "+widths[i+correction]+"px;' >"+day+"</div>";
        }

        return week_template;
    },
    blockDates_setter:function(value){
        return webix.toFunctor(value, this.$scope);
    },
    _day_css:function(day, bounds){
        var css = "";
        if (webix.Date.equal(day, this._current_time))
            css += " webix_cal_today";
        if (!this._checkDate(day))
            css+= " webix_cal_day_disabled";
        if (webix.Date.equal(day, this._selected_date_part))
            css += " webix_cal_select";
        if (day.getMonth() != bounds._month)
            css += " webix_cal_outside";
        if (this._settings.events)
            css+=" "+(this._settings.events(day) || "");
        css += " webix_cal_day";
        return css;
    },
    _body_template: function(widths, heights, bounds){
        var s = this._settings;
        var html = "";
        var day = webix.Date.datePart(webix.Date.copy(bounds._start));
        var start = s.weekNumber?1:0;
        var weekNumber = webix.Date.getISOWeek(webix.Date.add(day,2,"day", true));
        var min = this._settings.minDate || new Date(1,1,1);
        var max = this._settings.maxDate || new Date(9999,1,1);

        for (var y=0; y<heights.length; y++){
            html += "<div class='webix_cal_row' style='height:"+heights[y]+"px;line-height:"+heights[y]+"px'>";

            if (start){
                // recalculate week number for the first week of a year
                if(!day.getMonth() && day.getDate()<7)
                    weekNumber =  webix.Date.getISOWeek(webix.Date.add(day,2,"day", true));
                html += "<div class='webix_cal_week_num' aria-hidden='true' style='width:"+widths[0]+"px'>"+weekNumber+"</div>";
            }

            for (var x=start; x<widths.length; x++){
                var css = this._day_css(day, bounds);
                var d = this._settings.dayTemplate.call(this,day);
                var sel = webix.Date.equal(day, this._selected_date_part);
                var alabel = "";

                if(typeof d == "object"){
                    alabel = d.aria || alabel;
                    d = d.text;
                }
                else
                    alabel = webix.Date.dateToStr(webix.i18n.aria.dateFormat)(day);

                html += "<div day='"+x+"' role='gridcell' "+(day.getMonth() != bounds._month?"aria-hidden='true'":"")+" aria-label='"+alabel+
                    "' tabindex='"+(sel?"0":"-1")+"' aria-selected='"+(sel?"true":"false")+
                    "' class='"+css+"' style='width:"+widths[x]+"px'><span aria-hidden='true' class='webix_cal_day_inner'>"+d+"</span></div>";
                day = webix.Date.add(day, 1, "day");
                if(day.getHours()){
                    day = webix.Date.datePart(day);
                }
            }

            html += "</div>";
            weekNumber++;
        }
        return html;
    },
    _changeDate:function(dir, step, notset){
        var now = this._settings.date;
        if(!step) { step = this._zoom_logic[this._zoom_level]._changeStep; }
        if(!this._zoom_level){
            now = webix.Date.copy(now);
            now.setDate(1);
        }
        var next = webix.Date.add(now, dir*step, "month", true);
        this._changeDateInternal(now, next);
    },
    _changeDateInternal:function(now, next){
        if(this.callEvent("onBeforeMonthChange", [now, next])){
            if (this._zoom_level){
                this._update_zoom_level(next);
            }
            else{
                this.showCalendar(next);
            }
            this.callEvent("onAfterMonthChange", [next, now]);
        }
    },
    _zoom_logic:{
        "-2":{
            _isBlocked: function(i){
                var config = this._settings,
                    date = config.date,
                    isBlocked = false;

                var minHour = (config.minTime ? config.minTime[0] : 0);
                var maxHour = (config.maxTime ? (config.maxTime[0] + ( config.maxTime[1] ? 1 : 0 )) : 24);

                var minMinute = (config.minTime && (date.getHours()==minHour) ? config.minTime[1] : 0);
                var maxMinute = (config.maxTime && config.maxTime[1] && (date.getHours()==(maxHour-1)) ? config.maxTime[1] : 60);

                if(this._settings.blockTime){
                    var d = webix.Date.copy(date);
                    d.setMinutes(i);
                    isBlocked = this._settings.blockTime(d);
                }
                return (i < minMinute || i >= maxMinute || isBlocked);

            },
            _setContent:function(next, i){ next.setMinutes(i); },
            _findActive:function(date, mode, calendar){
                if(!this._isBlocked.call(calendar, date.getMinutes()))
                    return date;
                else{
                    var step = calendar._settings.minuteStep;
                    var newdate = webix.Date.add(date, mode =="right"?step:-step, "minute", true);
                    if(date.getHours() === newdate.getHours())
                        return  this._findActive(newdate, mode, calendar);
                }
            }
        },
        "-1":{
            _isBlocked: function(i){
                var config = this._settings,
                    date = config.date;

                var minHour = (config.minTime? config.minTime[0]:0);
                var maxHour = (config.maxTime? config.maxTime[0]+(config.maxTime[1]?1:0):24);

                if (i < minHour || i >= maxHour) return true;

                if(config.blockTime){
                    var d = webix.Date.copy(date);
                    d.setHours(i);

                    var minMinute = (config.minTime && (i==minHour) ? config.minTime[1] : 0);
                    var maxMinute = (config.maxTime && config.maxTime[1] && (i==(maxHour-1)) ? config.maxTime[1] : 60);

                    for (var j=minMinute; j<maxMinute; j+= config.minuteStep){
                        d.setMinutes(j);
                        if (!config.blockTime(d))
                            return false;
                    }
                    return true;
                }
            },
            _setContent:function(next, i){ next.setHours(i); },
            _keyshift:function(date, mode, calendar){
                var newdate, inc, step = calendar._settings.minuteStep;

                if(mode === "bottom" || mode === "top"){
                    date.setHours(mode==="bottom"?23:0);
                    date.setMinutes(mode==="bottom"?55:0);
                    date.setSeconds(0);
                    date.setMilliseconds(0);
                    newdate = date;
                }
                else if(mode === "left" || mode === "right"){//minutes

                    inc = (mode==="right"?step:-step);
                    if(mode === "left" && date.getMinutes() < step ) inc = 60-step;
                    if(mode === "right" && date.getMinutes() >= (60-step)) inc = step-60;
                    inc -= date.getMinutes()%step;
                    newdate = calendar._zoom_logic["-2"]._findActive(webix.Date.add(date, inc, "minute"), mode, calendar);
                }
                else if(mode === "up" || mode === "down"){ //hours
                    inc = mode==="down"?1:-1;
                    if(mode === "down" && date.getHours() === 23) inc = -23;
                    if(mode === "up" && date.getHours() === 0) inc = 23;
                    newdate = this._findActive(webix.Date.add(date, inc, "hour"), mode, calendar);
                }
                else if(mode === false)
                    newdate = this._findActive(date, mode, calendar);

                calendar.selectDate(newdate, false);

                if(newdate){
                    calendar._update_zoom_level(newdate);
                    calendar.selectDate(newdate, false);
                }

                return "webix_cal_block"+(mode === "left" || mode === "right"?"_min":"");
            },
            _findActive:function(date, mode, calendar){
                if(!this._isBlocked.call(calendar, date.getHours()))
                    return date;
                else{
                    var newdate = webix.Date.add(date, mode =="down"?1:-1, "hour", true);
                    if(date.getDate() === newdate.getDate())
                        return  this._findActive(newdate, mode, calendar);
                }
            }
        },
        "0":{//days
            _changeStep:1,
            _keyshift:function(date, mode, calendar){
                var newdate = date;
                if(mode === "pgup" || mode === "pgdown")
                    newdate = webix.Date.add(date, (mode==="pgdown"?1:-1), "month");
                else if(mode === "bottom")
                    newdate = new Date(date.getFullYear(), date.getMonth()+1, 0);
                else if(mode === "top")
                    newdate = new Date(date.setDate(1));
                else if(mode === "left" || mode === "right")
                    newdate = webix.Date.add(date, (mode==="right"?1:-1), "day");
                else if(mode === "up" || mode === "down")
                    newdate = webix.Date.add(date, (mode==="down"?1:-1), "week");

                if(!calendar._checkDate(newdate))
                    newdate = calendar._findActive(date, mode);

                if(newdate)
                    calendar.selectDate(newdate, true);
                return "webix_cal_day";
            },

        },
        "1":{    //months
            _isBlocked: function(i,calendar){
                var blocked = false, minYear, maxYear,
                min = calendar._settings.minDate||null,
                max = calendar._settings.maxDate||null,
                year = calendar._settings.date.getFullYear();

                if(min && max){
                    minYear = min.getFullYear();
                    maxYear = max.getFullYear();
                    if(year<minYear||year==minYear&&min.getMonth()>i || year>maxYear||year==maxYear&&max.getMonth()<i)
                        blocked = true;
                }
                return blocked;
            },
            _correctDate: function(date,calendar){
                if(date < calendar._settings.minDate){
                    date = webix.Date.copy(calendar._settings.minDate);
                }
                else if(date > calendar._settings.maxDate){
                    date = webix.Date.copy(calendar._settings.maxDate);
                }
                return date;
            },
            _getTitle:function(date){ return date.getFullYear(); },
            _getContent:function(i){ return webix.i18n.calendar.monthShort[i]; },
            _setContent:function(next, i){ if(i!=next.getMonth()) next.setDate(1);next.setMonth(i); },
            _changeStep:12,
            _keyshift:function(date, mode, calendar){
                var newdate = date;
                if(mode === "pgup" || mode === "pgdown")
                    newdate = webix.Date.add(date, (mode==="pgdown"?1:-1), "year");
                else if(mode === "bottom")
                    newdate = new Date(date.setMonth(11));
                else if(mode === "top")
                    newdate = new Date(date.setMonth(0));
                else if(mode === "left" || mode === "right")
                    newdate = webix.Date.add(date, (mode==="right"?1:-1), "month");
                else if(mode === "up" || mode === "down")
                    newdate = webix.Date.add(date, (mode==="down"?4:-4), "month");

                if(!calendar._checkDate(newdate))
                    newdate = calendar._findActive(date, mode);

                if(newdate){
                    calendar._update_zoom_level(newdate);
                    calendar.selectDate(newdate, false);
                }

                return "webix_cal_block";
            }
        },
        "2":{    //years
            _isBlocked: function(i,calendar){
                i += calendar._zoom_start_date;
                var blocked = false;
                var min = calendar._settings.minDate;
                var max = calendar._settings.maxDate;

                if( min && max && (min.getFullYear()>i || max.getFullYear()<i)){
                    blocked = true;
                }
                return blocked;
            },
            _correctDate: function(date,calendar){
                if(date < calendar._settings.minDate){
                    date = webix.Date.copy(calendar._settings.minDate);
                }
                else if(date > calendar._settings.maxDate){
                    date = webix.Date.copy(calendar._settings.maxDate);
                }
                return date;
            },
            _getTitle:function(date, calendar){
                var start = date.getFullYear();
                calendar._zoom_start_date = start = start - start%10 - 1;
                return start+" - "+(start+10);
            },
            _getContent:function(i, calendar){ return calendar._zoom_start_date+i; },
            _setContent:function(next, i, calendar){ next.setFullYear(calendar._zoom_start_date+i); },
            _changeStep:12*10,
            _keyshift:function(date, mode, calendar){
                var newdate = date;
                if(mode === "pgup" || mode === "pgdown")
                    newdate = webix.Date.add(date, (mode==="pgdown"?10:-10), "year");
                else if(mode === "bottom")
                    newdate = new Date(date.setYear(calendar._zoom_start_date+10));
                else if(mode === "top")
                    newdate = new Date(date.setYear(calendar._zoom_start_date));
                else if(mode === "left" || mode === "right")
                    newdate = webix.Date.add(date, (mode==="right"?1:-1), "year");
                else if(mode === "up" || mode === "down")
                    newdate = webix.Date.add(date, (mode==="down"?4:-4), "year");

                if(!calendar._checkDate(newdate))
                    newdate = calendar._findActive(date, mode);

                if(newdate){
                    calendar._update_zoom_level(newdate);
                    calendar.selectDate(newdate, false);
                }

                return "webix_cal_block";
            }
        }
    },
    _correctBlockedTime: function(){
        var i, isDisabledHour, isDisabledMinutes;
        isDisabledHour = this._zoom_logic[-1]._isBlocked.call(this,this._settings.date.getHours());
        if(isDisabledHour){
            for (i= 0; i< 24; i++){
                if(!this._zoom_logic[-1]._isBlocked.call(this,i)){
                    this._settings.date.setHours(i);
                    break;
                }
            }
        }
        isDisabledMinutes = this._zoom_logic[-2]._isBlocked.call(this,this._settings.date.getMinutes());
        if(isDisabledMinutes){
            for (i=0; i<60; i+=this._settings.minuteStep){
                if(!this._zoom_logic[-2]._isBlocked.call(this,i)){
                    this._settings.date.setMinutes(i);
                    break;
                }
            }
        }
    },
    _update_zoom_level:function(date){
        var config, css, height, i, index,  sections, selected, type, width, zlogic, value, temp;
        var html = "";

        config = this._settings;
        index = config.weekHeader?2: 1;
        zlogic = this._zoom_logic[this._zoom_level];
        sections  = this._contentobj.childNodes;

        if (date){
            config.date = date;
        }

        type = config.type;



        //store width and height of draw area
        if (!this._zoom_size){
            /*this._reserve_box_height = sections[index].offsetHeight +(index==2?sections[1].offsetHeight:0);*/

            this._reserve_box_height = this._contentobj.offsetHeight - config.headerHeight ;
            if(type != "year" && type != "month")
                this._reserve_box_height -= config.timepickerHeight;
            else if(this._icons){
                this._reserve_box_height -= 10;
            }
            this._reserve_box_width = sections[index].offsetWidth;
            this._zoom_size = 1;
        }

        //main section
        if (this._zoom_in){
            //hours and minutes
            height = this._reserve_box_height/6;
            var timeColNum = 6;
            var timeFormat = this._calendarTime||webix.i18n.timeFormat;
            var enLocale = timeFormat.match(/%([a,A])/);
            if(enLocale)
                timeColNum++;
            width = parseInt((this._reserve_box_width-3)/timeColNum,10);

            html += "<div class='webix_time_header'>"+this._timeHeaderTemplate(width,enLocale)+"</div>";
            html += "<div  class='webix_cal_body' style='height:"+this._reserve_box_height+"px'>";

            // check and change blocked selected time
            this._correctBlockedTime();

            html += "<div class='webix_hours'>";
            selected = config.date.getHours();
            temp = webix.Date.copy(config.date);

            for (i= 0; i< 24; i++){
                css="";
                if(enLocale){
                    if(i%4===0){
                        var label = (!i ? webix.i18n.am[0] : (i==12?webix.i18n.pm[0]:""));
                        html += "<div class='webix_cal_block_empty"+css+"' style='"+this._getCalSizesString(width,height)+"clear:both;"+"'>"+label+"</div>";
                    }
                }
                if(this._zoom_logic[-1]._isBlocked.call(this,i)){
                    css += " webix_cal_day_disabled";
                }
                else if(selected ==  i)
                    css += " webix_selected";


                temp.setHours(i);

                html += "<div aria-label='"+webix.Date.dateToStr(webix.i18n.aria.hourFormat)(temp)+"' role='gridcell'"+
                    " tabindex='"+(selected==i?"0":"-1")+"' aria-selected='"+(selected==i?"true":"false")+
                    "' class='webix_cal_block"+css+"' data-value='"+i+"' style='"+
                    this._getCalSizesString(width,height)+(i%4===0&&!enLocale?"clear:both;":"")+"'>"+webix.Date.toFixed(enLocale?(!i||i==12?12:i%12):i)+"</div>";
            }
            html += "</div>";

            html += "<div class='webix_minutes'>";
            selected = config.date.getMinutes();
            temp = webix.Date.copy(config.date);


            for (i=0; i<60; i+=config.minuteStep){
                css = "";
                if(this._zoom_logic[-2]._isBlocked.call(this,i)){
                    css = " webix_cal_day_disabled";
                }
                else if(selected ==  i)
                    css = " webix_selected";

                temp.setMinutes(i);

                html += "<div aria-label='"+webix.Date.dateToStr(webix.i18n.aria.minuteFormat)(temp)+"' role='gridcell' tabindex='"+(selected==i?"0":"-1")+
                    "' aria-selected='"+(selected==i?"true":"false")+"' class='webix_cal_block webix_cal_block_min"+css+"' data-value='"+i+"' style='"+
                    this._getCalSizesString(width,height)+(i%2===0?"clear:both;":"")+"'>"+webix.Date.toFixed(i)+"</div>";
            }
            html += "</div>";

            html += "</div>";
            html += "<div  class='webix_time_footer'>"+this._timeButtonsTemplate()+"</div>";
            this._contentobj.innerHTML = html;
        } else {
            //years and months

            //reset header
            var header = sections[0].childNodes;
            var labels = webix.i18n.aria["nav"+(this._zoom_level==1?"Year":"Decade")];
            header[0].innerHTML = zlogic._getTitle(config.date, this);
            header[1].setAttribute("aria-label", labels[0]);
            header[2].setAttribute("aria-label", labels[1]);

            height = this._reserve_box_height/3;
            width = this._reserve_box_width/4;
            if(this._checkDate(config.date))
                selected = (this._zoom_level==1?config.date.getMonth():config.date.getFullYear());
            for (i=0; i<12; i++){
                css = (selected == (this._zoom_level==1?i:zlogic._getContent(i, this)) ? " webix_selected" : "");
                if(zlogic._isBlocked(i,this)){
                    css += " webix_cal_day_disabled";
                }

                var format = webix.i18n.aria[(this._zoom_level==1?"month":"year")+"Format"];
                html+="<div role='gridcell' aria-label='"+webix.Date.dateToStr(format)(config.date)+
                    "' tabindex='"+(css.indexOf("selected")!==-1?"0":"-1")+
                    "' aria-selected='"+(css.indexOf("selected")!==-1?"true":"false")+
                    "' class='webix_cal_block"+css+"' data-value='"+i+"' style='"+this._getCalSizesString(width,height)+"'>"+
                    zlogic._getContent(i, this)+"</div>";
            }
            if(index-1){
                sections[index-1].style.display = "none";
            }
            sections[index].innerHTML = html;
            if(type != "year" && type != "month"){
                if(!sections[index+1])
                    this._contentobj.innerHTML += "<div  class='webix_time_footer'>"+this._timeButtonsTemplate()+"</div>";
                else
                    sections[index+1].innerHTML=this._timeButtonsTemplate();
            }
            sections[index].style.height = this._reserve_box_height+"px";
        }
    },
    _getCalSizesString: function(width,height){
        return "width:"+width+"px; height:"+height+"px; line-height:"+height+"px;";
    },
    _timeButtonsTemplate: function(){
        return "<input type='button' style='width:100%' class='webix_cal_done' value='"+webix.i18n.calendar.done+"'>";
    },
    _timeHeaderTemplate: function(width,enLocale){
        var w1 = width*(enLocale?5:4);
        var w2 = width*2;
        return "<div class='webix_cal_hours' style='width:"+w1+"px'>"+webix.i18n.calendar.hours+"</div><div class='webix_cal_minutes' style='width:"+w2+"px'>"+webix.i18n.calendar.minutes+"</div>";
    },
    _changeZoomLevel: function(zoom,date){
        var oldzoom = this._zoom_level;
        if(this.callEvent("onBeforeZoom",[zoom, oldzoom])){
            this._zoom_level = zoom;

            if(zoom)
                this._update_zoom_level(date);
            else
                this.showCalendar(date);
            this.callEvent("onAfterZoom",[zoom, oldzoom]);
        }
    },
    _correctDate:function(date){
        if(!this._checkDate(date) && this._zoom_logic[this._zoom_level]._correctDate)
            date = this._zoom_logic[this._zoom_level]._correctDate(date,this);
        return date;
    },
    _mode_selected:function(target){

        var next = this._locate_date(target);
        var zoom = this._zoom_level-(this._fixed?0:1);

        next = this._correctDate(next);
        if(this._checkDate(next)){
            this._changeZoomLevel(zoom, next);
            var type = this._settings.type;
            if(type == "month" || type == "year")
                this._selectDate(next);
        }
    },
    // selects date and redraw calendar
    _selectDate: function(date){
        if(this.callEvent("onBeforeDateSelect", [date])){
            this.selectDate(date, true);
            this.callEvent("onDateSelect", [date]);       // should be deleted in a future version
            this.callEvent("onAfterDateSelect", [date]);
        }
    },
    _locate_day:function(target){
        var cind = webix.html.index(target) - (this._settings.weekNumber?1:0);
        var rind = webix.html.index(target.parentNode);
        var date = webix.Date.add(this._getDateBoundaries()._start, cind + rind*7, "day", true);
        if (this._settings.timepicker){
            date.setHours(this._settings.date.getHours());
            date.setMinutes(this._settings.date.getMinutes());
        }
        return date;
    },
    _locate_date:function(target){
        var value = target.getAttribute("data-value")*1;
        var level = (target.className.indexOf("webix_cal_block_min")!=-1?this._zoom_level-1:this._zoom_level);
        var now = this._settings.date;
        var next = webix.Date.copy(now);

        this._zoom_logic[level]._setContent(next, value, this);

        return next;
    },
    on_click:{
        webix_cal_prev_button: function(e, id, target){
            this._changeDate(-1);
        },
        webix_cal_next_button: function(e, id, target){
            this._changeDate(1);
        },
        webix_cal_day_disabled: function(){
            return false;
        },
        webix_cal_outside: function(){
            if(!this._settings.navigation)
                return false;
        },
        webix_cal_day: function(e, id, target){
            var date = this._locate_day(target);
            this._selectDate(date);
        },
        webix_cal_time:function(e){
            if(this._zoom_logic[this._zoom_level-1]){
                this._zoom_in = true;
                var zoom = this._zoom_level - 1;
                this._changeZoomLevel(zoom);
            }
        },
        webix_range_time_start:function(){
            webix.$$(this._settings.master)._time_mode = "start";
        },
        webix_range_time_end:function(){
            webix.$$(this._settings.master)._time_mode = "end";
        },
        webix_cal_done:function(e){
            var date = webix.Date.copy(this._settings.date);
            date = this._correctDate(date);
            this._selectDate(date);
        },
        webix_cal_month_name:function(e){
            this._zoom_in = false;
            //maximum zoom reached
            if (this._zoom_level == 2 || !this._settings.monthSelect) return;

            var zoom = Math.max(this._zoom_level, 0) + 1;
            this._changeZoomLevel(zoom);
        },
        webix_cal_block:function(e, id, trg){
            if(this._zoom_in){
                if(trg.className.indexOf('webix_cal_day_disabled')!==-1)
                    return false;
                var next = this._locate_date(trg);
                this._update_zoom_level(next);
            }
            else{
                if(trg.className.indexOf('webix_cal_day_disabled')==-1)
                    this._mode_selected(trg);
            }
        }
    },
    _string_to_date: function(date, format){
        if (!date){
            return webix.Date.datePart(new Date());
        }
        if(typeof date == "string"){
            if (format)
                date = webix.Date.strToDate(format)(date);
            else
                date=webix.i18n.parseFormatDate(date);
        }

        return date;
    },
    _checkDate: function(date){
        var blockedDate = (this._settings.blockDates && this._settings.blockDates.call(this,date));
        var minDate = this._settings.minDate;
        var maxDate = this._settings.maxDate;
        var outOfRange = (date < minDate || date > maxDate);
        return !blockedDate &&!outOfRange;
    },
    _findActive:function(date, mode){
        var dir = (mode === "top" || mode ==="left" || mode === "pgup" || mode === "up") ? -1 : 1;
        var newdate = webix.Date.add(date, dir, "day", true);
        if(this._checkDate(newdate))
            return newdate;
        else{
            var compare;
            if(this._zoom_level === 0) compare = (date.getMonth() === newdate.getMonth());
            else if(this._zoom_level === 1 ) compare = (date.getFullYear() === newdate.getFullYear());
            else if(this._zoom_level === 2) compare = (newdate.getFullYear() > this._zoom_start_date && newdate.getFullYear() < this._zoom_start_date+10);

            if(compare)
                return this._findActive(newdate, mode);
        }
    },
    showCalendar: function(date) {
        date = this._string_to_date(date);
        this._settings.date = date;
        this.render();
        this.resize();
    },
    getSelectedDate: function() {
        return (this._selected_date)?webix.Date.copy(this._selected_date):this._selected_date;

    },
    getVisibleDate: function() {
        return webix.Date.copy(this._settings.date);
    },
    setValue: function(date, format){
        this.selectDate(date, true);
    },
    getValue: function(format){
        var date = this.getSelectedDate();
        if (format)
            date = webix.Date.dateToStr(format)(date);
        return date;
    },
    selectDate: function(date, show){
        if(date){
            date = this._string_to_date(date);
            this._selected_date = date;
            this._selected_date_part = webix.Date.datePart(webix.Date.copy(date));
        }
        else{ //deselect
            this._selected_date = null;
            this._selected_date_part = null;
            if(this._settings.date){
                webix.Date.datePart(this._settings.date);
            }
        }

        if (show)
            this.showCalendar(date);
        else if(show !==false)
            this.render();

        this.callEvent("onChange",[date]);
    },
    locate:function(){ return null; }

}, webix.KeysNavigation, webix.MouseEvents, webix.ui.view, webix.EventSystem);