7ad1bc37b1fe7e9531ca82e5893c43e38d5c17de
[Packages/TYPO3.CMS.git] / typo3 / js / backendsearch.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 backend search
29 *
30 * $Id$
31 */
32 var BackendSearch = 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 = $$('#backend-search-menu .toolbar-item img')[0].src;
43
44 $('search-query').observe('keypress', function(event) {
45 var keyCode;
46
47 if(!event) {
48 var event = window.event;
49 }
50
51 if(event.keyCode) {
52 keyCode = event.keyCode;
53 } else if(event.which) {
54 keyCode = event.which;
55 }
56
57 if(keyCode == Event.KEY_RETURN) {
58 this.invokeSearch();
59 }
60 }.bindAsEventListener(this));
61
62 $$('#backend-search-menu .toolbar-item')[0].observe('click', this.toggleMenu)
63 }.bindAsEventListener(this));
64 },
65
66 /**
67 * positions the menu below the toolbar icon, let's do some math!
68 */
69 positionMenu: function() {
70 var calculatedOffset = 0;
71 var parentWidth = $('backend-search-menu').getWidth();
72 var ownWidth = $$('#backend-search-menu div')[0].getWidth();
73 var parentSiblings = $('backend-search-menu').previousSiblings();
74
75 parentSiblings.each(function(toolbarItem) {
76 calculatedOffset += toolbarItem.getWidth() - 1;
77 // -1 to compensate for the margin-right -1px of the list items,
78 // which itself is necessary for overlaying the separator with the active state background
79
80 if(toolbarItem.down().hasClassName('no-separator')) {
81 calculatedOffset -= 1;
82 }
83 });
84 calculatedOffset = calculatedOffset - ownWidth + parentWidth;
85
86
87 $$('#backend-search-menu div')[0].setStyle({
88 left: calculatedOffset + 'px'
89 });
90 },
91
92 /**
93 * toggles the visibility of the menu and places it under the toolbar icon
94 */
95 toggleMenu: function() {
96 var toolbarItem = $$('#backend-search-menu > a')[0];
97 var menu = $$('#backend-search-menu div')[0];
98 toolbarItem.blur();
99
100 if(!toolbarItem.hasClassName('toolbar-item-active')) {
101 toolbarItem.addClassName('toolbar-item-active');
102 Effect.Appear(menu, {duration: 0.2});
103 $('search-query').activate();
104 } else {
105 toolbarItem.removeClassName('toolbar-item-active');
106 Effect.Fade(menu, {duration: 0.1});
107 }
108 },
109
110 /**
111 * calls the actual clear cache URL using an asynchronious HTTP request
112 */
113 invokeSearch: function() {
114 new Ajax.Request('alt_shortcut.php?ajax=1&editPage=' + top.rawurlencode($F('search-query')), {
115 method: 'get',
116 requestHeaders: {Accept: 'application/json'},
117 onSuccess: function(transport) {
118 var jsonResponse = transport.responseText.evalJSON(true);
119
120 switch(jsonResponse.type) {
121 case 'page':
122 top.loadEditId(jsonResponse.editRecord);
123 break;
124 case 'alternative':
125 top.content.window.location.href = 'alt_doc.php?returnUrl=dummy.php&edit[' + jsonResponse.alternativeTable + '][' + jsonResponse.alternativeUid + ']=edit'
126 break;
127 case 'search':
128 this.jump(
129 unescape('db_list.php?id=' + jsonResponse.firstMountPoint + '&search_field=' + jsonResponse.searchFor + '&search_levels=4'),
130 'web_list',
131 'web'
132 );
133 break;
134 }
135 }.bind(this)
136 });
137
138 $('search-query').clear();
139 this.toggleMenu();
140 },
141
142 /**
143 * jumps to a given URL in the content iframe, taken from alt_shortcut.php
144 *
145 * @param string the URL to jump to
146 * @param string module name
147 * @param string main module name
148 */
149 jump: function(url, modName, mainModName) {
150 // Clear information about which entry in nav. tree that might have been highlighted.
151 top.fsMod.navFrameHighlightedID = new Array();
152 if(top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
153 top.content.nav_frame.refresh_nav();
154 }
155
156 top.nextLoadModuleUrl = url;
157 top.goToModule(modName);
158 }
159
160 });
161
162 var TYPO3BackendSearchMenu = new BackendSearch();