d5f201f6b3809540c725a77442311e2476350e99
[sdc/sdc-workflow-designer.git] /
1 /*******************************************************************************
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  *******************************************************************************/
12 import {Component, Input, OnChanges, SimpleChanges, OnDestroy} from "@angular/core";
13 import {IntermediateCatchEvent} from "../../../model/workflow/intermediate-catch-event";
14 import {TimerEventDefinitionType} from "../../../model/workflow/timer-event-definition";
15 import {TranslateService} from "@ngx-translate/core";
16
17 @Component({
18     selector: 'wfm-intermediate-catch-event',
19     templateUrl: 'intermediate-catch-event.component.html',
20 })
21 export class IntermediateCatchEventComponent implements OnChanges, OnDestroy {
22     @Input() public node: IntermediateCatchEvent;
23
24     public checkedType: string;
25     public timeType = TimerEventDefinitionType;
26     public timeDate: string;
27     public timeDuration: any = {
28         year: 0,
29         month: 0,
30         day: 0,
31         hour: 0,
32         minute: 0,
33         second: 0
34     };
35
36     public locale: any;
37     private localeZh: any = {
38         firstDayOfWeek: 0,
39         dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
40         dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
41         monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
42         monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
43         dateFns: null,
44         confirm: '确定'
45     };
46
47     constructor(private translate: TranslateService) {
48         this.locale = translate.currentLang.indexOf('zh') > -1 ? this.localeZh : undefined;
49     }
50
51     public ngOnChanges(changes: SimpleChanges): void {
52         if (this.node && this.node.timerEventDefinition) {
53             this.checkedType = this.node.timerEventDefinition.type;
54         }
55         if (!this.checkedType) {
56             this.checkedType = this.timeType[this.timeType.timeDuration];
57         } else if (this.checkedType === this.timeType[this.timeType.timeCycle]) {
58             // 兼容老数据,把timeCycle转为timeDuration
59             this.checkedType = this.node.timerEventDefinition.type = this.timeType[this.timeType.timeDuration];
60         }
61
62         if (this.node.timerEventDefinition.timeDuration) {
63             this.transformStringToTimeDuration();
64         } else if (this.node.timerEventDefinition.timeCycle) {
65             // 兼容老数据,把timeCycle转为timeDuration
66             const timeCycleArray = this.node.timerEventDefinition.timeCycle.split('/');
67             this.node.timerEventDefinition.timeDuration = timeCycleArray.length > 1 ? timeCycleArray[1] : timeCycleArray[0];
68             this.node.timerEventDefinition.timeCycle = '';
69             this.transformStringToTimeDuration();
70         } else if (this.node.timerEventDefinition.timeDate) {
71             this.transformISOToDate();
72         }
73     }
74
75     public ngOnDestroy(): void {
76         if (this.checkedType === this.timeType[this.timeType.timeDuration]) {
77             this.transformTimeDurationToString();
78         } else {
79             this.timeDateChange();
80         }
81     }
82
83     private transformStringToTimeDuration(): void {
84         // R5/P1Y2M10DT2H30M
85         // P1Y3M5DT6H7M30S
86         this.timeDuration.year = this.splitTimeDuration('P', 'Y');
87         this.timeDuration.month = this.splitTimeDuration('Y', 'M');
88         this.timeDuration.day = this.splitTimeDuration('M', 'D');
89         this.timeDuration.hour = this.splitTimeDuration('D', 'H');
90         this.timeDuration.minute = this.splitTimeDuration('H', 'M');
91         this.timeDuration.second = this.splitTimeDuration('M', 'S');
92     }
93
94     private splitTimeDuration(startKey: string, endKey: string): number {
95         const timeDuration = this.node.timerEventDefinition.timeDuration;
96         let start = timeDuration.indexOf(startKey);
97         let end = timeDuration.indexOf(endKey);
98         if (startKey === 'H' || endKey === 'S') {
99             start = timeDuration.lastIndexOf(startKey);
100             end = timeDuration.lastIndexOf(endKey);
101         }
102         const result = parseInt(timeDuration.substring(start + 1, end));
103         if (isNaN(result)) {
104             return 0;
105         } else {
106             return result;
107         }
108     }
109
110     public timeTypeChange(type: string): void {
111         this.checkedType = type;
112         const timer = this.node.timerEventDefinition;
113         timer.type = type;
114         timer.timeCycle = '';
115         timer.timeDate = '';
116         timer.timeDuration = '';
117     }
118
119     public transformTimeDurationToString(): void {
120         // R5/P1Y2M10DT2H30M
121         this.node.timerEventDefinition.timeDuration = 'P'
122             + this.timeDuration.year + 'Y'
123             + this.timeDuration.month + 'M'
124             + this.timeDuration.day + 'D'
125             + 'T' + this.timeDuration.hour + 'H'
126             + this.timeDuration.minute + 'M'
127             + this.timeDuration.second + 'S';
128     }
129
130     private transformISOToDate(): void {
131         this.timeDate = new Date(this.node.timerEventDefinition.timeDate).toString();
132     }
133
134     private pad(value: number): string {
135         let result = value.toString();
136         if (result.length === 1) {
137             result = '0' + result;
138         }
139         return result;
140     }
141
142     private transformDateToISO(date: Date): string {
143         return date.getFullYear() + '-' + this.pad(date.getMonth() + 1) + '-' + this.pad(date.getDate()) + 'T'
144             + this.pad(date.getHours()) + ':' + this.pad(date.getMinutes()) + ':' + this.pad(date.getSeconds());
145     }
146
147     public timeDateChange(): void {
148         // 2007-04-05T12:30-02:00
149         if (this.timeDate) {
150             const date = new Date(this.timeDate);
151             this.node.timerEventDefinition.timeDate = this.transformDateToISO(date);
152             console.log(this.node.timerEventDefinition.timeDate);
153         }
154     }
155 }