faf7bfe087d6f2eac3587964f27ff6e789ba585f
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / opendocs.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2008 Benjamin Mack <mack@xnos.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 * class to handle the open documents menu, loads the open documents dynamically
28 */
29 var OpenDocs = Class.create({
30 ajaxScript: 'ajax.php',
31 ajaxID: 'tx_opendocs::backendmenu',
32 menuItem: 'open-documents-menu',
33 menu: null, // the <ul> tag
34 toolbarItem: null, // the <a> tag
35
36
37 /**
38 * registers for resize event listener and executes on DOM ready
39 */
40 initialize: function() {
41 Event.observe(window, 'load', function() {
42 this.getMenu();
43 Event.observe(window, 'resize', this.positionMenu.bindAsEventListener(this));
44 Event.observe(this.menuItem, 'click', this.toggleMenu.bindAsEventListener(this));
45 }.bindAsEventListener(this));
46 },
47
48
49 getMenu: function() {
50 this.menu = $$('#' + this.menuItem + ' ul')[0];
51 this.toolbarItem = $$('#'+this.menuItem+' a')[0];
52 },
53
54
55 /**
56 * positions the menu below the toolbar icon
57 */
58 positionMenu: function() {
59 var calculatedOffset = 0;
60 var ownWidth = this.menu.getWidth();
61 var parentWidth = $(this.menuItem).getWidth();
62 var parentSiblings = $(this.menuItem).previousSiblings();
63
64 parentSiblings.each(function(toolbarItem) {
65 calculatedOffset += toolbarItem.getWidth()-1;
66 });
67 calculatedOffset = calculatedOffset - ownWidth + parentWidth;
68 this.menu.setStyle({ left: calculatedOffset-2 + 'px' });
69 },
70
71
72 /**
73 * toggles the visibility of the menu and places it under the toolbar icon
74 */
75 toggleMenu: function() {
76 this.toolbarItem.blur();
77 if(!this.toolbarItem.hasClassName('toolbar-item-active')) {
78 this.showMenu();
79 } else {
80 this.hideMenu();
81 }
82 },
83
84
85 /**
86 * displays the menu and does the AJAX call to the TYPO3 backend
87 */
88 showMenu: function() {
89 new Ajax.Request(this.ajaxScript, {
90 parameters: 'ajaxID=' + this.ajaxID,
91 onSuccess: function(xhr) {
92 this.menu.innerHTML = xhr.responseText;
93 Effect.Appear(this.menu, {duration: 0.2});
94 }.bind(this)
95 });
96 this.positionMenu();
97 this.toolbarItem.addClassName('toolbar-item-active');
98 TYPO3BackendToolbarManager.hideOthers(this.toolbarItem);
99 },
100
101
102 /**
103 * hides the menu
104 */
105 hideMenu: function() {
106 Effect.Fade(this.menu, {duration: 0.1});
107 this.toolbarItem.removeClassName('toolbar-item-active');
108 }
109 });
110
111 var TYPO3BackendOpenDocs = new OpenDocs();