a78ad471040e56d26907bfad8dbcfe883401296d
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / res / jslib / ts_codecompletion / tsref.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2008-2010 Stephan Petzl <spetzl@gmx.at> and Christian Kartnig <office@hahnepeter.de>
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 * @fileoverview contains the TsRef class
28 * and the TsRefProperty and TsRefType helper classes
29 */
30
31 /**
32 * @class Represents a TsRefProperty in the tree
33 *
34 * @constructor
35 */
36 var TsRefProperty = function(parentType,name,value) {
37 this.parentType = parentType;
38 this.name = name;
39 this.value = value;
40 var descriptionCache = null;
41 this.getDescription = function(callBack) {
42 if(descriptionCache == null){
43 var urlParameters = '&ajaxID=tx_t3editor_TSrefLoader::getDescription' +
44 '&typeId=' + this.parentType +
45 '&parameterName=' + this.name;
46
47 new Ajax.Request(
48 T3editor.URL_typo3 + 'ajax.php',
49 {
50 method: 'get',
51 parameters: urlParameters,
52 onSuccess: function(transport) {
53 descriptionCache = transport.responseText;
54 callBack(transport.responseText);
55 }
56 }
57 );
58 } else {
59 callBack(descriptionCache);
60 }
61 }
62 }
63
64 /**
65 * @class Represents a TsRefType in the tree
66 *
67 * @constructor
68 */
69 var TsRefType = function(typeId) {
70 this.typeId = typeId;
71 this.properties = new Array();
72
73 // todo: types can have descriptions too!
74 this.getDescription = function() {
75 }
76 }
77
78 /**
79 * Construct a new TsRef object.
80 * @class This class receives the TsRef from the server and represents it as a tree
81 * also supplies methods for access to treeNodes
82 *
83 * @constructor
84 * @return A new TsRef instance
85 */
86 var TsRef = function() {
87 var typeTree = new Array();
88
89 var doc;
90
91 this.loadTsrefAsync = function() {
92 var urlParameters = '&ajaxID=tx_t3editor_TSrefLoader::getTypes';
93 new Ajax.Request(
94 T3editor.URL_typo3 + 'ajax.php',
95 {
96 method: 'get',
97 parameters: urlParameters,
98 onSuccess: function(transport) {
99 doc = eval('('+ transport.responseText +')');
100 buildTree();
101 }
102 }
103 );
104 }
105
106
107
108 function buildTree() {
109
110 typeTree = new Array();
111 for (var typeId in doc) {
112
113 var arr = doc[typeId];
114 typeTree[typeId] = new TsRefType(typeId);
115
116 if (arr['extends'] != null) {
117 typeTree[typeId]['extends'] = arr['extends'];
118 }
119 for (propName in arr.properties) {
120 var propType = arr.properties[propName].type;
121 typeTree[typeId].properties[propName] = new TsRefProperty(typeId,propName,propType);
122 }
123 }
124 for (var typeId in typeTree) {
125 if (typeTree[typeId]['extends'] != null) {
126 //console.log(typeId+" | "+typeTree[typeId].extends+" |");
127 addPropertiesToType(typeTree[typeId], typeTree[typeId]['extends'], 100);
128 }
129 }
130 }
131
132
133 function addPropertiesToType(addToType,addFromTypeNames,maxRecDepth){
134 if(maxRecDepth<0){
135 throw "Maximum recursion depth exceeded while trying to resolve the extends in the TSREF!";
136 return;
137 }
138 var exts = addFromTypeNames.split(',');
139 var i;
140 for(i=0;i<exts.length;i++){
141 //"Type 'array' which is used to extend 'undefined', was not found in the TSREF!"
142 if(typeTree[exts[i]]==null){
143 //console.log("Error: Type '"+exts[i]+"' which is used to extend '"+addToType.typeId+"', was not found in the TSREF!");
144 }else{
145 if(typeTree[exts[i]]['extends'] != null){
146 addPropertiesToType(typeTree[exts[i]],typeTree[exts[i]]['extends'],maxRecDepth-1);
147 }
148 var properties = typeTree[exts[i]].properties;
149 for(propName in properties){
150 // only add this property if it was not already added by a supertype (subtypes override supertypes)
151 if(addToType.properties[propName] == null){
152 addToType.properties[propName] = properties[propName];
153 }
154 }
155 }
156 }
157
158 }
159
160 this.getPropertiesFromTypeId = function(tId) {
161 if (typeTree[tId] != null) {
162 // clone is needed to assure that nothing of the tsref is overwritten by user setup
163 typeTree[tId].properties.clone = function() {
164 var result = new Array();
165 for (key in this) {
166 result[key] = new TsRefProperty(this[key].parentType,this[key].name,this[key].value);
167 }
168 return result;
169 }
170 return typeTree[tId].properties;
171 } else {
172 return new Array();
173 }
174 }
175
176 this.typeHasProperty = function(typeId,propertyName) {
177 if (typeTree[typeId] != null && typeTree[typeId].properties[propertyName] != null) {
178 return true;
179 } else {
180 return false;
181 }
182 }
183
184 this.getType = function(typeId){
185 return typeTree[typeId];
186 }
187 this.isType = function(typeId){
188 return (typeTree[typeId] != null);
189 }
190 }