/
field-account-link.tag
276 lines (200 loc) · 8.78 KB
/
field-account-link.tag
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
<field-account-link>
<div class="uk-text-center uk-panel uk-panel-framed" show="{!_value || (Array.isArray(_value) && !_value.length)}">
<img if="{opts.multiple}" class="uk-svg-adjust uk-text-muted" riot-src="{ App.base('/assets/app/media/icons/accounts.svg') }" width="50" data-uk-svg>
<img if="{!opts.multiple}" class="uk-svg-adjust uk-text-muted" riot-src="{ App.base('/assets/app/media/icons/login.svg') }" width="50" data-uk-svg>
<div>
<a class="uk-button uk-button-link" onclick="{selectAccount}">{ App.i18n.get('Select Account') }</a>
</div>
</div>
<div class="uk-panel uk-panel-box uk-panel-card uk-flex uk-flex-middle" if="{ready && _value && !opts.multiple}">
<div class="uk-flex-item-1 uk-margin-right">
<cp-account account="{_value}"></cp-account>
</div>
<div>
<a onclick="{removeAccount}"><i class="uk-icon-trash-o uk-text-danger"></i></a>
</div>
</div>
<div if="{ready && opts.multiple && Array.isArray(_value) && _value.length}">
<div class="uk-sortable" data-uk-sortable>
<div class="uk-panel uk-panel-box uk-panel-card uk-flex uk-flex-middle uk-margin-small-bottom" each="{account in _value}" data-account="{account}">
<div class="uk-flex-item-1 uk-margin-right">
<cp-account account="{account}"></cp-account>
</div>
<div>
<a onclick="{parent.removeAccount}"><i class="uk-icon-trash-o uk-text-danger"></i></a>
</div>
</div>
</div>
<p class="uk-text-center">
<a onclick="{selectAccount}" title="{ App.i18n.get('Add Account') }" data-uk-tooltip><i class="uk-icon-plus-circle"></i></a>
</p>
</div>
<div ref="modalSelectAccounts" class="uk-modal">
<div class="uk-modal-dialog uk-modal-dialog-large">
<a href="" class="uk-modal-close uk-close"></a>
<h3>{ App.i18n.get('Accounts') }</h3>
<div class="uk-margin uk-flex uk-flex-middle">
<div class="uk-form-icon uk-form uk-flex-item-1 uk-text-muted">
<i class="uk-icon-search"></i>
<input class="uk-width-1-1 uk-form-large uk-form-blank" type="text" ref="txtfilter" placeholder="{ App.i18n.get('Filter accounts...') }" onchange="{ updatefilter }">
</div>
<div show="{selected.length}">
<button type="button" class="uk-button uk-button-large uk-button-link" onclick="{linkSelected}">
<i class="uk-icon-link"></i> {selected.length} {App.i18n.get('Accounts')}
</button>
</div>
</div>
<div class="uk-margin-large-bottom" if="{ loading }">
<cp-preloader class="uk-container-center"></cp-preloader>
</div>
<div class="uk-text-xlarge uk-text-center uk-text-muted uk-margin-large" if="{ !_accounts.length && filter && !loading }">
{ App.i18n.get('No accounts found') }.
</div>
<table class="uk-table uk-table-tabbed uk-table-striped" show="{!loading && _accounts.length}">
<thead>
<tr>
<th show="{opts.multiple}"></th>
<th></th>
<th class="uk-text-small">{ App.i18n.get('Name') }</th>
<th class="uk-text-small">{ App.i18n.get('Email') }</th>
<th class="uk-text-small">{ App.i18n.get('Group') }</th>
<th></th>
</tr>
</thead>
<tbody>
<tr each="{account in _accounts}">
<td show="{parent.opts.multiple}"><input class="uk-checkbox" type="checkbox" onclick="{parent.toggleSelected}"></td>
<td><cp-gravatar email="{ account.email }" size="25" alt="{ account.name || account.user }"></cp-gravatar></td>
<td>{account.name}</td>
<td>{account.email}</td>
<td><span class="{ account.group=='admin' && 'uk-badge' }">{ account.group }</span></td>
<td width="20"><a onclick="{ parent.linkAccount }"><i class="uk-icon-link"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
var $this = this, cache = {};
this._value = null;
this._accounts = [];
this.selected = [];
this.loading = false;
this.ready = false;
this.on('mount', function() {
this.modal = UIkit.modal(this.refs.modalSelectAccounts, {modal:false});
this.modal.on('keydown', 'input',function(e){
if (e.keyCode == 13) {
e.preventDefault();
e.stopPropagation();
$this.updatefilter(e);
$this.update();
}
});
if (opts.multiple) {
App.$(this.root).on('stop.uk.sortable', function(){
var accounts = [];
App.$('.uk-sortable', $this.root).children().each(function(){
accounts.push(this.getAttribute('data-account'));
});
$this._value = [];
$this.update();
$this._value = accounts;
$this.$setValue($this._value);
});
}
this.ready = true;
this.update();
});
this.$updateValue = function(value) {
if (opts.multiple && !Array.isArray(value)) {
value = [];
}
if (this._value !== value) {
this._value = value;
this.update();
}
}.bind(this);
this.selectAccount = function() {
this.selected = [];
this.modal.find(':checked').prop('checked', false);
this.load();
this.modal.show();
}
this.load = function() {
var value = this.refs.txtfilter.value, options = {};
if (this.filter) {
options.filter = this.filter;
}
options.limt = 10;
this.loading = true;
App.request('/accounts/find', {options: options}).then(function(response) {
$this._accounts = response && Array.isArray(response.accounts) ? response.accounts : [];
$this.loading = false;
$this.update();
});
}
this.linkAccount = function(e) {
var account = e.item.account;
if (opts.multiple) {
if (!this._value || !Array.isArray(this._value)) {
this._value = [];
}
if (opts.limit && this._value.length >= opts.limit ) {
// Notice?
} else {
this._value.push(account._id);
this._value = _.uniq(this._value);
}
} else {
this._value = account._id;
}
setTimeout(function(){
$this.modal.hide();
}, 50);
this.$setValue(this._value);
}
this.removeAccount = function(e) {
if (opts.multiple) {
this._value.splice(this._value.indexOf(e.item.account), 1);
} else {
this._value = null;
}
this.$setValue(this._value);
}
this.toggleSelected = function(e) {
var account = e.item.account;
if (e.target.checked) {
this.selected.push(account._id);
} else {
var idx = this.selected.indexOf(account._id);
if (idx > -1) {
this.selected.splice(idx, 1);
}
}
}
this.linkSelected = function() {
if (!Array.isArray(this._value)) {
this._value = [];
}
this.selected.forEach(function(account) {
$this._value.push(account);
})
this._value = _.uniq(this._value);
this.$setValue(this._value);
this.modal.hide();
}
this.updatefilter = function(e) {
var load = this.filter ? true:false;
if (this.refs.txtfilter.value == this.filter) {
return;
}
this.filter = this.refs.txtfilter.value || null;
if (this.filter || load) {
this._accounts = [];
this.load();
}
return false;
}
</script>
</field-account-link>