6680add8959b6d372344888b65eedfc3b0438b7e
[Packages/TYPO3.CMS.git] / typo3 / js / clearcachemenu.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2007 Ingo Renner <ingo@typo3.org>
5 * All rights reserved
6 *
7 * This script is part of the TYPO3 project. The TYPO3 project is
8 * free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * The GNU General Public License can be found at
14 * http://www.gnu.org/copyleft/gpl.html.
15 * A copy is found in the textfile GPL.txt and important notices to the license
16 * from the author is found in LICENSE.txt distributed with these scripts.
17 *
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * class to handle the clear cache menu
29 *
30 * $Id$
31 */
32 var ClearCacheMenu = Class.create({
33
34 /**
35 * registers for resize event listener and executes on DOM ready
36 */
37 initialize: function() {
38 Event.observe(window, 'resize', this.positionMenu);
39
40 Event.observe(document, 'dom:loaded', function(){
41 this.positionMenu();
42 this.toolbarItemIcon = $$('#clear-cache-actions-menu .toolbar-item img')[0].src;
43
44 Event.observe('clear-cache-actions-menu', 'click', this.toggleMenu)
45
46 // observe all clicks on clear cache actions in the menu
47 $$('#clear-cache-actions-menu li a').each(function(element) {
48 Event.observe(element, 'click', this.clearCache.bind(this));
49 }.bindAsEventListener(this));
50 }.bindAsEventListener(this));
51 },
52
53 /**
54 * positions the menu below the toolbar icon, let's do some math!
55 */
56 positionMenu: function() {
57 var calculatedOffset = 0;
58 var parentWidth = $('clear-cache-actions-menu').getWidth();
59 var ownWidth = $$('#clear-cache-actions-menu ul')[0].getWidth();
60 var parentSiblings = $('clear-cache-actions-menu').previousSiblings();
61
62 parentSiblings.each(function(toolbarItem) {
63 calculatedOffset += toolbarItem.getWidth() - 1;
64 // -1 to compensate for the margin-right -1px of the list items,
65 // which itself is necessary for overlaying the separator with the active state background
66
67 if(toolbarItem.down().hasClassName('no-separator')) {
68 calculatedOffset -= 1;
69 }
70 });
71 calculatedOffset = calculatedOffset - ownWidth + parentWidth;
72
73
74 $$('#clear-cache-actions-menu ul')[0].setStyle({
75 left: calculatedOffset + 'px'
76 });
77 },
78
79 /**
80 * toggles the visibility of the menu and places it under the toolbar icon
81 */
82 toggleMenu: function() {
83 var toolbarItem = $$('#clear-cache-actions-menu > a')[0];
84 var menu = $$('#clear-cache-actions-menu ul')[0];
85 toolbarItem.blur();
86
87 if(!toolbarItem.hasClassName('toolbar-item-active')) {
88 toolbarItem.addClassName('toolbar-item-active');
89 Effect.Appear(menu, {duration: 0.2});
90 } else {
91 toolbarItem.removeClassName('toolbar-item-active');
92 Effect.Fade(menu, {duration: 0.1});
93 }
94 },
95
96 /**
97 * calls the actual clear cache URL using an asynchronious HTTP request
98 *
99 * @param Event prototype event object
100 */
101 clearCache: function(event) {
102 $$('#clear-cache-actions-menu .toolbar-item img')[0].src = 'gfx/spinner.gif';
103
104 new Ajax.Request(Event.element(event).href, {
105 'method': 'get',
106 'onComplete': function() {
107 $$('#clear-cache-actions-menu .toolbar-item img')[0].src = this.toolbarItemIcon;
108 }.bind(this)
109 });
110
111 Event.stop(event);
112 this.toggleMenu();
113 }
114
115 });
116
117 var TYPO3BackendClearCacheMenu = new ClearCacheMenu();