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
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
<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="flex-center" @click="selectLocal(item)">
        <div v-if="key !== 0" class="flex-center" style="width:50px;margin-right:3px;">
          <!-- <i class="d-block fas fa-long-arrow-alt-right"></i> -->
          <div class="linex"></div>
          <i class="d-block fas fa-caret-right timeline-out-icon"></i>
        </div>
 
        <div :class="['d-block fas fa-map-marker-alt',item.isActive?'timeline-icon-active':'timeline-out-icon']"></div>
        <span :class="['timeline-address',item.isActive?'timeline-icon-active':'timeline-out-icon']">{{item.address}}</span>
      </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: [
        {
          id: '001',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校教学楼',
          isActive: true,
          content: '2019-01-07',
          year: '2019',
          month: '01',
          day: '01',
          close: false
        },
        {
          id: '002',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校宿舍楼',
          isActive: false,
          content: '2019-01-07',
          year: '2019',
          month: '01',
          day: '02',
          close: false
        },
        {
          id: '003',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校东门',
          isActive: true,
          content: '2019-01-07',
          year: '2019',
          month: '02',
          day: '01',
          close: false
        },
        {
          id: '004',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校操场',
          isActive: false,
          content: '2019-01-07',
          year: '2019',
          month: '02',
          day: '02',
          close: false
        },
        {
          id: '005',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校食堂',
          isActive: true,
          content: '2019-01-07',
          year: '2019',
          month: '03',
          day: '01',
          close: false
        },
        {
          id: '006',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校篮球场',
          isActive: false,
          content: '2019-01-07',
          year: '2019',
          month: '03',
          day: '02',
          close: false
        },
        {
          id: '007',
          type: 'circle',
          color: 'red',
          tag: '测试',
          address: '学校哪路',
          isActive: true,
          content: '2019-01-07',
          year: '2019',
          month: '04',
          day: '01',
          close: false
        }
      ],
      //  记录左侧第一个数据的索引
      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() {
      console.log(this.leftIndex, 'this.leftIndex')
      if (this.leftIndex !== 0) {
        this.items = [...this.total.slice(this.leftIndex - 1, this.leftIndex), ...this.items]
        this.items = [...this.items.splice(0, 4)]
        this.leftIndex -= 1
        this.ringhtIndex -= 1
      }
    },
    goRight() {
      if (this.ringhtIndex !== 0) {
        if (this.total.length - this.ringhtIndex > 0) {
          this.items = [...this.items, ...this.total.slice(this.ringhtIndex, this.ringhtIndex + 1)]
          this.items = [...this.items.splice(1, 5)]
          this.leftIndex += 1
          this.ringhtIndex += 1
        }
      }
    },
    closeMonth(data, index) {
      console.log(data)
      if (data && data !== undefined) {
        if (data.close) {
          /** close为真说明是要展开 */
          let arr1 = this.items.splice(0, index + 1)
          console.log(arr1, 'arr1')
          let arr2 = this.total
          let arr3 = this.items
          arr1.forEach(item => {
            arr2 = arr2.filter(i => (i.id !== item.id))
            if (item.close) {
              arr2 = arr2.filter(i => (i.month === data.month))
            }
          })
          console.log(arr2, 'arr2')
          let arr4 = [...arr1, ...arr2]
          arr4.forEach(item => {
            arr3 = arr3.filter(i => (i.id !== item.id))
          })
          console.log(arr3, 'arr3')
          let arr5 = [...arr4, ...arr3]
          console.log(arr5, 'arr5')
          this.items = arr5.splice(0, 4)
          this.$set(data, 'close', false)
          console.log(this.items, '展开的数据')
        } else {
          /** 为假说明是收起 */
          /** 从轴过滤出来本月的数据 */
          let arr1 = this.items.filter(item => (item.month !== data.month || item.id === data.id))
          /** 从总数据中过滤出本月数据 */
          let arr2 = this.total.filter(item => (item.month !== data.month && item.id !== data.id))
          arr1.forEach(item => {
            arr2 = arr2.filter(i => (i.id !== item.id))
            if (item.close) {
              arr2 = arr2.filter(i => (i.month !== item.month))
            }
          })
          let arr3 = [...arr1, ...arr2]
          this.items = arr3.slice(0, 4)
          this.$set(data, 'close', true)
          console.log(this.items, '收起的数据')
        }
      }
    },
    InitTimeline() {
      this.items = this.total.slice(0, 4)
      if (this.items.length !== 0) {
        if (this.items.length <= 4) {
          this.leftIndex = 0
          this.ringhtIndex = this.items.length
        } 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)]
    },
    selectLocal(data) {
      // this.items.forEach(i => {
      //   if (i.videoReqNum !== data.videoReqNum) {
      //     this.$set(i, 'isActive', false)
      //   }
      // })
      this.$emit('changParam', data, 'tlList2')
    }
 
  },
  mounted() {
    this.total = this.dataList
    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 #26b4ff;
 
  .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;
  // color: #26b4ff;
}
.timeline-icon-active{
  color: rgb(240, 8, 8);
}
.btn-circle-timeline{
  width: 15px;
  height: 15px;
  text-align: center;
  padding: 0;
}
.timeline-address{
  margin-left: 5px;
}
.linex{
  width: 100%;
  height: 2px;
  border: 2px solid #26b4ff;
}
</style>