Last active
August 29, 2015 14:25
-
-
Save webcyou/cf4e87cece86806d0d0a to your computer and use it in GitHub Desktop.
TypeScript & AngularJS SelectSchedule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Author: Daisuke Takayama | |
* Website:http://www.webcyou.com/?p=5915 | |
*/ | |
'use strict'; | |
var e = eval, global = e('this'); | |
module Schedule { | |
/* | |
* コントローラーで利用するモデル | |
*/ | |
class ScheduleModel { | |
private ADD_SELECT_YEAR_NUM: number = 3; | |
private select_year_list: string[] = []; | |
private select_month_list: string[] = []; | |
constructor( | |
) { | |
this.setYearList(); | |
} | |
/** | |
* setter 日付け | |
*/ | |
public setYearList() { | |
var year = new Date().getFullYear(), | |
i; | |
for(i = 0; i < this.ADD_SELECT_YEAR_NUM; i++) { | |
this.select_year_list.push(String(year + i)); | |
} | |
this.setMonthList(); | |
} | |
public setMonthList() { | |
var i: number; | |
for(i = 1; i < 13; i++) { | |
this.select_month_list.push(String(i)); | |
} | |
} | |
/** | |
* getter 日付け | |
*/ | |
public getYearList(): string[] { | |
return this.select_year_list; | |
} | |
public getMonthList(): string[] { | |
return this.select_month_list; | |
} | |
public getDayList(year: number, month: number) { | |
var dayLength = new Date(year, month, 0).getDate(), | |
dayList: string[] = [], | |
i; | |
for(i = 0; i < dayLength; i++) { | |
dayList.push(String(1 + i)); | |
} | |
return dayList; | |
} | |
/** | |
* getter 日付け 初期 number | |
*/ | |
public getInitYearNum(): number { | |
var year = new Date().getFullYear(); | |
return this.select_year_list.indexOf(String(year)); | |
} | |
public getInitMonthNum(): number { | |
var month = new Date().getMonth() + 1; | |
return this.select_month_list.indexOf(String(month)); | |
} | |
public getInitDay(): string { | |
var nowDate = new Date(), | |
dayList = this.getDayList(nowDate.getFullYear(), nowDate.getMonth() + 1), | |
initNumber = dayList.indexOf(String(nowDate.getDate())); | |
return dayList[initNumber]; | |
} | |
} | |
export var app: ng.IModule = angular.module('Schedule'); | |
/** | |
* MainController内のangularjsから参照するパラメータ | |
*/ | |
interface MainControllerScope extends ng.IScope { | |
yearList: string[]; | |
monthList: string[]; | |
scheduleYear: string; | |
scheduleMonth: string; | |
scheduleDay: string; | |
getDayList: Function; | |
} | |
app | |
.factory('scheduleModel', [ | |
function () { | |
var scheduleModel: ScheduleModel = new ScheduleModel(); | |
return scheduleModel; | |
} | |
]) | |
.controller('MainController', [ | |
'$scope', | |
'scheduleModel', | |
function ( | |
$scope: MainControllerScope, | |
model: ScheduleModel | |
) { | |
$scope.yearList = model.getYearList(); | |
$scope.monthList = model.getMonthList(); | |
$scope.getDayList = (year: string, month: string): string[] => { | |
return model.getDayList(parseInt(year, 10), parseInt(month, 10)); | |
}; | |
$scope.scheduleYear = $scope.yearList[model.getInitYearNum()]; | |
$scope.scheduleMonth = $scope.monthList[model.getInitMonthNum()]; | |
$scope.scheduleDay = model.getInitDay(); | |
} | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment