couponPeriod.js /** * 优惠券新增、修改、详细页面:时段组件类 * @author 石冬冬 * @date 2017/7/23 */var couponPeriod = { /** * 验证 */ valid: { yes: true,//表示时段输入是否合法 msg: '输入时段不合法,请
/** * 优惠券新增、修改、详细页面:时段组件类 * @author 石冬冬 * @date 2017/7/23 */ var couponPeriod = { /** * 验证 */ valid: { yes: true,//表示时段输入是否合法 msg: '输入时段不合法,请检查是否跨时段区间!' }, /** * 常量 */ Cts: { index: 0, times: [], target: '#applyPeriodLayout' }, /** * 初始化 */ init: function (options) { $.extend(this.Cts, options); var act = coupon.Cts.act; var hasList = $('#applyPeriodLayout').attr('has-list'); var condition = (act == 'add' || (act == 'edit' && Number(hasList || 0)) == 0); if (act == 'edit') { this.Cts.index = Number($("#applyPeriodLayout").attr("data-size")) - 1; } if (condition) { this.funcs.addRow(this.Cts.target, this.Cts.index); } this.funcs.initTimes(); this.initEvts(); }, /** * 事件监听 */ initEvts: function () { var _this = this; var $target = this.Cts.target; /** * 添加事件监听 */ $('input[handle=addRow]').bind('click', function (e) { e.preventDefault(); _this.Cts.index++; _this.funcs.addRow($target, _this.Cts.index); }); }, /** * 函数成员 */ funcs: { /** * 初始化 times数组 */ initTimes: function () { $('#applyPeriodLayout div').each(function (i) { var index = $(this).attr('index'); var startTime = $(this).children('input[id=periodStart' + index + ']').val(); var endTime = $(this).children('input[id=periodEnd' + index + ']').val(); if (startTime && endTime) { couponPeriod.Cts.times.push([startTime, endTime]); } }); console.info("时段times=%s", couponPeriod.Cts.times.toString()); }, /** * 内部成员函数 */ member: { /** * 起止 * @param index */ startSelection: function (index) { return ''; }, /** * 截至 * @param index */ endSelection: function (index) { return ''; } }, /** * 添加 * @param index 索引 */ addRow: function (target, index) { var btn = (index == 0) ? '' : '至 ' + endSelection + ' ' + btn + ''; $(target).append(template) }, /** * 删除 */ revRow: function (index) { $('div.layout div[index=' + index + ']').remove(); couponPeriod.Cts.times[index] = null; }, /** * 验证输入日期的合法性 * @param first 是否是起至时段 (true/false) * @param index 当前行数索引 */ valid: function (first, index) { console.info("时段校验,first=%s,index=%s", first, index); var start = $('#periodStart' + index).val(); var end = $('#periodEnd' + index).val(); var times = couponPeriod.Cts.times; if (start && end && index > times.length) { couponPeriod.Cts.times.push([s, e]); } console.info("输出,times=%s", times.toString()); $(times).each(function (i) { var array = times[i]; if (array && array[0] && array[1]) { var before = array[0], after = array[1]; if (before) { before = before.replace('\:', ''); } if (after) { after = after.replace('\:', ''); } start = start.replace('\:', ''); end = end.replace('\:', ''); if (i != index && before && after) { start = Number(start); end = Number(end); before = Number(before); end = Number(end); var valid = (start > before && start < end) || (end > before && end < after) || (start == before && end == after); if (valid) { couponPeriod.valid.yes = false; $(document).alert(couponPeriod.valid.msg); return false; } } else { couponPeriod.valid.yes = true; } } }); } } };