ae18f2f70732eeebf3bb28c6cbdd08c703ef05fa
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Tabs.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Backend/Tabs
16 * This class handle the tabs in the TYPO3 backend.
17 * It stores the last active tab and open it again after a reload,
18 */
19 define(['jquery', 'TYPO3/CMS/Backend/Storage', 'bootstrap'], function ($, Storage) {
20 'use strict';
21
22 /**
23 * Tabs helper
24 *
25 * @type {{storage: (Storage.Client|*), cacheTimeInSeconds: number, storeLastActiveTab: bool}}
26 * @exports TYPO3/CMS/Backend/Tabs
27 */
28 var Tabs = {
29 storage: Storage.Client,
30 // cache lifetime in seconds
31 cacheTimeInSeconds: 1800,
32 storeLastActiveTab: true
33 };
34
35 /**
36 * Receive active tab from storage
37 *
38 * @param {String} id
39 * @returns {String}
40 */
41 Tabs.receiveActiveTab = function(id) {
42 var target = Tabs.storage.get(id) || '';
43 var expire = Tabs.storage.get(id + '.expire') || 0;
44 if (expire > Tabs.getTimestamp()) {
45 return target;
46 }
47 return '';
48 };
49
50 /**
51 * Store active tab in storage
52 *
53 * @param {String} id
54 * @param {String} target
55 */
56 Tabs.storeActiveTab = function(id, target) {
57 Tabs.storage.set(id, target);
58 Tabs.storage.set(id + '.expire', Tabs.getTimestamp() + Tabs.cacheTimeInSeconds);
59 };
60
61 /**
62 * Get unixtimestamp
63 *
64 * @returns {Number}
65 */
66 Tabs.getTimestamp = function() {
67 return Math.round((new Date()).getTime() / 1000);
68 };
69
70 $(function () {
71 $('.t3js-tabs').each(function() {
72 var $tabContainer = $(this);
73 Tabs.storeLastActiveTab = $tabContainer.data('storeLastTab') === 1;
74 var currentActiveTab = Tabs.receiveActiveTab($tabContainer.attr('id'));
75 if (currentActiveTab) {
76 $tabContainer.find('a[href="' + currentActiveTab + '"]').tab('show');
77 }
78 $tabContainer.on('show.bs.tab', function(e) {
79 if (Tabs.storeLastActiveTab) {
80 var id = e.currentTarget.id;
81 var target = e.target.hash;
82 Tabs.storeActiveTab(id, target);
83 }
84 });
85 });
86 });
87
88 return Tabs;
89 });