-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
318 additions
and
20 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
saturn-console-web/src/components/common/favorites/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Favorites from './src/main'; | ||
|
||
/* eslint-disable no-param-reassign */ | ||
Favorites.install = function install(Vue, options) { | ||
const { key = 'oaName', storage = localStorage } = options || {}; | ||
Vue.directive('favorites', { | ||
bind(el, binding, vnode) { | ||
/* eslint-disable no-param-reassign */ | ||
function handler() { | ||
let path = location.hash.substring(1); | ||
let title = path.split('?')[0].substring(1); | ||
let group = ''; | ||
const map = JSON.parse(storage.getItem(key) || '{}'); | ||
const parent = vnode.context; | ||
const self = vnode.componentInstance; | ||
if (binding.modifiers.del) { | ||
const item = map[binding.value]; | ||
delete map[binding.value]; | ||
storage.setItem(key, JSON.stringify(map)); | ||
self.$emit('favorites-del', true, 'del', item); | ||
} else if (binding.modifiers.add) { | ||
if (!Object.prototype.hasOwnProperty.call(map, path)) { | ||
if (typeof binding.value === 'string') { | ||
title = binding.value; | ||
} else if (typeof binding.value === 'object') { | ||
if (binding.value.title) { | ||
title = binding.value.title; | ||
} | ||
if (binding.value.path) { | ||
path = binding.value.path; | ||
} | ||
if (binding.value.group) { | ||
group = binding.value.group; | ||
} | ||
} | ||
map[path] = { title, path, group, date: new Date() }; | ||
storage.setItem(key, JSON.stringify(map)); | ||
self.$emit('favorites-add', true, 'add', map[path]); | ||
} else { | ||
self.$emit('favorites-add', false, 'add', map[path]); | ||
} | ||
} | ||
parent.$set(parent, binding.arg || 'favorites', Object.values(map)); | ||
self.$emit('favorites-show', true, 'show', Object.values(map)); | ||
} | ||
if (el.addEventListener) { | ||
el.addEventListener('click', handler, false); | ||
} else if (el.attachEvent) { | ||
el.attachEvent('onclick', handler); | ||
} else { | ||
el.onclick = handler; | ||
} | ||
}, | ||
}); | ||
Vue.prototype.$favorites = function favorites(path, title, group) { | ||
const map = JSON.parse(storage.getItem(key) || '{}'); | ||
if (title) { | ||
const index = path || location.hash.substring(1); | ||
Object.assign(map[index], { title, group }); | ||
storage.setItem(key, JSON.stringify(map)); | ||
return map[index]; | ||
} else if (path) { | ||
return map[path]; | ||
} | ||
return Object.values(map); | ||
}; | ||
Vue.component(Favorites.name, Favorites); | ||
}; | ||
|
||
export default Favorites; |
202 changes: 202 additions & 0 deletions
202
saturn-console-web/src/components/common/favorites/src/main.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<template> | ||
<div class="fav-container"> | ||
<el-popover trigger="click" ref="popover" title="我的收藏" @show="show = true" @hide="show = false" popper-class="my-fav" placement="bottom" width="400"> | ||
<el-form :model="form"> | ||
<el-table :data="favorites" :show-header="true" v-if="show"> | ||
<el-table-column property="title" label="名称" show-overflow-tooltip> | ||
<template slot-scope="scope"> | ||
<el-form-item v-if="scope.row.path === editPath" style="margin-bottom: 0;"> | ||
<el-input v-model="form.title" :placeholder="scope.row.title"></el-input> | ||
</el-form-item> | ||
<router-link :to="{ path: scope.row.path}" v-else> | ||
{{scope.row.title}} | ||
</router-link> | ||
</template> | ||
</el-table-column> | ||
<!-- <el-table-column property="date" label="日期" show-overflow-tooltip> | ||
<template slot-scope="scope"> | ||
{{scope.row.date | formatDate}} | ||
</template> | ||
</el-table-column> --> | ||
<el-table-column | ||
property="group" | ||
label="分组" | ||
show-overflow-tooltip | ||
:filters="groupsList" | ||
:filter-method="filterGroup" | ||
filter-placement="bottom-end"> | ||
<template slot-scope="scope"> | ||
<el-form-item v-if="scope.row.path === editPath" style="margin-bottom: 0;"> | ||
<el-select allow-create filterable size="small" v-model="form.group" style="width: 100%"> | ||
<el-option v-for="item in groupsList" :label="item.text" :value="item.value" :key="item.value"></el-option> | ||
</el-select> | ||
</el-form-item> | ||
<span v-else>{{scope.row.group || '暂无分组'}}</span> | ||
</template> | ||
</el-table-column> | ||
<el-table-column width="70" label="操作"> | ||
<template slot-scope="scope"> | ||
<div v-if="scope.row.path === editPath"> | ||
<el-tooltip class="item" effect="dark" content="保存" placement="top" key="floppy"> | ||
<el-button type="text" @click="submitForm(scope.row.path)"><i class="fa fa-floppy-o" aria-hidden="true"></i></el-button> | ||
</el-tooltip> | ||
<el-tooltip class="item" effect="dark" content="取消" placement="top" key="undo"> | ||
<el-button type="text" @click.stop="resetFields"><i class="fa fa-undo" aria-hidden="true"></i></el-button> | ||
</el-tooltip> | ||
</div> | ||
<div v-else> | ||
<el-tooltip class="item" effect="dark" content="修改" placement="top" key="edit"> | ||
<el-button type="text" icon="el-icon-edit" @click="edit(scope.row, scope.row.path)"></el-button> | ||
</el-tooltip> | ||
<el-tooltip class="item" effect="dark" content="删除" placement="top" key="delete"> | ||
<el-button type="text" icon="el-icon-delete text-danger" v-favorites:favorites.del="scope.row.path" @favorites-del="showMessage"></el-button> | ||
</el-tooltip> | ||
</div> | ||
</template> | ||
</el-table-column> | ||
</el-table> | ||
</el-form> | ||
</el-popover> | ||
<el-popover | ||
trigger="click" | ||
ref="popover1" | ||
title="添加收藏" | ||
placement="bottom" | ||
v-model="visible" | ||
popper-class="my-fav" | ||
width="350"> | ||
<el-form :model="form"> | ||
<el-form-item label="名称" label-width="40px" style="margin: 9px 0;"> | ||
<el-input v-model="form.title" size="small" auto-complete="off" placeholder="输入名称"></el-input> | ||
</el-form-item> | ||
<el-form-item label="分组" label-width="40px" style="margin-bottom: 9px;"> | ||
<el-select clearable allow-create filterable size="small" v-model="form.group" style="width: 100%"> | ||
<el-option v-for="item in groupsList" :label="item.text" :value="item.value" :key="item.value"></el-option> | ||
</el-select> | ||
</el-form-item> | ||
<div style="text-align: right; margin: 0"> | ||
<el-button size="mini" type="default" @click="resetFields">取 消</el-button> | ||
<el-button type="primary" size="mini" @click="submitForm()">确 定</el-button> | ||
</div> | ||
</el-form> | ||
</el-popover> | ||
<el-tooltip class="item" effect="dark" content="添加收藏" placement="top-start"> | ||
<el-button type="text" v-popover:popover1 v-favorites:favorites.add="unit" @favorites-add="showMessage"> | ||
<i :class="['fa', isFav ? 'fa-heart' : 'fa-heart-o']" aria-hidden="true"></i> | ||
</el-button> | ||
</el-tooltip> | ||
<el-tooltip class="item" effect="dark" content="查看收藏" placement="top-start"> | ||
<el-button type="text" v-popover:popover v-favorites:favorites> | ||
<i :class="['fa', hasFav ? 'fa-star' : 'fa-star-o']" aria-hidden="true"></i> | ||
</el-button> | ||
</el-tooltip> | ||
</div> | ||
</template> | ||
<script> | ||
export default { | ||
name: 'Favorites', | ||
props: ['unit'], | ||
data() { | ||
return { | ||
show: false, | ||
visible: false, | ||
favorites: this.$favorites(), | ||
editPath: '', | ||
form: { | ||
title: '', | ||
group: '', | ||
}, | ||
}; | ||
}, | ||
watch: { | ||
$route: function init() { | ||
this.favorites = this.$favorites(); | ||
}, | ||
}, | ||
computed: { | ||
isFav() { | ||
return this.favorites.some(item => item.path === location.hash.substring(1)); | ||
}, | ||
hasFav() { | ||
return this.favorites.length; | ||
}, | ||
groupsList() { | ||
const arr = []; | ||
this.favorites.forEach((ele) => { | ||
arr.push(ele.group); | ||
}); | ||
const groupArr = Array.from(new Set(arr)); | ||
return groupArr.map((obj) => { | ||
const rObj = {}; | ||
rObj.text = obj; | ||
rObj.value = obj; | ||
return rObj; | ||
}); | ||
}, | ||
}, | ||
methods: { | ||
filterGroup(value, row) { | ||
return row.group === value; | ||
}, | ||
resetFields() { | ||
this.form.title = ''; | ||
this.form.group = ''; | ||
this.favorites = this.$favorites(); | ||
this.visible = false; | ||
this.$nextTick(() => { this.editPath = ''; }); | ||
}, | ||
submitForm(path) { | ||
this.$favorites(path, this.form.title, this.form.group); | ||
this.favorites = this.$favorites(); | ||
this.resetFields(); | ||
this.editPath = ''; | ||
}, | ||
edit(data, editPath) { | ||
this.form.title = data.title; | ||
this.form.group = data.group; | ||
this.editPath = editPath; | ||
}, | ||
showMessage(status, op, data) { | ||
if (op === 'add') { | ||
this.edit(data); | ||
const messageText = status ? '该资源收藏成功!' : '该资源已收藏,勿重复收藏!'; | ||
const messageType = status ? 'success' : 'error'; | ||
this.$message.customMessage(messageType, messageText); | ||
} else if (op === 'del') { | ||
this.$message.customMessage('success', '该收藏资源已移除成功!'); | ||
} | ||
}, | ||
}, | ||
}; | ||
</script> | ||
<style lang="sass"> | ||
.fav-container { | ||
display: inline-block; | ||
margin: 0 10px; | ||
height: 40px; | ||
line-height: 40px; | ||
} | ||
.my-fav { | ||
&.el-popover { | ||
padding: 8px; | ||
} | ||
.el-popover__title { | ||
margin-bottom: 5px; | ||
padding: 5px 0; | ||
font-weight: 800; | ||
font-size: 15px; | ||
} | ||
.el-table { | ||
td, th { | ||
height: 30px; | ||
} | ||
} | ||
.el-table__column-filter-trigger { | ||
i { | ||
color: #0000ff; | ||
font-size: 14px; | ||
font-weight: bolder; | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters