Files
EdgeAdmin/web/public/js/components/server/http-cache-refs-config-box.js

277 lines
9.0 KiB
JavaScript
Raw Normal View History

2021-05-24 09:02:55 +08:00
Vue.component("http-cache-refs-config-box", {
props: ["v-cache-refs", "v-cache-config", "v-cache-policy-id", "v-web-id", "v-max-bytes"],
mounted: function () {
let that = this
sortTable(function (ids) {
let newRefs = []
ids.forEach(function (id) {
that.refs.forEach(function (ref) {
if (ref.id == id) {
newRefs.push(ref)
}
})
})
2021-06-09 17:14:31 +08:00
that.updateRefs(newRefs)
that.change()
})
},
2021-05-24 09:02:55 +08:00
data: function () {
let refs = this.vCacheRefs
if (refs == null) {
refs = []
}
let maxBytes = this.vMaxBytes
let id = 0
refs.forEach(function (ref) {
// preset variables
id++
ref.id = id
ref.visible = true
// check max size
if (ref.maxSize != null && maxBytes != null && maxBytes.count > 0 && teaweb.compareSizeCapacity(ref.maxSize, maxBytes) > 0) {
ref.overMaxSize = maxBytes
}
})
2021-05-24 09:02:55 +08:00
return {
refs: refs,
id: id // 用来对条件进行排序
2021-05-24 09:02:55 +08:00
}
},
methods: {
addRef: function (isReverse) {
2021-05-24 09:02:55 +08:00
window.UPDATING_CACHE_REF = null
let height = window.innerHeight
if (height > 500) {
height = 500
}
let that = this
teaweb.popup("/servers/server/settings/cache/createPopup?isReverse=" + (isReverse ? 1 : 0), {
2021-05-24 09:02:55 +08:00
height: height + "px",
callback: function (resp) {
2021-06-09 17:14:31 +08:00
let newRef = resp.data.cacheRef
if (newRef.conds == null) {
return
}
that.id++
2021-06-09 17:14:31 +08:00
newRef.id = that.id
if (newRef.isReverse) {
let newRefs = []
let isAdded = false
that.refs.forEach(function (v) {
if (!v.isReverse && !isAdded) {
newRefs.push(newRef)
isAdded = true
}
newRefs.push(v)
})
if (!isAdded) {
newRefs.push(newRef)
}
that.updateRefs(newRefs)
} else {
that.refs.push(newRef)
}
// move to bottom
var afterChangeCallback = function () {
setTimeout(function () {
let rightBox = document.querySelector(".right-box")
if (rightBox != null) {
rightBox.scrollTo(0, isReverse ? 0 : 100000)
}
}, 100)
}
that.change(afterChangeCallback)
2021-05-24 09:02:55 +08:00
}
})
},
updateRef: function (index, cacheRef) {
2023-02-10 11:15:06 +08:00
window.UPDATING_CACHE_REF = teaweb.clone(cacheRef)
2021-05-24 09:02:55 +08:00
let height = window.innerHeight
if (height > 500) {
height = 500
}
let that = this
teaweb.popup("/servers/server/settings/cache/createPopup", {
height: height + "px",
callback: function (resp) {
resp.data.cacheRef.id = that.refs[index].id
2021-05-24 09:02:55 +08:00
Vue.set(that.refs, index, resp.data.cacheRef)
that.change()
2022-09-03 18:14:34 +08:00
that.$refs.cacheRef[index].updateConds(resp.data.cacheRef.conds, resp.data.cacheRef.simpleCond)
that.$refs.cacheRef[index].notifyChange()
2021-05-24 09:02:55 +08:00
}
})
},
disableRef: function (ref) {
ref.isOn = false
this.change()
},
enableRef: function (ref) {
ref.isOn = true
this.change()
},
2021-05-24 09:02:55 +08:00
removeRef: function (index) {
let that = this
teaweb.confirm("确定要删除此缓存设置吗?", function () {
that.refs.$remove(index)
that.change()
2021-05-24 09:02:55 +08:00
})
},
2021-06-09 17:14:31 +08:00
updateRefs: function (newRefs) {
this.refs = newRefs
if (this.vCacheConfig != null) {
this.vCacheConfig.cacheRefs = newRefs
}
},
2021-05-24 09:02:55 +08:00
timeUnitName: function (unit) {
switch (unit) {
case "ms":
return "毫秒"
case "second":
return "秒"
case "minute":
return "分钟"
case "hour":
return "小时"
case "day":
return "天"
case "week":
return "周 "
}
return unit
},
change: function (callback) {
this.$forceUpdate()
// 自动保存
if (this.vCachePolicyId != null && this.vCachePolicyId > 0) { // 缓存策略
Tea.action("/servers/components/cache/updateRefs")
.params({
cachePolicyId: this.vCachePolicyId,
refsJSON: JSON.stringify(this.refs)
})
.post()
} else if (this.vWebId != null && this.vWebId > 0) { // Server Web or Group Web
Tea.action("/servers/server/settings/cache/updateRefs")
.params({
webId: this.vWebId,
refsJSON: JSON.stringify(this.refs)
})
.success(function (resp) {
if (resp.data.isUpdated) {
teaweb.successToast("保存成功", null, function () {
if (typeof callback == "function") {
callback()
}
})
}
})
.post()
}
},
search: function (keyword) {
if (typeof keyword != "string") {
keyword = ""
}
this.refs.forEach(function (ref) {
if (keyword.length == 0) {
ref.visible = true
return
}
ref.visible = false
// simple cond
if (ref.simpleCond != null && typeof ref.simpleCond.value == "string" && teaweb.match(ref.simpleCond.value, keyword)) {
ref.visible = true
return
}
// composed conds
if (ref.conds == null || ref.conds.groups == null || ref.conds.groups.length == 0) {
return
}
ref.conds.groups.forEach(function (group) {
if (group.conds != null) {
group.conds.forEach(function (cond) {
if (typeof cond.value == "string" && teaweb.match(cond.value, keyword)) {
ref.visible = true
}
})
}
})
})
this.$forceUpdate()
2021-05-24 09:02:55 +08:00
}
},
template: `<div>
<input type="hidden" name="refsJSON" :value="JSON.stringify(refs)"/>
<div>
2021-06-08 22:58:39 +08:00
<p class="comment" v-if="refs.length == 0">暂时还没有缓存条件</p>
<table class="ui table selectable celled" v-show="refs.length > 0" id="sortable-table">
2021-05-24 09:02:55 +08:00
<thead>
<tr>
<th style="width:1em"></th>
<th>缓存条件</th>
<th style="width: 7em">缓存时间</th>
<th class="three op">操作</th>
2021-05-24 09:02:55 +08:00
</tr>
</thead>
<tbody v-for="(cacheRef, index) in refs" :key="cacheRef.id" :v-id="cacheRef.id" v-show="cacheRef.visible !== false">
<tr>
<td style="text-align: center;"><i class="icon bars handle grey"></i> </td>
2022-09-25 10:33:10 +08:00
<td :class="{'color-border': cacheRef.conds != null && cacheRef.conds.connector == 'and', disabled: !cacheRef.isOn}" :style="{'border-left':cacheRef.isReverse ? '1px #db2828 solid' : ''}">
2022-09-03 18:14:34 +08:00
<http-request-conds-view :v-conds="cacheRef.conds" ref="cacheRef" :class="{disabled: !cacheRef.isOn}" v-if="cacheRef.conds != null && cacheRef.conds.groups != null"></http-request-conds-view>
<http-request-cond-view :v-cond="cacheRef.simpleCond" ref="cacheRef" v-if="cacheRef.simpleCond != null"></http-request-cond-view>
<!-- 特殊参数 -->
<grey-label v-if="cacheRef.key != null && cacheRef.key.indexOf('\${args}') < 0">忽略URI参数</grey-label>
<grey-label v-if="cacheRef.minSize != null && cacheRef.minSize.count > 0">
{{cacheRef.minSize.count}}{{cacheRef.minSize.unit}}
<span v-if="cacheRef.maxSize != null && cacheRef.maxSize.count > 0">- {{cacheRef.maxSize.count}}{{cacheRef.maxSize.unit.toUpperCase()}}</span>
</grey-label>
<grey-label v-else-if="cacheRef.maxSize != null && cacheRef.maxSize.count > 0">0 - {{cacheRef.maxSize.count}}{{cacheRef.maxSize.unit.toUpperCase()}}</grey-label>
<grey-label v-if="cacheRef.overMaxSize != null"><span class="red">系统限制{{cacheRef.overMaxSize.count}}{{cacheRef.overMaxSize.unit.toUpperCase()}}</span> </grey-label>
2021-12-07 10:45:49 +08:00
<grey-label v-if="cacheRef.methods != null && cacheRef.methods.length > 0">{{cacheRef.methods.join(", ")}}</grey-label>
<grey-label v-if="cacheRef.expiresTime != null && cacheRef.expiresTime.isPrior && cacheRef.expiresTime.isOn">Expires</grey-label>
<grey-label v-if="cacheRef.status != null && cacheRef.status.length > 0 && (cacheRef.status.length > 1 || cacheRef.status[0] != 200)">状态码{{cacheRef.status.map(function(v) {return v.toString()}).join(", ")}}</grey-label>
2023-07-31 17:11:42 +08:00
<grey-label v-if="cacheRef.allowPartialContent">分片缓存</grey-label>
<grey-label v-if="cacheRef.alwaysForwardRangeRequest">Range回源</grey-label>
<grey-label v-if="cacheRef.enableIfNoneMatch">If-None-Match</grey-label>
<grey-label v-if="cacheRef.enableIfModifiedSince">If-Modified-Since</grey-label>
<grey-label v-if="cacheRef.enableReadingOriginAsync">支持异步</grey-label>
</td>
<td :class="{disabled: !cacheRef.isOn}">
<span v-if="!cacheRef.isReverse">{{cacheRef.life.count}} {{timeUnitName(cacheRef.life.unit)}}</span>
<span v-else class="red">不缓存</span>
2021-05-24 09:02:55 +08:00
</td>
<td>
<a href="" @click.prevent="updateRef(index, cacheRef)">修改</a> &nbsp;
<a href="" v-if="cacheRef.isOn" @click.prevent="disableRef(cacheRef)">暂停</a><a href="" v-if="!cacheRef.isOn" @click.prevent="enableRef(cacheRef)"><span class="red"></span></a> &nbsp;
2021-05-24 09:02:55 +08:00
<a href="" @click.prevent="removeRef(index)">删除</a>
</td>
</tr>
</tbody>
2021-05-24 09:02:55 +08:00
</table>
<p class="comment" v-if="refs.length > 1">所有条件匹配顺序为从上到下可以拖动左侧的<i class="icon bars"></i></p>
2021-05-24 09:02:55 +08:00
2023-06-10 17:35:45 +08:00
<button class="ui button tiny" @click.prevent="addRef(false)" type="button">+添加缓存条件</button> &nbsp; &nbsp; <a href="" @click.prevent="addRef(true)" style="font-size: 0.8em">+</a>
2021-05-24 09:02:55 +08:00
</div>
<div class="margin"></div>
</div>`
})