b3505f005dc17aa4dda80dda4dfa5c3eb39c7d4d
[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 ajaxIDloadMenu: 'tx_opendocs::backendMenu',
32 ajaxIDcloseDoc: 'tx_opendocs::closeDocument',
33 menuItem: 'open-documents-menu',
34 menu: null, // the <div> tag
35 toolbarItem: null, // the <a> tag
36
37
38 /**
39 * registers for resize event listener and executes on DOM ready
40 */
41 initialize: function() {
42 Event.observe(window, 'load', function() {
43 this.ajaxScript = top.TS.PATH_typo3 + this.ajaxScript; // can't be initialized earlier
44 this.getMenu();
45 Event.observe(window, 'resize', this.positionMenu.bindAsEventListener(this));
46 Event.observe(this.toolbarItem, 'click', this.toggleMenu.bindAsEventListener(this));
47 }.bindAsEventListener(this));
48 },
49
50
51 getMenu: function() {
52 this.toolbarItem = $(this.menuItem).firstChild;
53 this.menu = this.toolbarItem.nextSibling;
54 },
55
56
57 /**
58 * positions the menu below the toolbar icon
59 */
60 positionMenu: function() {
61 var calculatedOffset = 0;
62 var ownWidth = this.menu.getWidth();
63 var parentWidth = $(this.menuItem).getWidth();
64 var parentSiblings = $(this.menuItem).previousSiblings();
65
66 parentSiblings.each(function(toolbarItem) {
67 calculatedOffset += toolbarItem.getWidth()-1;
68 });
69 calculatedOffset = calculatedOffset - ownWidth + parentWidth;
70 this.menu.setStyle({ left: calculatedOffset-2 + 'px' });
71 },
72
73
74 /**
75 * toggles the visibility of the menu and places it under the toolbar icon
76 */
77 toggleMenu: function(event) {
78 Event.stop(event);
79 this.toolbarItem.blur();
80 if(!this.toolbarItem.hasClassName('toolbar-item-active')) {
81 this.showMenu();
82 } else {
83 this.hideMenu();
84 }
85 },
86
87
88
89 /**
90 * displays the menu and does the AJAX call to the TYPO3 backend
91 */
92 showMenu: function() {
93 new Ajax.Updater(this.menu, this.ajaxScript, {
94 parameters: { ajaxID: this.ajaxIDloadMenu },
95 onSuccess: function(xhr) {
96 if (!this.menu.visible()) {
97 Effect.Appear(this.menu, {
98 duration: 0.2,
99 afterFinish: function() { this.positionMenu(); }.bind(this)
100 });
101 }
102 }.bind(this)
103 });
104 if (!this.toolbarItem.hasClassName('toolbar-item-active')) {
105 this.toolbarItem.addClassName('toolbar-item-active');
106 TYPO3BackendToolbarManager.hideOthers(this.toolbarItem);
107 }
108 },
109
110
111 /**
112 * hides the menu
113 */
114 hideMenu: function() {
115 Effect.Fade(this.menu, {duration: 0.1} );
116 this.toolbarItem.removeClassName('toolbar-item-active');
117 },
118
119
120 /**
121 * updates the number of open documents in the toolbar
122 */
123 updateNumberOfDocs: function(num, doNotUpdateMenu) {
124 if (num < 0) {
125 num = $$('tr.opendoc').length;
126 }
127 if (num == 0) {
128 num = '';
129 }
130 $('tx-opendocs-num').innerHTML = num;
131 if (this.menu.visible() && !doNotUpdateMenu) {
132 this.showMenu();
133 }
134 },
135
136 /**
137 * this function calls the backend to close an open documentshould let the
138 */
139 closeDocument: function(md5sum) {
140 new Ajax.Updater(this.menu, this.ajaxScript, {
141 parameters: { ajaxID: this.ajaxIDcloseDoc, md5sum: md5sum },
142 onSuccess: function() { this.updateNumberOfDocs(-1, true); }.bind(this)
143 });
144 return false;
145 }
146
147 });
148
149 var TYPO3BackendOpenDocs = new OpenDocs();
150