DateDirective.js 31.8 KB
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
(function(){
	angular.module("app")
		.directive("myDatepicker",["ToolService","DateService","UtilService",myDatepicker])
		.directive("singleDatepicker",singleDatepicker)
		.directive("myDateweek",['UtilService',myDateweek])
		.directive("myDatemonth",['UtilService',myDatemonth]);

		var tempVersion = new Date().getTime();
		/*日期 多日期*/
		function myDatepicker(ToolService,DateService,UtilService) {
		    var today = Date.today();
		    var defaultOpsn = {
		        abbrWeeks: ['日', '一', '二', '三', '四', '五', '六'],
		        format: 'YYYY-MM-DD',
		        min: null,
		        max: today,
		        start: today,
		        end: today,
		        current: today,
		        months: 2, //默认显示2个面板
		        chooseCls: 'is-choose',
		        todayCls: 'is-today'
		    };

		    return {
		        restrict: 'EA',
		        transclude: true,
		        replace:true,
		        templateUrl: '/template/common/datepicker.html?r='+tempVersion,
		        scope: {
		            dateName:'@', //日期传递名称
		            noMax:"@", //是否有最大日期限制
		            rangeDays:"@", //最多日期范围 -1:不需要控制范围
		            rangeDays2:"=", //最多日期范围,动态
		            initDay:"=", //初始化显示日期
		            maxDay:'@',  //最大日期选择,默认为今天,-1为昨天。。。
		            maxDay2:"=", //同上,动态传参
		            operationSel:"=", //当前可否操作
		            min:"="
		        },
		        link: function(scope, elem, attrs) {
		        	scope.maxDay = scope.maxDay || scope.maxDay2;

		        	var ddd = -1;
		        	if(UtilService.isNullStr(scope.maxDay)){
		        		ddd = 0;
		        	}
		        	var cacheDates = {
		            	'yesday':DateService.getYesDay(),
		            	'd7':DateService.getLastDays(-7,ddd),
		            	'd14':DateService.getLastDays(-14,ddd),
		            	'd30':DateService.getLastDays(-30,ddd),
		            	'-w1':DateService.getWeekRangeDate(-1,ddd),
		            	'w0':DateService.getWeekRangeDate(0,ddd),
		            	'-m1':DateService.getMonthRangeDate(-1,ddd),
		            	'm0':DateService.getMonthRangeDate(0,ddd)
		            }
		            scope.$watch("operationSel",function(n){
		             	if(angular.isUndefined(scope.operationSel)){
			             	scope.operSel = true;
			            }
			            else{
			            	scope.operSel = n;
			            }
		            });
		            //根据选择日期设置相对时间范围flag
		            scope.getRelativeFlag = function(startDate,endDate){
		            	var relFlag = "";
		            	for(var k in cacheDates){
	                		var range = cacheDates[k];
	                		if(startDate == range.startDate && endDate == range.endDate){
	                			relFlag = k;
	                			break;
	                		}
	                	}
	                	scope.selDayflag = relFlag;
		            }
		            //把初始化日期赋值到日期框里面
		            scope.$watch("initDay",function(n){
		            	if(angular.isDefined(n) && n.startDate){
		            		if(!scope.options.start.isSameDay(new Date(n.startDate)) || !scope.options.end.isSameDay(new Date(n.endDate))){
		            			scope.getRelativeFlag(n.startDate,n.endDate);
			            		scope.options.start = scope.completeStrat = new Date(n.startDate);
			            		scope.options.end = scope.completeEnd = new Date(n.endDate);
		            		}
		            	}
		            });
		        	scope.selme = false;
		        	scope.$watch("rangeDays+rangeDays2",function(e){
		        		var rds = scope.rangeDays || scope.rangeDays2;
		        		if(angular.isDefined(rds)){
			        		scope.rgDays = Number(rds);
			        	}else{
			        		scope.rgDays = 31;
			        	}
		        	});
		        	
		            var maxTime = today,emitName = scope.dateName || "dateCustos";
		            if(scope.maxDay && scope.maxDay == -1){
		            	maxTime = Date.yesterday();
		            }
		            //如果最大可选日期不是当天,则在当前年上加上100年
		            if(scope.noMax && scope.noMax == 'true'){
		                var future = Date.today();
		                future.setFullYear(today.getFullYear()+100);
		                maxTime = future;
		            }

		            var minTime = "";
		            scope.$watch("min",function(n){
		            	if(angular.isDefined(n)){
		            		minTime = new Date(n).addDays(-1);
		            	}
		            });
		            var defaultOps = UtilService.cloneJSON(defaultOpsn);
		            var setOption = function() {
		                defaultOps.start = maxTime;
		                defaultOps.end = maxTime;
		                defaultOps.max = maxTime;
		                defaultOps.current = today;
		                scope.options = angular.extend(defaultOps, scope.options || {});
		                //清除时分秒毫秒
		                scope.options.start.clearTime();
		                scope.options.end.clearTime();

		                scope.completeStrat = scope.options.start;
		                scope.completeEnd = scope.options.end;
		                scope.customStrat = scope.options.start.toFormat(scope.options.format);
		                scope.customEnd = scope.options.end.toFormat(scope.options.format);

		                //最后一个日期面板的日期
		                scope.panelEndDate = Date.getFirstDay(scope.options.end); 
		            };
		            setOption();
		            var render = function(date) {
		                var panels = [],
		                    months = scope.options.months,
		                    date = (date || scope.panelEndDate).clone();
		                for (var p = 0; p < months; p++) {
		                    var weeks = [],
		                        dateOfPanel = date.clone().addMonths(-(months - 1 - p)), //每个日期面板的日期
		                        firstDay = Date.getFirstDay(dateOfPanel), //该月的第一天
		                        firstWeekDay = firstDay.getDay(), //该月的一个星期
		                        daysOfMonth = Date.getDaysInMonth(dateOfPanel.getFullYear(), dateOfPanel.getMonth()), //该月有几天
		                        rowNums = Math.ceil((firstWeekDay + daysOfMonth) / 7), //该月日历的行数
		                        curDate = firstDay.clone().addDays(-firstWeekDay); //循环遍历的日期

		                    for (var i = 0; i < rowNums; i++) {
		                        // weeks.push([])
		                        var days = [];
		                        for (var j = 0; j < 7; j++) {
		                            var tmp = curDate.clone();
		                            days.push({
		                                day: tmp,
		                                choose: tmp.between(scope.options.start, scope.options.end),
		                                other: tmp.getMonth() != dateOfPanel.getMonth(),
		                                today: Date.isSameDay(tmp, today) //是否是今日
		                            });
		                            curDate = curDate.addDays(1);
		                        }

		                        weeks.push(days);
		                    }

		                    panels.push({
		                        date: dateOfPanel,
		                        weeks: weeks
		                    });
		                }
		                //可选最大时间不是今天,禁用"今天,昨天,最近七天按钮,默认显示最大天数"
		                // if (maxTime !== today) {
		                //     scope.complete();
		                //     $(elem).parent().prevAll().hide();
		                // } else {
		                    $(elem).parent().prevAll().show();
		                // }
		                scope.panels = panels;
		                scope.wstyle = {"width":(months*274 + 80)+"px"};
		            };
		            //初始化面板上日期的css
		            scope.cellClass = function(cell) {
		            	if(cell.other){
		            		return "other-month";
		            	}
		            	else if(cell.day.isSameDay(scope.options.start) || cell.day.isSameDay(scope.options.end)){
		            		return "start_end";
		            	}
		            	else if(cell.day.between(scope.options.start, scope.options.end)){
		            		return "choose";
		            	}
		            	else if(cell.day.isSameDay(scope.options.current) && cell.day.isSameDay(maxTime)){
		            		return "is-today";
		            	}
		            	else if(cell.day.isAfter(maxTime) || (minTime!='' && cell.day.isBefore(minTime))){
		            		return "disabled";
		            	}
		            	else{
		            		return "";
		            	}
		            };
		            //切换月份
		            scope.changeMonth = function(index, date, num) {
		                scope.dateMonth = scope.panelEndDate.addMonths(num);
		                render(scope.dateMonth);
		            };
		            //点击显示或隐藏日期面板
		            scope.showDateTable = function() {
		            	if(!scope.operSel){return false;}

		            	var curDateWrap = $(elem).find(".dateTabRange");
		            	$(".dateTabRange").each(function(){
		            		if($(this)!==curDateWrap){
		            			$(this).addClass("ng-hide");
		            		}
		            	});
		            	if(curDateWrap.hasClass("ng-hide")){
		            		scope.showFlag = true;
		            		curDateWrap.removeClass("ng-hide")
		            	}
		            	else{
		            		scope.showFlag = false;
		            		curDateWrap.addClass("ng-hide")
		            	}

		                // if (typeof scope.showFlag == 'undefined') {
		                // 	$(".dateTabRange").addClass("ng-hide");
		                //     scope.showFlag = true;
		                // } else if (scope.showFlag == true) {
		                //     scope.showFlag = false;
		                // } else if (scope.showFlag == false) {
		                // 	$(".dateTabRange").addClass("ng-hide");
		                //     scope.showFlag = true;
		                // }
		            }


		            //选择具体日期
		            var chooseCounts = 0;
		            scope.chooseDate = function(cell) {
		                //是否大于今天
		                // if (cell.day.isAfter(today)) { 
		                //     return;
		                // }
		                //是否大于可选择最大时间
		                if (cell.day.isAfter(maxTime)) { 
		                    return;
		                }
		                //是否小于可选择最小时间
		                if (minTime!='' && cell.day.isBefore(minTime)) { 
		                    return;
		                }
		                if (chooseCounts >= 2) chooseCounts = 0;
		                chooseCounts++;

		                if (chooseCounts === 1) {
		                    scope.options.end = scope.options.start = cell.day;
		                } else if (chooseCounts === 2) {
		                    cell.day.isAfter(scope.options.start) ? scope.options.end = cell.day : scope.options.start = cell.day;
		                }
		            };
		            
		            scope.setRelDate = function(flag){
		            	
		            	var daterange = cacheDates[flag];
		            	if(scope.rgDays!=-1){
		            		var diff = diffDate(daterange.startDate,daterange.endDate);
			            	if(diff > scope.rgDays){
			            		ToolService.showTips("日期范围不能大于"+scope.rgDays+"天");
			            		return false;
			            	}
		            	}

		            	scope.selDayflag = flag;
		            	if(minTime!='' && new Date(daterange.startDate).isBefore(minTime)){
		            		ToolService.showTips("超出最小可选日期限制了");
		            		return;
		            	}
		            	else{
		            		scope.options.start = new Date(daterange.startDate);
		            	}

		            	if(minTime!='' && new Date(daterange.endDate).isBefore(minTime)){
		            		ToolService.showTips("超出最小可选日期限制了");
		            		return;
		            	}
		            	else{
		            		scope.options.end = new Date(daterange.endDate);
		            	}
		            	
		            	scope.complete(1);
		            }
		            //点击确定,完成日期选择,传递日期
		            scope.complete = function(isrel) {
		            	if(angular.isUndefined(isrel) && scope.rgDays!=-1){
		            		var diff = diffDate(scope.options.start,scope.options.end)
			            	if(diff > scope.rgDays){
			            		ToolService.showTips("日期范围不能大于"+scope.rgDays+"天");
			            		return false;
			            	}
		            	}
		            	
		            	scope.completeStrat = scope.options.start;
		                scope.completeEnd = scope.options.end;

		                var startDate = scope.completeStrat.toFormat(scope.options.format),endDate = scope.completeEnd.toFormat(scope.options.format);
		                if(angular.isUndefined(isrel)){
		                	scope.getRelativeFlag(startDate,endDate);
		                	
		                }
		               	scope.$emit(emitName, {"startDate":startDate,"endDate":endDate,"dateflag":scope.selDayflag});
		                
		                scope.showFlag = false;
		            };
		            //取消选择,关闭面板
		            scope.cancel = function() {
		            	if(scope.options.start != scope.completeStrat || scope.options.end != scope.completeEnd){
		            		scope.options.start = scope.completeStrat;
			                scope.options.end = scope.completeEnd;
			                scope.getRelativeFlag(scope.completeStrat.toFormat(scope.options.format),scope.completeEnd.toFormat(scope.options.format));
		            	}
		            	scope.showFlag = false;
		            };
		            //检测选择日期操作
		            scope.$watch('options.start+options.end', function() {
		            	scope.customStrat = scope.options.start.toFormat(scope.options.format);
		                scope.customEnd = scope.options.end.toFormat(scope.options.format);
		            });
		            render();

		            $(document).on("click",function(){
		                scope.cancel();
		                scope.$apply();
		            });
		        }
		    };
		};

		/*单日期控件*/
		function singleDatepicker() {
		    var today = Date.today();
		    var defaultOps = {
		        abbrWeeks: ['日', '一', '二', '三', '四', '五', '六'],
		        format: 'YYYY-MM-DD',
		        min: null,
		        max: today,
		        start: today,
		        end: today,
		        current: today,
		        months: 1, 
		        chooseCls: 'is-choose',
		        todayCls: 'is-today'
		    };

		    return {
		        restrict: 'EA',
		        transclude: true,
		        replace:true,
		        templateUrl: '/template/common/datepicker.html?r='+tempVersion,
		        scope: {
		            dateName:"@",
		            campareDays:"=",
		            operationSel:"=" //当前可否操作
		        },
		        link: function(scope, elem, attrs) {
		        	scope.$watch("operationSel",function(n){
		             	if(angular.isUndefined(scope.operationSel)){
			             	scope.operSel = true;
			            }
			            else{
			            	scope.operSel = n;
			            }
		            });

		             var maxTime = today,emitName = scope.dateName;
		             var setOption = function() {
		                defaultOps.start = maxTime;
		                defaultOps.end = maxTime;
		                defaultOps.max = maxTime;
		                defaultOps.current = maxTime;
		                scope.options = angular.extend(defaultOps, scope.options || {});
		                //清除时分秒毫秒
		                scope.options.start.clearTime();
		                scope.options.end.clearTime();

		                scope.completeStrat = scope.options.start;
		                scope.completeEnd = scope.options.end;
		                scope.customStrat = scope.options.start.toFormat(scope.options.format);
		                scope.customEnd = scope.options.end.toFormat(scope.options.format);

		                //最后一个日期面板的日期
		                scope.panelEndDate = Date.getFirstDay(scope.options.end); 
		            };
		            setOption();
		            var render = function(date) {
		                var panels = [],
		                    months = scope.options.months,
		                    date = (date || scope.panelEndDate).clone();

		                months = 1;
		                for (var p = 0; p < months; p++) {
		                    var weeks = [],
		                        dateOfPanel = date.clone().addMonths(-(months - 1 - p)), //每个日期面板的日期
		                        firstDay = Date.getFirstDay(dateOfPanel), //该月的第一天
		                        firstWeekDay = firstDay.getDay(), //该月的一个星期
		                        daysOfMonth = Date.getDaysInMonth(dateOfPanel.getFullYear(), dateOfPanel.getMonth()), //该月有几天
		                        rowNums = Math.ceil((firstWeekDay + daysOfMonth) / 7), //该月日历的行数
		                        curDate = firstDay.clone().addDays(-firstWeekDay); //循环遍历的日期

		                    for (var i = 0; i < rowNums; i++) {
		                        // weeks.push([])
		                        var days = [];
		                        for (var j = 0; j < 7; j++) {
		                            var tmp = curDate.clone();
		                            days.push({
		                                day: tmp,
		                                choose: tmp.between(scope.options.start, scope.options.end),
		                                other: tmp.getMonth() != dateOfPanel.getMonth(),
		                                today: Date.isSameDay(tmp, today) //是否是今日
		                            });
		                            curDate = curDate.addDays(1);
		                        }

		                        weeks.push(days);
		                    }

		                    panels.push({
		                        date: dateOfPanel,
		                        weeks: weeks
		                    });
		                }
		                //可选最大时间不是今天,禁用"今天,昨天,最近七天按钮,默认显示最大天数"
		                if (maxTime !== today) {
		                    scope.complete();
		                    $(elem).parent().prevAll().hide();
		                } else {
		                    $(elem).parent().prevAll().show();
		                }
		                scope.panels = panels;
		                scope.wstyle = {"width":months*274+"px"};
		            };
		            scope.cellClass = function(cell) {
		                return cell.other ? "other-month" :
		                    cell.day.between(scope.options.start, scope.options.end) ? "choose" :
		                    cell.day.isSameDay(maxTime) ? "is-today" :
		                    cell.day.isAfter(maxTime) ? "disabled" : '';
		            };
		            scope.changeMonth = function(index, date, num) {
		                scope.dateMonth = scope.panelEndDate.addMonths(num);
		                render(scope.dateMonth);
		            };
		            
		            scope.showDateTable = function() {
		            	if(!scope.operSel){return false;}
		            	
		                if (typeof scope.showFlag == 'undefined') {
		                    scope.showFlag = true;
		                } else if (scope.showFlag == true) {
		                    scope.showFlag = false;
		                } else if (scope.showFlag == false) {
		                    scope.showFlag = true;
		                }
		            }

		            var chooseCounts = 0;
		            scope.chooseDate = function(cell) {
		                //是否大于今天
		                if (cell.day.isAfter(today)) { 
		                    return;
		                }
		                //是否大于可选择最大时间
		                if (cell.day.isAfter(maxTime)) { 
		                    return;
		                }
		                if (chooseCounts >= 2) chooseCounts = 0;
		                chooseCounts++;

		                //如果有对比日期,用所选日期-去对比天数=对比开始日期
		                if(angular.isDefined(scope.campareDays) && scope.campareDays>0){
		                    scope.options.end = cell.day;
		                    scope.options.start = new Date(addDate(cell.day,-scope.campareDays));
		                }else{
		                    scope.options.end = scope.options.start = cell.day;
		                }
		                scope.complete();
		            };
		            scope.complete = function() {
		                scope.completeStrat = scope.options.start;
		                scope.completeEnd = scope.options.end;
		              
		                if(angular.isDefined(scope.campareDays) && scope.campareDays>0){
		                    scope.$emit(emitName, scope.completeStrat.toFormat(scope.options.format) + ' - ' + scope.completeEnd.toFormat(scope.options.format));
		                }else{
		                    scope.$emit(emitName, scope.completeStrat.toFormat(scope.options.format));
		                }
		                scope.showFlag = false;
		            };
		            scope.cancel = function() {
		                scope.options.start = scope.completeStrat;
		                scope.options.end = scope.completeEnd;
		                scope.showFlag = false;
		            };
		            scope.$watch('options.start+options.end', function() {
		                scope.customStrat = scope.options.start.toFormat(scope.options.format);
		                scope.customEnd = scope.options.end.toFormat(scope.options.format);
		            });
		            render();

		            $(document).on("click",function(){
		                scope.cancel();
		                scope.$apply();
		            });
		        }
		    };
		};


	/*日期 选择周*/
	function myDateweek(UtilService) {
	    var today = Date.yesterday();

	    return {
	        restrict: 'EA',
	        transclude: true,
	        replace:true,
	        templateUrl: '/template/common/dateweek.html?r='+tempVersion,
	        scope: {
	            dateName:"@",
	            weeks:"@"
	        },
	        link: function(scope, elem, attrs) {
	            scope.options = {};
	            if(scope.weeks){
	                scope.options.weeks = scope.weeks;
	            }else{
	                scope.options.weeks = 1;
	            }

	            scope.showDateTable = function() {
	                if (typeof scope.showFlag == 'undefined') {
	                    scope.showFlag = true;
	                } else if (scope.showFlag == true) {
	                    scope.showFlag = false;
	                } else if (scope.showFlag == false) {
	                    scope.showFlag = true;
	                }
	            }

	            var nyear = scope.nowyear = today.getFullYear();
	            var selectday = today.clone();

	            var nowW = new Date().getDay();
	            nowW = nowW == 0 ? 7 : nowW;
	            scope.options.start = scope.options.end = {
	                week : getNowWeek(),
	                date : new Date().clone().addDays(7 - nowW),
	                year : scope.nowyear
	            }

	            scope.setWeeklist = function(){
	                scope.weeklist = [];
	                var weeknum = 7;
	                var firstDay = new Date(scope.nowyear, 0, 1),fw = firstDay.getDay();
	                var maxDay = new Date(scope.nowyear, 11, 31),rowweek = [];

	                fw = fw == 0 ? 7 :fw;
	                var endDay = firstDay.clone().addDays(7 - fw),w = 0;
	                if(fw > 0 && fw < 5){ //当年的第一天为周五周六周日并到上一年
	                    w = 1;
	                    rowweek.push({
	                        week : w,
	                        // date : endDay.toFormat("YYYY-MM-DD"),
	                        date : endDay,
	                        year : scope.nowyear
	                    });
	                }

	                while(endDay < maxDay && !maxDay.isSameDay(endDay)){
	                    w ++ ;
	                    endDay = endDay.clone().addDays(7);
	                    rowweek.push({
	                        week : w,
	                        // date : endDay.toFormat("YYYY-MM-DD"),
	                        date : endDay,
	                        year : scope.nowyear
	                    });

	                    if(w % weeknum == 0){
	                        scope.weeklist.push(rowweek);
	                        rowweek = [];
	                    }
	                }
	   
	                var lastDay = new Date(scope.nowyear, 11, 31),lastW = lastDay.getDay();
	                //当年最后一天是周五周六或者周日时,移到下一年
	                if(lastW > 0 && lastW < 4){
	                    if(rowweek.length == 0){
	                        scope.weeklist[scope.weeklist.length-1].pop();
	                    }else{
	                        rowweek.pop();
	                    }
	                }

	                if(rowweek.length > 0){
	                    scope.weeklist.push(rowweek);
	                }
	            }

	            scope.changeYear = function(n){
	                scope.nowyear+=n;
	                scope.setWeeklist();
	            }
	            scope.setWeeklist();

	            var chooseCounts = 0;
	            scope.chooseDate = function(cell) {
	                if(cell.date.clone().addDays(-6).isAfter(today)){
	                    return false;
	                }
	                if(scope.options.weeks == 1){
	                    selectday = cell.date;
	                    scope.options.start = scope.options.end = cell;
	                    scope.$emit(scope.dateName,cell.year+""+getStr(cell.week));
	                    scope.cancel();
	                }else{ //2
	                    if (chooseCounts >= 2) chooseCounts = 0;
	                    chooseCounts++;

	                    if(chooseCounts == 1){
	                        scope.options.start = scope.options.end = cell;
	                    }else{
	                        var preCell = scope.options.start;
	                        if(preCell.date > cell.date){
	                            scope.options.start = cell;
	                        }else{
	                            scope.options.end = cell;
	                        }
	                    }
	                }
	                
	            }
	            scope.complete = function(){
	                var start = scope.options.start.year + "" + UtilService.getStr(scope.options.start.week);
	                var end = scope.options.end.year + "" + UtilService.getStr(scope.options.end.week);
	                scope.$emit(scope.dateName,{start:start,end:end});
	                scope.cancel();
	            }
	            scope.cellClass = function(cell) {
	                if(cell.date.between(scope.options.start.date,scope.options.end.date)){
	                    return "choose";
	                }
	                else if((cell.year == scope.options.start.year && cell.week == scope.options.start.week) || (cell.year == scope.options.end.year && cell.week == scope.options.end.week)){
	            		return "start_end";
	            	}
	                else if(today.between(cell.date.clone().addDays(-6),cell.date)){
	                    return "is-now";
	                }
	                else if(cell.date.isAfter(today)){
	                    return "disabled";
	                }
	                else{
	                    return '';
	                }
	            };
	            scope.cancel = function() {
	                scope.showFlag = false;
	            };

	            $(document).on("click",function(){
	                scope.cancel();
	                scope.$apply();
	            });
	        }
	    };
	};
	/*日期 选择月*/
	function myDatemonth(UtilService) {
	    var today = Date.yesterday();
	    return {
	        restrict: 'EA',
	        transclude: true,
	        replace:true,
	        templateUrl: '/template/common/datemonth.html?r='+tempVersion,
	        scope: {
	            dateName:"@",
	            months:"@"
	        },
	        link: function(scope, elem, attrs) {
	            var nyear = scope.nowyear = today.getFullYear();
	            var selectmonth = scope.nowMonth = today.getMonth() + 1;

	            scope.options = {};
	            if(scope.months){
	                scope.options.months = scope.months;
	            }else{
	                scope.options.months = 1;
	            }
	           
	            scope.showDateTable = function() {
	                if (typeof scope.showFlag == 'undefined') {
	                    scope.showFlag = true;
	                } else if (scope.showFlag == true) {
	                    scope.showFlag = false;
	                } else if (scope.showFlag == false) {
	                    scope.showFlag = true;
	                }
	            }
	            scope.changeYear = function(n){
	                scope.nowyear+=n;
	                scope.setmonthList();
	            }
	            scope.setmonthList = function(){
	                scope.monthList = [];
	                var rowNums = 4,row = [];
	                for(var i=1;i<=12;i++){
	                    row.push({
	                        month : i,
	                        year : scope.nowyear,
	                        date : new Date(scope.nowyear,i-1,1)
	                    });

	                    if(angular.isUndefined(scope.options.start) && i == selectmonth && scope.nowyear == nyear){
	                        scope.options.start = scope.options.end = row.slice(-1)[0];
	                    }

	                    if(i%rowNums == 0){
	                        scope.monthList.push(row);
	                        row = [];
	                    }
	                }
	            }
	            scope.setmonthList();

	            var chooseCounts = 0;
	            scope.chooseDate = function(cell) {
	                if(cell.year > nyear || (cell.month > scope.nowMonth && cell.year == nyear)){
	                    return false;
	                }

	                if(scope.options.months == 1){
	                    selectmonth = cell.month;
	                    scope.options.start = scope.options.end = cell;
	                    scope.$emit(scope.dateName,cell.year+""+ UtilService.getStr(cell.month));
	                    scope.cancel();
	                }else{ //2
	                    if (chooseCounts >= 2) chooseCounts = 0;
	                    chooseCounts++;

	                    if(chooseCounts == 1){
	                        scope.options.start = scope.options.end = cell;
	                    }else{
	                        var preCell = scope.options.start;
	                        if(preCell.year > cell.year || (preCell.year == cell.year && preCell.month > cell.month)){
	                            scope.options.start = cell;
	                        }else{
	                            scope.options.end = cell;
	                        }
	                    }
	                }
	            }
	            scope.complete = function(){
	                var start = scope.options.start.year + "" + UtilService.getStr(scope.options.start.month);
	                var end = scope.options.end.year + "" + UtilService.getStr(scope.options.end.month);
	       
	                scope.$emit(scope.dateName,{start:start,end:end});
	                scope.cancel();
	            }
	            scope.cellClass = function(cell) {
	                if(cell.date.between(scope.options.start.date,scope.options.end.date)){
	                    return "choose";
	                }
	                else if(cell.month == scope.nowMonth && cell.year == nyear){
	                    return "is-today";
	                }
	                else if(cell.year > nyear || (cell.month > scope.nowMonth && cell.year == nyear)){
	                    return "disabled";
	                }
	                else{
	                    return '';
	                }
	            };
	            scope.cancel = function() {
	                scope.showFlag = false;
	            };
	            $(document).on("click",function(){
	                scope.cancel();
	                scope.$apply();
	            });
	        }
	    };
	};
})();