Vue.component("url-patterns-box", { props: ["value"], data: function () { let patterns = [] if (this.value != null) { patterns = this.value } return { patterns: patterns, isAdding: false, addingPattern: {"type": "wildcard", "pattern": ""}, editingIndex: -1, patternIsInvalid: false, windowIsSmall: window.innerWidth < 600 } }, methods: { add: function () { this.isAdding = true let that = this setTimeout(function () { that.$refs.patternInput.focus() }) }, edit: function (index) { this.isAdding = true this.editingIndex = index this.addingPattern = { type: this.patterns[index].type, pattern: this.patterns[index].pattern } }, confirm: function () { if (this.requireURL(this.addingPattern.type)) { let pattern = this.addingPattern.pattern.trim() if (pattern.length == 0) { let that = this teaweb.warn("请输入URL", function () { that.$refs.patternInput.focus() }) return } } if (this.editingIndex < 0) { this.patterns.push({ type: this.addingPattern.type, pattern: this.addingPattern.pattern }) } else { this.patterns[this.editingIndex].type = this.addingPattern.type this.patterns[this.editingIndex].pattern = this.addingPattern.pattern } this.notifyChange() this.cancel() }, remove: function (index) { this.patterns.$remove(index) this.cancel() this.notifyChange() }, cancel: function () { this.isAdding = false this.addingPattern = {"type": "wildcard", "pattern": ""} this.editingIndex = -1 }, patternTypeName: function (patternType) { switch (patternType) { case "wildcard": return "通配符" case "regexp": return "正则" case "images": return "常见图片文件" case "audios": return "常见音频文件" case "videos": return "常见视频文件" } return "" }, notifyChange: function () { this.$emit("input", this.patterns) }, changePattern: function () { this.patternIsInvalid = false let pattern = this.addingPattern.pattern switch (this.addingPattern.type) { case "wildcard": if (pattern.indexOf("?") >= 0) { this.patternIsInvalid = true } break case "regexp": if (pattern.indexOf("?") >= 0) { let pieces = pattern.split("?") for (let i = 0; i < pieces.length - 1; i++) { if (pieces[i].length == 0 || pieces[i][pieces[i].length - 1] != "\\") { this.patternIsInvalid = true } } } break } }, requireURL: function (patternType) { return patternType == "wildcard" || patternType == "regexp" } }, template: `
[{{patternTypeName(pattern.type)}}] {{pattern.pattern}}  

通配符正则表达式中不能包含问号(?)及问号以后的内容。

` })