-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.timeSelector.js
491 lines (460 loc) · 16.2 KB
/
jquery.timeSelector.js
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
480
481
482
483
484
485
486
487
488
489
490
491
/**
* jquery.timeSelect
* 时间选择插件
*
* @autor abel
* @createAt 2017/06/16
*
* @version
* 2.0.0
*
* 1.2.0
*
* 1.0.1
* fix
* 1.0.0
*
*/
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';
var template = [
'<div class="timeselector-box">',
'<div class="timeselector-box-hd">',
'<div class="timeselector-tips">支持shift选择和拖动选择</div>',
'<div class="timeitem-example-wrap"><span class="timeitem-example is-active"></span>投放时间段 <span class="timeitem-example"></span>暂停时间段</div>',
'</div>',
'<div class="timeselector-box-bd">',
'<table class="timeselector-table">',
'<thead>',
'<tr>',
'<th rowspan="2" class="first-head">',
'<div>时间</div>',
'<span class="line"></span>',
'<div>星期</div>',
'</th>',
'<th colspan="12">上午</th>',
'<th colspan="12">下午</th>',
'</tr>',
'<tr>',
];
for (var i = 0; i < 24; i++) {
template.push('<th>' + i + '</th>')
}
Array.prototype.push.apply(template, [
'</tr>',
'</thead>',
'<tbody></tbody>',
'</table>',
'<div class="select-mask"></div>',
'</div>',
'<div class="timeselector-box-ft">',
'<div><button target="workday">工作日</button><button target="weekend">周末</button><button target="selectAll">全选</button><button target="reverse">反选</button><button target="clear">清空</button></div>',
'<div><dl class="timeselector-select-result">',
'<dt>已选择日期:</dt>',
'<dd></dd>',
'</dl></div>',
'</div>',
'</div>'
]);
var weekMap = ['一', '二', '三', '四', '五', '六', '天'];
function TimeSelector(el, options) {
this.$container = $(el);
this.options = options;
this.init();
}
TimeSelector.version = "1.0.0";
/**
* 计算区间
* @example
* [0,1,2,3]
* [
* [0,3]
* ]
*
* [0,1,2,3,5,6,7]
* [
* [0,3],
* [5,7]
* ]
*
* [0,2,3,5,6,7]
* [
* [0,0],
* [2,3],
* [5,7]
* ]
* @param {Array} array 区间组数
* @return {Array[Array]} 返回分段的区间数组
*/
TimeSelector.timeRange = function(array) {
var result = []
var i, length = array.length,
currentAtt;
// array.sort(function(a, b) {
// return a > b
// });
result.push([array[0], array[0]])
currentAtt = result[result.length - 1];
for (i = 1; i < length; i++) {
if (currentAtt[1] + 1 == array[i]) {
currentAtt[1] = array[i]
} else {
result.push([array[i], array[i]]);
currentAtt = result[result.length - 1]
}
}
return result;
}
/**
* 拟人化
* 将指定数据转化成指定话术
*
* 例如:
* {
* 0:[1,2,3,4,5,6,7,..,24]
* }
*
* 星期天:全天
*
* {
* 0:[1,2,3,4,5,10,12,...,24]
* }
*
* 星期天:1点~5点 10点~24点
*
* @param {Object} data
* @return {String}
*/
TimeSelector.humanize = function(data) {
var weekMap = ['天', '一', '二', '三', '四', '五', '六'];
function voice(arr) {
var i = 0,
j = 0,
length = arr.length,
len,
result = [];
$.each(arr, function(_, item) {
if (item[0] == item[1]) {
result.push(item[0] + ':00')
} else {
result.push(item[0] + ':00' + '~' + arr[i][1] + ':00')
}
})
return result;
}
var list = [];
for (var k in data) {
//console.log('星期' + weekMap[k]);
list.push('星期' + weekMap[k] + ':' + voice(TimeSelector.timeRange(data[k])).join(','))
}
return list;
}
TimeSelector.prototype = {
init: function() {
var $template = $(template.join(''));
var tr = '';
var maxRow = 7,
maxCol = 24;
var i, j;
for (i = 0; i < maxRow; i++) {
tr += '<tr><td>星期' + weekMap[i] + '</td>';
for (j = 0; j < maxCol; j++) {
tr += '<td class="time-item" data-time-row="' + i + '" data-time-col="' + j + '"></td>';
}
tr += '</tr>';
}
$template.find('tbody').html(tr);
this.$container.append($template);
this.$box = this.$container.find('.timeselector-table tbody');
this.set(this.options.defaultData);
if (this.options.editMode) {
this.bind();
}
},
change: function() {
this.humanize();
},
bind: function() {
var lastClickItem, //上次点击的
isShift;
var that = this;
var isMousedown = false,
lastMousedownItem,
fisrtItemPos = {},
lastItemPos = {},
isMove = false;
var colWidth = this.$box.find('.time-item')[0].offsetWidth,
colHeight = this.$box.find('.time-item')[0].offsetHeight;
this.$box
.on('click', '.time-item', function() {
var $this = $(this);
$this.toggleClass('is-active');
//是否按下shift
if (isShift && lastClickItem) {
var lastRow = lastClickItem.attr('data-time-row'),
lastCol = parseInt(lastClickItem.attr('data-time-col'), 10),
row = $this.attr('data-time-row'),
col = parseInt($this.attr('data-time-col'), 10);
var selectData = that._selectRange(lastRow, lastCol + 1, row, col + 1);
that.set(selectData);
lastClickItem = null;
}
lastClickItem = $this;
isMousedown = false;
$('.select-mask').hide();
that.change();
})
.on('mousedown', '.time-item', function(e) {
var $target = $(e.target)
$('.select-mask').css($target.offset());
isMousedown = true;
lastMousedownItem = $(this);
fisrtItemPos.row = $target.attr('data-time-row');
fisrtItemPos.col = $target.attr('data-time-col');
fisrtItemPos.x = e.pageX;
fisrtItemPos.y = e.pageY;
})
.on('mousemove', '.time-item', function(e) {
if (isMousedown) {
if (Math.abs(fisrtItemPos.x - e.pageX) > colWidth ||
Math.abs(fisrtItemPos.y - e.pageY) > colHeight) {
isMove = true;
var $target = $(e.target);
// var col = $target.attr('data-time-col'),
// row = $target.attr('data-time-row');
//移动记录最后一次坐标 以免移出无法计算文字
lastItemPos.row = $target.attr('data-time-row');
lastItemPos.col = $target.attr('data-time-col');
$('.select-mask')
.show()
.css({
top: Math.min(fisrtItemPos.row, lastItemPos.row) * colHeight + that.$container.find('thead').height(),
left: Math.min(fisrtItemPos.col, lastItemPos.col) * colWidth + that.$box.find('td')[0].getBoundingClientRect().width,
width: (Math.abs(fisrtItemPos.col - lastItemPos.col) + 1) * colWidth,
height: (Math.abs(fisrtItemPos.row - lastItemPos.row) + 1) * colHeight,
});
} else {
isMove = false;
}
}
})
.on('mouseup', '.time-item', function(e) {
var $target = $(e.target);
if (isMousedown && isMove) {
var selectData = that._selectRange(fisrtItemPos.row, parseInt(fisrtItemPos.col) + 1, $target.attr('data-time-row'), parseInt($target.attr('data-time-col')) + 1);
$('.select-mask').hide();
that.set(selectData);
}
isMousedown = false;
});
this.$container.on('click', 'button[target]', function() {
var target = $(this).attr('target');
that[target] && that[target]();
})
$(document).on('keydown', function(event) {
isShift = event.which == 16;
}).on('keyup', function(event) {
isShift = false;
}).on('mouseup', function(e) {
var $target = $(e.target);
if (isMousedown && isMove) {
var selectData = that._selectRange(fisrtItemPos.row, parseInt(fisrtItemPos.col) + 1, lastItemPos.row, parseInt(lastItemPos.col) + 1);
$('.select-mask').hide();
that.set(selectData);
}
isMousedown = false;
});
},
/**
* 获得选择范围
* @example
* same line 同一行
* _selectRange(0,2,0,5);
* 0-0
* 2-5
* return { 0:[ 2,3,4,5 ] }
*
* enjambment 跨行
* _selectRange(0,2,3,23);
* 0-3
* 2-23
* return {
* 0:[ 2,3,4,5,...,23],
* 1:[ 2,3,4,5,...,23],
* 2:[ 2,3,4,5,...,23],
* 3:[ 2,3,4,5,...,23]
* }
*
* reverse selection 反向选择
* _selectRange(3,5,0,2);
* 0-3
* 2-5
* return {
* 0:[ 2,3,4,5],
* 1:[ 2,3,4,5],
* 2:[ 2,3,4,5],
* 3:[ 2,3,4,5]
* }
* @param {Number} lastRow 上一行
* @param {Number} lastCol 上一列
* @param {Number} row 行号
* @param {Number} col 类号
* @return {object} 范围对象
*/
_selectRange: function(lastRow, lastCol, row, col) {
if (!lastRow || !lastCol || !row || !col) {
return;
}
var _lastRow = Math.max(lastRow, row),
_row = Math.min(lastRow, row),
_lastCol = Math.max(lastCol, col),
_col = Math.min(lastCol, col);
var selectData = {},
currentCol;
do {
currentCol = _col;
selectData[_row + 1] = []
do {
selectData[_row + 1].push(currentCol - 1);
}
while (currentCol++ != _lastCol);
} while (_row++ != _lastRow)
return selectData;
},
clear: function() {
this.$box.find('tr td').removeClass('is-active');
this.change();
return this;
},
selectAll: function() {
this.$box.find('tr td.time-item').addClass('is-active');
this.change();
return this;
},
/**
* 反选
* @return {[type]}
*/
reverse: function() {
this.$box.find('.time-item').toggleClass('is-active');
this.change();
return this;
},
//周末
weekend: function() {
this.$box.find('tr .time-item').removeClass('is-active');
this.$box.find('tr').eq('5').find('.time-item').addClass('is-active');
this.$box.find('tr').eq('6').find('.time-item').addClass('is-active');
this.change();
return this;
},
//工作日
workday: function() {
this.$box.find('tr .time-item').removeClass('is-active');
this.$box.find('tr').eq('0').find('.time-item').addClass('is-active');
this.$box.find('tr').eq('1').find('.time-item').addClass('is-active');
this.$box.find('tr').eq('2').find('.time-item').addClass('is-active');
this.$box.find('tr').eq('3').find('.time-item').addClass('is-active');
this.$box.find('tr').eq('4').find('.time-item').addClass('is-active');
this.change();
return this;
},
/**
* 设置选中状态
* @param {Object} data
* @param {Boolean} force 是否强制覆盖,默认 false,true则清空
*
*/
set: function(data, force) {
var k, i, len, tr;
if (force) {
this.clear();
}
for (k in data) {
tr = this.$box.find('tr').eq(k == 0 ? 6 : k - 1);
if (data[k]) {
len = data[k].length;
for (i = 0; i < len; i++) {
tr.find('td').eq(data[k][i] + 1).addClass('is-active');
}
}
};
this.change();
return this
},
get: function() {
var result = {},
_result = []
this.$box.find('tr').each(function(index) {
_result = [];
$(this).find('td.time-item').each(function(_index) {
if ($(this).hasClass('is-active')) {
_result.push(_index);
}
});
if (_result.length != 0) {
result[index == 6 ? 0 : index + 1] = _result;
}
});
return result;
},
disable: function(data) {
if (data) {
this._each(data, function(item, key, index) {
this.$box.find('tr').eq(key).find('td').eq(item).addClass('is-disable')
});
} else {
this.$box.find('.time-item').addClass('is-disable')
}
return this;
},
/**
* 遍历所有节点的方法
* @privte
* @param {Function} callback 回调方法
* item,key,index
*/
_each(data, callback) {
var data = data || this.get();
for (var k in data) {
$.each(data[k], function(item, index) {
callback && callback(item, k, index);
})
}
return this;
},
humanize: function() {
var list = TimeSelector.humanize(this.get());
this.$container.find(".timeselector-select-result dd").remove();
var $dd = $.map(list, function(item) {
return '<dd>' + item + '</dd>'
})
this.$container.find(".timeselector-select-result").append($dd);
return this;
}
}
$.fn.timeSelector = function() {
var _timeSelector = this.data('$timeSelector')
if (!_timeSelector) {
var option = arguments[0],
options = $.extend({}, $.fn.timeSelector.defaults, option);
_timeSelector = new TimeSelector(this[0], options);
this.data('$timeSelector', _timeSelector);
}
return _timeSelector
}
$.fn.timeSelector.humanize = TimeSelector.humanize;
$.fn.timeSelector.defaults = {
defaultData: {}, //默认数据
editMode: true, //编辑模式
};
$.fn.timeSelector.Constructor = TimeSelector;
}));