-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex-step.js
More file actions
146 lines (113 loc) · 3.46 KB
/
index-step.js
File metadata and controls
146 lines (113 loc) · 3.46 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
import Ember from 'ember';
import DS from 'ember-data';
const {
get,
set,
computed,
computed: { alias },
Component,
inject: {
service,
},
setProperties,
A,
} = Ember;
export default Component.extend({
ajax: service('ajax'),
patternsLoading: service('patterns-loading'),
logoVisiability: service(),
page: 1,
loadingPatterns: alias('patternsLoading.loadingPatterns'),
patterns: [],
colorIds: A(),
backgroundMargin: { right: 10, left: 10 , top: 0 },
startAt: moment().subtract(14, 'days'), // 7 daysRadius * 2
endAt: moment(),
chartData: computed('[email protected]', 'startAt', 'endAt', 'daysRangeOffset', function() {
const patterns = get(this, 'patterns');
if(!patterns) {
return;
}
const ids = patterns.mapBy('id');
const chartDataPromise = get(this, 'chartDataPromise');
const promise = get(this, 'ajax').request('/charts_pattern', {
data: {
pattern_ids: ids,
start_at: get(this, 'startAt').format('YYYY-MM-DD'),
end_at: get(this, 'endAt').format('YYYY-MM-DD'),
offset: get(this, 'daysRangeOffset') || 1,
}
}).then((data) => data.charts_pattern);
set(chartDataPromise, 'promise', promise);
return chartDataPromise;
}),
init() {
this._super(...arguments);
set(this, 'chartDataPromise', DS.PromiseArray.create({}));
},
didInsertElement() {
this._super(...arguments);
// needs to be loaded
get(this, 'chartData');
set(this, 'logoVisiability.showHeaderPath', false);
const resize = () => {
const selection = this.$();
if (!selection) {
return;
}
const width = selection.width();
set(this, 'indexPageWidth', width);
};
window.addEventListener("resize", resize);
resize();
},
willDestroyElement(){
set(this, '_isDestroyed', true);
},
authorName: computed('[email protected]', function() {
const names = get(this, 'patterns').mapBy('authorName').compact();
const notEmpty = names.length > 0 ? names.filter((i) => i.length > 0).length > 0 : null;
return notEmpty ? get(names, 'firstObject') : 'User';
}),
daysRangeOffset: computed('indexPageWidth', 'daysRange', function() {
const backgroundMargin = get(this, 'backgroundMargin');
const backgroundWidth = get(this, 'indexPageWidth') - backgroundMargin.left - backgroundMargin.right;
const daysRange = get(this, 'daysRange');
return Math.ceil((daysRange*backgroundMargin.left)/backgroundWidth);
}),
daysRange: computed('startAt', 'endAt', function() {
return moment.duration(get(this, 'endAt') - get(this, 'startAt')).asDays();
}),
fixPatternColors(chartData) {
chartData.map((chart) => {
return this.addChartAttributes(chart.series);
});
},
addChartAttributes(series) {
let index = 0;
series.map((i) => { // setColorId()
i.index = index;
index += 1;
})
},
setColorId() {
return get(this, 'colorIds').shiftObject();
},
actions: {
newPattern() {
this.sendAction('onCreate');
},
edit(pattern){
this.sendAction('onEdit', pattern);
},
crossedTheLine() {
set(this, 'loadingPatterns', true);
this.sendAction('onRequest', this.incrementProperty('page', 1));
},
navigate(days) {
const startAt = moment(get(this, 'startAt')).add(days, 'days');
const endAt = moment(get(this, 'endAt')).add(days, 'days');
setProperties(this, { startAt: startAt, endAt: endAt });
},
}
});