599a6f8c352ef27646b7e37c5c32684d13cf48cc
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Resources / Public / JavaScript / opendocs.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 * class to handle the open documents menu, loads the open documents dynamically
16 *
17 */
18 var OpenDocs = Class.create({
19 menu: null,
20 toolbarItemIcon: null,
21
22 /**
23 * registers for resize event listener and executes on DOM ready
24 */
25 initialize: function() {
26
27 Ext.onReady(function() {
28 Event.observe(
29 window, 'resize',
30 function() { TYPO3BackendToolbarManager.positionMenu('tx-opendocs-menu'); }
31 );
32 TYPO3BackendToolbarManager.positionMenu('tx-opendocs-menu');
33 this.toolbarItemIcon = $$('#tx-opendocs-menu .toolbar-item span.t3-icon')[0];
34
35 Event.observe($$('#tx-opendocs-menu .toolbar-item')[0], 'click', this.toggleMenu);
36 this.menu = $$('#tx-opendocs-menu .toolbar-item-menu')[0];
37 }, this);
38 },
39
40 /**
41 * toggles the visibility of the menu and places it under the toolbar icon
42 */
43 toggleMenu: function(event) {
44 var toolbarItem = $$('#tx-opendocs-menu > a')[0];
45 var menu = $$('#tx-opendocs-menu .toolbar-item-menu')[0];
46 toolbarItem.blur();
47
48 if(!toolbarItem.hasClassName('toolbar-item-active')) {
49 toolbarItem.addClassName('toolbar-item-active');
50 Effect.Appear(menu, {duration: 0.2});
51 TYPO3BackendToolbarManager.hideOthers(toolbarItem);
52 } else {
53 toolbarItem.removeClassName('toolbar-item-active');
54 Effect.Fade(menu, {duration: 0.1});
55 }
56
57 if(event) {
58 Event.stop(event);
59 }
60 },
61
62 /**
63 * displays the menu and does the AJAX call to the TYPO3 backend
64 */
65 updateMenu: function() {
66 var origToolbarItemIcon = this.toolbarItemIcon.src;
67 this.toolbarItemIcon.src = 'gfx/spinner.gif';
68
69 new Ajax.Updater(
70 this.menu,
71 top.TS.PATH_typo3 + TYPO3.settings.ajaxUrls['OpendocsController::renderMenu'], {
72 onComplete: function(xhr) {
73 this.toolbarItemIcon.src = origToolbarItemIcon;
74 }.bind(this)
75 }
76 );
77 },
78
79 /**
80 * updates the number of open documents in the toolbar according to the
81 * first parameter. If "num" is smaller than "0", the number of opendocs
82 * is counted from the open menu
83 *
84 * @param integer number of open documents
85 * @param boolean flag to explicitly update the menu
86 */
87 updateNumberOfDocs: function(num, explicitlyUpdateMenu) {
88 if (explicitlyUpdateMenu) {
89 // re-render the menu e.g. if a document was closed inside the menu
90 this.updateMenu();
91 }
92
93 if (num < 0) {
94 num = $$('#tx-opendocs-menu tr.opendoc').length;
95 }
96
97 $('tx-opendocs-counter').writeAttribute('value', num);
98 },
99
100 /**
101 * closes an open document
102 */
103 closeDocument: function(md5sum) {
104 new Ajax.Updater(
105 this.menu,
106 top.TS.PATH_typo3 + TYPO3.settings.ajaxUrls['OpendocsController::closeDocument'], {
107 parameters: {
108 md5sum: md5sum
109 },
110 onComplete: function() {
111 this.updateNumberOfDocs(-1, false);
112 }.bind(this)
113 }
114 );
115
116 this.updateNumberOfDocs(-1, true);
117 }
118
119 });
120
121 var TYPO3BackendOpenDocs = new OpenDocs();