riku
2024-02-27 7578c3ff65329b2269f099475eb687e963efac1c
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
var Table = {
  _data: [],
  _tableData: [], //表格内部统一格式的数据
  _curPage: 1, //当前显示页码
  _tPage: 1, // 总页数
  _perPage: 200, //表格分页展示,单页最大数据量
  _factorMode: '',
  _table: '',
  _tableCover: '',
  _selectIndex: ['1', '2', '3'], //第一列为时间,不变;后续默认展示3列数据
  _onClick: {},
  _lastSelectIndex: -1,
 
  // 正序排列比较函数
  _compareDes: function (x, y) {
    if (x < y) {
      return -1;
    } else if (x > y) {
      return 1;
    } else {
      return 0;
    }
  },
  // 倒序排列比较函数
  _compareDesc: function (x, y) {
    if (x < y) {
      return 1;
    } else if (x > y) {
      return -1;
    } else {
      return 0;
    }
  },
 
  /**
   * 新建表格
   * @param {*} elementId
   * @param {*} data
   */
  table: function (elementId, data, factorMode, onclick, onRow, defaultIndex) {
    if (defaultIndex != undefined) {
      this._selectIndex = defaultIndex;
    }
    this._onRow = onRow;
    this._onClick = onclick;
    this._data = [];
    this._data.push.apply(this._data, data);
    this._dataTransform(this._data);
    this._curPage = 1;
 
    this._tPage = Math.ceil(data.length / this._perPage);
 
    var curData = this._data.slice(
      this._perPage * (this._curPage - 1),
      this._perPage * this._curPage
    );
 
    this._table = $('#' + elementId);
    this._table.empty();
    // this._table.addClass('border-table')
 
    var thead = $('<thead></thead>');
    var trH = $('<tr></tr>');
    trH.addClass('tr-head');
 
    var th = $('<th></th>');
    th.append('时间');
    th.prop('num', 'time');
    this.setSortListener(th);
    trH.append(th);
 
    var factorTypes = Object.keys(Util.factorIndex[factorMode]);
    for (let i = 0; i < factorTypes.length; i++) {
      var key = factorTypes[i];
      var n = Util.factorIndex[factorMode][key];
      var th = $('<th></th>');
      th.prop('index', key);
      th.prop('num', i);
      this.setSortListener(th);
      th.append(n);
      trH.append(th);
      if (this._selectIndex.indexOf(key) != -1) {
        th.show();
      } else {
        th.hide();
      }
    }
    thead.append(trH);
 
    this._table.append(thead);
 
    var tbody = $('<tbody></tbody>');
    tbody.addClass('scrollbar');
    this._refreshTbody(curData, tbody);
    this._table.append(tbody);
 
    this._setPage();
    this.refreshHeadWidth(this._table);
  },
  /**
   * 增减表头列
   * @param {*} index
   */
  onChangeTh: function (index, add) {
    var ths = this._table.find('th');
    var tds = this._table.find('td');
    for (let i = 0; i < ths.length; i++) {
      var th = $(ths[i]);
      if (th.prop('index') == index) {
        if (add) {
          th.show();
        } else {
          th.hide();
        }
      }
    }
    for (let i = 0; i < tds.length; i++) {
      var td = $(tds[i]);
      if (td.prop('index') == index) {
        if (add) {
          td.show();
        } else {
          td.hide();
        }
      }
    }
    if (add) {
      this._selectIndex.push(index);
    } else {
      var i = this._selectIndex.indexOf(index);
      this._selectIndex.splice(i, 1);
    }
  },
  /**
   * 插入新数据
   * @param {*} data
   */
  onNewData: function (data, merge) {
    var tbody = $(this._table.find('tbody')[0]);
    if (merge != true) {
      this._data = [];
      tbody.empty();
    }
    this._data.push.apply(this._data, data);
    this._dataTransform(this._data);
    this._refreshTbody(data, tbody);
    // this._table.append(tbody)
    return this._data.length;
  },
 
  _refreshTbody: function (data, tbody) {
    const that = this;
    data.forEach((e, i) => {
      var trC = $('<tr></tr>');
      trC.attr('id', `row-${i}`);
      trC.attr('index', i);
      trC.addClass('tr-content');
      trC.on('onmouseover', function () {
        this.style.backgroundColor = 'var(--select_color)';
      });
      trC.on('onmouseout', function () {
        this.style.backgroundColor = 'transparent';
      });
      trC.on('click', function () {
        that.changeRowColor($(this).attr('index'));
        Table._onClick(e, i + (that._curPage - 1) * that._perPage);
      });
 
      if (this._onRow != undefined) {
        this._onRow(trC, e, this._selectIndex);
      } else {
        var td = $('<td></td>');
        td.append(e.time.split(' ')[1]);
        trC.append(td);
        e.values.forEach((v) => {
          var td = $('<td></td>');
          td.prop('index', v.factorId);
          var tdV = v.factorData.toFixed(1);
          if (v.factorId == '17') {
            tdV = Util.windDir(parseInt(tdV));
          }
          td.append(tdV);
          trC.append(td);
 
          if (this._selectIndex.indexOf(v.factorId) != -1) {
            td.show('fast');
          } else {
            td.hide('fast');
          }
        });
      }
      tbody.append(trC);
    });
    if (data.length == 0) {
      tbody.hide();
    } else {
      tbody.show();
    }
  },
 
  refreshHeadWidth: function (table) {
    var _table = table;
    if (typeof table === 'string') {
      _table = $('#' + table);
    }
    var map = new Map();
    var ths = _table.find('th');
    var trs = $(_table.children('tbody').get(0)).children('tr');
    var tds = $(trs.get(0)).children('td');
    for (let i = 0; i < ths.length; i++) {
      let w = $(tds[i]).innerWidth();
      $(ths[i]).width(function (n, c) {
        if (c > w) {
          // $(tds[i]).width(c - 4*2 - 2)//4: td的padding
          map.set(i, c);
          return c;
        } else {
          return w - 2;
        }
      }); //2: 减去两边边框
    }
    for (const [key, value] of map) {
      for (let i = 0; i < trs.length; i++) {
        const tr = trs[i];
        var tds = $(tr).children('td');
        var td = $(tds[key]);
        var pad = td.outerWidth() - td.width(); //padding + border的宽度
        td.width(value - pad);
      }
    }
  },
 
  _setPage: function () {
    PageHelper.myPageInit({
      pages: Table._tPage,
      currentPage: 1,
      element: '.my-page',
      callback: function (page) {
        Table._curPage = page;
        var curData = Table._data.slice(
          Table._perPage * (page - 1),
          Table._perPage * page
        );
        var tbody = $(Table._table.find('tbody')[0]);
        tbody.empty();
        Table._refreshTbody(curData, tbody);
        Table.refreshHeadWidth(Table._table);
      },
    });
  },
 
  /**
   *  数据结构变换
   *  给数据列表每个值添加下标属性index
   */
  _dataTransform: function (data) {
    for (let i = 0; i < data.length; i++) {
      data[i].index = i;
    }
  },
 
  /**
   * 开启均值计算
   * 根据给定的表格内单元格范围,计算其均值
   */
  _firstData: undefined,
  _lastData: undefined,
  _oldClick: undefined,
  openAvg: function (callback) {
    this.oldClick = this._onClick;
    this._onClick = function (e) {
      if (this._firstData == undefined) {
        this._firstData = e;
      } else if (this._lastData == undefined) {
        this._lastData = e;
        // 计算均值
        var i1 = this._firstData.index;
        var i2 = this._lastData.index;
        var list = this._data.slice(i1, i2 + 1);
        this._firstData = undefined;
        this._lastData = undefined;
        callback(list);
      } else {
        // 此分支理论上不应该出现
        var i1 = this._firstData.index;
        var i2 = this._lastData.index;
        var list = this._data.slice(i1, i2 + 1);
        this._firstData = undefined;
        this._lastData = undefined;
        callback(list);
      }
    };
  },
 
  /**
   * 关闭均值计算
   */
  closeAvg: function () {
    this._onClick = this._oldClick;
    this._oldClick = undefined;
  },
 
  /**
   * 给表头设置点击排序事件
   * sort: 0:代表当前不以此属性排序;
   *         1:代表当前以此属性正序排序;
   *         2:代表当前以此属性倒序排序;
   */
  setSortListener: function (th) {
    th.prop('sort', 0);
    this.lastTh = th;
    th.on('click', function () {
      var sort = th.prop('sort');
      var num = th.prop('num');
 
      // 清除上一次排序列表头的排序图案以及排序顺序
      if (Table.lastTh.prop('num') != num) {
        Table.lastTh.prop('sort', 0);
        var text = Table.lastTh.html();
        var i = text.indexOf('<');
        if (i != -1) {
          text = text.substring(0, i);
          Table.lastTh.html(text);
        }
        Table.lastTh = th;
      }
 
      if (sort == 0) {
        th.prop('sort', 1);
        var text = th.html();
        th.html(`${text}<i class="fa fa-caret-up" aria-hidden="true"></i>`);
      } else if (sort == 1) {
        th.prop('sort', 2);
        var text = th.html();
        var i = text.indexOf('<');
        text = text.substring(0, i);
        th.html(`${text}<i class="fa fa-caret-down" aria-hidden="true"></i>`);
      } else {
        th.prop('sort', 0);
        var text = th.html();
        var i = text.indexOf('<');
        text = text.substring(0, i);
        th.html(text);
      }
 
      Table._data = Table._data.sort(function (x, y) {
        if (sort == 0) {
          if (num == 'time') {
            return Table._compareDes(x.index, y.index);
          } else {
            return Table._compareDes(
              x.values[num].factorData,
              y.values[num].factorData
            );
          }
        } else if (sort == 1) {
          if (num == 'time') {
            return Table._compareDesc(x.index, y.index);
          } else {
            return Table._compareDesc(
              x.values[num].factorData,
              y.values[num].factorData
            );
          }
        } else {
          return Table._compareDes(x.index, y.index);
        }
      });
      var curData = Table._data.slice(
        Table._perPage * (Table._curPage - 1),
        Table._perPage * Table._curPage
      );
      var tbody = $(Table._table.find('tbody')[0]);
      tbody.empty();
      Table._refreshTbody(curData, tbody);
      // Table.refreshHeadWidth(Table._table)
    });
  },
 
  changeRowColor(thisIndex) {
    const index = thisIndex % this._perPage;
    if (this._lastSelectIndex != -1) {
      $(`#row-${this._lastSelectIndex}`).css('background-color', '');
    }
    this._lastSelectIndex = index;
    const row = $(`#row-${index}`);
    row.css('background-color', 'var(--select_color)');
    var tbody = $(this._table.find('tbody')[0]);
    tbody.animate(
      {
        scrollTop: row.outerHeight() * index - 350 + 'px',
      },
      500
    );
    // tbody.scrollTop(0, 100);
    // this._table.scrollTop = 100
  },
 
  /**
   * 定位至索引数据处
   * @param {Number} index 数据索引,从0开始
   */
  locate(index) {
    const page = Math.ceil((index + 1) / this._perPage);
    PageHelper.clickPageFun(page);
    this.changeRowColor(index);
  },
};