liuxiaolong
2019-05-06 3e0536f508aad49f743e7bfabca34e3980a1b6e2
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
<template>
  <div class="flex-center timeline-out-content">
    <fTemplate @click.native="goLeft()">
      <i class="d-block fas fa-chevron-left timeline-out-icon"></i>
    </fTemplate>
    <div :style="`float:left;width:${(1/(items.length))*100}%`" v-for='(item,key) in items' :key="key">
      <div class="timeline-container" @click="selectLocal(item)">
        <div :class="[item.isActive?'timeitem-circle-isActive':'timeitem-circle']" @click.native="goLeft()">
          <div class="timeitem-daynum flex-center">
            {{item.monthfirst?item.showMonth:item.day}}
            <b-btn v-if="item.monthfirst" variant="primary" class="btn-circle-timeline" @click="closeMonth(item,key)">
              <i v-if="!item.close" class="ion d-block ion-md-arrow-dropleft"></i>
              <i v-if="item.close" class="ion d-block ion-md-arrow-dropright"></i>
            </b-btn>
          </div>
        </div>
      </div>
    </div>
    <fTemplate @click.native="goRight()">
      <i class="d-block fas fa-chevron-right timeline-out-icon"></i>
    </fTemplate>
  </div>
</template>
<script>
export default {
  name: 'light-timeline',
  components: { },
  props: {
    dataList: {
      type: Array,
      default: () => {}
    },
    activeColor: {
      type: String,
      default: '#26B4FF'
    },
    indexVal: {
      default: 0
    }
  },
  data() {
    return {
      presetReg: /defaultColor|orange|yellow/,
      // indexVal: 0
      items: [
      ],
      total: [],
      //  记录左侧第一个数据的索引
      leftIndex: 0,
      //  记录右侧第一个数据的索引
      ringhtIndex: 0
    }
  },
  watch: {
    dataList: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          console.log(newVal, 'timeline newVal')
          this.total = this.dataList
          this.InitTimeline()
        }
      },
      deep: true
    }
  },
  methods: {
    getColorClass(color) {
      return this.presetReg.test(color) ? color : ''
    },
    getStyle(item) {
      const color =
        item.color && item.color !== '' ? item.color : this.activeColor
      if (!this.presetReg.test(color)) {
        return item.type === 'star'
          ? this.makeStarColor(color)
          : this.makeCircleColor(color)
      }
      return {}
    },
    makeCircleColor(color) {
      return { border: `2px solid ${color}` }
    },
    makeStarColor(color) {
      return { stroke: color }
    },
    clickTimerIteam(json, key) {
      // this.indexVal = key
      this.$emit('clickBackfn', json, key)
    },
 
    goLeft() {
      if (this.leftIndex !== 0) {
        console.log(this.leftIndex, 'this.leftIndex')
        if (this.items[0].monthfirst) {
          let arr1 = this.total.filter((i, index) => {
            if (i.monthfirst && i.close) {
              this.$set(i, 'index', index)
              return true
            }
          })
          if (arr1.length > 0 && arr1) {
            arr1.forEach((i, index) => {
              let beforeNode = this.total[this.leftIndex - 1]
              if (i.year === beforeNode.year && i.month === beforeNode.month) {
                let arr2 = this.total.filter((temp, index) => {
                  if (temp.monthfirst) {
                    return true
                  } else {
                    if (temp.year === i.year && temp.month === i.month) {
                      return false
                    } else {
                      return true
                    }
                  }
                })
                /** 过滤掉之前收起来的天数 */
                if (arr2.length >= 5) {
                  this.items = arr2.slice(arr1.length - 1, arr1.length + 4)
                  this.ringhtIndex = this.items[4].index
                } else {
                  this.items = arr2
                }
                this.leftIndex = this.items[0].index
              }
            })
          } else {
            /** 左移一个,直接通过索引左移 */
            this.items = this.total.slice(this.leftIndex - 1, this.ringhtIndex)
            this.leftIndex = this.leftIndex - 1
            this.ringhtIndex = this.ringhtIndex - 1
          }
        } else {
          /** 左移一个,直接通过索引左移 */
          this.items = this.total.slice(this.leftIndex - 1, this.ringhtIndex)
          this.leftIndex = this.leftIndex - 1
          this.ringhtIndex = this.ringhtIndex - 1
        }
      }
    },
    goRight() {
      console.log(this.total, '======this.total')
      if (this.ringhtIndex !== (this.total.length - 1)) {
        /** 判断第一个数据是否是收起状态 */
        console.log(this.items, '======this.items')
        if (this.items[0].close) {
          /** 如果是修改左侧索引位置 */
          this.total.filter((i, index) => {
            if (i.year === this.items[0].year && i.month === this.items[0].month) {
              if (this.total[index + 1].year === this.items[0].year && this.total[index + 1].month === this.items[0].month) {
                return true
              } else {
                this.leftIndex = index
                return false
              }
            }
          })
        }
        console.log(this.leftIndex, 'this.leftIndex', this.ringhtIndex)
        /** 不考虑那么多,直接根据索引往右移动一个 */
        this.items = this.total.slice(this.leftIndex + 1, this.ringhtIndex + 2)
        this.leftIndex = this.leftIndex + 1
        this.ringhtIndex = this.ringhtIndex + 1
      }
    },
    closeMonth(data, key) {
      console.log(data, '收起某月')
      if (data && data !== undefined) {
        if (data.close) {
          /** close为真说明是要展开 */
          this.items = this.total.slice(this.leftIndex, this.leftIndex + 5)
          this.ringhtIndex = this.items[4].index
          this.$set(data, 'close', false)
        } else {
          /** 从总数据中过滤掉该月的数据 */
          let arr = this.total.filter((e) => {
            if (e.monthfirst) {
              return true
            } else {
              if (e.year === data.year && e.month === data.month) {
                return false
              } else {
                return true
              }
            }
          })
          console.log(arr, '收起某月')
          if (arr.length <= 5) {
            this.items = arr
          } else {
            this.items = arr.slice(0, 5)
          }
          const findEle = (element) => {
            return element.Id === this.items[this.items.length - 1].Id
          }
          /** 右边的索引 */
          this.ringhtIndex = this.total.findIndex(findEle)
          // console.log(this.total.findIndex(findEle), '=======最后一个元素的索引')
          this.$set(data, 'close', true)
        }
      }
    },
    selectLocal(data) {
      this.$nextTick(() => {
        this.$emit('changParam', data, 'tlList')
      })
    },
    /** 初始化时间轴,给左右索引赋值 */
    InitTimeline() {
      this.items = this.total.slice(0, 5)
      if (this.items.length !== 0) {
        if (this.items.length <= 5) {
          this.leftIndex = 0
          this.ringhtIndex = this.items.length - 1
        } else {
          this.leftIndex = 0
          this.ringhtIndex = 4
        }
      }
    },
    /** 设置一段时间显示在时间轴的前面 */
    setStartTime(arr) {
      // arr应该是一个按照时间排序的有序数组
      if (arr !== undefined && arr.length !== 0) {
        let startNode = arr[0]
        let endNode = arr[arr.length - 1]
        this.total.forEach((item, index) => {
          if (item.id === startNode.id) {
            this.leftIndex = index
          }
          if (item.id === endNode.id) {
            this.ringhtIndex = index
          }
        })
      }
      this.items = [...this.total.slice(this.leftIndex + 1, this.ringhtIndex + 1)]
    }
 
  },
  mounted() {
    this.total = this.dataList
    this.total.forEach((i, index) => {
      this.$set(i, 'index', index)
    })
    console.log(this.total, 'this.total')
    this.InitTimeline()
  }
}
</script>
<style lang="scss">
$font-family-no-number: 'Chinese Quote', -apple-system, BlinkMacSystemFont,
  'Segoe UI', Roboto, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  'Helvetica Neue', Helvetica, Arial, sans-serif;
$font-family: 'Helvetica Neue For Number', $font-family-no-number;
$tag-family: Consolas, Menlo, Courier, monospace;
 
$defaultColor: #ccc;
$orange: #ed9153;
$yellow: #fbd157;
$font-color: #606c76;
$font-size: 14px;
$left-pad: 5rem;
$item-pad: 1rem;
$icon-size: 16px;
$sm-icon-size: 10px;
$lg-icon-size: 24px;
 
$colors: (
  defaultColor: $defaultColor,
  orange: $orange,
  yellow: $yellow
);
 
@mixin square($val) {
  width: $val;
  height: $val;
}
 
@mixin circle($diameter, $color) {
  @include square($diameter);
  border-radius: $diameter/2 + 2px;
  background: white;
  border: 2px solid $color;
}
 
@mixin line-point($val) {
  box-sizing: border-box;
  position: absolute;
  left: -$item-pad;
  margin-left: -($val/2) + 1px;
  z-index: 1;
}
 
@mixin make-circle($diameter, $color) {
  @include line-point($diameter);
  @include circle($diameter, $color);
}
 
@mixin make-star($val, $color) {
  @include line-point($val);
  @include square($val);
  margin-top: -($val/6);
  path {
    stroke: $color;
    stroke-width: 4px;
    fill: white;
  }
}
 
:root {
  --defaultColor: $defaultColor;
  --orange: $orange;
  --yellow: $yellow;
}
 
.line-container {
  color: $font-color;
  font-size: $font-size;
  font-family: $font-family;
  box-sizing: border-box;
  position: relative;
  list-style: none;
  margin: 0.5rem;
  width: 32px;
  padding-left: $left-pad + 1rem;
  &::after {
    position: absolute;
    content: '';
    left: $left-pad;
    top: 0;
    width: 100%;
    height: 2px;
    background-color: $defaultColor; //lighten($defaultColor, 90%);
  }
 
  .line-item {
    padding: $item-pad;
    position: relative;
  }
  .item-circle {
    cursor: pointer;
    margin-top: -6px;
    transition: 0.5s all;
    @include make-circle($icon-size, $defaultColor);
    @each $key, $val in $colors {
      &.#{$key} {
        border: 2px solid $val;
      }
    }
  }
  .item-star {
    cursor: pointer;
    @include make-star($lg-icon-size, $defaultColor);
    @each $key, $val in $colors {
      &.#{$key} {
        path {
          stroke: $val;
        }
      }
    }
  }
  .item-tag {
    position: absolute;
    left: -($left-pad + 1rem);
    width: 65px;
    text-align: center;
    color: lighten($font-color, 20%);
    font-size: $font-size/1.2;
    cursor: pointer;
  }
  .item-content {
    white-space: pre-line;
    &.star {
      font-weight: bold;
      font-size: $font-size * 1.1;
    }
    cursor: pointer;
  }
}
 
.line{
  width: 32px;
  height: 2px;
  border: 2px solid $font-color;
}
 
.timeline-container{
  width: 100%;
  height: 2px;
  border: 2px solid #cccccc;
 
  .timeitem-circle{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: white;
    border: 2px solid #cccccc;
    margin-left: 50px;
    margin-top: -7px;
  }
  .timeitem-circle-isActive{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: rgb(240, 8, 8);
    border: 2px solid #cccccc;
    margin-left: 50px;
    margin-top: -7px;
  }
  .timeitem-daynum{
    margin-top: -20px;
    width: 110px;
    margin-left: -55px;
  }
}
 
.timeline-out-icon{
  margin-top: 0px;
}
.btn-circle-timeline{
  width: 15px;
  height: 15px;
  text-align: center;
  padding: 0;
}
</style>