20faa0ebfab0f659ff4b7575b9e573dfb264a43e
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / DocumentHeader.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 DocumentHeader source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 define('TYPO3/CMS/Backend/DocumentHeader', ['jquery'], function($) {
15
16 var DocumentHeader = {
17 $documentHeader: null,
18 $documentHeaderBars: null,
19 $documentHeaderNavigationBar: null,
20 $documentHeaderSearchBar: null,
21 $moduleBody: null,
22 direction: 'down',
23 reactionRange: 300,
24 lastPosition: 0,
25 currentPosition: 0,
26 changedPosition: 0,
27 settings: {
28 margin: 24,
29 offset: 100,
30 selectors: {
31 moduleDocumentHeader: '.t3js-module-docheader',
32 moduleDocheaderBar: '.t3js-module-docheader-bar',
33 moduleNavigationBar: '.t3js-module-docheader-bar-navigation',
34 moduleButtonBar: '.t3js-module-docheader-bar-buttons',
35 moduleSearchBar: '.t3js-module-docheader-bar-search',
36 moduleBody: '.t3js-module-body'
37
38 },
39 }
40 };
41
42 /**
43 * Reposition
44 */
45 DocumentHeader.reposition = function() {
46 DocumentHeader.$documentHeader.css('height', 'auto');
47 DocumentHeader.$documentHeaderBars.css('height', 'auto');
48 DocumentHeader.$moduleBody.css('padding-top', DocumentHeader.$documentHeader.outerHeight() + DocumentHeader.settings.margin);
49 };
50
51 /**
52 * Resize
53 */
54 DocumentHeader.resize = function() {
55 DocumentHeader.reposition();
56 };
57
58 /**
59 * Scroll
60 */
61 DocumentHeader.scroll = function() {
62 DocumentHeader.currentPosition = $(window).scrollTop();
63 if (DocumentHeader.currentPosition > DocumentHeader.lastPosition){
64 if (DocumentHeader.direction !== 'down'){
65 DocumentHeader.direction = 'down';
66 DocumentHeader.changedPosition = DocumentHeader.currentPosition;
67 }
68 } else if (DocumentHeader.currentPosition < DocumentHeader.lastPosition){
69 if (DocumentHeader.direction !== 'up'){
70 DocumentHeader.direction = 'up';
71 DocumentHeader.changedPosition = DocumentHeader.currentPosition;
72 }
73 }
74 if(DocumentHeader.direction === "up" && (DocumentHeader.changedPosition - DocumentHeader.reactionRange) < DocumentHeader.currentPosition){
75 DocumentHeader.$documentHeader.css('margin-top', 0);
76 }
77 if(DocumentHeader.direction === "down" && (DocumentHeader.changedPosition + DocumentHeader.reactionRange) < DocumentHeader.currentPosition){
78 DocumentHeader.$documentHeader.css('margin-top', (DocumentHeader.$documentHeaderNavigationBar.outerHeight() + 4) * -1);
79 }
80 DocumentHeader.lastPosition = DocumentHeader.currentPosition;
81 };
82
83 /**
84 * Start
85 */
86 DocumentHeader.start = function() {
87 DocumentHeader.reposition();
88 $(window).on('resize', DocumentHeader.resize);
89 $(window).on('scroll', DocumentHeader.scroll);
90 };
91
92 /**
93 * Initialize
94 */
95 DocumentHeader.initialize = function() {
96 DocumentHeader.$documentHeader = $(DocumentHeader.settings.selectors.moduleDocumentHeader);
97 if(DocumentHeader.$documentHeader.length > 0){
98 DocumentHeader.$documentHeaderBars = $(DocumentHeader.settings.selectors.moduleDocheaderBar);
99 DocumentHeader.$documentHeaderNavigationBar = $(DocumentHeader.settings.selectors.moduleNavigationBar);
100 DocumentHeader.$documentHeaderSearchBar = $(DocumentHeader.settings.selectors.moduleSearchBar).remove();
101 if(DocumentHeader.$documentHeaderSearchBar.length > 0){
102 DocumentHeader.$documentHeader.append(DocumentHeader.$documentHeaderSearchBar);
103 }
104 DocumentHeader.$moduleBody = $(DocumentHeader.settings.selectors.moduleBody);
105 DocumentHeader.start();
106 }
107 };
108
109 return function () {
110 $(document).ready(function() {
111 DocumentHeader.initialize();
112 });
113 TYPO3.DocumentHeader = DocumentHeader;
114 return DocumentHeader;
115 }();
116
117 });