1/** 2 * @package PickMeUp - jQuery datepicker plugin 3 * @author Nazar Mokrynskyi <[email protected]> 4 * @author Stefan Petre <www.eyecon.ro> 5 * @copyright Copyright (c) 2013-2014, Nazar Mokrynskyi 6 * @copyright Copyright (c) 2008-2009, Stefan Petre 7 * @license MIT License, see license.txt 8 */ 9(function (d) { 10 function getMaxDays () { 11 var tmpDate = new Date(this.toString()), 12 d = 28, 13 m = tmpDate.getMonth(); 14 while (tmpDate.getMonth() == m) { 15 ++d; 16 tmpDate.setDate(d); 17 } 18 return d - 1; 19 } 20 d.addDays = function (n) { 21 this.setDate(this.getDate() + n); 22 }; 23 d.addMonths = function (n) { 24 var day = this.getDate(); 25 this.setDate(1); 26 this.setMonth(this.getMonth() + n); 27 this.setDate(Math.min(day, getMaxDays.apply(this))); 28 }; 29 d.addYears = function (n) { 30 var day = this.getDate(); 31 this.setDate(1); 32 this.setFullYear(this.getFullYear() + n); 33 this.setDate(Math.min(day, getMaxDays.apply(this))); 34 }; 35 d.getDayOfYear = function() { 36 var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0); 37 var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0); 38 var time = now - then; 39 return Math.floor(time / 24*60*60*1000); 40 }; 41})(Date.prototype); 42(function ($) { 43 var instances_count = 0; 44 $.pickmeup = $.extend($.pickmeup || {}, { 45 date : new Date, 46 flat : false, 47 first_day : 1, 48 prev : '◀', 49 next : '▶', 50 mode : 'single', 51 select_year : true, 52 select_month : true, 53 select_day : true, 54 view : 'days', 55 calendars : 1, 56 format : 'd-m-Y', 57 position : 'bottom', 58 trigger_event : 'click touchstart', 59 class_name : '', 60 separator : ' - ', 61 hide_on_select : false, 62 min : null, 63 max : null, 64 render : function () {}, 65 change : function () {return true;}, 66 before_show : function () {return true;}, 67 show : function () {return true;}, 68 hide : function () {return true;}, 69 fill : function () {return true;}, 70 locale : { 71 days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], 72 daysShort : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], 73 daysMin : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], 74 months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], 75 monthsShort : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 76 } 77 }); 78 var views = { 79 years : 'pmu-view-years', 80 months : 'pmu-view-months', 81 days : 'pmu-view-days' 82 }, 83 tpl = { 84 wrapper : '<div class="pickmeup" />', 85 head : function (d) { 86 var result = ''; 87 for (var i = 0; i < 7; ++i) { 88 result += '<div>' + d.day[i] + '</div>' 89 } 90 return '<div class="pmu-instance">' + 91 '<nav>' + 92 '<div class="pmu-prev pmu-button">' + d.prev + '</div>' + 93 '<div class="pmu-month pmu-button" />' + 94 '<div class="pmu-next pmu-button">' + d.next + '</div>' + 95 '</nav>' + 96 '<nav class="pmu-day-of-week">' + result + '</nav>' + 97 '</div>'; 98 },
99 body : function (elements, container_class_name) { 100 var result = ''; 101 for (var i = 0; i < elements.length; ++i) { 102 result += '<div class="' + elements[i].class_name + ' pmu-button">' + elements[i].text + '</div>' 103 } 104 return '<div class="' + container_class_name + '">' + result + '</div>'; 105 } 106 }; 107 function fill () { 108 var options = $(this).data('pickmeup-options'), 109 pickmeup = this.pickmeup, 110 current_cal = Math.floor(options.calendars / 2), 111 actual_date = options.date, 112 current_date = options.current, 113 local_date, 114 header, 115 html, 116 instance, 117 today = (new Date).setHours(0,0,0,0).valueOf(), 118 shown_date_from, 119 shown_date_to, 120 tmp_date; 121 /** 122 * Remove old content except header navigation 123 */ 124 pickmeup.find('.pmu-instance > :not(nav)').remove(); 125 /** 126 * If several calendars should be shown 127 */ 128 for (var i = 0; i < options.calendars; i++) { 129 local_date = new Date(current_date); 130 instance = pickmeup.find('.pmu-instance').eq(i); 131 if (pickmeup.hasClass('pmu-view-years')) { 132 local_date.addYears((i - current_cal) * 12); 133 header = (local_date.getFullYear() - 6) + ' - ' + (local_date.getFullYear()+5); 134 } else if (pickmeup.hasClass('pmu-view-months')) { 135 local_date.addYears(i - current_cal); 136 header = local_date.getFullYear(); 137 } else if (pickmeup.hasClass('pmu-view-days')) { 138 local_date.addMonths(i - current_cal); 139 header = formatDate(local_date, 'B, Y', options.locale); 140 } 141 if (!shown_date_to) { 142 if (options.max) { 143 // If all dates in this month (months in year or years in years block) are after max option - set next month as current 144 // in order not to show calendar with all disabled dates 145 tmp_date = new Date(local_date); 146 if (options.select_day) { 147 tmp_date.addMonths(options.calendars - 1); 148 } else if (options.select_month) { 149 tmp_date.addYears(options.calendars - 1); 150 } else { 151 tmp_date.addYears((options.calendars - 1) * 12); 152 } 153 if (tmp_date > options.max) { 154 --i; 155 current_date.addMonths(-1); 156 shown_date_to = undefined; 157 continue; 158 } 159 } 160 } 161 shown_date_to = new Date(local_date); 162 if (!shown_date_from) { 163 shown_date_from = new Date(local_date); 164 // If all dates in this month are before min option - set next month as current in order not to show calendar with all disabled dates 165 shown_date_from.setDate(1); 166 shown_date_from.addMonths(1); 167 shown_date_from.addDays(-1); 168 if (options.min && options.min > shown_date_from) { 169 --i; 170 current_date.addMonths(1); 171 shown_date_from = undefined; 172 continue; 173 } 174 } 175 instance 176 .find('.pmu-month') 177 .text(header); 178 html = ''; 179 var is_year_selected = function (year) { 180 return ( 181 options.mode == 'range' && 182 year >= new Date(actual_date[0]).getFullYear() && 183 year <= new Date(actual_date[1]).getFullYear() 184 ) || 185 ( 186 options.mode == 'multiple' && 187 actual_date.reduce(function (prev, current) { 188 prev.push(new Date(current).getFullYear()); 189 return prev; 190 }, []).indexOf(year) !== -1 191 ) || 192 new Date(actual_date).getFullYear() == year; 193 }; 194 var is_months_selected = function (year, month) { 195 var first_year = new Date(actual_date[0]).getFullYear(), 196 lastyear = new Date(actual_date[1]).getFullYear(), 197 first_month = new Date(actual_date[0]).getMonth(), 198 last_month = new Date(actual_date[1]).getMonth(); 199 return ( 200 options.mode == 'range' && 201 year > first_year && 202 year < lastyear 203 ) || 204 ( 205 options.mode == 'range' && 206 year == first_year && 207 year < lastyear && 208 month >= first_month 209 ) || 210 ( 211 options.mode == 'range' && 212 year > first_year && 213 year == lastyear && 214 month <= last_month 215 ) || 216 ( 217 options.mode == 'range' && 218 year == first_year && 219 year == lastyear && 220 month >= first_month && 221 month <= last_month 222 ) || 223 ( 224 options.mode == 'multiple' && 225 actual_date.reduce(function (prev, current) { 226 current = new Date(current); 227 prev.push(current.getFullYear() + '-' + current.getMonth()); 228 return prev; 229 }, []).indexOf(year + '-' + month) !== -1 230 ) || 231 ( 232 new Date(actual_date).getFullYear() == year && 233 new Date(actual_date).getMonth() == month 234 ) 235 }; 236 (function () { 237 var years = [], 238 start_from_year = local_date.getFullYear() - 6, 239 min_year = new Date(options.min).getFullYear(),
240 max_year = new Date(options.max).getFullYear(), 241 year; 242 for (var j = 0; j < 12; ++j) { 243 year = { 244 text : start_from_year + j, 245 class_name : [] 246 }; 247 if ( 248 ( 249 options.min && year.text < min_year 250 ) || 251 ( 252 options.max && year.text > max_year 253 ) 254 ) { 255 year.class_name.push('pmu-disabled'); 256 } else if (is_year_selected(year.text)) { 257 year.class_name.push('pmu-today'); 258 } 259 year.class_name = year.class_name.join(' '); 260 years.push(year); 261 } 262 html += tpl.body(years, 'pmu-years'); 263 })(); 264 (function () { 265 var months = [], 266 current_year = local_date.getFullYear(), 267 min_year = new Date(options.min).getFullYear(), 268 min_month = new Date(options.min).getMonth(), 269 max_year = new Date(options.max).getFullYear(), 270 max_month = new Date(options.max).getMonth(), 271 month; 272 for (var j = 0; j < 12; ++j) { 273 month = { 274 text : options.locale.monthsShort[j], 275 class_name : [] 276 }; 277 if ( 278 ( 279 options.min && 280 ( 281 current_year < min_year || 282 ( 283 j < min_month && current_year == min_year 284 ) 285 ) 286 ) || 287 ( 288 options.max && 289 ( 290 current_year > max_year || 291 ( 292 j > max_month && current_year >= max_year 293 ) 294 ) 295 ) 296 ) { 297 month.class_name.push('pmu-disabled'); 298 } else if (is_months_selected(current_year, j)) { 299 month.class_name.push('pmu-today'); 300 } 301 month.class_name = month.class_name.join(' '); 302 months.push(month); 303 } 304 html += tpl.body(months, 'pmu-months'); 305 })(); 306 (function () { 307 var days = [], 308 current_month = local_date.getMonth(), 309 day; 310 // Correct first day in calendar taking into account first day of week (Sunday or Monday) 311 (function () { 312 local_date.setDate(1); 313 var day = (local_date.getDay() - options.first_day) % 7; 314 local_date.addDays(-(day + (day < 0 ? 7 : 0))); 315 })(); 316 for (var j = 0; j < 42; ++j) { 317 day = { 318 text : local_date.getDate(), 319 class_name : [] 320 }; 321 if (current_month != local_date.getMonth()) { 322 day.class_name.push('pmu-not-in-month'); 323 } 324 if (local_date.getDay() == 0) { 325 day.class_name.push('pmu-sunday'); 326 } else if (local_date.getDay() == 6) { 327 day.class_name.push('pmu-saturday'); 328 } 329 var from_user = options.render(local_date) || {}, 330 val = local_date.valueOf(), 331 disabled = (options.min && options.min > local_date) || (options.max && options.max < local_date); 332 if (from_user.disabled || disabled) { 333 day.class_name.push('pmu-disabled'); 334 } else if ( 335 from_user.selected || 336 options.date == val || 337 $.inArray(val, options.date) !== -1 || 338 ( 339 options.mode == 'range' && val >= options.date[0] && val <= options.date[1] 340 ) 341 ) { 342 day.class_name.push('pmu-selected'); 343 } 344 if (val == today) { 345 day.class_name.push('pmu-today'); 346 } 347 if (from_user.class_name) { 348 day.class_name.push(from_user.class_name); 349 } 350 day.class_name = day.class_name.join(' '); 351 days.push(day); 352 // Move to next day 353 local_date.addDays(1); 354 } 355 html += tpl.body(days, 'pmu-days'); 356 })(); 357 instance.append(html); 358 } 359 shown_date_from.setDate(1); 360 shown_date_to.setDate(1); 361 shown_date_to.addMonths(1); 362 shown_date_to.addDays(-1); 363 pickmeup.find('.pmu-prev').css( 364 'visibility', 365 options.min && options.min >= shown_date_from ? 'hidden' : 'visible' 366 ); 367 pickmeup.find('.pmu-next').css( 368 'visibility', 369 options.max && options.max <= shown_date_to ? 'hidden' : 'visible' 370 ); 371 options.fill.apply(this); 372 } 373 function parseDate (date, format, separator, locale) { 374 if (date.constructor == Date) { 375 return date; 376 } else if (!date) { 377 return new Date; 378 } 379 var splitted_date = date.split(separator); 380 if (splitted_date.length > 1) { 381 splitted_date.forEach(function (element, index, array) { 382 array[index] = parseDate($.trim(element), format, separator, locale); 383 }); 384 return splitted_date; 385 } 386 var months_text = locale.monthsShort.join(')(') + ')(' + locale.months.join(')('), 387 separator = new RegExp('[^0-9a-zA-Z(' + months_text + ')]+'), 388 parts = date.split(separator), 389 against = format.split(separator), 390 d, 391 m, 392 y, 393 h, 394 min, 395 now = new Date(); 396 for (var i = 0; i < parts.length; i++) { 397 switch (against[i]) { 398 case 'b': 399 m = locale.monthsShort.indexOf(parts[i]); 400 break; 401 case 'B': 402 m = locale.months.indexOf(parts[i]); 403 break; 404 case 'd':
405 case 'e': 406 d = parseInt(parts[i],10); 407 break; 408 case 'm': 409 m = parseInt(parts[i], 10)-1; 410 break; 411 case 'Y': 412 case 'y': 413 y = parseInt(parts[i], 10); 414 y += y > 100 ? 0 : (y < 29 ? 2000 : 1900); 415 break; 416 case 'H': 417 case 'I': 418 case 'k': 419 case 'l': 420 h = parseInt(parts[i], 10); 421 break; 422 case 'P': 423 case 'p': 424 if (/pm/i.test(parts[i]) && h < 12) { 425 h += 12; 426 } else if (/am/i.test(parts[i]) && h >= 12) { 427 h -= 12; 428 } 429 break; 430 case 'M': 431 min = parseInt(parts[i], 10); 432 break; 433 } 434 } 435 var parsed_date = new Date( 436 y === undefined ? now.getFullYear() : y, 437 m === undefined ? now.getMonth() : m, 438 d === undefined ? now.getDate() : d, 439 h === undefined ? now.getHours() : h, 440 min === undefined ? now.getMinutes() : min, 441 0 442 ); 443 if (isNaN(parsed_date * 1)) { 444 parsed_date = new Date; 445 } 446 return parsed_date; 447 } 448 function formatDate (date, format, locale) { 449 var m = date.getMonth(); 450 var d = date.getDate(); 451 var y = date.getFullYear(); 452 var w = date.getDay(); 453 var s = {}; 454 var hr = date.getHours(); 455 var pm = (hr >= 12); 456 var ir = (pm) ? (hr - 12) : hr; 457 var dy = date.getDayOfYear(); 458 if (ir == 0) { 459 ir = 12; 460 } 461 var min = date.getMinutes(); 462 var sec = date.getSeconds(); 463 var parts = format.split(''), part; 464 for (var i = 0; i < parts.length; i++) { 465 part = parts[i]; 466 switch (part) { 467 case 'a': 468 part = locale.daysShort[w]; 469 break; 470 case 'A': 471 part = locale.days[w]; 472 break; 473 case 'b': 474 part = locale.monthsShort[m]; 475 break; 476 case 'B': 477 part = locale.months[m]; 478 break; 479 case 'C': 480 part = 1 + Math.floor(y / 100); 481 break; 482 case 'd': 483 part = (d < 10) ? ("0" + d) : d; 484 break; 485 case 'e': 486 part = d; 487 break; 488 case 'H': 489 part = (hr < 10) ? ("0" + hr) : hr; 490 break; 491 case 'I': 492 part = (ir < 10) ? ("0" + ir) : ir; 493 break; 494 case 'j': 495 part = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; 496 break; 497 case 'k': 498 part = hr; 499 break; 500 case 'l': 501 part = ir; 502 break; 503 case 'm': 504 part = (m < 9) ? ("0" + (1+m)) : (1+m); 505 break; 506 case 'M': 507 part = (min < 10) ? ("0" + min) : min; 508 break; 509 case 'p': 510 case 'P': 511 part = pm ? "PM" : "AM"; 512 break; 513 case 's': 514 part = Math.floor(date.getTime() / 1000); 515 break; 516 case 'S': 517 part = (sec < 10) ? ("0" + sec) : sec; 518 break; 519 case 'u': 520 part = w + 1; 521 break; 522 case 'w': 523 part = w; 524 break; 525 case 'y': 526 part = ('' + y).substr(2, 2); 527 break; 528 case 'Y': 529 part = y; 530 break; 531 } 532 parts[i] = part; 533 } 534 return parts.join(''); 535 } 536 function update_date () { 537 var $this = $(this), 538 options = $this.data('pickmeup-options'), 539 current_date = options.current, 540 new_value; 541 switch (options.mode) { 542 case 'multiple': 543 new_value = current_date.setHours(0,0,0,0).valueOf(); 544 if ($.inArray(new_value, options.date) !== -1) { 545 $.each(options.date, function (index, value){ 546 if (value == new_value) { 547 options.date.splice(index,1); 548 return false; 549 } 550 return true; 551 }); 552 } else { 553 options.date.push(new_value); 554 } 555 break; 556 case 'range': 557 if (!options.lastSel) { 558 options.date[0] = current_date.setHours(0,0,0,0).valueOf(); 559 } 560 new_value = current_date.setHours(0,0,0,0).valueOf(); 561 if (new_value <= options.date[0]) { 562 options.date[1] = options.date[0]; 563 options.date[0] = new_value; 564 } else { 565 options.date[1] = new_value; 566 } 567 options.lastSel = !options.lastSel; 568 break; 569 default: 570 options.date = current_date.valueOf(); 571 break; 572 } 573 var prepared_date = prepareDate(options); 574 if ($this.is('input')) { 575 $this.val(options.mode == 'single' ? prepared_date[0] : prepared_date[0].join(options.separator)); 576 } 577 options.change.apply(this, prepared_date); 578 if ( 579 options.hide_on_select && 580 ( 581 options.mode != 'range' || 582 !options.lastSel 583 ) 584 ) { 585 options.binded.hide(); 586 return false; 587 } 588 } 589 function click (e) { 590 var el = $(e.target); 591 if (!el.hasClass('pmu-button')) { 592 el = el.closest('.pmu-button'); 593 } 594 if (el.length) { 595 if (el.hasClass('pmu-disabled')) { 596 return false;
597 } 598 var $this = $(this), 599 options = $this.data('pickmeup-options'), 600 instance = el.parents('.pmu-instance').eq(0), 601 root = instance.parent(), 602 instance_index = $('.pmu-instance', root).index(instance); 603 if (el.parent().is('nav')) { 604 if (el.hasClass('pmu-month')) { 605 options.current.addMonths(instance_index - Math.floor(options.calendars / 2)); 606 if (root.hasClass('pmu-view-years')) { 607 // Shift back to current date, otherwise with min value specified may jump on few (tens) years forward 608 if (options.mode != 'single') { 609 options.current = new Date(options.date[options.date.length - 1]); 610 } else { 611 options.current = new Date(options.date); 612 } 613 if (options.select_day) { 614 root.removeClass('pmu-view-years').addClass('pmu-view-days'); 615 } else if (options.select_month) { 616 root.removeClass('pmu-view-years').addClass('pmu-view-months'); 617 } 618 } else if (root.hasClass('pmu-view-months')) { 619 if (options.select_year) { 620 root.removeClass('pmu-view-months').addClass('pmu-view-years'); 621 } else if (options.select_day) { 622 root.removeClass('pmu-view-months').addClass('pmu-view-days'); 623 } 624 } else if (root.hasClass('pmu-view-days')) { 625 if (options.select_month) { 626 root.removeClass('pmu-view-days').addClass('pmu-view-months'); 627 } else if (options.select_year) { 628 root.removeClass('pmu-view-days').addClass('pmu-view-years'); 629 } 630 } 631 } else { 632 if (el.hasClass('pmu-prev')) { 633 options.binded.prev(false); 634 } else { 635 options.binded.next(false); 636 } 637 } 638 } else if (!el.hasClass('pmu-disabled')) { 639 if (root.hasClass('pmu-view-years')) { 640 options.current.setFullYear(parseInt(el.text(), 10)); 641 if (options.select_month) { 642 root.removeClass('pmu-view-years').addClass('pmu-view-months'); 643 } else if (options.select_day) { 644 root.removeClass('pmu-view-years').addClass('pmu-view-days'); 645 } else { 646 options.binded.update_date(); 647 } 648 } else if (root.hasClass('pmu-view-months')) { 649 options.current.setMonth(instance.find('.pmu-months .pmu-button').index(el)); 650 options.current.setFullYear(parseInt(instance.find('.pmu-month').text(), 10)); 651 if (options.select_day) { 652 root.removeClass('pmu-view-months').addClass('pmu-view-days'); 653 } else { 654 options.binded.update_date(); 655 } 656 // Move current month to the first place 657 options.current.addMonths(Math.floor(options.calendars / 2) - instance_index); 658 } else { 659 var val = parseInt(el.text(), 10); 660 options.current.addMonths(instance_index - Math.floor(options.calendars / 2)); 661 if (el.hasClass('pmu-not-in-month')) { 662 options.current.addMonths(val > 15 ? -1 : 1); 663 } 664 options.current.setDate(val); 665 options.binded.update_date(); 666 } 667 } 668 options.binded.fill(); 669 } 670 return false; 671 } 672 function prepareDate (options) { 673 var result; 674 if (options.mode == 'single') { 675 result = new Date(options.date); 676 return [formatDate(result, options.format, options.locale), result]; 677 } else { 678 result = [[],[]]; 679 $.each(options.date, function(nr, val){ 680 var date = new Date(val); 681 result[0].push(formatDate(date, options.format, options.locale)); 682 result[1].push(date); 683 }); 684 return result; 685 } 686 } 687 function show (force) { 688 var pickmeup = this.pickmeup; 689 if (force || !pickmeup.is(':visible')) { 690 var $this = $(this), 691 options = $this.data('pickmeup-options'), 692 pos = $this.offset(), 693 viewport = { 694 l : document.documentElement.scrollLeft, 695 t : document.documentElement.scrollTop, 696 w : document.documentElement.clientWidth, 697 h : document.documentElement.clientHeight 698 }, 699 top = pos.top, 700 left = pos.left; 701 options.binded.fill(); 702 if ($this.is('input')) { 703 $this 704 .pickmeup('set_date', parseDate($this.val(), options.format, options.separator, options.locale)) 705 .keydown(function (e) { 706 if (e.which == 9) { 707 $this.pickmeup('hide'); 708 } 709 }); 710 } 711 options.before_show(); 712 switch (options.position){ 713 case 'top': 714 top -= pickmeup.outerHeight(); 715 break; 716 case 'left': 717 left -= pickmeup.outerWidth(); 718 break; 719 case 'right': 720 left += this.offsetWidth; 721 break; 722 case 'bottom': 723 top += this.offsetHeight; 724 break; 725 } 726 if (top + pickmeup.offsetHeight > viewport.t + viewport.h) { 727 top = pos.top - pickmeup.offsetHeight; 728 } 729 if (top < viewport.t) { 730 top = pos.top + this.offsetHeight + pickmeup.offsetHeight; 731 } 732 if (left + pickmeup.offsetWidth > viewport.l + viewport.w) { 733 left = pos.left - pickmeup.offsetWidth; 734 } 735 if (left < viewport.l) { 736 left = pos.left + this.offsetWidth 737 } 738 if (options.show() == false) { 739 return; 740 } 741 pickmeup.css({ 742 display : 'inline-block', 743 top : top + 'px', 744 left : left + 'px' 745 }); 746 $(document) 747 .on( 748 'mousedown' + options.events_namespace, 749 options.binded.hide 750 ) 751 .on( 752 'resize' + options.events_namespace, 753 [ 754 true 755 ], 756 options.binded.forced_show 757 ); 758 } 759 } 760 function forced_show () { 761 show.call(this, true); 762 } 763 function hide (e) { 764 if ( 765 !e || 766 !e.target || //Called directly 767 ( 768 e.target != this && //Clicked not on element itself 769 !(this.pickmeup.get(0).compareDocumentPosition(e.target) & 16) //And not o its children 770 ) 771 ) { 772 var pickmeup = this.pickmeup, 773 options = $(this).data('pickmeup-options'); 774 if (options.hide() != false) { 775 pickmeup.hide(); 776 $(document) 777 .off('mousedown', options.binded.hide) 778 .off('resize', options.binded.forced_show); 779 options.date[1] = options.date[0]; 780 options.lastSel = false;
781 } 782 } 783 } 784 function update () { 785 var options = $(this).data('pickmeup-options'); 786 $(document) 787 .off('mousedown', options.binded.hide) 788 .off('resize', options.binded.forced_show); 789 options.binded.forced_show(); 790 } 791 function clear () { 792 var options = $(this).data('pickmeup-options'); 793 if (options.mode != 'single') { 794 options.date = []; 795 options.lastSel = false; 796 options.binded.fill(); 797 } 798 } 799 function prev (fill) { 800 if (typeof fill == 'undefined') { 801 fill = true; 802 } 803 var root = this.pickmeup; 804 var options = $(this).data('pickmeup-options'); 805 if (root.hasClass('pmu-view-years')) { 806 options.current.addYears(-12); 807 } else if (root.hasClass('pmu-view-months')) { 808 options.current.addYears(-1); 809 } else if (root.hasClass('pmu-view-days')) { 810 options.current.addMonths(-1); 811 } 812 if (fill) { 813 options.binded.fill(); 814 } 815 816 817 818 } 819 function next (fill) { 820 if (typeof fill == 'undefined') { 821 fill = true; 822 } 823 var root = this.pickmeup; 824 var options = $(this).data('pickmeup-options'); 825 if (root.hasClass('pmu-view-years')) { 826 options.current.addYears(12); 827 } else if (root.hasClass('pmu-view-months')) { 828 options.current.addYears(1); 829 } else if (root.hasClass('pmu-view-days')) { 830 options.current.addMonths(1); 831 } 832 if (fill) { 833 options.binded.fill(); 834 } 835 } 836 function get_date (formatted) { 837 var options = $(this).data('pickmeup-options'), 838 prepared_date = prepareDate(options); 839 if (typeof formatted === 'string') { 840 var date = prepared_date[1]; 841 if (date.constructor == Date) { 842 return formatDate(date, formatted, options.locale) 843 } else { 844 return date.map(function (value) { 845 return formatDate(value, formatted, options.locale); 846 }); 847 } 848 } else { 849 return prepared_date[formatted ? 0 : 1]; 850 } 851 } 852 function set_date (date) { 853 var options = $(this).data('pickmeup-options'); 854 options.date = date; 855 if (typeof options.date === 'string') { 856 options.date = parseDate(options.date, options.format, options.separator, options.locale).setHours(0,0,0,0); 857 } else if (options.date.constructor == Date) { 858 options.date.setHours(0,0,0,0); 859 } 860 if (!options.date) { 861 options.date = new Date; 862 options.date.setHours(0,0,0,0); 863 } 864 if (options.mode != 'single') { 865 if (options.date.constructor != Array) { 866 options.date = [options.date.valueOf()]; 867 if (options.mode == 'range') { 868 options.date.push(((new Date(options.date[0])).setHours(0,0,0,0)).valueOf()); 869 } 870 } else { 871 for (var i = 0; i < options.date.length; i++) { 872 options.date[i] = (parseDate(options.date[i], options.format, options.separator, options.locale).setHours(0,0,0,0)).valueOf(); 873 } 874 if (options.mode == 'range') { 875 options.date[1] = ((new Date(options.date[1])).setHours(0,0,0,0)).valueOf(); 876 } 877 } 878 } else { 879 options.date = options.date.constructor == Array ? options.date[0].valueOf() : options.date.valueOf(); 880 } 881 options.current = new Date (options.mode != 'single' ? options.date[0] : options.date); 882 options.binded.fill(); 883 } 884 function destroy () { 885 var $this = $(this), 886 options = $this.data('pickmeup-options'); 887 $this.removeData('pickmeup-options'); 888 $this.off(options.events_namespace); 889 $(document).off(options.events_namespace); 890 $(this.pickmeup).remove(); 891 } 892 $.fn.pickmeup = function (initial_options) { 893 if (typeof initial_options === 'string') { 894 var data, 895 parameters = Array.prototype.slice.call(arguments, 1); 896 switch (initial_options) { 897 case 'hide': 898 case 'show': 899 case 'clear': 900 case 'update': 901 case 'prev': 902 case 'next': 903 case 'destroy': 904 this.each(function () { 905 data = $(this).data('pickmeup-options'); 906 if (data) { 907 data.binded[initial_options](); 908 } 909 }); 910 break; 911 case 'get_date': 912 data = this.data('pickmeup-options'); 913 if (data) { 914 return data.binded.get_date(parameters[0]); 915 } else { 916 return null; 917 } 918 break; 919 case 'set_date': 920 this.each(function () { 921 data = $(this).data('pickmeup-options'); 922 if (data) { 923 data.binded[initial_options].apply(this, parameters); 924 } 925 }); 926 } 927 return this; 928 } 929 return this.each(function () { 930 var $this = $(this); 931 if ($this.data('pickmeup-options')) { 932 return; 933 } 934 var i, 935 option, 936 options = $.extend({}, $.pickmeup, initial_options || {}); 937 for (i in options) { 938 option = $this.data('pmu-' + i); 939 if (typeof option !== 'undefined') { 940 options[i] = option; 941 } 942 } 943 // 4 conditional statements in order to account all cases 944 if (options.view == 'days' && !options.select_day) { 945 options.view = 'months'; 946 } 947 if (options.view == 'months' && !options.select_month) { 948 options.view = 'years'; 949 } 950 if (options.view == 'years' && !options.select_year) { 951 options.view = 'days'; 952 } 953 if (options.view == 'days' && !options.select_day) { 954 options.view = 'months'; 955 } 956 options.calendars = Math.max(1, parseInt(options.calendars, 10) || 1); 957 options.mode = /single|multiple|range/.test(options.mode) ? options.mode : 'single'; 958 if (typeof options.min === 'string') { 959 options.min = parseDate(options.min, options.format, options.separator, options.locale).setHours(0,0,0,0); 960 } else if (options.min && options.min.constructor == Date) { 961 options.min.setHours(0,0,0,0); 962 } 963 if (typeof options.max === 'string') { 964 options.max = parseDate(options.max, options.format, options.separator, options.locale).setHours(0,0,0,0); 965 } else if (options.max && options.max.constructor == Date) { 966 options.max.setHours(0,0,0,0); 967 } 968 if (!options.select_day) { 969 if (options.min) { 970 options.min = new Date(options.min); 971 options.min.setDate(1); 972 options.min = options.min.valueOf(); 973 } 974 if (options.max) { 975 options.max = new Date(options.max); 976 options.max.setDate(1); 977 options.max = options.max.valueOf(); 978 } 979 } 980 if (typeof options.date === 'string') { 981 options.date = parseDate(options.date, options.format, options.separator, options.locale).setHours(0,0,0,0); 982 } else if (options.date.constructor == Date) { 983 options.date.setHours(0,0,0,0); 984 } 985 if (!options.date) { 986 options.date = new Date; 987 options.date.setHours(0,0,0,0); 988 } 989 if (options.mode != 'single') { 990 if (options.date.constructor != Array) { 991 options.date = [options.date.valueOf()]; 992 if (options.mode == 'range') { 993 options.date.push(((new Date(options.date[0])).setHours(0,0,0,0)).valueOf()); 994 } 995 } else { 996 for (i = 0; i < options.date.length; i++) { 997 options.date[i] = (parseDate(options.date[i], options.format, options.separator, options.locale).setHours(0,0,0,0)).valueOf(); 998 } 999 if (options.mode == 'range') { 1000 options.date[1] = ((new Date(options.date[1])).setHours(0,0,0,0)).valueOf(); 1001 } 1002 } 1003 options.current = new Date(options.date[0]); 1004 // Set days to 1 in order to handle them consistently 1005 if (!options.select_day) { 1006 for (i = 0; i < options.date.length; ++i) { 1007 options.date[i] = new Date(options.date[i]); 1008 options.date[i].setDate(1); 1009 options.date[i] = options.date[i].valueOf(); 1010 // Remove duplicates 1011 if ( 1012 options.mode != 'range' && 1013 options.date.indexOf(options.date[i]) !== i 1014 ) { 1015 delete options.date.splice(i, 1); 1016 --i; 1017 } 1018 } 1019 } 1020 } else { 1021 options.date = options.date.valueOf(); 1022 options.current = new Date(options.date); 1023 if (!options.select_day) { 1024 options.date = new Date(options.date); 1025 options.date.setDate(1); 1026 options.date = options.date.valueOf(); 1027 } 1028 } 1029 options.current.setDate(1); 1030 options.current.setHours(0,0,0,0); 1031 var cnt, 1032 pickmeup = $(tpl.wrapper); 1033 this.pickmeup = pickmeup; 1034 if (options.class_name) { 1035 pickmeup.addClass(options.class_name); 1036 } 1037 var html = ''; 1038 for (i = 0; i < options.calendars; i++) { 1039 cnt = options.first_day; 1040 html += tpl.head({ 1041 prev : options.prev, 1042 next : options.next, 1043 day : [ 1044 options.locale.daysMin[(cnt++) % 7], 1045 options.locale.daysMin[(cnt++) % 7], 1046 options.locale.daysMin[(cnt++) % 7], 1047 options.locale.daysMin[(cnt++) % 7], 1048 options.locale.daysMin[(cnt++) % 7], 1049 options.locale.daysMin[(cnt++) % 7], 1050 options.locale.daysMin[(cnt++) % 7] 1051 ] 1052 }); 1053 } 1054 $this.data('pickmeup-options', options); 1055 for (i in options) { 1056 if (['render', 'change', 'before_show', 'show', 'hide'].indexOf(i) != -1) { 1057 options[i] = options[i].bind(this); 1058 } 1059 } 1060 options.binded = { 1061 fill : fill.bind(this), 1062 update_date : update_date.bind(this), 1063 click : click.bind(this), 1064 show : show.bind(this), 1065 forced_show : forced_show.bind(this), 1066 hide : hide.bind(this), 1067 update : update.bind(this), 1068 clear : clear.bind(this), 1069 prev : prev.bind(this), 1070 next : next.bind(this), 1071 get_date : get_date.bind(this), 1072 set_date : set_date.bind(this), 1073 destroy : destroy.bind(this) 1074 }; 1075 options.events_namespace = '.pickmeup-' + (++instances_count); 1076 pickmeup 1077 .on('click touchstart', options.binded.click) 1078 .addClass(views[options.view]) 1079 .append(html) 1080 .on( 1081 $.support.selectstart ? 'selectstart' : 'mousedown', 1082 function(e){ 1083 e.preventDefault(); 1084 } 1085 ); 1086 options.binded.fill(); 1087 if (options.flat) { 1088 pickmeup.appendTo(this).css({ 1089 position : 'relative', 1090 display : 'inline-block' 1091 }); 1092 } else { 1093 pickmeup.appendTo(document.body); 1094 // Multiple events support 1095 var trigger_event = options.trigger_event.split(' '); 1096 for (i = 0; i < trigger_event.length; ++i) {
1097 trigger_event[i] += options.events_namespace; 1098 } 1099 trigger_event = trigger_event.join(' '); 1100 $this.on(trigger_event, options.binded.show); 1101 } 1102 }); 1103 }; 1104})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.