-
Notifications
You must be signed in to change notification settings - Fork 348
/
channels.js
225 lines (209 loc) · 7.43 KB
/
channels.js
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
/*global getUrlRoot, VuFind */
VuFind.register('channels', function Channels() {
function addLinkButtons(elem) {
var links;
try {
links = JSON.parse(elem.dataset.linkJson);
} catch (e) {
console.error("Error parsing " + elem.dataset.linkJson);
return;
}
if (links.length === 0) {
return;
}
var $cont = $(
'<div class="dropdown">' +
'<button class="btn btn-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" aria-label="' + VuFind.translate('toggle_dropdown') + '">' +
VuFind.icon("ui-dots-menu") +
'</button>' +
'</div>'
);
var $list = $('<ul class="dropdown-menu"></ul>');
for (var i = 0; i < links.length; i++) {
var li = $('<li/>');
li.append(
$('<a/> ', {
'href': links[i].url,
'class': links[i].label,
'html': '<i class="fa ' + links[i].icon + '"></i> ' + VuFind.translate(links[i].label)
})
);
$list.append(li);
}
$cont.append($list);
$(elem).siblings('.channel-title').append($cont);
}
var currentPopover = false;
function isCurrentPopoverRecord(record) {
return record && currentPopover
&& record.data('record-id') === currentPopover.data('record-id');
}
function switchPopover(record) {
// Hide the old popover:
if (currentPopover) {
currentPopover.popover('hide');
}
// Special case: if the new popover is the same as the old one, reset the
// current popover status so that the next click will open it again (toggle)
if (isCurrentPopoverRecord(record)) {
currentPopover = false;
} else {
// Default case: set the currentPopover to the new incoming value:
currentPopover = record;
}
// currentPopover has now been updated; show it if appropriate:
if (currentPopover) {
currentPopover.popover('show');
}
}
function redrawPopover(record, html) {
// Only update the popover if the context hasn't changed since the
// AJAX call was triggered.
if (isCurrentPopoverRecord(record)) {
record.data('bs.popover').tip().find('.popover-content').html(html);
}
record.data('bs.popover').options.content = html;
}
// Truncate lines to height with ellipses
function clampLines(el) {
var words = el.innerHTML.split(" ");
while (el.scrollHeight > el.offsetHeight) {
words.pop();
el.innerHTML = words.join(" ") + VuFind.translate("eol_ellipsis");
}
}
function setupChannelSlider(i, op) {
$(op).find(".slide").removeClass("hidden");
$(op).slick({
slidesToShow: 6,
slidesToScroll: 6,
infinite: false,
rtl: $(document.body).hasClass("rtl"),
responsive: [
{
breakpoint: 768,
settings: {
slidesToShow: 3,
slidesToScroll: 3
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
$(op).on('swipe', function channelDrag() {
switchPopover(false);
});
let recordContent = {};
function showChannelRecord(event) {
const record = $(event.delegateTarget);
record.popover({
content: VuFind.translate('loading_ellipsis'),
html: true,
placement: 'bottom',
trigger: 'focus',
container: '#' + record.closest('.channel').attr('id')
});
const recordID = record.data("record-id");
if (!(recordID in recordContent)) {
const controls = '<div class="btn-group btn-group-justified">'
+ '<a href="' + VuFind.path + '/Channels/Record?'
+ 'id=' + encodeURIComponent(record.attr('data-record-id'))
+ '&source=' + encodeURIComponent(record.attr('data-record-source'))
+ '" class="btn btn-default">' + VuFind.translate('channel_expand') + '</a>'
+ '<a href="' + record.attr('href') + '" class="btn btn-default">' + VuFind.translate('View Record') + '</a>'
+ '</div>';
$.ajax({
url: VuFind.path + getUrlRoot(record.attr('href')) + '/AjaxTab',
type: 'POST',
data: {tab: 'description'}
})
.done(function channelPopoverDone(data) {
recordContent[recordID] = controls + data;
})
.fail(function channelPopoverFail(error) {
console.error(error);
recordContent[recordID] = controls + VuFind.translate('no_description');
})
.always(function channelPopoverFinally() {
switchPopover(record);
redrawPopover(record, recordContent[recordID]);
});
} else {
switchPopover(record);
redrawPopover(record, recordContent[recordID]);
// redraw needs to happen since element is destroyed on hide
}
return false;
}
// don't follow links
$(op).find('.channel-record').off("click").on("click", () => false);
// click and tab
$(op).find('.channel-record').off("focus").on("focus", showChannelRecord);
// Channel add buttons
addLinkButtons(op);
$('.channel-add-menu[data-group="' + op.dataset.group + '"].hidden')
.clone()
.removeClass('hidden')
.prependTo($(op).parent(".channel-wrapper"));
// Fix title overflow
op.querySelectorAll(".channel-record-title").forEach(clampLines);
}
var bindChannelAddMenu; // circular dependency fix for jshint
function selectAddedChannel(e) {
$.ajax(e.target.href).done(function addChannelAjaxDone(data) {
var list = $(e.target).closest('.dropdown-menu');
var $testEls = $('<div>' + data + '</div>').find('.channel-wrapper');
var $dest = $(e.target).closest('.channel-wrapper');
// Remove dropdown link
$('[data-token="' + e.target.dataset.token + '"]').parent().remove();
// Insert new channels
$testEls.each(function addRetrievedNonEmptyChannels(i, element) {
var $testEl = $(element);
// Make sure the channel has content
if ($testEl.find('.channel-record').length === 0) {
$dest.after(
'<div class="channel-wrapper">'
+ '<div class="channel-title no-results">'
+ '<h2>' + $testEl.find('h2').html() + '</h2>'
+ VuFind.translate('nohit_heading')
+ '</div></div>'
);
} else {
$dest.after($testEl);
$testEl.find('.channel').each(setupChannelSlider);
$testEl.find('.channel').each(bindChannelAddMenu);
}
if (list.children().length === 0) {
$('.channel-add-menu[data-group="' + list.closest('.channel-add-menu').data('group') + '"]').remove();
}
});
});
return false;
}
bindChannelAddMenu = function bindChannelAddMenuFunc(iteration, channel) {
var scope = $(channel).parent(".channel-wrapper");
$(scope).find('.channel-add-menu .dropdown-menu a').on("click", selectAddedChannel);
$(scope).find('.channel-add-menu .add-btn').on("click", function addChannels(e) {
var links = $(e.target).closest('.channel-add-menu').find('.dropdown-menu a');
for (var i = 0; i < links.length && i < 2; i++) {
links[i].click();
}
});
};
function init () {
$('.channel').each(setupChannelSlider);
$('.channel').each(bindChannelAddMenu);
$(document).on("hidden.bs.popover", function deselectPopover(e) {
if (isCurrentPopoverRecord($(e.target))) {
switchPopover(false);
}
});
}
return { init: init };
});