За последние 24 часа нас посетили 20076 программистов и 1650 роботов. Сейчас ищут 1800 программистов ...

Помогите с javascript

Тема в разделе "JavaScript и AJAX", создана пользователем Solonik, 5 дек 2012.

  1. Solonik

    Solonik Активный пользователь

    С нами с:
    2 дек 2012
    Сообщения:
    17
    Симпатии:
    0
    Код (Text):
    1. DateInput = (function($) {
    2.  
    3. function DateInput(el, opts) {
    4.   if (typeof(opts) != "object") opts = {};
    5.   $.extend(this, DateInput.DEFAULT_OPTS, opts);
    6.  
    7.   this.input = $(el);
    8.   this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "keydownHandler", "selectDate");
    9.  
    10.   this.build();
    11.   this.selectDate();
    12.   this.hide();
    13. };
    14. DateInput.DEFAULT_OPTS = {
    15.   month_names: ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"],
    16.   short_month_names: ["Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"],
    17.    day_names: ["Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота"],
    18.   short_day_names: ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"],
    19.   start_of_week: 1
    20. };
    21. DateInput.prototype = {
    22.   build: function() {
    23.     var monthNav = $('<p class="month_nav">' +
    24.       '<span class="button prev" title="[Page-Up]">«</span>' +
    25.       ' <span class="month_name"></span> ' +
    26.       '<span class="button next" title="[Page-Down]">»</span>' +
    27.       '</p>');
    28.     this.monthNameSpan = $(".month_name", monthNav);
    29.     $(".prev", monthNav).click(this.bindToObj(function() { this.moveMonthBy(-1); }));
    30.     $(".next", monthNav).click(this.bindToObj(function() { this.moveMonthBy(1); }));
    31.    
    32.     var yearNav = $('<p class="year_nav">' +
    33.       '<span class="button prev" title="[Ctrl+Page-Up]">«</span>' +
    34.       ' <span class="year_name"></span> ' +
    35.       '<span class="button next" title="[Ctrl+Page-Down]">»</span>' +
    36.       '</p>');
    37.     this.yearNameSpan = $(".year_name", yearNav);
    38.     $(".prev", yearNav).click(this.bindToObj(function() { this.moveMonthBy(-12); }));
    39.     $(".next", yearNav).click(this.bindToObj(function() { this.moveMonthBy(12); }));
    40.    
    41.     var nav = $('<div class="nav"></div>').append(monthNav, yearNav);
    42.    
    43.     var tableShell = "<table><thead><tr>";
    44.     $(this.adjustDays(this.short_day_names)).each(function() {
    45.       tableShell += "<th>" + this + "</th>";
    46.     });
    47.     tableShell += "</tr></thead><tbody></tbody></table>";
    48.    
    49.     this.dateSelector = this.rootLayers = $('<div class="date_selector"></div>').append(nav, tableShell).insertAfter(this.input);
    50.    
    51.     if ($.browser.msie && $.browser.version < 7) {
    52.      
    53.       this.ieframe = $('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);
    54.       this.rootLayers = this.rootLayers.add(this.ieframe);
    55.      
    56.       $(".button", nav).mouseover(function() { $(this).addClass("hover") });
    57.       $(".button", nav).mouseout(function() { $(this).removeClass("hover") });
    58.     };
    59.    
    60.     this.tbody = $("tbody", this.dateSelector);
    61.    
    62.     this.input.change(this.bindToObj(function() { this.selectDate(); }));
    63.     this.selectDate();
    64.   },
    65.  
    66.   selectMonth: function(date) {
    67.     var newMonth = new Date(date.getFullYear(), date.getMonth(), 1);
    68.    
    69.     if (!this.currentMonth || !(this.currentMonth.getFullYear() == newMonth.getFullYear() &&
    70.                                 this.currentMonth.getMonth() == newMonth.getMonth())) {
    71.      
    72.       this.currentMonth = newMonth;
    73.      
    74.       var rangeStart = this.rangeStart(date), rangeEnd = this.rangeEnd(date);
    75.       var numDays = this.daysBetween(rangeStart, rangeEnd);
    76.       var dayCells = "";
    77.      
    78.       for (var i = 0; i <= numDays; i++) {
    79.         var currentDay = new Date(rangeStart.getFullYear(), rangeStart.getMonth(), rangeStart.getDate() + i, 12, 00);
    80.        
    81.         if (this.isFirstDayOfWeek(currentDay)) dayCells += "<tr>";
    82.        
    83.         if (currentDay.getMonth() == date.getMonth()) {
    84.           dayCells += '<td class="selectable_day" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>';
    85.         } else {
    86.           dayCells += '<td class="unselected_month" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>';
    87.         };
    88.        
    89.         if (this.isLastDayOfWeek(currentDay)) dayCells += "</tr>";
    90.       };
    91.       this.tbody.empty().append(dayCells);
    92.      
    93.       this.monthNameSpan.empty().append(this.monthName(date));
    94.       this.yearNameSpan.empty().append(this.currentMonth.getFullYear());
    95.      
    96.       $(".selectable_day", this.tbody).click(this.bindToObj(function(event) {
    97.         this.changeInput($(event.target).attr("date"));
    98.       }));
    99.      
    100.       $("td[date=" + this.dateToString(new Date()) + "]", this.tbody).addClass("today");
    101.      
    102.       $("td.selectable_day", this.tbody).mouseover(function() { $(this).addClass("hover") });
    103.       $("td.selectable_day", this.tbody).mouseout(function() { $(this).removeClass("hover") });
    104.     };
    105.    
    106.     $('.selected', this.tbody).removeClass("selected");
    107.     $('td[date=' + this.selectedDateString + ']', this.tbody).addClass("selected");
    108.   },
    109.  
    110.   selectDate: function(date) {
    111.     if (typeof(date) == "undefined") {
    112.       date = this.stringToDate(this.input.val());
    113.     };
    114.     if (!date) date = new Date();
    115.    
    116.     this.selectedDate = date;
    117.     this.selectedDateString = this.dateToString(this.selectedDate);
    118.     this.selectMonth(this.selectedDate);
    119.   },
    120.  
    121.   changeInput: function(dateString) {
    122.     this.input.val(dateString).change();
    123.     this.hide();
    124.   },
    125.  
    126.   show: function() {
    127.     this.rootLayers.css("display", "block");
    128.     $([window, document.body]).click(this.hideIfClickOutside);
    129.     this.input.unbind("focus", this.show);
    130.     $(document.body).keydown(this.keydownHandler);
    131.     this.setPosition();
    132.   },
    133.  
    134.   hide: function() {
    135.     this.rootLayers.css("display", "none");
    136.     $([window, document.body]).unbind("click", this.hideIfClickOutside);
    137.     this.input.focus(this.show);
    138.     $(document.body).unbind("keydown", this.keydownHandler);
    139.   },
    140.  
    141.   hideIfClickOutside: function(event) {
    142.     if (event.target != this.input[0] && !this.insideSelector(event)) {
    143.       this.hide();
    144.     };
    145.   },
    146.  
    147.   insideSelector: function(event) {
    148.     var offset = this.dateSelector.position();
    149.     offset.right = offset.left + this.dateSelector.outerWidth();
    150.     offset.bottom = offset.top + this.dateSelector.outerHeight();
    151.    
    152.     return event.pageY < offset.bottom &&
    153.            event.pageY > offset.top &&
    154.            event.pageX < offset.right &&
    155.            event.pageX > offset.left;
    156.   },
    157.  
    158.   keydownHandler: function(event) {
    159.     switch (event.keyCode)
    160.     {
    161.       case 9:
    162.       case 27:
    163.         this.hide();
    164.         return;
    165.       break;
    166.       case 13:
    167.         this.changeInput(this.selectedDateString);
    168.       break;
    169.       case 33:
    170.         this.moveDateMonthBy(event.ctrlKey ? -12 : -1);
    171.       break;
    172.       case 34:
    173.         this.moveDateMonthBy(event.ctrlKey ? 12 : 1);
    174.       break;
    175.       case 38:
    176.         this.moveDateBy(-7);
    177.       break;
    178.       case 40:
    179.         this.moveDateBy(7);
    180.       break;
    181.       case 37:
    182.         this.moveDateBy(-1);
    183.       break;
    184.       case 39:
    185.         this.moveDateBy(1);
    186.       break;
    187.       default:
    188.         return;
    189.     }
    190.     event.preventDefault();
    191.   },
    192.  
    193.   stringToDate: function(string) {
    194.     var matches;
    195.     if (matches = string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)) {
    196.       return new Date(matches[3], this.shortMonthNum(matches[2]), matches[1], 12, 00);
    197.     } else {
    198.       return null;
    199.     };
    200.   },
    201.  
    202.   dateToString: function(date) {
    203.     return date.getDate() + " " + this.short_month_names[date.getMonth()] + " " + this.day_names[date.getDay()] + " " + date.getFullYear();
    204.   },
    205.  
    206.   setPosition: function() {
    207.     var offset = this.input.offset();
    208.     this.rootLayers.css({
    209.       top: offset.top + this.input.outerHeight(),
    210.       left: offset.left
    211.     });
    212.    
    213.     if (this.ieframe) {
    214.       this.ieframe.css({
    215.         width: this.dateSelector.outerWidth(),
    216.         height: this.dateSelector.outerHeight()
    217.       });
    218.     };
    219.   },
    220.  
    221.   moveDateBy: function(amount) {
    222.     var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth(), this.selectedDate.getDate() + amount);
    223.     this.selectDate(newDate);
    224.   },
    225.  
    226.   moveDateMonthBy: function(amount) {
    227.     var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth() + amount, this.selectedDate.getDate());
    228.     if (newDate.getMonth() == this.selectedDate.getMonth() + amount + 1) {
    229.      
    230.       newDate.setDate(0);
    231.     };
    232.     this.selectDate(newDate);
    233.   },
    234.  
    235.   moveMonthBy: function(amount) {
    236.     var newMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + amount, this.currentMonth.getDate());
    237.     this.selectMonth(newMonth);
    238.   },
    239.  
    240.   monthName: function(date) {
    241.     return this.month_names[date.getMonth()];
    242.   },
    243.  
    244.   bindToObj: function(fn) {
    245.     var self = this;
    246.     return function() { return fn.apply(self, arguments) };
    247.   },
    248.  
    249.   bindMethodsToObj: function() {
    250.     for (var i = 0; i < arguments.length; i++) {
    251.       this[arguments[i]] = this.bindToObj(this[arguments[i]]);
    252.     };
    253.   },
    254.  
    255.   indexFor: function(array, value) {
    256.     for (var i = 0; i < array.length; i++) {
    257.       if (value == array[i]) return i;
    258.     };
    259.   },
    260.  
    261.   monthNum: function(month_name) {
    262.     return this.indexFor(this.month_names, month_name);
    263.   },
    264.  
    265.   shortMonthNum: function(month_name) {
    266.     return this.indexFor(this.short_month_names, month_name);
    267.   },
    268.  
    269.   shortDayNum: function(day_name) {
    270.     return this.indexFor(this.short_day_names, day_name);
    271.   },
    272.  
    273.   daysBetween: function(start, end) {
    274.     start = Date.UTC(start.getFullYear(), start.getMonth(), start.getDate());
    275.     end = Date.UTC(end.getFullYear(), end.getMonth(), end.getDate());
    276.     return (end - start) / 86400000;
    277.   },
    278.  
    279.   changeDayTo: function(dayOfWeek, date, direction) {
    280.     var difference = direction * (Math.abs(date.getDay() - dayOfWeek - (direction * 7)) % 7);
    281.     return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference);
    282.   },
    283.  
    284.   rangeStart: function(date) {
    285.     return this.changeDayTo(this.start_of_week, new Date(date.getFullYear(), date.getMonth()), -1);
    286.   },
    287.  
    288.   rangeEnd: function(date) {
    289.     return this.changeDayTo((this.start_of_week - 1) % 7, new Date(date.getFullYear(), date.getMonth() + 1, 0), 1);
    290.   },
    291.  
    292.   isFirstDayOfWeek: function(date) {
    293.     return date.getDay() == this.start_of_week;
    294.   },
    295.  
    296.   isLastDayOfWeek: function(date) {
    297.     return date.getDay() == (this.start_of_week - 1) % 7;
    298.   },
    299.  
    300.   adjustDays: function(days) {
    301.     var newDays = [];
    302.     for (var i = 0; i < days.length; i++) {
    303.       newDays[i] = days[(i + this.start_of_week) % 7];
    304.     };
    305.     return newDays;
    306.   }
    307. };
    308.  
    309. $.fn.date_input = function(opts) {
    310.   return this.each(function() { new DateInput(this, opts); });
    311. };
    312. $.date_input = { initialize: function(opts) {
    313.   $("input.date_input").date_input(opts);
    314. } };
    315.  
    316. return DateInput;
    317. })(jQuery);
    Хотелось бы сделать если выбрано сегодня выводилось бы одно, если выбрано завтра то другое, если дата вчера и позднее то третье.

    Помогите пожалуйста разобраться кто знает. Всю голову сломал.
    Выводилось не alert, а document.write
     
  2. Your

    Your Старожил

    С нами с:
    2 июл 2011
    Сообщения:
    4.074
    Симпатии:
    7
    Вы таймер счета дней пытаетесь сделать, или что?))

    Что-это такое :D