28c6218330585d2fd307c5d87483cf3414941b12
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / jslib / ts_codecompletion / completionresult.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2008-2009 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 CompletionResult class
28 */
29
30 /**
31 * @class this class post-processes the result from the codecompletion, so that it can be
32 * displayed in the next step.
33 * @constructor
34 * @param tsRef the TsRef Tree
35 * @param tsTreeNode the current Node in the codetree built by the parser
36 * @return a new CompletionResult instance
37 */
38 var CompletionResult = function(tsRef,tsTreeNode) {
39 var currentTsTreeNode = tsTreeNode;
40 var tsRef = tsRef;
41
42 /**
43 * returns the type of the currentTsTreeNode
44 */
45 this.getType = function() {
46 var val = currentTsTreeNode.getValue();
47 if (tsRef.isType(val)) {
48 return tsRef.getType(val);
49 } else {
50 return null;
51 }
52 }
53
54 /**
55 * returns a list of possible path completions (proposals), which is:
56 * a list of the children of the current TsTreeNode (= userdefined properties)
57 * and a list of properties allowed for the current object in the TsRef
58 * remove all words from list that don't start with the string in filter
59 * @param {String} filter beginning of the words contained in the proposal list
60 * @returns an Array of Proposals
61 */
62 this.getFilteredProposals = function(filter) {
63
64 var defined = new Array();
65 var propArr = new Array();
66 var childNodes = currentTsTreeNode.getChildNodes();
67 var value = currentTsTreeNode.getValue();
68 // first get the childNodes of the Node (=properties defined by the user)
69 for (key in childNodes) {
70 if (typeof(childNodes[key].value) != "undefined" && childNodes[key].value != null) {
71 propObj = new Object();
72 propObj.word = key;
73 if(tsRef.typeHasProperty(value,childNodes[key].name)){
74 propObj.cssClass = 'definedTSREFProperty';
75 propObj.type = childNodes[key].value;
76 } else {
77 propObj.cssClass = 'userProperty';
78 if (tsRef.isType(childNodes[key].value)) {
79 propObj.type = childNodes[key].value;
80 } else {
81 propObj.type = '';
82 }
83 }
84 propArr.push(propObj);
85 defined[key] = true;
86 }
87 }
88
89 // then get the tsref properties
90 var props = tsRef.getPropertiesFromTypeId(currentTsTreeNode.getValue());
91 for (key in props) {
92 // show just the TSREF properties - no properties of the array-prototype and no properties which have been defined by the user
93 if (props[key].value != null && !defined[key]) {
94 propObj = new Object();
95 propObj.word = key;
96 propObj.cssClass = 'undefinedTSREFProperty';
97 propObj.type = props[key].value;
98 propArr.push(propObj);
99 }
100 }
101
102 var result = [];
103 var wordBeginning = "";
104 for (var i=0; i < propArr.length;i++) {
105 wordBeginning = propArr[i].word.substring(0, filter.length);
106 if (filter == "" || filter == null || wordBeginning.toLowerCase() == filter.toLowerCase()) {
107 result.push(propArr[i]);
108 }
109 }
110 return result;
111 }
112 }