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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/* global OC.Backbone, Handlebars, Promise, _ */
/**
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
(function(OC, $, _, Handlebars) {
'use strict';
var MENU_TEMPLATE = ''
+ '<input id="contactsmenu-search" type="search" placeholder="Search contacts …" value="{{searchTerm}}">'
+ '<div class="content">'
+ '</div>';
var CONTACTS_LIST_TEMPLATE = ''
+ '{{#unless contacts.length}}'
+ '<div class="emptycontent">'
+ ' <div class="icon-search"></div>'
+ ' <h2>' + t('core', 'No contacts found') + '</h2>'
+ '</div>'
+ '{{/unless}}'
+ '<div id="contactsmenu-contacts"></div>'
+ '{{#if contactsAppEnabled}}<div class="footer"><a href="{{contactsAppURL}}">' + t('core', 'Show all contacts …') + '</a></div>{{/if}}';
var LOADING_TEMPLATE = ''
+ '<div class="emptycontent">'
+ ' <div class="icon-loading"></div>'
+ ' <h2>{{loadingText}}</h2>'
+ '</div>';
var ERROR_TEMPLATE = ''
+ '<div class="emptycontent">'
+ ' <div class="icon-search"></div>'
+ ' <h2>' + t('core', 'There was an error loading your contacts') + '</h2>'
+ '</div>';
var CONTACT_TEMPLATE = ''
+ '{{#if contact.avatar}}'
+ '<img src="{{contact.avatar}}" class="avatar">'
+ '{{else}}'
+ '<div class="avatar"></div>'
+ '{{/if}}'
+ '<div class="body">'
+ ' <div class="full-name">{{contact.fullName}}</div>'
+ ' <div class="last-message">{{contact.lastMessage}}</div>'
+ '</div>'
+ '{{#if contact.topAction}}'
+ '<a class="top-action" href="{{contact.topAction.hyperlink}}" title="{{contact.topAction.title}}">'
+ ' <img src="{{contact.topAction.icon}}">'
+ '</a>'
+ '{{/if}}'
+ '{{#if contact.hasTwoActions}}'
+ '<a class="second-action" href="{{contact.secondAction.hyperlink}}" title="{{contact.secondAction.title}}">'
+ ' <img src="{{contact.secondAction.icon}}">'
+ '</a>'
+ '{{/if}}'
+ '{{#if contact.hasManyActions}}'
+ ' <span class="other-actions icon-more"></span>'
+ ' <div class="menu popovermenu">'
+ ' <ul>'
+ ' {{#each contact.actions}}'
+ ' <li>'
+ ' <a href="{{hyperlink}}">'
+ ' <img src="{{icon}}">'
+ ' <span>{{title}}</span>'
+ ' </a>'
+ ' </li>'
+ ' {{/each}}'
+ ' </ul>'
+ ' </div>'
+ '{{/if}}';
/**
* @class Contact
*/
var Contact = OC.Backbone.Model.extend({
defaults: {
fullName: '',
lastMessage: '',
actions: [],
hasOneAction: false,
hasTwoActions: false,
hasManyActions: false
},
/**
* @returns {undefined}
*/
initialize: function() {
// Add needed property for easier template rendering
if (this.get('actions').length === 0) {
this.set('hasOneAction', true);
} else if (this.get('actions').length === 1) {
this.set('hasTwoActions', true);
this.set('secondAction', this.get('actions')[0]);
} else {
this.set('hasManyActions', true);
}
}
});
/**
* @class ContactCollection
*/
var ContactCollection = OC.Backbone.Collection.extend({
model: Contact
});
/**
* @class ContactsListView
*/
var ContactsListView = OC.Backbone.View.extend({
/** @type {ContactsCollection} */
_collection: undefined,
/** @type {array} */
_subViews: [],
/**
* @param {object} options
* @returns {undefined}
*/
initialize: function(options) {
this._collection = options.collection;
},
/**
* @returns {self}
*/
render: function() {
var self = this;
self.$el.html('');
self._subViews = [];
self._collection.forEach(function(contact) {
var item = new ContactsListItemView({
model: contact
});
item.render();
self.$el.append(item.$el);
item.on('toggle:actionmenu', self._onChildActionMenuToggle, self);
self._subViews.push(item);
});
return self;
},
/**
* Event callback to propagate opening (another) entry's action menu
*
* @param {type} $src
* @returns {undefined}
*/
_onChildActionMenuToggle: function($src) {
this._subViews.forEach(function(view) {
view.trigger('parent:toggle:actionmenu', $src);
});
}
});
/**
* @class CotnactsListItemView
*/
var ContactsListItemView = OC.Backbone.View.extend({
/** @type {string} */
className: 'contact',
/** @type {undefined|function} */
_template: undefined,
/** @type {Contact} */
_model: undefined,
/** @type {boolean} */
_actionMenuShown: false,
events: {
'click .icon-more': '_onToggleActionsMenu'
},
/**
* @param {object} data
* @returns {undefined}
*/
template: function(data) {
if (!this._template) {
this._template = Handlebars.compile(CONTACT_TEMPLATE);
}
return this._template(data);
},
/**
* @param {object} options
* @returns {undefined}
*/
initialize: function(options) {
this._model = options.model;
this.on('parent:toggle:actionmenu', this._onOtherActionMenuOpened, this);
},
/**
* @returns {self}
*/
render: function() {
this.$el.html(this.template({
contact: this._model.toJSON()
}));
this.delegateEvents();
// Show placeholder iff no avatar is available (avatar is rendered as img, not div)
this.$('div.avatar').imageplaceholder(this._model.get('fullName'));
// Show tooltip for top action
this.$('.top-action').tooltip({placement: 'left'});
// Show tooltip for second action
this.$('.second-action').tooltip({placement: 'left'});
return this;
},
/**
* Toggle the visibility of the action popover menu
*
* @private
* @returns {undefined}
*/
_onToggleActionsMenu: function() {
this._actionMenuShown = !this._actionMenuShown;
if (this._actionMenuShown) {
this.$('.menu').show();
} else {
this.$('.menu').hide();
}
this.trigger('toggle:actionmenu', this.$el);
},
/**
* @private
* @argument {jQuery} $src
* @returns {undefined}
*/
_onOtherActionMenuOpened: function($src) {
if (this.$el.is($src)) {
// Ignore
return;
}
this._actionMenuShown = false;
this.$('.menu').hide();
}
});
/**
* @class ContactsMenuView
*/
var ContactsMenuView = OC.Backbone.View.extend({
/** @type {undefined|function} */
_loadingTemplate: undefined,
/** @type {undefined|function} */
_errorTemplate: undefined,
/** @type {undefined|function} */
_contentTemplate: undefined,
/** @type {undefined|function} */
_contactsTemplate: undefined,
/** @type {undefined|ContactCollection} */
_contacts: undefined,
events: {
'input #contactsmenu-search': '_onSearch'
},
/**
* @returns {undefined}
*/
_onSearch: _.debounce(function() {
this.trigger('search', this.$('#contactsmenu-search').val());
}, 700),
/**
* @param {object} data
* @returns {string}
*/
loadingTemplate: function(data) {
if (!this._loadingTemplate) {
this._loadingTemplate = Handlebars.compile(LOADING_TEMPLATE);
}
return this._loadingTemplate(data);
},
/**
* @param {object} data
* @returns {string}
*/
errorTemplate: function(data) {
if (!this._errorTemplate) {
this._errorTemplate = Handlebars.compile(ERROR_TEMPLATE);
}
return this._errorTemplate(data);
},
/**
* @param {object} data
* @returns {string}
*/
contentTemplate: function(data) {
if (!this._contentTemplate) {
this._contentTemplate = Handlebars.compile(MENU_TEMPLATE);
}
return this._contentTemplate(data);
},
/**
* @param {object} data
* @returns {string}
*/
contactsTemplate: function(data) {
if (!this._contactsTemplate) {
this._contactsTemplate = Handlebars.compile(CONTACTS_LIST_TEMPLATE);
}
return this._contactsTemplate(data);
},
/**
* @param {object} options
* @returns {undefined}
*/
initialize: function(options) {
this.options = options;
},
/**
* @param {string} text
* @returns {undefined}
*/
showLoading: function(text) {
this.render();
this._contacts = undefined;
this.$('.content').html(this.loadingTemplate({
loadingText: text
}));
},
/**
* @returns {undefined}
*/
showError: function() {
this.render();
this._contacts = undefined;
this.$('.content').html(this.errorTemplate());
},
/**
* @param {object} viewData
* @param {string} searchTerm
* @returns {undefined}
*/
showContacts: function(viewData, searchTerm) {
this._contacts = viewData.contacts;
this.render({
contacts: viewData.contacts
});
var list = new ContactsListView({
collection: viewData.contacts
});
list.render();
this.$('.content').html(this.contactsTemplate({
contacts: viewData.contacts,
searchTerm: searchTerm,
contactsAppEnabled: viewData.contactsAppEnabled,
contactsAppURL: OC.generateUrl('/apps/contacts')
}));
this.$('#contactsmenu-contacts').html(list.$el);
},
/**
* @param {object} data
* @returns {self}
*/
render: function(data) {
var searchVal = this.$('#contactsmenu-search').val();
this.$el.html(this.contentTemplate(data));
// Focus search
this.$('#contactsmenu-search').val(searchVal);
this.$('#contactsmenu-search').focus();
return this;
}
});
/**
* @param {Object} options
* @param {jQuery} options.el
* @param {jQuery} options.trigger
* @class ContactsMenu
*/
var ContactsMenu = function(options) {
this.initialize(options);
};
ContactsMenu.prototype = {
/** @type {jQuery} */
$el: undefined,
/** @type {jQuery} */
_$trigger: undefined,
/** @type {ContactsMenuView} */
_view: undefined,
/** @type {Promise} */
_contactsPromise: undefined,
/**
* @param {Object} options
* @param {jQuery} options.el - the element to render the menu in
* @param {jQuery} options.trigger - the element to click on to open the menu
* @returns {undefined}
*/
initialize: function(options) {
this.$el = options.el;
this._$trigger = options.trigger;
this._view = new ContactsMenuView({
el: this.$el
});
this._view.on('search', function(searchTerm) {
this._loadContacts(searchTerm);
}, this);
OC.registerMenu(this._$trigger, this.$el, function() {
this._toggleVisibility(true);
}.bind(this));
this.$el.on('beforeHide', function() {
this._toggleVisibility(false);
}.bind(this));
},
/**
* @private
* @param {boolean} show
* @returns {Promise}
*/
_toggleVisibility: function(show) {
if (show) {
return this._loadContacts();
} else {
this.$el.html('');
return Promise.resolve();
}
},
/**
* @private
* @param {string|undefined} searchTerm
* @returns {Promise}
*/
_getContacts: function(searchTerm) {
var url = OC.generateUrl('/contactsmenu/contacts');
return Promise.resolve($.ajax(url, {
method: 'POST',
data: {
filter: searchTerm
}
}));
},
/**
* @param {string|undefined} searchTerm
* @returns {undefined}
*/
_loadContacts: function(searchTerm) {
var self = this;
if (!self._contactsPromise) {
self._contactsPromise = self._getContacts(searchTerm);
}
if (_.isUndefined(searchTerm) || searchTerm === '') {
self._view.showLoading(t('core', 'Loading your contacts …'));
} else {
self._view.showLoading(t('core', 'Looking for {term} …', {
term: searchTerm
}));
}
return self._contactsPromise.then(function(data) {
// Convert contact entries to Backbone collection
data.contacts = new ContactCollection(data.contacts);
self._view.showContacts(data, searchTerm);
}, function(e) {
self._view.showError();
console.error('There was an error loading your contacts', e);
}).then(function() {
// Delete promise, so that contacts are fetched again when the
// menu is opened the next time.
delete self._contactsPromise;
}).catch(console.error.bind(this));
}
};
OC.ContactsMenu = ContactsMenu;
})(OC, $, _, Handlebars);
|