var widgets = []; var editor = CodeMirror(document.getElementById('code_batch'), { mode: "htmlmixed", lineNumbers: true, matchBrackets: true, lineWrapping: true, cursorHeight: 0.85, includeWidgets: true, autofocus: false, placeholder: 'Enter up to 50 tracking numbers, each per line.', scrollbarStyle: "simple", lineNumberFormatter: function (line) { return line + '.' } }); editor.on('changes', function (editor) { $('.tm-batch-action-group').addClass('tm-batch-action-on') clearTimeout(waiting) waiting = setTimeout(updateHints(editor), 50) }) editor.setSize("100%", 176); /*$('#tracknumber').click(function () { $('#input_tracking_box').hide(); $('#codemirror_box').css('position', 'relative') $('#codemirror_box').css('left', 'unset') editor.focus(); })*/ var waiting = setTimeout(updateHints(editor), 100) function tryNumber(number) { var ev = editor.getValue(); if (ev) { var v = ev.split("\n") for (let i = 0; i < v.length; i++) { if (v[i] === number) { return; } } editor.setValue(ev + '\n' + number) } else { editor.setValue(number) } } function updateHints(editor) { var t, a = this, o = editor.getValue(); if (o === '') { return; } var s = o.split("\n") editor.operation(function () { for (var t = 0; t < widgets.length; ++t) { editor.removeLineWidget(widgets[t]); } var divs = $('.CodeMirror-lines .CodeMirror-code .CodeMirror-line'); var divs2 = $('.CodeMirror-lines .CodeMirror-code .CodeMirror-gutter-wrapper .CodeMirror-linenumber'); widgets.length = 0 if (o) { for (var a = 0; a < s.length; a++) { var e = createClearBox(editor, a) var r = editor.addLineWidget(a, e, { coverGutter: false, noHScroll: true }); widgets.push(r) if (s[a] !== '') { var patt = /^[0-9a-zA-Z-]{4,50}$/i; if (!patt.test(s[a])) { divs.eq(a).addClass('worn-number') divs2.eq(a).addClass('worn-number') } else { divs.eq(a).removeClass('worn-number') divs2.eq(a).removeClass('worn-number') } } } } }); t = editor.getScrollInfo(); a = editor.charCoords({ line: editor.getCursor().line + 1, ch: 0 }, "local").top; t.top + t.clientHeight - 28 < a && editor.scrollTo(null, a - t.clientHeight + 28) } function CheckWornNumber() { var worn_div = $('.CodeMirror-lines .CodeMirror-code .worn-number'); var worn_div2 = $('.CodeMirror-lines .CodeMirror-code .CodeMirror-gutter-wrapper .CodeMirror-linenumber .worn-number'); var sg = editor.getValue(); if (worn_div.length > 0 && sg !== '') { $('#worn-tips').show() $('#try-track').hide(); } else { if (sg === '') { $('.tm-batch-action-group').removeClass('tm-batch-action-on') worn_div.each(function (index, item) { $(item).removeClass('worn-number') }) worn_div2.each(function (index, item) { $(item).removeClass('worn-number') }) }else{ var newVar = sg.split("\n"); if (newVar.length>50) { clearInterval(timerC) var text = newVar.splice(0,50); var finalText = text[49] text = text.join("\n"); editor.setValue(text); editor.setCursor(50,finalText.length) setInterval(CheckWornNumber) } } $('#worn-tips').hide(); $('#try-track').show() } } var timerC = setInterval(CheckWornNumber, 1000) function actionClear() { editor.setValue("") editor.focus() } function actionformat() { let pattern = /[0-9a-z-_]{5,}/ig; if (!editor.getValue()) return false; let match = editor.getValue().match(pattern); if (!match || match.length <= 0) return editor.setValue(""); match = Array.from(new Set(match)); let numbers = []; match.forEach(element => { let m = element.match(/[0-9A-Za-z]/g); if (/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(element)) return false if (m && m.length >= 1) numbers.push(element); }); if (numbers.length === 0) return editor.setValue(""); editor.setValue(numbers.join('\n')); } function isActionformat() { let is = false let val = editor.getValue(); let nums = val.split('\n') if (nums.length > 0) { nums.some(v => { if (!/^[0-9a-zA-Z-]{0,50}$/.test(v)) { return is = true }else { return is = false } }) } return is } function createClearBox(n, o) { var a = document; var t = a.createElement("a") return t.style = "display:block", t.className = 'delete-number', t.setAttribute("href", "javascript:void(0)"), t.setAttribute("data-index", o), // t.setAttribute("events", "{C:功能操作,A:全局-单行删除单号}"), t.setAttribute("title", 'Remove'), t.appendChild(a.createElement("span")).innerHTML = '', t.onclick = function () { var t = this.getAttribute("data-index") , a = -1; if (null !== t) { t -= 0; var e = n.getValue().split("\n"); if (0 === t) e.shift(); else if (o === e.length - 1) e.pop(), t--; else { var r = e.slice(0, t) , i = e.slice(t + 1); e = r.concat(i), a = n.getScrollInfo().top } n.setValue(e.join("\n")), n.setCursor(t, n.getLine(t).length), -1 < a && n.scrollTo(null, a) } n.focus() }, t }