/
rb-assignshare.jsx
308 lines (276 loc) · 10.9 KB
/
rb-assignshare.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright (c) REBUILD <https://getrebuild.com/> and its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/
/* eslint-disable no-unused-vars */
// ~~ 分派
class DlgAssign extends RbModalHandler {
constructor(props) {
super(props)
this.onView = !!window.RbViewPage
this.specs = ['assign', '分派']
}
render() {
return (
<RbModal title={this.specs[1]} ref={(c) => this._dlg = c}>
<div className="form">
{this.onView === true ? null : (
<div className="form-group row pb-0">
<label className="col-sm-3 col-form-label text-sm-right">{this.specs[1]}哪些记录</label>
<div className="col-sm-7">
<div className="form-control-plaintext">{'选中的记录 (' + this.state.ids.length + '条)'}</div>
</div>
</div>
)}
<div className="form-group row">
<label className="col-sm-3 col-form-label text-sm-right">{this.specs[1]}给谁</label>
<div className="col-sm-7">
{this._useUserSelector()}
</div>
</div>
{this.state.cascadesShow !== true ? (
<div className="form-group row">
<div className="col-sm-7 offset-sm-3"><a href="#" onClick={this._showCascade}>同时{this.specs[1]}关联记录</a></div>
</div>
) : (<div className="form-group row">
<label className="col-sm-3 col-form-label text-sm-right">选择关联记录</label>
<div className="col-sm-7">
<select className="form-control form-control-sm" ref={(c) => this._cascades = c}>
{(this.state.cascadesEntity || []).map((item) => {
return <option key={'option-' + item[0]} value={item[0]}>{item[1]}</option>
})}
</select>
</div>
</div>)}
<div className="form-group row footer">
<div className="col-sm-7 offset-sm-3" ref={(c) => this._btns = c}>
<button className="btn btn-primary btn-space" type="button" data-loading-text="请稍后" onClick={() => this.post()}>确定</button>
<a className="btn btn-link btn-space" onClick={() => this.hide()}>取消</a>
</div>
</div>
</div>
</RbModal>
)
}
_showCascade = () => {
event && event.preventDefault()
$.get(`/commons/metadata/references?entity=${this.props.entity}`, (res) => {
this.setState({ cascadesShow: true, cascadesEntity: res.data }, () => {
$(this._cascades).select2({
multiple: true,
placeholder: '选择关联实体 (可选)'
}).val(null).trigger('change')
})
})
}
_useUserSelector() {
return <UserSelector hideDepartment={true} hideRole={true} hideTeam={true} multiple={false} ref={(c) => this._UserSelector = c} />
}
post() {
let users = this._UserSelector.val()
if (!users || users.length === 0) { RbHighbar.create('请选择' + this.specs[1] + '给谁'); return }
if ($.type(users) === 'array') users = users.join(',')
const cass = this.state.cascadesShow === true ? $(this._cascades).val().join(',') : ''
const $btns = $(this._btns).find('.btn').button('loading')
$.post(`/app/entity/record-${this.specs[0]}?id=${this.state.ids.join(',')}&cascades=${cass}&to=${users}`, (res) => {
if (res.error_code === 0) {
this.setState({ cascadesShow: false })
this._UserSelector.clearSelection()
$(this._cascades).val(null).trigger('change')
this.hide()
const affected = res.data.assigned || res.data.shared || 0
if (affected > 0 && rb.env === 'dev') RbHighbar.success('已成功' + this.specs[1] + ' ' + affected + ' 条记录')
else RbHighbar.success('记录已' + this.specs[1])
setTimeout(() => {
if (window.RbListPage) RbListPage._RbList.reload()
if (window.RbViewPage) location.reload()
}, 500)
} else {
RbHighbar.error(res.error_msg)
}
$btns.button('reset')
})
}
// -- Usage
/**
* @param {*} props
*/
static create(props) {
const that = this
if (that.__HOLDER) that.__HOLDER.show(props)
else renderRbcomp(<DlgAssign {...props} />, null, function () { that.__HOLDER = this })
}
}
// ~~ 共享
class DlgShare extends DlgAssign {
constructor(props) {
super(props)
this.specs = ['share', '共享']
}
_useUserSelector() {
return <UserSelector ref={(c) => this._UserSelector = c} />
}
// -- Usage
/**
* @param {*} props
*/
static create(props) {
const that = this
if (that.__HOLDER2) that.__HOLDER2.show(props)
else renderRbcomp(<DlgShare {...props} />, null, function () { that.__HOLDER2 = this })
}
}
// ~~ 取消共享(批量模式)
class DlgUnshare extends RbModalHandler {
constructor(props) {
super(props)
this.state.whichUsers = 'ALL'
}
render() {
return (
<RbModal title="取消共享" ref={(c) => this._dlg = c}>
<div className="form">
<div className="form-group row">
<label className="col-sm-3 col-form-label text-sm-right">取消共享哪些记录</label>
<div className="col-sm-7">
<div className="form-control-plaintext">{'选中的记录 (' + this.state.ids.length + '条)'}</div>
</div>
</div>
<div className="form-group row pt-0 pb-0">
<label className="col-sm-3 col-form-label text-sm-right">取消哪些用户</label>
<div className="col-sm-7">
<div className="mt-1">
<label className="custom-control custom-control-sm custom-radio custom-control-inline mb-2">
<input className="custom-control-input" name="whichUsers" type="radio" checked={this.state.whichUsers === 'ALL'} onChange={() => this.whichMode(true)} />
<span className="custom-control-label">全部用户</span>
</label>
<label className="custom-control custom-control-sm custom-radio custom-control-inline mb-2">
<input className="custom-control-input" name="whichUsers" type="radio" checked={this.state.whichUsers === 'SPEC'} onChange={() => this.whichMode()} />
<span className="custom-control-label">指定用户</span>
</label>
</div>
<div className={'mb-2 ' + (this.state.whichUsers === 'ALL' ? 'hide' : '')}>
<UserSelector ref={(c) => this._UserSelector = c} />
</div>
</div>
</div>
<div className="form-group row footer">
<div className="col-sm-7 offset-sm-3" ref={(c) => this._btns = c}>
<button className="btn btn-primary btn-space" type="button" data-loading-text="请稍后" onClick={() => this.post()}>确定</button>
<a className="btn btn-link btn-space" onClick={() => this.hide()}>取消</a>
</div>
</div>
</div>
</RbModal>
)
}
whichMode(isAll) {
this.setState({ whichUsers: isAll === true ? 'ALL' : 'SPEC' })
}
post() {
let users = this._UserSelector.val()
if (this.state.whichUsers === 'ALL') {
users = '$ALL$'
} else {
if (!users || users.length === 0) { RbHighbar.create('请选择取消用户'); return }
users = users.join(',')
}
const $btns = $(this._btns).find('.btn').button('loading')
$.post(`/app/entity/record-unshare-batch?id=${this.state.ids.join(',')}&to=${users}`, (res) => {
if (res.error_code === 0) {
this._UserSelector.clearSelection()
this.hide()
if (res.data.unshared > 0 && rb.env === 'dev') RbHighbar.success('成功取消共享 ' + res.data.unshared + ' 条记录')
else RbHighbar.success('已取消共享')
setTimeout(() => {
if (window.RbListPage) RbListPage._RbList.reload()
}, 500)
} else {
RbHighbar.error(res.error_msg)
}
$btns.button('reset')
})
}
// -- Usage
/**
* @param {*} props
*/
static create(props) {
const that = this
if (that.__HOLDER) that.__HOLDER.show(props)
else renderRbcomp(<DlgUnshare {...props} />, null, function () { that.__HOLDER = this })
}
}
// ~~ 管理共享
class DlgShareManager extends RbModalHandler {
constructor(props) {
super(props)
this.state.selectedAccess = []
}
render() {
return (
<RbModal title={(this.props.unshare === true ? '管理' : '查看') + '共享用户'} ref={(c) => this._dlg = c}>
<div className="sharing-list">
<ul className="list-unstyled list-inline">
{(this.state.sharingList || []).map((item) => {
return (
<li className={`list-inline-item ${this.state.selectedAccess.includes(item[1]) ? 'active' : ''}`} key={`access-${item[1]}`}>
<div onClick={() => this.clickUser(item[1])} title={`由 ${item[3]} 共享于 ${item[2]}`}>
<UserShow id={item[0][0]} name={item[0][1]} showName={true} />
<i className="zmdi zmdi-check" />
</div>
</li>
)
})}
</ul>
</div>
<div className="dialog-footer" ref={(c) => this._btns = c}>
{this.props.unshare === true && <button className="btn btn-primary btn-space" type="button" onClick={() => this.post()}>取消共享</button>}
<button className="btn btn-secondary btn-space" type="button" onClick={() => this.hide()}>取消</button>
</div>
</RbModal>
)
}
componentDidMount() {
$.get(`/app/entity/shared-list?id=${this.props.id}`, (res) => {
this.setState({ sharingList: res.data })
})
}
clickUser(id) {
if (this.props.unshare !== true) return
const s = this.state.selectedAccess
if (s.includes(id)) s.remove(id)
else s.push(id)
this.setState({ selectedAccess: s })
}
post() {
const s = this.state.selectedAccess
if (s.length === 0) { RbHighbar.create('请选择需要取消共享的用户'); return }
const $btns = $(this._btns).button('loading')
$.post(`/app/entity/record-unshare?id=${s.join(',')}&record=${this.props.id}`, (res) => {
if (res.error_code === 0) {
this.hide()
if (rb.env === 'dev') RbHighbar.success('已取消 ' + res.data.unshared + ' 位用户的共享')
else RbHighbar.success('共享已取消')
setTimeout(() => {
if (window.RbViewPage) location.reload()
}, 500)
} else {
RbHighbar.error(res.error_msg)
}
$btns.button('reset')
})
}
// -- Usage
/**
* @param {*} id
* @param {*} unshare
*/
static create(id, unshare) {
const props = { id: id, unshare: unshare !== false }
const that = this
if (that.__HOLDER) that.__HOLDER.show(props)
else renderRbcomp(<DlgShareManager {...props} />, null, function () { that.__HOLDER = this })
}
}