-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathTabHandler.js
369 lines (315 loc) · 10.2 KB
/
TabHandler.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
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
/**
* @file js/controllers/TabHandler.js
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class TabHandler
* @ingroup js_controllers
*
* @brief A basic handler for a tabbed set of pages.
*
* See <http://jqueryui.com/demos/tabs/> for documentation on JQuery tabs.
* Attach this handler to a div that contains a <ul> with a <li> for each tab
* to be created.
*/
(function($) {
/**
* @constructor
*
* @extends $.pkp.classes.Handler
*
* @param {jQueryObject} $tabs A wrapped HTML element that
* represents the tabbed interface.
* @param {Object} options Handler options.
*/
$.pkp.controllers.TabHandler = function($tabs, options) {
var pageUrl, pageAnchor, pattern, pageAnchors, tabAnchors, i,
self = this;
this.parent($tabs, options);
// Attach the tabs event handlers.
this.bind('tabsbeforeactivate', this.tabsBeforeActivate);
this.bind('tabsactivate', this.tabsActivate);
this.bind('tabscreate', this.tabsCreate);
this.bind('tabsbeforeload', this.tabsBeforeLoad);
this.bind('tabsload', this.tabsLoad);
this.bind('addTab', this.addTab);
if (options.emptyLastTab) {
this.emptyLastTab_ = options.emptyLastTab;
}
// if the page has been loaded with an #anchor
// determine what tab that is for and set the
// options.selected value to it so it gets used
// when tabs() are initialized.
pageUrl = document.location.toString();
if (pageUrl.match('#')) {
pageAnchor = pageUrl.split('#')[1];
tabAnchors = $tabs.find('li a');
for (i = 0; i < tabAnchors.length; i++) {
if (pageAnchor == tabAnchors[i].getAttribute('name')) {
// Matched on anchor name.
options.selected = i;
}
}
}
// Render the tabs as jQueryUI tabs.
$tabs.tabs({
// Enable AJAX-driven tabs with JSON messages.
beforeLoad: function(event, ui) {
ui.ajaxSettings.dataType = 'json';
ui.jqXHR.setRequestHeader('Accept', 'application/json');
ui.ajaxSettings.dataFilter = self.callbackWrapper(self.dataFilter);
},
disabled: options.disabled,
active: options.selected
});
// Load a tab when the URL hash changes to a named tab
// Original issue: https://github.com/pkp/pkp-lib/issues/1787
// This technique introduced to resolve tab activation errors from #1787.
// See: https://github.com/pkp/pkp-lib/issues/4352
window.addEventListener('hashchange', function(e) {
var parts = e.newURL.split('#'), hash, $tab;
if (parts.length < 2) {
return;
}
hash = parts[1];
$tab = $tabs.find('li > a[name="' + hash + '"]');
if ($tab.length) {
$tab.click();
}
}, false);
};
$.pkp.classes.Helper.inherits(
$.pkp.controllers.TabHandler, $.pkp.classes.Handler);
//
// Private properties
//
/**
* The current tab.
* @private
* @type {jQueryObject}
*/
$.pkp.controllers.TabHandler.prototype.$currentTab_ = null;
/**
* The current tab index.
* @private
* @type {number}
*/
$.pkp.controllers.TabHandler.prototype.currentTabIndex_ = 0;
//
// Public methods
//
/**
* Event handler that is called when a tab is selected.
*
* @param {HTMLElement} tabsElement The tab element that triggered
* the event.
* @param {Event} event The triggered event.
* @param {jQueryObject} ui The tabs ui data.
* @return {boolean} Should return true to continue tab loading.
*/
$.pkp.controllers.TabHandler.prototype.tabsBeforeActivate =
function(tabsElement, event, ui) {
var unsavedForm = false;
this.$currentTab_.find('form').each(function(index) {
if ($.pkp.classes.Handler.hasHandler($('#' + $(this).attr('id')))) {
var handler = $.pkp.classes.Handler.getHandler(
$('#' + $(this).attr('id')));
if (handler.formChangesTracked) {
unsavedForm = true;
return false; // found an unsaved form, no need to continue with each().
}
}
});
this.$currentTab_.find('.hasDatepicker').datepicker('hide');
if (unsavedForm) {
if (!confirm(pkp.localeKeys['form.dataHasChanged'])) {
return false;
} else {
this.trigger('unregisterAllForms');
}
}
if (this.emptyLastTab_) {
// bind a single (i.e. one()) error event handler to prevent
// propagation if the tab being unloaded no longer exists.
// We cannot simply getHandler() since that in of itself throws
// an Error.
$(window).one('error', function(msg, url, line) { return false; });
if (this.$currentTab_) {
// Unbind global events for handlers embedded in this tab's
// content.
this.unbindPartial(this.$currentTab_);
this.$currentTab_.empty();
}
}
return true;
};
/**
* Event handler that is called when a tab is created.
*
* @param {HTMLElement} tabsElement The tab element that triggered
* the event.
* @param {Event} event The triggered event.
* @param {jQueryObject} ui The tabs ui data.
* @return {boolean} Should return true to continue tab loading.
*/
$.pkp.controllers.TabHandler.prototype.tabsCreate =
function(tabsElement, event, ui) {
// Save the tab index.
this.currentTabIndex_ = ui.tab.index();
// Save a reference to the current panel.
this.$currentTab_ = ui.panel.jquery ? ui.panel : $(ui.panel);
return true;
};
/**
* Event handler that is called when a tab is activated
*
* @param {HTMLElement} tabsElement The tab element that triggered
* the event.
* @param {Event} event The triggered event.
* @param {jQueryObject} ui The tabs ui data.
* @return {boolean} Should return true to continue tab loading.
*/
$.pkp.controllers.TabHandler.prototype.tabsActivate =
function(tabsElement, event, ui) {
// Save the tab index.
this.currentTabIndex_ = ui.newTab.index();
// Save a reference to the current panel.
this.$currentTab_ = ui.newPanel.jquery ? ui.newPanel : $(ui.newPanel);
return true;
};
/**
* Event handler that is called after a remote tab was loaded.
*
* @param {HTMLElement} tabsElement The tab element that triggered
* the event.
* @param {Event} event The triggered event.
* @param {jQueryObject} ui The tabs ui data.
* @return {boolean} Should return true to continue tab loading.
*/
$.pkp.controllers.TabHandler.prototype.tabsLoad =
function(tabsElement, event, ui) {
return true;
};
/**
* Callback that that is triggered before the tab is loaded.
*
* @param {HTMLElement} tabsElement The tab element that triggered
* the event.
* @param {Event} event The triggered event.
* @param {jQueryObject} ui The tabs ui data.
*/
$.pkp.controllers.TabHandler.prototype.tabsBeforeLoad =
function(tabsElement, event, ui) {
// We must unbind global events before the new tab content is loaded.
// This reaches out to the tab content element and unbinds any events
// attached to that element or any embedded handlers before it gets
// destroyed.
this.unbindPartial($('#' + ui.tab.attr('aria-controls')));
// Initialize AJAX settings for loading tab content remotely
ui.ajaxSettings.cache = false;
ui.ajaxSettings.dataFilter = this.callbackWrapper(this.dataFilter);
};
/**
* Callback that processes AJAX data returned by the server before
* it is displayed in a tab.
*
* @param {Object} ajaxOptions The options object from which the
* callback originated.
* @param {string} jsonString Unparsed JSON data returned from the server.
* @return {string} The tab mark-up.
*/
$.pkp.controllers.TabHandler.prototype.dataFilter =
function(ajaxOptions, jsonString) {
var jsonData = this.handleJson($.parseJSON(jsonString));
if (jsonData === false) {
return '';
}
return JSON.stringify(jsonData.content);
};
/**
* Callback that processes data returned by the server when
* an 'addTab' event is received.
*
* This is useful e.g. when the results of a form handler
* should be sent to a different tab in the containing tabset.
*
* @param {HTMLElement} divElement The parent DIV element
* which contains the tabs.
* @param {Event} event The triggered event (addTab).
* @param {{url: string, title: string}} jsonContent The tabs ui data.
*/
$.pkp.controllers.TabHandler.prototype.addTab =
function(divElement, event, jsonContent) {
var $element = this.getHtmlElement(),
numTabs = $element.children('ul').children('li').length + 1,
$anchorElement = $('<a/>')
.text(jsonContent.title)
.attr('href', jsonContent.url),
$closeSpanElement = $('<a/>')
.addClass('close')
.text(pkp.localeKeys['common.close'])
.attr('href', '#'),
$liElement = $('<li/>')
.append($anchorElement)
.append($closeSpanElement);
// Get the "close" button working
$closeSpanElement.click(function() {
var $liElement = $(this).closest('li'),
$divElement = $('#' + $liElement.attr('aria-controls')),
thisTabIndex, unsavedForm;
// Check to see if any unsaved changes need to be confirmed
unsavedForm = false;
$divElement.find('form').each(function() {
var handler = $.pkp.classes.Handler.getHandler($(this));
if (handler.formChangesTracked) {
// Confirm before proceeding
if (!confirm(pkp.localeKeys['form.dataHasChanged'])) {
unsavedForm = true;
return false;
}
}
});
if (!unsavedForm) {
$divElement.find('form').each(function() {
var handler = $.pkp.classes.Handler.getHandler($(this));
if (handler) {
handler.unregisterForm();
}
});
// If the panel being closed is currently selected, move off first.
thisTabIndex = $liElement.eq(0).index();
if ($element.tabs('option', 'active') == thisTabIndex) {
$element.tabs('option', 'active', thisTabIndex - 1);
}
$liElement.remove();
$divElement.remove();
$element.tabs('refresh');
}
});
// Add the new tab element and refresh the tab set.
$element.children('ul').append($liElement);
$element.tabs('refresh');
$element.tabs('option', 'active', numTabs - 1);
};
//
// Protected methods
//
/**
* Get the current tab.
* @protected
* @return {jQueryObject} The current tab.
*/
$.pkp.controllers.TabHandler.prototype.getCurrentTab = function() {
return this.$currentTab_;
};
/**
* Get the current tab index.
* @protected
* @return {number} The current tab index.
*/
$.pkp.controllers.TabHandler.prototype.getCurrentTabIndex = function() {
return this.currentTabIndex_;
};
}(jQuery));