'use strict'; $(() => { autoFlashHeight(); const limitObj = (function(list){ const limits = list; let curLimit; const getConditionHtml = function(condition) { const html = []; for (const c of condition) { if (html.length > 0) html.push('
'); html.push(`${c.alias} ${c.operation} ${c.value}`); } return html.join(''); }; const getOptionHtml = function(option) { const html = []; html.push('', option.limit ? '' : '',''); html.push(``, getConditionHtml(option.condition), ''); html.push(`${ (!option.limit ? '--' : option.lower + '%') } `); html.push(`${ (!option.limit ? '--' : option.upper + '%') } `); html.push(`${option.hint}`); html.push(` `); return html.join(''); }; const loadLimitOption = function(limit) { const html = []; for (const option of limit.options) { html.push(``, getOptionHtml(option), ''); } $('#limitOptions').html(html.join('')); }; const setCurLimit = function(limit) { curLimit = limit; if (!limit) return; loadLimitOption(limit); $('dd[limitId]').removeClass('bg-warning'); $(`dd[limitId=${curLimit.limit_id}]`).addClass('bg-warning'); }; const getCurLimit = function() { return curLimit; }; const getLimitCaptionHtml = function(limit) { return `
${limit.name}
` + '
'; }; const getLimitHtml = function(limit) { const html = []; html.push(`
`, getLimitCaptionHtml(limit), '
'); return html.join(''); }; const addLimit = function() { postData('/setting/limit/save', {add: 1}, function(result) { limits.push(result.add); $('#limit-list').append(getLimitHtml(result.add)); }); }; const renameLimit = function(limit_id, name) { postData('/setting/limit/save', { update: { limit_id, name }}, function(result){ const limit = limits.find(x => { return x.limit_id === result.update.limit_id; }); limit.name = result.update.name; limit.options.forEach(x => { x.name === limit.name; }); $(`dd[limitId=${limit.limit_id}]`).html(getLimitCaptionHtml(limit)); }); }; const delLimit = function(limit_id){ postData('/setting/limit/save', {del: limit_id}, function(result) { $(`dd[limitId=${result.del}]`).remove(); }); }; const addOption = function(option) { const limit = limits.find(x => { return x.limit_id === option.limit_id; }); limit.options.push(option); loadLimitOption(curLimit); }; const updateOption = function(option) { const limit = limits.find(x => { return x.limit_id === option.limit_id; }); const orgOption = limit.options.find(x => { return x.id === option.id; }); if (!orgOption) return; _.assignIn(orgOption, option); loadLimitOption(curLimit); }; const delOption = function (option) { const limit = limits.find(x => { return x.limit_id === option.limit_id; }); const index = limit.options.findIndex(x => { return x.id === option.id; }); limit.options.splice(index, 1); loadLimitOption(curLimit); }; if (limits.length > 0) setCurLimit(limits[0]); return { setCurLimit, getCurLimit, addLimit, delLimit, renameLimit, addOption, updateOption, delOption, } })(limitList); const optionSaveObj = (function(){ let limit_id, limit_name; const addCheckHtml = function(check) { const html = []; html.push('', `${check.alias} ${check.operation} ${check.value}`, '', ''); $('#loc-list').append(html.join('')); }; $('#loc-add').click(() => { const field = $('#loc-field').val(); const locType = locInfo[field]; if (!locType) { toastr.warning('未知判断条件'); return; } const data = JSON.parse(JSON.stringify(locType)); data.operation = $('#loc-operation').val(); data.value = $('#loc-value').val(); if (!data.value) { toastr.warning('请输入判断值'); return; } data.value = _.toInteger(data.value); if (_.isNil(data.value) || data.value < 0) { toastr.warning('判断值仅限0、正整数'); return; } addCheckHtml(data); }); $('body').on('click', 'a[name=loc-del]', function() { $(this).parent().parent().remove(); }); const showOptionModal = function(option, limit) { limit_id = option ? option.limit_id : limit.limit_id; limit_name = option ? option.name : limit.name; $('#lo-id').val(option ? option.id : ''); $('#lo-limit')[0].checked = option && option.limit; $('#lo-lower').val(option ? option.lower : 0); $('#lo-upper').val(option ? option.upper : 100); $('#lo-hint').val(option ? option.hint : ''); $('#loc-list').html(''); if (option) { for (const c of option.condition) { addCheckHtml(c); } } $('#save-limit-option').modal('show'); }; const getOptionData = function() { const data = {}; try { data.limit = $('#lo-limit')[0].checked; data.lower = parseInt($('#lo-lower').val()); data.upper = parseInt($('#lo-upper').val()); data.hint = $('#lo-hint').val(); data.condition = []; const clist = $('td[locinfo]'); for (const c of clist) { const info = c.getAttribute('locinfo').split('&^&'); data.condition.push({ alias: info[0], field: info[1], check: info[2], operation: info[3], value: parseInt(info[4]), }); } const id = $('#lo-id').val(); if (id) { data.id = id; } else { data.limit_id = limit_id; } return data; } catch(err) { if (!data.lower) { toastr.warning('计量下限请输入0-100的整数'); return; } if (!data.upper) { toastr.warning('计量上限请输入0-100的整数'); return; } if (data.hint.length > 20) { toastr.warning('超限提示过长,请再精简'); return; } toastr.warning('判断条件错误'); return; } }; return { show: showOptionModal, data: getOptionData, } })(); $('#add-lo').click(() => { optionSaveObj.show(null, limitObj.getCurLimit()); }); $('body').on('click', 'a[name=editLimitOption]', function() { const optionId = $(this).parent().parent().attr('optionId'); const curLimit = limitObj.getCurLimit(); if (!curLimit) return; const option = curLimit.options.find(x => { return x.id == optionId; }); optionSaveObj.show(option, curLimit); }); $('body').on('click', 'a[name=delLimitOption]', function() { const optionId = $(this).parent().parent().attr('optionId'); const curLimit = limitObj.getCurLimit(); if (!curLimit) return; const option = curLimit.options.find(x => { return x.id == optionId; }); if (curLimit.options.length === 1) { toastr.warning('当前配置,仅剩最后一个判断,如需删除,请直接删除配置'); return; } postData('/setting/limit/saveOption', { del: option }, function (result) { if (result.del) limitObj.delOption(result.del); $('#save-limit-option').modal('hide'); }); }); $('#save-lo-ok').click(function() { const loData = optionSaveObj.data(); const updateData = {}; if (loData.id) { updateData.update = loData; } else { updateData.add = loData; } postData('/setting/limit/saveOption', updateData, function (result) { if (result.add) limitObj.addOption(result.add); if (result.update) limitObj.updateOption(result.update); $('#save-limit-option').modal('hide'); }) }); $('body').on('click', '.table-file', function(e) { if (this.getAttribute('renaming') === '1') return; if (e.target.tagName === 'A' || e.target.tagName === 'I' || e.target.tagName === 'INPUT') return; const limitId = this.getAttribute('limitId'); const limit = limitList.find(x => { return x.limit_id === limitId; }); limitObj.setCurLimit(limit); }); $('body').on('mouseenter', ".table-file", function(){ $(this).children(".btn-group-table").css("display","block"); }); $('body').on('mouseleave', ".table-file", function(){ $(this).children(".btn-group-table").css("display","none"); }); $('body').on('click', 'a[name=renameLimit]', function(e){ $(this).parents('.table-file').attr('renaming', '1'); $(`#${this.getAttribute('aria-describedby')}`).remove(); const limitId = $(this).parents('.table-file').attr('limitId'); const limit = limitList.find(x => { return x.limit_id === limitId; }); if (!limit) return; const html = []; html.push(`
`); html.push(''); $(`.table-file[limitId=${limitId}]`).html(html.join('')); e.stopPropagation(); }); $('body').on('click', 'a[name=renameOk]', function(){ const limitId = $(this).parents('.table-file').attr('limitId'); const newName = $(this).parents('.table-file').find('input').val(); limitObj.renameLimit(limitId, newName); $(this).parents('.table-file').attr('renaming', '0'); }); $('body').on('click', 'a[name=renameCancel]', function() { $(this).parents('.table-file').attr('renaming', '0'); const limitId = $(this).parents('.table-file').attr('limitId'); const limit = limitList.find(x => { return x.limit_id === limitId; }); if (!limit) return; const html = []; html.push(`
${limit.name}
`); html.push(''); $(`.table-file[limitId=${limitId}]`).html(html.join('')); }); $('body').on('click', 'a[name=delLimit]', function(e){ e.stopPropagation(); const limitId = $(this).parents('.table-file').attr('limitId'); limitObj.delLimit(limitId); }); $('#addLimit').click(function() { limitObj.addLimit(); }); });