863e388df884c96004ecfc99e8b3669bb8021a04
[Packages/TYPO3.CMS.git] / typo3 / js / common.js
1 /***************************************************************
2 *
3 * javascript functions regarding the TYPO3 wrapper
4 * for the javascript library "prototype".
5 *
6 * Copyright notice
7 *
8 * (c) 2008-2010 Benjamin Mack <www.xnos.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 backend provided by
12 * Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
13 *
14 * Released under GNU/GPL (see license file in /typo3/)
15 *
16 * This script is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * This copyright notice MUST APPEAR in all copies of this script
21 *
22 ***************************************************************/
23
24 // Please make sure that prototype.js is loaded before loading this
25 // file in your script, the responder is only added if prototype was loaded
26
27 if (Prototype) {
28 // adding generic a responder to use when a AJAX request is done
29 Ajax.Responders.register({
30 onCreate: function(request, transport) {
31
32 // if the TYPO3 AJAX backend is used,
33 // the onSuccess & onComplete callbacks are hooked
34 if (request.url.indexOf("ajax.php") === -1) {
35 return;
36 }
37
38 var origSuccess = request.options.onSuccess, origComplete = request.options.onComplete;
39
40 // hooking "onSuccess"
41 if (origSuccess) {
42 request.options.onSuccess = function(xhr, json) {
43 if (!json) {
44 T3AJAX.showError(xhr);
45 } else {
46 origSuccess(xhr, json);
47 }
48 }
49 }
50
51 // hooking "onComplete", using T3Error handler if available
52 if (origComplete) {
53 request.options.onComplete = function(xhr, json) {
54 if (!json && request.options.onT3Error) {
55 request.options.onT3Error(xhr, json);
56 } else if (!json) {
57 T3AJAX.showError(xhr);
58 } else {
59 origComplete(xhr, json);
60 }
61 };
62 }
63 }
64 });
65 }
66
67 var T3AJAX = {};
68 T3AJAX.showError = function(xhr, json) {
69 if (typeof xhr.responseText !== undefined && xhr.responseText) {
70 if (typeof Ext.MessageBox !== undefined) {
71 Ext.MessageBox.alert('TYPO3', xhr.responseText);
72 }
73 else {
74 alert(xhr.responseText);
75 }
76 }
77 };
78
79 // common storage and global object, could later hold more information about the current user etc.
80 var TYPO3 = {
81 // store instances that only should be running once
82 _instances: {},
83 getInstance: function(className) {
84 return TYPO3._instances[className] || false;
85 },
86 addInstance: function(className, instance) {
87 TYPO3._instances[className] = instance;
88 return instance;
89 },
90
91 helpers: {
92 // creates an array by splitting a string into parts, taking a delimiter
93 split: function(str, delim) {
94 var res = [];
95 while (str.indexOf(delim) > 0) {
96 res.push(str.substr(0, str.indexOf(delim)));
97 str = str.substr(str.indexOf(delim) + delim.length);
98 }
99 return res;
100 }
101 }
102 };